写一个51单片机程序自制电压表用液晶屏显示电压值
时间: 2023-10-08 15:09:32 浏览: 64
好的,以下是一个简单的51单片机程序,可以通过ADC转换获取输入的电压值,然后将其显示在液晶屏上。
```c
#include <reg51.h>
#include <intrins.h>
// 定义液晶屏接口
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^2;
sbit D7 = P2^7;
sbit D6 = P2^6;
sbit D5 = P2^5;
sbit D4 = P2^4;
// 定义ADC输入引脚
sbit ADC_IN = P3^0;
// 定义延时函数
void delay(unsigned int i) {
while(i--) {
_nop_();
}
}
// 初始化液晶屏
void init_LCD() {
delay(150);
RS = 0;
RW = 0;
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 1;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 1;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 1;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 1;
EN = 0;
delay(5);
EN = 1;
D7 = 1;
D6 = 0;
D5 = 0;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
EN = 0;
delay(5);
EN = 1;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
EN = 0;
delay(5);
}
// 写入指令
void write_instruction(unsigned char instruction) {
RS = 0;
RW = 0;
EN = 1;
D7 = instruction & 0x80;
D6 = instruction & 0x40;
D5 = instruction & 0x20;
D4 = instruction & 0x10;
EN = 0;
delay(5);
EN = 1;
D7 = instruction & 0x08;
D6 = instruction & 0x04;
D5 = instruction & 0x02;
D4 = instruction & 0x01;
EN = 0;
delay(5);
}
// 写入数据
void write_data(unsigned char data) {
RS = 1;
RW = 0;
EN = 1;
D7 = data & 0x80;
D6 = data & 0x40;
D5 = data & 0x20;
D4 = data & 0x10;
EN = 0;
delay(5);
EN = 1;
D7 = data & 0x08;
D6 = data & 0x04;
D5 = data & 0x02;
D4 = data & 0x01;
EN = 0;
delay(5);
}
// 获取ADC转换结果
unsigned int get_ADC_result() {
unsigned int result;
ADC_IN = 1;
delay(10);
ADC_CONTR = 0x90;
delay(10);
ADC_CONTR |= 0x40;
while(!(ADC_CONTR & 0x10));
result = ADC_RES;
result |= ADC_RESL << 8;
return result;
}
// 将数字转换为字符串
void int_to_str(unsigned int num, unsigned char* str) {
unsigned char i;
for(i = 0; num > 0; i++) {
str[i] = num % 10 + '0';
num /= 10;
}
str[i] = '\0';
}
// 主函数
void main() {
unsigned int voltage;
unsigned char str[6];
init_LCD();
write_instruction(0x01);
write_instruction(0x06);
write_instruction(0x0c);
while(1) {
voltage = get_ADC_result() * 5 / 1024;
int_to_str(voltage, str);
write_instruction(0x80);
write_data('V');
write_data(':');
write_data(' ');
write_data(str[4]);
write_data(str[3]);
write_data(str[2]);
write_data('.');
write_data(str[1]);
write_data(str[0]);
delay(100);
}
}
```
这个程序通过ADC转换获取输入的电压值,然后将其转换为字符串,并显示在液晶屏上。要注意的是,ADC输入引脚必须使用单片机的ADC功能才能正常工作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)