#include<reg51.h>
unsigned char dispbuf[4];
unsigned int getdata;
unsigned long temp;
sbit ST=P3^0;
sbit OE=P3^1;
sbit EOC=P3^2;
sbit CLK=P3^3;
sbit A0=P3^4;
sbit B0=P3^5;
sbit C0=P3^6;
sbit LCD_RS=P1^0;
sbit LCD_RW=P1^1;
sbit LCD_E=P1^2;
#define LCD_DATA P2
unsigned char LcdBuf1[]={"voltmeter"};
unsigned char LcdBuf2[]={"Voltage:0.00V"};
void delay(unsigned int time)
{
while(time--);
}
void LCD_EN(void)
{
LCD_E=1;
delay(100);
LCD_E=0;
}
void WriteCommandLcd(unsigned char command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DATA=command;
LCD_EN();
}
void display_xy(unsigned char x,unsigned char y)
{
if(y==1)
x+=0x40;
x+=0x80;
WriteCommandLcd(x);
}
void lcd_init(void)
{
WriteCommandLcd(0x38);
WriteCommandLcd(0x08);
WriteCommandLcd(0x01);
WriteCommandLcd(0x06);
WriteCommandLcd(0x0c);
}
void WriteDataLcd(unsigned char wdata)
{
LCD_RS=1;
LCD_RW=0;
LCD_DATA=wdata;
LCD_EN();
}
void display_string(unsigned char x,unsigned char y,unsigned char *s)
{
display_xy(x,y);
while(*s)
{
WriteDataLcd(*s);
s++;
}
}
void display_char(unsigned char x,unsigned char y,unsigned char dat)
{
display_xy(x,y);
WriteDataLcd(dat);
}
void TimeInitial(void)
{
TMOD=0x10;
TH1=(65536-200)/256;
TL1=(65536-200)%256;
EA=1;
ET1=1;
TR1=1;
}
void passageway(void)
{
A0=1;
B0=1;
C0=0;
}
void start(void)
{
ST=0;
OE=0;
ST=1;
ST=0;
}
void read_dat(void)
{
OE=1;
getdata=P0;
OE=0;
}
void dat_change(void)
{
unsigned int voltage_val;
temp=getdata*1.0/255*500;
dispbuf[0]=temp%1000%100%10;
dispbuf[1]=temp%1000%100/10;
dispbuf[2]=temp%1000/100;
dispbuf[3]=temp/1000;
LcdBuf2[10]=dispbuf[2]+0x30;
LcdBuf2[11]='.';
LcdBuf2[12]=dispbuf[1]+0x30;
LcdBuf2[13]=dispbuf[0]+0x30;
}
main()
{
TimeInitial();
lcd_init();
display_string(3,0,LcdBuf1);
while(1)
{
passageway();
start();
while(EOC==0);
read_dat();
dat_change();
display_string(0,1,LcdBuf2);
}
}
void T1_int(void) interrupt 3 using 0
{
TH1=(65536-200)/256;
TL1=(65536-200)%256;
CLK=~CLK;
}
数字电压表仿真+hex.zip
(21.24 KB, 下载次数: 38)
|