❶ 解釋c語言編寫的交通燈程序
你這個程序,需要知道原理圖或模擬圖,結合電路才知道那些是用來控制什麼。
不過,那些Pnn,應該是在程序的面定義的引腳,如P37,應該是用
sbit P37=P3^7;這樣的語句定義過的,就是P3.7的引腳去控制什麼電路了。
引腳是不是控制LED,用LED排列成的8字形的計時牌啊,就像數碼管似的。因交通燈的倒計時版就是由很多的LED燈排列而成的,並不是用現成的數碼管。
❷ 求一89c52單片機c語言程序,流水燈+數碼管 共同作用
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define LED P3
uchar code TAB[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char a,i;
unsigned long int temp;
void init();
void display();
void delay(unsigned char );
void main()
{
init();
while(1)
{
LED=0xfe;//給一盞燈亮
for(i=0;i<8;i++)
{ delay(100);
LED=(LED<<1);//循環左移實現流水燈功能
}
display();
}
}
void init()
{
temp=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==20)
{
a=0;
temp++;
if(temp==10) temp=0;
}
}
void display()
{
P1=0xf0;
P0=TAB[temp];//不斷讀table裡面的書顯示0到10
delay(1);
}
void delay(unsigned char z)
{
uint x,y;
for(x=0;x<z;x++)
for(y=1;y<600;y++);
}
❸ C語言編程來實現交通燈數碼管顯示時間
這個問題太籠統了。
❹ 單片機交通燈c語言
#include<reg51.h>
#defineucharunsignedchar
#defineuintunsignedint
sbitRED_A=P3^0;//東西向指示燈
sbitYELLOW_A=P3^1;
sbitGREEN_A=P3^2;
sbitRED_B=P3^3;//南北向指示燈
sbitYELLOW_B=P3^4;
sbitGREEN_B=P3^5;
sbitKEY1=P1^0;
sbitKEY2=P1^1;
sbitKEY3=P1^2;
//延時倍數,閃爍次數,操作類型變數
ucharFlash_Count=0,Operation_Type=1,LEDsng,LEDsns,LEDewg,LEDews,discnt;
uintTime_Count=0,time;
ucharledtab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
voiddisplaysn()
{
LEDsng=((time-Time_Count)/20)%10;
LEDsns=((time-Time_Count)/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
voiddisplayew()
{
LEDewg=((time-Time_Count)/20)%10;
LEDews=((time-Time_Count)/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
//定時器0中斷函數
voidT0_INT()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
switch(Operation_Type)
{
case1://東西向綠燈與南北向紅燈亮
if((Time_Count%20)==0)displayew();
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=time)return;
Time_Count=0;
Operation_Type=2;
break;
case2://東西向黃燈開始閃爍,綠燈關閉
LEDewg=0x0;
LEDews=0x0;
if(++Time_Count!=8)return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10)return;//閃爍
Flash_Count=0;
Operation_Type=3;
break;
case3://東西向紅燈與南北向綠燈亮
if((Time_Count%20)==0)displaysn();
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=time)return;
Time_Count=0;
Operation_Type=4;
break;
case4://南北向黃燈開始閃爍,綠燈關閉
LEDsng=0x0;
LEDsns=0x0;
if(++Time_Count!=8)return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10)return;//閃爍
Flash_Count=0;
Operation_Type=1;
break;
}
}
voidt1_isr()interrupt3
{
TR1=0;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
switch(discnt)
{
case0:
P2=0x02;
P0=ledtab[LEDewg];
break;
case1:
P2=0x01;
P0=ledtab[LEDews];
break;
case2:
P2=0x08;
P0=ledtab[LEDsng];
break;
case3:
P2=0x04;
P0=ledtab[LEDsns];
break;
default:discnt=0;break;
}
discnt++;
discnt&=0x03;
}
voiddelay()
{
uinti;
for(i=0;i<1000;i++);
}
//主程序
voidmain()
{
TMOD=0x11;//T0方式1
EA=1;
ET0=1;
TR0=1;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
ET1=1;
time=600;
Time_Count=600;
Time_Count=0;
Operation_Type=1;
while(1)
{
if(KEY1==0) //按一下加1S
{
delay();
if(KEY1==0)
{
while(KEY1==0);
TR0=0;
time+=20;
LEDsng=(time/20)%10;
LEDsns=(time/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
}
if(KEY2==0) //按一下減1S
{
delay();
if(KEY2==0)
{
while(KEY2==0);
TR0=0;
time-=20;
if(time==0)time=20;
LEDewg=(time/20)%10;
LEDews=(time/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
}
if(KEY3==0) //啟動
{
delay();
if(KEY3==0)
{
while(KEY2==0);
TR0=1;
Time_Count=0;
}
}
}
}
❺ 有沒有C語言版 的交通燈控制,要求要有數碼管來顯示倒計時
參考:
http://hi..com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/1001478e1372bcf9f11f367b.html
❻ 用C語言些寫交通燈的 程序,要 用到 數碼管計時的,是 十字路口的 那 種,可以幫我 寫移一下不
博客上有,測試通過了的。
❼ 單片機交通燈c程序 只有紅綠燈 和數碼顯示
參考《51單片機C語言創新教程》溫子祺等著。
源碼轉自:《51單片機C語言創新教程》。
/*實驗名稱:交通燈實驗
*描述:交通燈實驗要求紅燈亮15秒,綠燈亮10秒,黃燈亮5秒,
當紅燈切換為綠燈或者綠燈切換為紅燈,
要實現燈閃爍。紅燈、綠燈、黃燈的點亮持續時間可以通過串口來修改,
並在下一個循環中更新數值。
*作者:溫子祺
*修改日期:2010/5/4
*說明:代碼注釋與講解詳見《51單片機C語言創新教程》溫子祺等著,北京航空航天大學出版社
*/
#include"stc.h"
typedefunsignedcharUINT8;
typedefunsignedint UINT16;
typedefunsignedlongUINT32;
typedefcharINT8;
typedefintINT16;
typedeflongINT32;
#defineTIMER0_INITIAL_VALUE5000
#defineHIGH1
#defineLOW0
#defineON1
#defineOFF0
#defineSEG_PORTP0
#defineLS164_DATA(x){if((x))P0_4=1;elseP0_4=0;}
#defineLS164_CLK(x){if((x))P0_5=1;elseP0_5=0;}
#defineNORTH_R_LIGHT(x){if((x))P2_0=0;elseP2_0=1;}
#defineNORTH_Y_LIGHT(x){if((x))P2_1=0;elseP2_1=1;}
#defineNORTH_G_LIGHT(x){if((x))P2_2=0;elseP2_2=1;}
#defineSOUTH_R_LIGHT(x){if((x))P2_3=0;elseP2_3=1;}
#defineSOUTH_Y_LIGHT(x){if((x))P2_4=0;elseP2_4=1;}
#defineSOUTH_G_LIGHT(x){if((x))P2_5=0;elseP2_5=1;}
#defineTRAFFIC_STATUS_10
#defineTRAFFIC_STATUS_21
#defineTRAFFIC_STATUS_32
#defineUART_MARKER0xEE
UINT8Timer0IRQEvent=0;
UINT8Time1SecEvent=0;
UINT8Time500MsEvent=0;
UINT8TimeCount=0;
UINT8SegCurPosition=0;
UINT8LightOrgCount[4]={15,5,15,5};
UINT8LightCurCount[4]={15,5,15,5};
UINT8TrafficLightStatus=0;
codeUINT8SegCode[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
UINT8SegBuf[4]={0};
codeUINT8SegPosition[4]={0x07,0x0b,0x0d,0x0e};
typedefstruct_LIGHT_VAL
{
UINT8Head;
UINT8val[4];
}LIGHT_VAL;
typedefunion_LIGHT_VAL_EX
{
LIGHT_VALlv;
UINT8p[5];
}LIGHT_VAL_EX;
voidLS164Send(UINT8byte)
{
UINT8j;
for(j=0;j<=7;j++)
{
if(byte&(1<<(7-j)))
{
LS164_DATA(HIGH);
}
else
{
LS164_DATA(LOW);
}
LS164_CLK(LOW);
LS164_CLK(HIGH);
}
}
voidRefreshDisplayBuf(UINT8s1) //刷新顯示緩存
{
SegBuf[0]=s1%10;
SegBuf[1]=s1/10;
SegBuf[2]=s1%10;
SegBuf[3]=s1/10;
}
voidSegDisplay(void)
{
UINT8t;
t=SegCode[SegBuf[SegCurPosition]];
SEG_PORT|=0x0f;
LS164Send(t);
SEG_PORT=(SEG_PORT|0x0f)&SegPosition[SegCurPosition];
if(++SegCurPosition>=4)
{
SegCurPosition=0;
}
}
voidTimerInit(void)
{
TH1=0;
TL1=0;
TH0=(65536-TIMER0_INITIAL_VALUE)/256;
TL0=(65536-TIMER0_INITIAL_VALUE)%256;//定時1MS
TMOD=0x51; /*01010001T1計數,T0定時*/
}
voidTimer0Start(void)
{
TR0=1; //啟動計時器1
ET0=1;
}
voidTimer0Stop(void)
{
TR0=0; //啟動計時器1
ET0=0;
}
voidPortInit(void)
{
P0=P1=P2=P3=0xFF;
}
voidUartInit(void)
{
SCON=0x40;
T2CON=0x34;
RCAP2L=0xD9;
RCAP2H=0xFF;
REN=1;
ES=1;
}
voidUartSendByte(UINT8byte)
{
SBUF=byte;
while(TI==0);
TI=0;
}
voidUartPrintfString(INT8*str)
{
while(str&&*str)
{
UartSendByte(*str++);
}
}
voidmain(void)
{
UINT8i=0;
PortInit();
TimerInit();
Timer0Start();
UartInit();
RefreshDisplayBuf(LightCurCount[0]);
EA=1;
NORTH_R_LIGHT(ON);
SOUTH_G_LIGHT(ON);
while(1)
{
if(Timer0IRQEvent)
{
Timer0IRQEvent=0;
TimeCount++;
if(TimeCount>=200)
{
TimeCount=0;
if(LightCurCount[0])
{
TrafficLightStatus=0;
}
elseif(LightCurCount[1])
{
TrafficLightStatus=1;
}
elseif(LightCurCount[2])
{
TrafficLightStatus=2;
}
elseif(LightCurCount[3])
{
TrafficLightStatus=3;
}
else
{
for(i=0;i<4;i++)
{
LightCurCount[i]=LightOrgCount[i];
}
TrafficLightStatus=0;
}
switch(TrafficLightStatus)
{
case0:
{
NORTH_R_LIGHT(ON);
SOUTH_R_LIGHT(OFF);
NORTH_G_LIGHT(OFF);
SOUTH_G_LIGHT(ON);
NORTH_Y_LIGHT(OFF);
SOUTH_Y_LIGHT(OFF);
}
break;
case1:
{
if(LightCurCount[1]%2)
{
NORTH_R_LIGHT(ON);
SOUTH_G_LIGHT(ON);
}
else
{
NORTH_R_LIGHT(OFF);
SOUTH_G_LIGHT(OFF);
}
NORTH_Y_LIGHT(ON);
SOUTH_Y_LIGHT(ON);
}
break;
case2:
{
NORTH_R_LIGHT(OFF);
SOUTH_R_LIGHT(ON);
NORTH_G_LIGHT(ON);
SOUTH_G_LIGHT(OFF);
NORTH_Y_LIGHT(OFF);
SOUTH_Y_LIGHT(OFF);
}
break;
case3:
{
if(LightCurCount[3]%2)
{
NORTH_G_LIGHT(ON);
SOUTH_R_LIGHT(ON);
}
else
{
NORTH_G_LIGHT(OFF);
SOUTH_R_LIGHT(OFF);
}
NORTH_Y_LIGHT(ON);
SOUTH_Y_LIGHT(ON);
}
break;
default:break;
}
RefreshDisplayBuf(LightCurCount[TrafficLightStatus]);
LightCurCount[TrafficLightStatus]--;
}
SegDisplay();
}
}
}
voidUartIRQ(void)interrupt4
{
staticUINT8cnt=0;
staticLIGHT_VAL_EXLightValEx;
if(RI)
{
RI=0;
LightValEx.p[cnt++]=SBUF;
if(LightValEx.lv.Head==UART_MARKER)
{
if(cnt>=5)
{
for(cnt=1;cnt<5;cnt++)
{
LightOrgCount[cnt-1]=LightValEx.lv.val[cnt];
LightCurCount[cnt-1]=LightValEx.lv.val[cnt];
}
cnt=0;
UartPrintfString("設置交通燈完成 ");
}
}
else
{
cnt=0;
}
}
}
voidTimer0IRQ(void)interrupt1
{
ET0 =0;
TH0=(65536-TIMER0_INITIAL_VALUE)/256;
TL0=(65536-TIMER0_INITIAL_VALUE)%256;//定時1MS
Timer0IRQEvent=1;
ET0 =1;
}
=====================================================================
坐等拿分!
❽ 基於51單片機的C語言的交通燈設計
老大,網上輸入「單片機 交通燈 」一大堆。