ADC0823得到的数据通过51单片机显示在LCD显示屏的程序
时间: 2023-06-13 15:02:48 浏览: 107
以下是ADC0823得到的数据通过51单片机显示在LCD显示屏的程序:
```
#include <reg51.h>
#define LCD_DATA P2
sbit RS = P3^0;
sbit RW = P3^1;
sbit EN = P3^2;
unsigned int adc_value;
void delay_ms(unsigned int ms)
{
unsigned int i, j;
for(i = 0; i < ms; i++)
{
for(j = 0; j < 114; j++);
}
}
void init_lcd()
{
delay_ms(15);
lcd_cmd(0x38);
delay_ms(5);
lcd_cmd(0x38);
delay_ms(5);
lcd_cmd(0x38);
delay_ms(5);
lcd_cmd(0x38);
lcd_cmd(0x08);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x0c);
}
void lcd_cmd(unsigned char cmd)
{
LCD_DATA = cmd;
RS = 0;
RW = 0;
EN = 1;
delay_ms(1);
EN = 0;
}
void lcd_data(unsigned char dat)
{
LCD_DATA = dat;
RS = 1;
RW = 0;
EN = 1;
delay_ms(1);
EN = 0;
}
void main()
{
unsigned char str[16];
init_lcd();
while(1)
{
adc_value = read_adc();
lcd_cmd(0x80);
sprintf(str, "ADC Value:%4d", adc_value);
for(i = 0; i < 16; i++)
{
lcd_data(str[i]);
}
delay_ms(500);
}
}
unsigned int read_adc()
{
unsigned int adc_in;
ADC0802 = 0x80;
delay_ms(1);
adc_in = ADC0802;
adc_in = (adc_in << 8) | ADC0802;
return adc_in;
}
```
说明:
- LCD显示屏通过P2口连接到51单片机;
- RS、RW、EN分别连接到P3.0、P3.1、P3.2;
- 通过read_adc()函数读取ADC0823的数据;
- sprintf()函数将ADC值转换成字符串;
- 循环输出字符串到LCD显示屏,每次间隔500毫秒。
阅读全文