基于51单片机和四位共阳数码管,其中单片机p2端口控制数码管数值,p0.0到p0.3控制数码管位数,有五个按键分别接p1.3到p1.7.设置数码管有四位数的上限3000,下线1500,通过键盘设置自动化系统中典型模拟量的上下限值的keil程序
时间: 2024-02-26 09:56:50 浏览: 71
好的,下面是一个基于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用来选择正在编辑的数码管位数。
希望这个程序可以帮助到你!
阅读全文