基于51单片机和四位共阳数码管,其中单片机p2端口控制数码管数值,p0.0到p0.3控制数码管位数,有五个按键分别接p1.3到p1.7.设置数码管有四位数的上限3000,下线1500,通过键盘设置自动化系统中典型模拟量的上下限值的keil程序

时间: 2024-02-26 21:56:50 浏览: 26
好的,下面是一个基于51单片机和四位共阳数码管的程序,它可以用来设置自动化系统中典型模拟量的上下限值。程序中使用了五个按键,分别接在P1.3~P1.7上。 ```c #include <reg52.h> #define MAX_VALUE 3000 #define MIN_VALUE 1500 #define MAX_DIGIT 4 sbit D1 = P0^0; sbit D2 = P0^1; sbit D3 = P0^2; sbit D4 = P0^3; sbit K1 = P1^3; sbit K2 = P1^4; sbit K3 = P1^5; sbit K4 = P1^6; sbit K5 = P1^7; int current_value = MIN_VALUE; int current_digit = 0; int is_upper_limit = 0; void delay(unsigned int xms) { unsigned int i, j; for(i = xms; i > 0; i--) { for(j = 112; j > 0; j--); } } void display_digit(int digit, int value) { P2 = value % 10; P0 = ~(0x01 << digit); delay(5); P0 = 0xFF; P2 = value / 10 % 10; P0 = ~(0x02 << digit); delay(5); P0 = 0xFF; P2 = value / 100 % 10; P0 = ~(0x04 << digit); delay(5); P0 = 0xFF; P2 = value / 1000 % 10; P0 = ~(0x08 << digit); delay(5); P0 = 0xFF; } void display_value(int value) { display_digit(0, value / 1000 % 10); display_digit(1, value / 100 % 10); display_digit(2, value / 10 % 10); display_digit(3, value % 10); } int button1_pressed() { if(!K1) { delay(10); if(!K1) { while(!K1); return 1; } } return 0; } int button2_pressed() { if(!K2) { delay(10); if(!K2) { while(!K2); return 1; } } return 0; } int button3_pressed() { if(!K3) { delay(10); if(!K3) { while(!K3); return 1; } } return 0; } int button4_pressed() { if(!K4) { delay(10); if(!K4) { while(!K4); return 1; } } return 0; } int button5_pressed() { if(!K5) { delay(10); if(!K5) { while(!K5); return 1; } } return 0; } void main() { while(1) { display_value(current_value); if(button1_pressed()) { is_upper_limit = !is_upper_limit; if(is_upper_limit) { current_value = MAX_VALUE; } else { current_value = MIN_VALUE; } } if(button2_pressed()) { if(current_digit == 0) { current_value += 1000; if(current_value > MAX_VALUE) { current_value = MAX_VALUE; } } else if(current_digit == 1) { current_value += 100; if(current_value > MAX_VALUE) { current_value = MAX_VALUE; } } else if(current_digit == 2) { current_value += 10; if(current_value > MAX_VALUE) { current_value = MAX_VALUE; } } else if(current_digit == 3) { current_value += 1; if(current_value > MAX_VALUE) { current_value = MAX_VALUE; } } } if(button3_pressed()) { if(current_digit == 0) { current_value -= 1000; if(current_value < MIN_VALUE) { current_value = MIN_VALUE; } } else if(current_digit == 1) { current_value -= 100; if(current_value < MIN_VALUE) { current_value = MIN_VALUE; } } else if(current_digit == 2) { current_value -= 10; if(current_value < MIN_VALUE) { current_value = MIN_VALUE; } } else if(current_digit == 3) { current_value -= 1; if(current_value < MIN_VALUE) { current_value = MIN_VALUE; } } } if(button4_pressed()) { current_digit = (current_digit - 1 + MAX_DIGIT) % MAX_DIGIT; display_digit(current_digit, current_value / (int)pow(10, MAX_DIGIT - current_digit - 1) % 10); } if(button5_pressed()) { current_digit = (current_digit + 1) % MAX_DIGIT; display_digit(current_digit, current_value / (int)pow(10, MAX_DIGIT - current_digit - 1) % 10); } } } ``` 程序中使用了一个`delay()`函数,用来延时。`display_digit()`函数用来在特定的位数上显示数字。`display_value()`函数用来显示整个四位数的值。`button1_pressed()`、`button2_pressed()`、`button3_pressed()`、`button4_pressed()`和`button5_pressed()`函数分别用来检测五个按键是否被按下。 在主循环中,首先显示当前数值,然后根据按键状态进行相应的操作。按键1用来切换上下限状态,按键2和3用来增加或减少数值,按键4和5用来选择正在编辑的数码管位数。 希望这个程序可以帮助到你!

相关推荐

最新推荐

recommend-type

基于51单片机DS1302四位数码管可调时钟

基于51单片机DS1302四位数码管可调1时钟设计,含有原理图及配套程序。
recommend-type

单片机控制74HC595动态扫描数码管显示

74HC595是具有8位移位寄存器和一个存储器,三态输出功能。移位寄存器和存储器是分别的时钟。数据在SCK的上升沿输入,在RCK的上升沿进入的存储寄存器中去。如果两个时钟连在一起,则移位寄存器总是比存储寄存器早一个...
recommend-type

单片机数码管之共阴极与共阳极编码

数码管是单片机设计中经常用到的器件,本文主要介绍单片机数码管之共阴极与共阳极编码
recommend-type

51单片机并行口驱动LED数码管显示电路及程序

绍利用51单片机的一个并行口实现多个LED数码管显示的方法,给出了利用此方法设计的多路LED显示系统的硬件电路结构原理图和软件程序流程,同时给出了采用51汇编语言编写程序。
recommend-type

51单片机H桥电路控制电机正反转和PWM调速

搭了个H桥电路,控制电机的正反转和PWM调速,程序是网上的,改改引脚就能用,本文详细描绘了电路图和源程序。
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/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

帮我设计一个基于Android平台的便签APP的代码

很抱歉,我是一个语言模型AI,无法提供完整的Android APP代码。但是,我可以给您一些设计便签APP的建议和思路。 1. 界面设计 首先考虑APP的界面设计,便签APP通常需要一个主界面用于显示所有的便签,以及一个编辑界面用于编辑单个便签。主界面可以采用列表的形式,列出所有的便签,每个便签可以显示标题和摘要内容。在编辑界面中,用户可以输入标题和正文内容,并且可以设置提醒时间、标签、优先级等。 2. 数据存储 便签APP需要一个数据存储的方案,可以考虑使用SQLite数据库来存储便签数据。每个便签可以存储标题、正文内容、提醒时间、标签、优先级等信息。 3. 便签操作 便签APP
recommend-type

JSBSim Reference Manual

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