程序:
#include<reg52.h>
#define LCD_DB P2
sbit LCD_RS = P1^0;
sbit LCD_RW = P1^1;
sbit LCD_EN = P1^2;
sbit AD_OE=P3^1;
sbit AD_EOC=P3^2;
sbit AD_A=P3^4;
sbit AD_B=P3^5;
sbit AD_C=P3^6;
sbit AD_START=P3^0;
unsigned char Zh1[]=" voltmeter ";
unsigned char Zh2[]=" Voltage: ";
unsigned char sj,ai;
int AD_sj=0,AD_ai=0;
void Busy() //检忙
{
unsigned char sta;
LCD_DB = 0xff;
LCD_RS = 0;
LCD_RW = 1;
do
{
LCD_EN = 1;
sta = LCD_DB;
LCD_EN = 0;
}
while(sta&0x80);
}
void Lcd_Write_Cmd(unsigned char cmd) //命令
{
Busy();
LCD_RS = 0;
LCD_RW = 0;
LCD_DB = cmd;
LCD_EN = 1;
LCD_EN = 0;
}
void Lcd_Write_Data(unsigned char dat) //数据
{
Busy();
LCD_RS = 1;
LCD_RW = 0;
LCD_DB = dat;
LCD_EN = 1;
LCD_EN = 0;
}
void InitLcd() //初始化
{
Lcd_Write_Cmd(0x38);
Lcd_Write_Cmd(0x08);
Lcd_Write_Cmd(0x01);
Lcd_Write_Cmd(0x06);
Lcd_Write_Cmd(0x0c);
}
void LcdSetCursor(unsigned char x,unsigned char y) //坐标
{
unsigned char addr;
if(y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
Lcd_Write_Cmd(addr|0X80);
}
void LcdShowStr(unsigned char x,unsigned char y,unsigned char *s) //字符串
{
LcdSetCursor(x,y);
while(*s)
{
Lcd_Write_Data(*s);
s++;
}
}
//AD转换
void AD_xsgs() //显示格式
{
AD_sj=5.00/255*sj*100;
Zh2[10]=(AD_sj/100)+0x30;
Zh2[11]='.';
Zh2[12]=(AD_sj%100/10)+0x30;
Zh2[13]=(AD_sj%100%10)+0x30;
Zh2[14]='V';
}
void AD_csh() //AD主程序
{
AD_A=0;
AD_B=0;
AD_C=0;
AD_START=1;
AD_START=0;
AD_OE=0;
AD_START=1;
AD_START=0;
while(AD_EOC==1);
AD_OE=1;
sj=P0;
AD_OE=0;
}
void ad ()
{
AD_A=1;
AD_B=0;
AD_C=0;
AD_START=1;
AD_START=0;
AD_OE=0;
AD_START=1;
AD_START=0;
while(AD_EOC==1);
AD_OE=1;
ai=P0;
AD_OE=0;
}
void main()
{
InitLcd();
LcdShowStr(0,0,Zh1);
LcdShowStr(0,1,Zh2);
while(1)
{
AD_csh();
AD_xsgs();
ad ();
LcdShowStr(0,1,Zh2);
}
}
|