片段代码: //名称:定时器控制交通信号灯 //说明: // 1.东西向绿灯与南北向红灯亮5s; // 2.东西向绿灯灭,黄灯闪烁5次; // 3.东西向红灯与南北向绿灯亮5s; // 4.南北向绿灯灭,黄灯闪烁5次; #include #define uchar unsigned char #define uint unsigned int sbit red_a=P0^0; sbit yellow_a=P0^1; sbit green_a=P0^2; sbit red_b=P0^3; sbit yellow_b=P0^4; sbit green_b=P0^5; uchar time_coun t=0,flash_count=0,operation_type=1; //T0中断函数 void T0_INT() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; switch (operation_type) { case 1: red_a=0;yellow_a=0;green_a=1; red_b=1;yellow_b=0;green_b=0; //50ms*100=5s if(++time_count!=100) return; time_count=0; operation_type=2;//下一操作 break; case 2: if(++time_count!=8) return; time_count=0; yellow_a=!yellow_a;green_a=0; //闪烁5次 t=0,flash_count=0,operation_type=1; //T0中断函数 void T0_INT() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; switch (operation_type) { case 1: red_a=0;yellow_a=0;green_a=1; red_b=1;yellow_b=0;green_b=0; //50ms*100=5s if(++time_count!=100) return; time_count=0; operation_type=2;//下一操作 break; case 2: if(++time_count!=8) return; time_count=0; yellow_a=!yellow_a;green_a=0; //闪烁5次