四个方向,每个方向9个交通灯,单片机串行控制,支持485通信
全部资料51hei下载地址:
交通灯系统控制模块.rar
(71.27 KB, 下载次数: 16)
- #include <reg51.h>
- #include <intrins.h>
- #include <hc595.h>
- #include <shumaguan.h>
- #define NOP() _nop_()
- #define uchar unsigned char
- #define uint unsigned int
- #define FOSC 11059200L //System frequency
- #define BAUD 9600 //UART baudrate
- sbit RS485E=P3^7;
- uint count;
- uchar second,cnt;
- uchar YELLOW_warning = 5;
- uchar GREEN_pass = 10;
- uchar WORK = 1;
- uchar a;
- uchar buff_on;
- uchar cnt_buff;
- uchar rs485[5];
- void InitTimer0(void)
- {
- TMOD = 0x01;
- TH0 = 0x0F8;
- TL0 = 0x0CD;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- void Timer0Interrupt(void) interrupt 1
- {
- TH0 = 0x0F8;
- TL0 = 0x0CD;
- count++;
- if(count == 500)
- {
- count = 0;
- second++;
- if(second % (GREEN_pass) == 0 || second % (YELLOW_warning + GREEN_pass) == 0 )
- {
-
- cnt++;
- if(cnt == 12)cnt=0;
-
- }
- if(second == YELLOW_warning + GREEN_pass )
- second = 0;
- Test(cnt);
- }
-
- }
- void InitUART(void)
- {
- TMOD |= 0x20;
- SCON |= 0x50;
- TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
- PCON = 0x00;
- ES = 1;
- TR1 = 1;
- }
- void UARTInterrupt(void) interrupt 4
- {
- if(RI)
- {
- a = SBUF;
- if(a==0x55)
- {
- buff_on =1;
- cnt_buff = 0;
- }
- if(buff_on)
- {
- rs485[cnt_buff]=a;
- cnt_buff++;
- if(cnt_buff==5)
- {
- cnt_buff=0;
- buff_on =0;
- }
-
- }
- RI = 0;
- }
- else
- TI = 0;
-
- }
- //void T0Interrupt(void) interrupt 0
- //{
- // uchar i ;
- // TR0 = 0;
- // GREEN_pass++;
- // if(GREEN_pass == 26)
- // GREEN_pass = 10;
- // for(i=0;i<100;i++)
- // {
- // LED_HC595SendData(S,GREEN_pass);
- // LED_HC595SendData(X,GREEN_pass);
- // LED_HC595SendData(Z,GREEN_pass);
- // LED_HC595SendData(Y,GREEN_pass);
- // }
- // TR0 = 1;
- //}
- //void T1Interrupt(void) interrupt 2
- //{
- // WORK =!WORK;
- // TR0=!TR0;
- //}
- void main(void)
- {
- InitTimer0();
- InitUART();
- // IT0=1; //???μ??′¥·¢
- // EX0=1;
- // IT1=1; //???μ??′¥·¢
- // EX1=1;
- SG3 = 0;
- XG3 = 0;
- ZG3 = 0;
- YG3 = 0;
- EA = 1;
- RS485E=0;
- T_HC595SendData(S,GREEN,STRAT);
- T_HC595SendData(Z,RED,STRAT);
- T_HC595SendData(X,GREEN,STRAT);
- T_HC595SendData(Y,RED,STRAT);
- while(1)
- {
-
- // LED_HC595SendData(S,rs485[0]);
- // LED_HC595SendData(X,rs485[1]);
- // LED_HC595SendData(Z,rs485[2]);
- // LED_HC595SendData(Y,rs485[3]);
- if(WORK )
- {
- switch(cnt)
- {
- case 0:
- LED_HC595SendData(S,GREEN_pass - second);
- LED_HC595SendData(X,GREEN_pass - second);
- LED_HC595SendData(Z,(GREEN_pass+YELLOW_warning)*3 - second);
- LED_HC595SendData(Y,(GREEN_pass+YELLOW_warning)*3 - second);
- break;
- case 1:
- LED_HC595SendData(S,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(X,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(Z,(GREEN_pass+YELLOW_warning)*3 - second);
- LED_HC595SendData(Y,(GREEN_pass+YELLOW_warning)*3 - second);
- break;
- case 2:
- LED_HC595SendData(S,GREEN_pass - second);
- LED_HC595SendData(X,GREEN_pass - second);
- LED_HC595SendData(Z,(GREEN_pass+YELLOW_warning)*2 - second);
- LED_HC595SendData(Y,(GREEN_pass+YELLOW_warning)*2 - second);
- break;
- case 3:
- LED_HC595SendData(S,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(X,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(Z,(GREEN_pass+YELLOW_warning)*2 - second);
- LED_HC595SendData(Y,(GREEN_pass+YELLOW_warning)*2- second);
- break;
- case 4:
- LED_HC595SendData(S,GREEN_pass - second);
- LED_HC595SendData(X,GREEN_pass - second);
- LED_HC595SendData(Z,(GREEN_pass+YELLOW_warning) - second);
- LED_HC595SendData(Y,(GREEN_pass+YELLOW_warning) - second);
- break;
- case 5:
- LED_HC595SendData(S,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(X,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(Z,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(Y,GREEN_pass+YELLOW_warning - second);
- break;
- case 6:
- LED_HC595SendData(Z,GREEN_pass - second);
- LED_HC595SendData(Y,GREEN_pass - second);
- LED_HC595SendData(S,(GREEN_pass+YELLOW_warning)*3 - second);
- LED_HC595SendData(X,(GREEN_pass+YELLOW_warning)*3 - second);
- break;
- case 7:
- LED_HC595SendData(Z,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(Y,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(S,(GREEN_pass+YELLOW_warning)*3 - second);
- LED_HC595SendData(X,(GREEN_pass+YELLOW_warning)*3 - second);
- break;
- case 8:
- LED_HC595SendData(Z,GREEN_pass - second);
- LED_HC595SendData(Y,GREEN_pass - second);
- LED_HC595SendData(S,(GREEN_pass+YELLOW_warning)*2 - second);
- LED_HC595SendData(X,(GREEN_pass+YELLOW_warning)*2 - second);
- break;
- case 9:
- LED_HC595SendData(Z,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(Y,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(S,(GREEN_pass+YELLOW_warning)*2 - second);
- LED_HC595SendData(X,(GREEN_pass+YELLOW_warning)*2- second);
- break;
- case 10:
- LED_HC595SendData(Z,GREEN_pass - second);
- LED_HC595SendData(Y,GREEN_pass - second);
- LED_HC595SendData(S,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(X,GREEN_pass+YELLOW_warning - second);
- break;
- case 11:
- LED_HC595SendData(Z,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(Y,GREEN_pass +YELLOW_warning - second);
- LED_HC595SendData(S,GREEN_pass+YELLOW_warning - second);
- LED_HC595SendData(X,GREEN_pass+YELLOW_warning - second);
- break;
- }
-
-
- }
- else
- {
- T_HC595SendData(S,RED,STRAT);
- T_HC595SendData(Z,RED,STRAT);
- T_HC595SendData(X,RED,STRAT);
- T_HC595SendData(Y,RED,STRAT);
- }
- if(rs485[0]==0x55 && rs485[4] == 0x44 )
- {
- if( (rs485[3]==0||rs485[3]==0x011) && (rs485[1]<25 && rs485[2]<15))
- {
- GREEN_pass = rs485[1];
- YELLOW_warning = rs485[2];
- if(rs485[3] == 0x11)
- {
- WORK = 0;
- TR0 = 0;
- }
- else
- {
- WORK = 1;
- TR0 = 1;
- }
- }
-
- }
- }
- }
复制代码
|