#include <STC8h.H>
#include <stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
// uint Get_ADC12bitResult(uchar channel);
//void iO();
sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
void delay(unsigned int ms) //@11.0592MHz
{
unsigned char data i, j;
while(ms--)
{
i = 15;
j = 90;
do
{
while (--j);
} while (--i);
}}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
delay(1);
EN_SET;
delay(1);
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
delay(1);
EN_CLR;
delay(1);
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
delay(1);
EN_CLR;
delay(1);
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
delay(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while ((*s)!='\0')
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
delay(5);
LCD_Write_Com(0x38);
delay(5);
LCD_Write_Com(0x38);
delay(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
delay(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
void adc_init() {
ADCCFG =ADCCFG|0x20; // 将P1.0设置为ADC功能
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = 0x80;
}
unsigned int adc_read()
{
ADC_CONTR |= 0x40;
delay(3);
while ((ADC_CONTR&0x20)==0);
ADC_CONTR &= ~0x20;
return (ADC_RES << 8) + ADC_RESL;
}
void main() {
unsigned int voltage;
unsigned char i;
char str[16];
LCD_Init();
adc_init();
while (1) {
voltage = adc_read();
sprintf(str, "Voltage: %.2fV", voltage * 5.0 / 4096);
LCD_Write_Com(0x80); // 设置光标位置为第一行第一列
for ( i = 0; i < 16; i++) {
LCD_Write_Data(str[i]);
}
delay(1000); // 延时1秒钟
}
}
有没有大佬回答!这个显示没有报错,但是运行不了,是哪个原因?
|