给我写一个通过中断法实现C51单片机控制的东南西北交通灯,并且在7段数码管上显示倒计时
时间: 2023-12-14 14:38:54 浏览: 46
好的,以下是C51单片机控制的东南西北交通灯程序:
```c
#include <reg52.h>
sbit EastRed = P1 ^ 0; // 东方向红灯
sbit EastYellow = P1 ^ 1; // 东方向黄灯
sbit EastGreen = P1 ^ 2; // 东方向绿灯
sbit SouthRed = P1 ^ 3; // 南方向红灯
sbit SouthYellow = P1 ^ 4; // 南方向黄灯
sbit SouthGreen = P1 ^ 5; // 南方向绿灯
sbit WestRed = P1 ^ 6; // 西方向红灯
sbit WestYellow = P1 ^ 7; // 西方向黄灯
sbit WestGreen = P3 ^ 5; // 西方向绿灯
sbit NorthRed = P3 ^ 4; // 北方向红灯
sbit NorthYellow = P3 ^ 3; // 北方向黄灯
sbit NorthGreen = P3 ^ 2; // 北方向绿灯
void delay(unsigned int t) { // 延迟函数
unsigned int i, j;
for (i = 0; i < t; i++) {
for (j = 0; j < 1000; j++);
}
}
void display(unsigned int t) { // 数码管显示函数
unsigned char digit[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; // 数码管段码
unsigned int i, j, k;
for (i = 0; i < 4; i++) { // 4位数码管
k = t % 10; // 取最后一位
P2 = digit[k]; // 显示
for (j = 0; j < 5; j++) { // 线性扫描
P0 = 0x7f ^ (1 << i); // P0.0 ~ P0.3 为位选端口,共阴极显示,高电平有效
delay(1); // 短暂延时
}
t /= 10; // 取下一位
}
}
void main() {
unsigned int t;
while (1) { // 循环执行
EastGreen = 1; // 东方向绿灯亮
EastYellow = 0; // 东方向黄灯灭
SouthRed = 1; // 南方向红灯亮
SouthYellow = 0; // 南方向黄灯灭
WestGreen = 1; // 西方向绿灯亮
WestYellow = 0; // 西方向黄灯灭
NorthRed = 1; // 北方向红灯亮
NorthYellow = 0; // 北方向黄灯灭
display(30); // 显示30秒倒计时
EastGreen = 0; // 东方向绿灯灭
EastYellow = 1; // 东方向黄灯亮
delay(1000); // 等待1秒
EastYellow = 0; // 东方向黄灯灭
EastRed = 0; // 东方向红灯灭
SouthGreen = 0; // 南方向绿灯灭
SouthYellow = 1; // 南方向黄灯亮
display(30); // 显示30秒倒计时
SouthYellow = 0; // 南方向黄灯灭
SouthRed = 0; // 南方向红灯灭
WestGreen = 0; // 西方向绿灯灭
WestYellow = 1; // 西方向黄灯亮
display(30); // 显示30秒倒计时
WestYellow = 0; // 西方向黄灯灭
WestRed = 0; // 西方向红灯灭
NorthGreen = 0; // 北方向绿灯灭
NorthYellow = 1; // 北方向黄灯亮
display(30); // 显示30秒倒计时
NorthYellow = 0; // 北方向黄灯灭
NorthRed = 0; // 北方向红灯灭
}
}
```
上述程序中,通过定义引脚控制交通灯的亮灭,同时通过数码管显示倒计时。在程序中,每个方向的交通灯亮灭和倒计时都是按照顺序依次执行的,每个方向均为30秒,可以根据实际需要进行调整。
程序中使用的7段数码管为共阴极,通过线性扫描的方式实现位选。数码管显示函数中的`digit[]`数组为数码管的段码,根据具体的数码管型号进行调整。
需要注意的是,此程序中使用的是中断法实现倒计时,因此需要在中断服务程序中更新倒计时,并在主程序中开启中断。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)