- /*******************************************************
- 红绿灯//需验证程序
- *******************************************************/
- #include "reg51.h"
- #define uchar unsigned char
- #define uint unsigned int
- sfr AUXR=0x8E;
- uchar shijian=0,aa=0,deng=3;
- void init()
- {
- AUXR &= 0x7F;
- TMOD &= 0xF0;
- TL0 = 0x00;
- TH0 = 0x4C;
- TF0 = 0;
- TR0 = 1;
- ET0=1;
- EA=1;
- P0 = 0xff;
- }
- void main()
- {
- init();
- while(1)
- {
- if(shijian==0)
- {
- switch(deng)
- {
- case 0:shijian=70;deng=1;break;//绿
- case 1:shijian=70;deng=2;break;//黄
- case 2:shijian=70;deng=3;break;//红
- case 3:shijian=70;deng=0;break;//黄
- }
- }
- /*时间每秒减一*/
- switch(deng)
- {
- case 0:
- if(shijian%2==0)
- P1=0x00;
- if(shijian%2==1)
- P1=0xff;
- break;
- case 1:P1=0xff;break;//绿
- case 2:
- if(shijian%2==0)
- P1=0x00;
- if(shijian%2==1)
- P1=0xff;
- break;
- case 3:P1=0x00;break;//红
- }
- }
- }
- void T0_time() interrupt 1
- {
- aa++;
- if(aa==50)
- {
- aa=0;
- shijian--;
- }
- }
- 简单的这样写就可以
复制代码 |