|
led模擬交通燈 新手福利c語言程序代碼
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P1^0; //東西向燈
sbit YELLOW_A=P1^1;
sbit GREEN_A=P1^2;
sbit RED_B=P1^3; //南北向燈
sbit YELLOW_B=P1^4;
sbit GREEN_B=P1^5;
uchar Flash_Count=0,Operation_Type=1;//閃爍次數(shù),操作類型變量
//延時
void DelayMS(uint x)
{
uchar i;while(x--)
for(i=0;i<120;i++);
}
//交通燈切換
void Traffic_Light()
{
switch(Operation_Type)
{
case 1://東西向綠燈與南北向紅燈亮
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
break;
case 2://東西向黃燈閃爍,綠燈關(guān)閉
DelayMS(300);
YELLOW_A=~YELLOW_A;
YELLOW_B=~YELLOW_B;
GREEN_A=0;
if(++Flash_Count!=10) return;//閃爍5次
Flash_Count=0;
Operation_Type=3;break;
case 3://東西向紅燈,南北向綠燈亮
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
DelayMS(2000);
Operation_Type=4;break;
case 4://南北向黃燈閃爍5次
DelayMS(300);
YELLOW_B=~YELLOW_B;
YELLOW_A=~YELLOW_A;
GREEN_B=0;
if(++Flash_Count!=10)
return;
Flash_Count=0;
Operation_Type=1;
}
}
//主程序
void main()
{
while(1)Traffic_Light();
}
|
-
1.png
(37.27 KB, 下載次數(shù): 152)
下載附件
2018-1-15 14:06 上傳
電路圖
-
-
led交通燈keil.rar
2018-1-15 14:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
17.2 KB, 下載次數(shù): 14, 下載積分: 黑幣 -5
軟件部分
-
-
led交通燈仿真.rar
2018-1-15 14:03 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
32.04 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5
仿真部分
評分
-
查看全部評分
|