您的位置 首页 ADAS

二极管(STC89C52): 编写程序完成相似交通灯

一硬件设计二软件设计1程序功能编写程序实现类似交通灯:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮,红灯亮后,南北向由红灯变为绿

一. 硬件规划

二. 软件规划
1. 程序功用
编写程序完成相似交通灯:东西向绿灯亮若干秒,黄灯闪耀5次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪耀5尔后变红灯,东西向变绿灯,如此重复
2. 程序源码
#include
#define uchar unsigned char
#define uint unsigned int
sbit red_east_west = P1 ^ 0;
sbit yellow_east_west = P1 ^ 1;
sbit green_east_west = P1 ^ 2;
sbit red_south_north = P1 ^ 3;
sbit yellow_south_north = P1 ^ 4;
sbit green_south_north = P1 ^ 5;
uchar flash_count = 0;
uchar operation_type = 1;
void delayms(uint xms);
void traffic_light();
void main()
{
while(1)
traffic_light();
}
void traffic_light()
{
switch(operation_type)
{
case 1: //green between east and west, red between south and north
red_east_west = 1; yellow_east_west = 1; green_east_west = 0;
red_south_north = 0; yellow_south_north = 1; green_south_north = 1;
delayms(2000);
operation_type = 2;
break;
case 2: //yellow flash between east and west, green close between east and west
delayms(300);
yellow_east_west = ~yellow_east_west; green_east_west = 1;
if (++flash_count != 10)
return;
flash_count = 0;
operation_type = 3;
break;
case 3: //red between east and west, green between south and north
red_east_west = 0; yellow_east_west = 1; green_east_west = 1;
red_south_north = 1; yellow_south_north = 1; green_south_north = 0;
delayms(2000);
operation_type = 4;
break;
case 4: //yellow flash between south and north
delayms(300);
yellow_south_north = ~yellow_south_north; green_south_north = 1;
if (++flash_count != 10)
return;
flash_count = 0;
operation_type = 1;
break;
}
}
void delayms(uint xms)
{
uint i, j;
for (i = xms; i > 0; i–)
for (j = 110; j > 0; j–)
;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/adas/262296.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部