希望对大家有帮助
单片机源程序如下:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit wei_dx1=P2^0;
- sbit wei_dx2=P2^1;
- sbit wei_nb1=P3^6;
- sbit wei_nb2=P3^7;
- sbit nb_y=P2^2;
- sbit nb_g=P2^3;
- sbit nb_r=P2^4;
- sbit dx_y=P2^5;
- sbit dx_g=P2^6;
- sbit dx_r=P2^7;
- sbit choic=P1^7;
- sbit kongzhi=P1^6;
- uchar count=0;
- uchar time_long=25,time_short=20;
- uchar flag_1s,flag_500ms,flag_light=0,flag_long=1;
- uchar show_nb=25,show_dx=30,time_nb=0,time_dx=0;
- uchar code show[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极
- //uchar code show[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=500;y>0;y--);
- }
- void Timer0Init(void)
- {
- EA = 1;
- TMOD = 0X11;
- ET0 = 1;
- TR0 = 1;
- TH0 = 0x3c;
- TL0 = 0xb0;
- }
- void time(void) interrupt 1
- {
- TH0 = 0x3c;
- TL0 = 0xb0;
- count++;flag_light++;
- if(count==20)
- {
- count=0;
- flag_1s=1;
- }
- if(flag_light % 12 == 0)
- flag_500ms = ~flag_500ms;
- }
- void do_time()
- {
- if(flag_long==1)
- {
- if(flag_1s==1) //南北绿灯亮长
- {
- flag_1s=0;
- show_nb--;
- show_dx--;
- if(show_nb==0)
- {
- show_nb=5;
- }
- if(show_dx==0)
- {
- flag_long=0;
- time_dx=1;
- time_nb=0;
- }
- }
- }
- if(flag_long==0)
- {
- if(flag_1s==1) //东西绿灯亮短
- {
- flag_1s=0;
- show_nb--;
- show_dx--;
- if(show_dx==0)
- {
- show_dx=5;
- }
- if(show_nb==0)
- {
- flag_long=1;
- time_dx=0;
- time_nb=1;
- }
- }
- }
- }
- //红绿灯显示
- void light()
- {
- if(flag_long==1)
- {
- if(show_dx>5)
- {
- nb_g=0;
- nb_y=1;
- nb_r=1;
- dx_g=1;
- dx_y=1;
- dx_r=0;
- }
- else if(show_dx<=5)
- {
- nb_g=1;
- nb_r=1;
- dx_g=1;
- dx_y=1;
- dx_r=0;
- if(flag_500ms==0) //黄灯闪烁
- {
- nb_y=0; //亮
- }
- else
- {
- nb_y=1; //灭
- }
- }
- }
- if(flag_long==0)
- {
- if(show_nb>5)
- {
- nb_g=1;
- nb_y=1;
- nb_r=0;
- dx_g=0;
- dx_y=1;
- dx_r=1;
- }
- else if(show_nb<=5)
- {
- nb_g=1;
- nb_r=0;
- nb_y=1;
- dx_g=1;
- dx_r=1;
- if(flag_500ms==0) //黄灯闪烁
- {
- dx_y=0; //亮
- }
- else
- {
- dx_y=1; //灭
- }
- }
- }
- }
- void display()
- {
- P0=0xff; //共阴极改
- wei_dx1=0;
- wei_dx2=0;
- wei_nb1=0;
- wei_nb2=0;
- P0=show[show_nb%10];
- wei_dx1=1;
- wei_dx2=1;
- wei_nb1=0;
- wei_nb2=1;
- delay(3);
- P0=0xff;
- P0=show[show_nb/10];
- wei_dx1=1;
- wei_dx2=1;
- wei_nb1=1;
- wei_nb2=0;
- delay(3);
- P0=0xff;
- P0=show[show_dx%10];
- wei_dx1=0;
- wei_dx2=1;
- wei_nb1=1;
- wei_nb2=1;
- delay(3);
- P0=0xff;
- P0=show[show_dx/10];
- wei_dx1=1;
- wei_dx2=0;
- wei_nb1=1;
- wei_nb2=1;
- delay(3);
- }
- void main()
- {
- choic=1;
- kongzhi=1;
- Timer0Init();
- while(1)
- {
- if(choic==1)
- {
- do_time();
- light();
- if(time_dx==1)
- {
- show_nb=time_long;
- show_dx=time_short;
- time_dx=0; //重置0,不然time_dx==1括号里的程序会一直赋值
- };
- if(time_nb==1)
- {
- show_nb=time_long;
- show_dx=30;
- time_nb=0;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
太阳能.zip
(19.59 KB, 下载次数: 6)
|