當前位置:首頁 » 編程語言 » 交通燈單片機c語言

交通燈單片機c語言

發布時間: 2024-06-07 19:18:00

1. 鍗曠墖鏈篊璇璦綰㈢豢鐏紼嬪簭錛岃佹眰錛氭暟鐮佺℃樉紺烘椂闂村掕℃椂銆16*16LED鐐歸樀鏄劇ず妯″潡鏄劇ず綰㈢豢榛勭伅銆

#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define BLKN 2 //鍒楅攣瀛樺櫒鏁
sbit G = P1^7; //P1.7涓烘樉紺轟笡闄鍏佽告帶鍒朵俊鍙風鍙
sbit RCLK = P2^0; //P2.0涓鴻緭鍑洪攣瀛樺拹鑼勫櫒鏃墮挓淇″彿絝
sbit SCLR = P2^1; //P2.1涓虹Щ浣嶅瘎瀛樺櫒娓呪棆絝
void delay(uint); //寤舵椂鍑芥暟
uchar data display[32]; //鏄劇ず緙撳瓨
uchar code BMP[][32]={{16,0,16,0,37,248,36,64,120,64,8,64,16,64,32,64,124,64,0,64,0,64,12,64,112,64,3,252,0,0,0,0},
{16,0,19,240,32,16,41,240,120,16,19,252,32,64,122,72,1,80,0,224,25,80,102,76,0,64,1,192,0,0,0,0},
{4,64,4,64,63,248,4,64,127,252,1,0,31,240,17,16,31,240,17,16,31,240,8,32,16,16,32,8,0,0,0,0}}; //瀛楁ā琛'綰⑩樷欑豢鈥樷橀粍鈥
void main()
{
register uchar i,j;
SCON=0x00; //涓插彛宸ヤ綔妯″紡0錛氱Щ浣嶅瘎瀛樺櫒鏂瑰紡
TMOD=0x01; //瀹氭椂鍣═0宸ヤ綔鏂瑰紡1錛16浣嶆柟寮
P1=0x3f; //P1絝鍙e垵鍊礆細鍏佽告帴鏀躲侀攣瀛樸佹樉紺
TR0=1; //鍚鍔ㄥ畾鏃跺櫒T0
ET0=1; //鍏佽稿畾鏃跺櫒T0涓鏂
EA=1; //寮鍚鎬諱腑鏂錛
while(1)
{
delay(2000);
for(j=0;j<3;j++)
{
for(i=0;i<32;i++)
{
display[i]=~BMP[j][i];
if (i%2) delay(100);
}
delay(10000);
}
}
}
void delay(uint x)
{
register uchar i,j;
for (i=x;i>0;i--)
for (j=120;j>0;j--);
}
void leddisplay(void) interrupt 1 using 1
{
register uchar i, j=BLKN;
TH0 =(65536-100)/256; //琛¢儜瀵熻懼畾鏄劇ず灞忓埛鏂扮巼姣忕60甯
TL0 =(65536-100)%256;
i = P1; //璇誨彇褰撳墠鏄劇ず鐨勮屽彿
i = ++i & 0x0f; //琛屽彿鍔1錛屽睆钄介珮4浣
while(j)
{
j--;
SBUF = display[i*BLKN + j]; //閫佹樉紺烘暟鎹
while (!TI);
TI = 0;
} //瀹屾垚涓琛屾暟鎹鐨勫彂閫
G = 1; //娑堥殣錛堝叧闂鏄劇ず錛
P1 &= 0xf0; //琛屽彿絝鍙f竻鈼
RCLK = 1; //鏄劇ず鏁版嵁鎵撳叆杈撳嚭閿佸瓨鍣
P1 |= i; //鍐欏叆琛屽彿
RCLK = 0; //閿佸瓨鏄劇ず鏁版嵁
G = 0; //鎵撳紑鏄劇ず
}

2. 交通燈 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;

}

=====================================================================

坐等拿分!

3. 單片機模擬交通燈C語言例子,看不懂,求解釋

問題1:Flash_count每加1黃燈的引腳狀態取反一次,引腳高低電平取反兩次是閃一下。(原來是滅,取反,亮了,再取反,滅了,這才完成了一次閃爍。)因此Flash_count每加2才是閃爍一次。
問題2:1、return有函數返回數值作用,在函數末尾添加return (數字)可以使函數在被調用的地方返回一個值。例:uchar hanshu(){return 99;} void main(){num=hanshu();}則num值為1.
2、return有完全結束循環的作用,在循環中插入return可以徹底結束循環。
例:while(1){if(i==0)return; (後文省略)}如果i==1,則一直死循環,當i==0時跳出循環。
3、return可以結束函數。
例:void hanshu(){ return; while(1); }這個子函數不會陷入死循環,while(1)根本執行不到。
您的函數中到達return後其實子函數Light()也結束了,其實跳出了該函數,返回到main中了,
只不過由於main中的while的作用再次重頭進入了而已。
4、return在switch語句中還有終止查找的作用。這個您網路switch就可以了

4. 單片機交通燈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;

}

}

}

}

熱點內容
開通了免密為什麼還要密碼 發布:2024-11-27 04:17:42 瀏覽:14
excel導入資料庫java 發布:2024-11-27 04:16:21 瀏覽:623
ps不能存儲bpm 發布:2024-11-27 04:04:12 瀏覽:612
jquery代碼加密 發布:2024-11-27 03:54:51 瀏覽:593
數字密碼人格是什麼原理 發布:2024-11-27 03:46:34 瀏覽:426
華為怎麼看手機的配置 發布:2024-11-27 03:27:42 瀏覽:381
php函數作用域 發布:2024-11-27 03:26:11 瀏覽:176
pythonasteval 發布:2024-11-27 03:21:14 瀏覽:563
電腦伺服器機什麼意思 發布:2024-11-27 03:18:59 瀏覽:837
本地存儲是否允許 發布:2024-11-27 03:08:02 瀏覽:411