Diseño de control de semáforo basado en 51 microcontroladores
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; luz
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3 // luces norte y sur
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
uchar Flash_Count=0,Operation_Type=1; //recuento de flash, tipo de operación variable
Caso "100 ejemplos de capacitación en programación en lenguaje C de un solo chip: basado en 8051 y simulación PROTEUS"
5
Escuela de Información y Mecánica y Ingeniería Eléctrica, Universidad Normal de Shanghai--Ni Jifeng
// Delay
void DelayMS(uint x)
{
uchar i ;
while( x--) for(i=0;i<120;i++);
}
// Cambio de semáforo
void Traffic_Light()
{
switch(Operation_Type)
{
caso 1: // La luz verde en dirección este-oeste está encendida y la luz roja en dirección norte-sur está encendida
p>RED_A=1;YELLOW_A=1;. GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
rotura;
caso 2:
RetrasoMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10) return; //Flash 5 veces
Flash_Count=0;
Operation_Type=3;
romper;
caso 3:
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;<
DelayMS(2000);
Operation_Type=4;
break;
caso 4: //Las luces amarillas norte y sur parpadean 5 veces
RetrasoMS(300);
YELLOW_B=~YELLOW_B
YELLOW_B=1;GREEN_B=1;
RED_B=1;YELLOW_B =1; GREEN_B=0;< GREEN_B=1;
if(++Flash_Count!=10) devuelve;
Flash_Count=0;
Operation_Type= 1;
}
// Programa principal
void main()
{
mientras(1 ) Tráfico_Semáforo( );
}