51开发板代码完成以下功能1 开发板上选三个LED,分别设置为 红灯、绿灯、黄灯(板子上都是红色,可以标记一下)其中,红灯10秒,绿灯15秒,黄闪5秒,往返循环 2 数码管显示当前信号灯的倒计时 3 USB转串口,接串口调试助手,可以实时输出:当前灯色+剩余时间

时间: 2024-03-10 11:48:56 浏览: 25
以下是51单片机的代码实现,需要连接三个LED灯,一个数码管和一个USB转串口模块。其中,红灯连接P1.0口,绿灯连接P1.1口,黄灯连接P1.2口,数码管连接P2口,USB转串口模块连接P3口。 ``` #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char // 数码管位选和段选控制 sbit dula = P2^6; sbit wela = P2^7; // LED灯控制 sbit red_led = P1^0; sbit green_led = P1^1; sbit yellow_led = P1^2; // 定义计时器的初值 uint red_time = 10000; // 10秒 uint green_time = 15000; // 15秒 uint yellow_time = 5000; // 5秒 // 定义倒计时标志位 uchar red_flag = 0; uchar green_flag = 0; uchar yellow_flag = 0; // 定义串口发送函数 void send_char(uchar c) { SBUF = c; while(TI == 0); TI = 0; } // 定义数码管显示函数 void display(uint time) { uint i; uchar code num[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f }; for(i = 0; i < 100; i++) { dula = 1; P0 = num[time % 10]; dula = 0; wela = 1; P0 = 0xf7; wela = 0; time /= 10; _nop_(); } } // 定义主函数 void main() { TMOD = 0x01; // 定时器0工作模式1 TH0 = 0xfc; // 定时器0初值 TL0 = 0x18; TR0 = 1; // 启动定时器0 ET0 = 1; // 开启定时器0中断 EA = 1; // 开启总中断 red_led = 1; // 初始为红灯亮 while(1) { if(red_flag) { // 红灯倒计时 display(red_time); send_char('R'); send_char(':'); send_char((red_time / 10000) % 10 + '0'); send_char((red_time / 1000) % 10 + '0'); send_char((red_time / 100) % 10 + '0'); send_char((red_time / 10) % 10 + '0'); send_char('\r'); send_char('\n'); } else if(green_flag) { // 绿灯倒计时 display(green_time); send_char('G'); send_char(':'); send_char((green_time / 10000) % 10 + '0'); send_char((green_time / 1000) % 10 + '0'); send_char((green_time / 100) % 10 + '0'); send_char((green_time / 10) % 10 + '0'); send_char('\r'); send_char('\n'); } else if(yellow_flag) { // 黄灯倒计时 display(yellow_time); send_char('Y'); send_char(':'); send_char((yellow_time / 10000) % 10 + '0'); send_char((yellow_time / 1000) % 10 + '0'); send_char((yellow_time / 100) % 10 + '0'); send_char((yellow_time / 10) % 10 + '0'); send_char('\r'); send_char('\n'); } } } // 定时器0中断处理函数 void timer0() interrupt 1 { static uint count = 0; TH0 = 0xfc; TL0 = 0x18; count++; if(count == 1000) { // 1秒钟计数一次 count = 0; if(red_flag) { // 红灯倒计时 if(red_time > 0) { red_time--; } else { red_time = 10000; red_led = 0; yellow_led = 1; yellow_flag = 1; red_flag = 0; } } else if(green_flag) { // 绿灯倒计时 if(green_time > 0) { green_time--; } else { green_time = 15000; green_led = 0; yellow_led = 1; yellow_flag = 1; green_flag = 0; } } else if(yellow_flag) { // 黄灯倒计时 if(yellow_time > 0) { yellow_time--; } else { yellow_time = 5000; yellow_led = 0; green_led = 1; green_flag = 1; yellow_flag = 0; } } } } ``` 在串口调试助手中设置波特率为9600,连接上USB转串口模块后,即可实时输出当前灯色和剩余时间。

相关推荐

最新推荐

recommend-type

飞腾FT-2000-4开发板上移植Ubuntu18.04.5且更换飞腾内核4.19.8教程

1.PCIe显卡是必须的,因为飞腾开发板没有显示,显卡需自己配,选个AMD主流的显卡就行。 2.有流量安卓手机是必须的,系统装起来后,开发板上是没有接口可以上网的,唯一上网的方式就是使用安卓手机的“USB共享网络”...
recommend-type

Linux开发板启动需要登录密码设置方法

在编译busybox的时候把相关的选项选上,就可以支持用户密码登录了,见下图。 编译好busybox之后,在可执行文件中,可以找一个文件名为login的软链接,就是负责用户验证登录的。
recommend-type

arm开发板上运行的第一个程序

arm开发板上运行的第一个程序,有详细的说明,可以参考一下
recommend-type

Xilinx KC705开发板原理图

Xilinx KC705开发板原理图 Xilinx KC705开发板原理图 Xilinx KC705开发板原理图 Xilinx KC705开发板原理图
recommend-type

nucleo-g070RB开发板-数据手册

nucleo-g070RB开发板-数据手册,包含开发板的资源介绍,以及功能特性等,可查阅原理图及板框尺寸图。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。