找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1398|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

我的單片機(jī)交通燈設(shè)計(jì)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:770492 發(fā)表于 2020-6-5 17:37 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit RED_A = P0^0;
sbit YELLOW_A = P0^1;
sbit GREEN_A = P0^2;
sbit RED_B = P0^3;
sbit YELLOW_B = P0^4;
sbit GREEN_B = P0^5;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
uchar reckon ,Flash_Count = 0,Operation_Type = 1,Key_Flag_Idx,t,Second_Counts,yellow=0,i,m;
uchar DSY_CODE[]=
{
         0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};//顯示代碼表
void T0_INT() interrupt 1
{
         TH0  = (65535-50000)/256;
        TL0  = (65535-50000)%256;
        if(++i==20)//精確到一秒
        {
                 i = 0;
                --Second_Counts;
                P3 = DSY_CODE[Second_Counts/10];//十位
                P2 = DSY_CODE[Second_Counts%10];//個(gè)位
                if(Second_Counts == 0) Second_Counts = t;//等于0后重新開(kāi)始
        switch(Operation_Type)
        {
                 case 1:
                        RED_A=0;YELLOW_A=0;GREEN_A=1;//東西道綠燈亮
                        RED_B=1;YELLOW_B=0;GREEN_B=0;//南北道紅燈亮
                        if(--reckon != 0) return;
                        reckon=m;
                        Operation_Type = 2;
                        break;
                case 2:
                        if(++yellow !=1) return;
                        yellow=0;
                        YELLOW_A=!YELLOW_A;//東西道黃燈閃
                        GREEN_A=0;
                        if(++Flash_Count !=5) return;//閃爍5秒
                        Flash_Count=0;
                        Operation_Type = 3;
                        break;
                case 3:
                        RED_A=1;YELLOW_A=0;GREEN_A=0;//東西道紅燈亮
                        RED_B=0;YELLOW_B=0;GREEN_B=1;//南北道綠燈亮
                        if(--reckon !=0 ) return;
                        reckon=m;
                        Operation_Type = 4;
                        break;        
                case 4:
                        if(++yellow != 1) return;
                        yellow=0;
                        YELLOW_B=!YELLOW_B;//南北道黃燈閃
                        GREEN_B=0;
                        if(++Flash_Count !=5)//閃爍5秒
                                return;
                        Flash_Count=0;
                        Operation_Type = 1;
                        break;        
        }
}
}

void main()
{        
   t=20;
        m=t-5;//為黃燈閃爍節(jié)省出時(shí)間
   EA=1;
        ET0=1;
        TR0=1;
   P1 = 0x3f;
        P2 = 0x3f;
        i = 0;
        reckon=m;
        Second_Counts = t;
         TMOD = 0x01;//定時(shí)器為模式以1
        while(1)
        {
           if(K1==0)//時(shí)間變長(zhǎng)
         {
         t=t+5;
         if(t>=70)
         t=20;
         m=t-5;
         }
                 if(K2==0)//時(shí)間變短
         {
         t=t-5;
         if(t<=5)                        //警告不可連續(xù)按按鈕,一次紅綠燈時(shí)長(zhǎng)餒只可按一次
         t=20;
         m=t-5;
         }
         if(K3==0)//恢復(fù)正常
         {
         t=20;
         m=t-5;
         }
        }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表