欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

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

QQ登錄

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

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

51單片機(jī)實(shí)現(xiàn)交通燈

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:649548 發(fā)表于 2019-11-25 23:37 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
//上電南北通行30秒,5秒倒計(jì)時(shí)亮黃燈,30秒結(jié)束,東西通行,后面依次交替,
//增加了2個(gè)應(yīng)急按鍵,按下S3南北通行30秒,30秒結(jié)束時(shí)回到應(yīng)急前的狀態(tài),同理按下S2.....
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define led P0
sbit s3=P3^5;//南北應(yīng)急通行
sbit s2=P3^4;//東西應(yīng)急通行
sbit wei1=P2^1;sbit wei0=P2^0;//數(shù)碼管位選
sbit red0=P2^5;sbit green0=P2^6;sbit yellow0=P2^7;//南北紅綠黃燈
sbit red1=P2^4;sbit green1=P2^2;sbit yellow1=P2^3;//東西紅綠黃燈
//uchar code display[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0 ---- 9,共陽(yáng)
uchar code display[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//共陰數(shù)碼管0---9,a---f,滅
uchar buf[2]={3,0};//初始顯示30
uchar num0,num1;//1s計(jì)時(shí)
uchar counter0=30;//30s倒計(jì)時(shí)
uchar counter1=30;//30s倒計(jì)時(shí)
bit flag1=0;//正常南北東西切換
bit flag2=0;//應(yīng)急南北東西切換
bit flag3=0;//正常與應(yīng)急切換,初始為正常
uchar temp;//緩存標(biāo)記
//==============================================================================================
void time0_init()
{
        TMOD=0x11;//定時(shí)器0,1均工作于方式1
        TH0=(65535-50000)/256;//定時(shí)50ms
        TL0=(65535-50000)%256;
           TH1=(65535-50000)/256;//定時(shí)50ms
        TL1=(65535-50000)%256;
        ET0=ET1=EA=TR0=1;TR1=0;
}
void delay1m(uint x)//毫秒級(jí)延時(shí)函數(shù)
{
  uint i,j;
  for(i=0;i<x;i++)      //連數(shù)x次,約 x ms
      for(j=0;j<120;j++);   //數(shù)120 次,約1 ms
}
void scan()//數(shù)碼管掃描函數(shù)
{
        wei1=1;wei0=0;led=display[buf[1]];
        delay1m(5);led=0x00;
        wei1=0;wei0=1;led=display[buf[0]];
        delay1m(5);led=0x00;
}
void convert(uchar dat)//將數(shù)據(jù)轉(zhuǎn)成適合數(shù)碼管顯示的數(shù)
{
        buf[0]=dat/10;
        buf[1]=dat%10;
}
void main()//主函數(shù)
{
         time0_init();//初始化
         while(1)
         {
                  if(flag3==0){convert(counter0);}//正常數(shù)據(jù)轉(zhuǎn)換
                 else if(flag3==1){{convert(counter1);}}//應(yīng)急數(shù)據(jù)轉(zhuǎn)換
                  scan();//倒計(jì)時(shí)顯示
                 //===================================================================================
                 if((flag1==0)&&(flag3==0))//南北通行
                 {
                          red0=green1=yellow0=yellow1=1;green0=red1=0;
                         if(counter0<=5){yellow0=red1=0;red0=green0=yellow1=green1=1;}
                 }
                 else if((flag1==1)&&(flag3==0))//東西通行
                 {
                          red1=yellow1=green0=yellow0=1;green1=red0=0;
                         if(counter0<=5){yellow1=red0=0;red1=green1=yellow0=green0=1;}
                 }
                 //===================================================================================
                 if(s3==0)//南北應(yīng)急按鍵
                 {
                         delay1m(3);
                        if(s3==0)
                        {
                            TR0=0;flag3=1;flag2=0;
                                TR1=1;counter1=30;
                       
                        }while(!s3);
                 }
                 if(s2==0)//東西應(yīng)急按鍵
                 {
                         delay1m(3);
                        if(s2==0)
                        {
                                TR0=0;flag3=flag2=1;
                                TR1=1;counter1=30;
                       
                        }while(!s2);
                 }
                 //====================================================================================
                 if((flag2==0)&&(flag3==1))//南北應(yīng)急通行
                 {
                          red0=yellow0=green1=yellow1=1;green0=red1=0;
                         if(counter1<=5){yellow0=red1=0;red0=green0=yellow1=green1=1;}
                 }
                 else if((flag2==1)&&(flag3==1))//東西應(yīng)急通行
                 {
                          red1=yellow1=green0=yellow0=1;green1=red0=0;
                         if(counter1<=5){yellow1=red0=0;red1=green1=yellow0=green0=1;}
                 }
         }
}
void time0_interrupt()interrupt 1
{
        TH0=(65535-50000)/256;//定時(shí)50ms
        TL0=(65535-50000)%256;
        num0++;
        if(num0==20)//1s時(shí)間
        {
                num0=0;counter0--;
                if(counter0==0){counter0=30;flag1=~flag1;}
        }

}
void time1_interrupt()interrupt 3
{
        TH1=(65535-50000)/256;//定時(shí)50ms
        TL1=(65535-50000)%256;
        num1++;
        if(num1==20)//1s時(shí)間
        {
                num1=0;counter1--;
                if(counter1==0){TR0=1;flag3=0;}
        }
}

交通燈.png (96.71 KB, 下載次數(shù): 42)

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

使用道具 舉報(bào)

沙發(fā)
ID:650094 發(fā)表于 2019-11-26 17:04 | 只看該作者
為什么下載了執(zhí)行不了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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