#include<reg51.h> #include<intrins.h> #include<string.h> #define uchar unsigned char #define uint unsigned int sbit CS=P1^0; sbit CLK=P1^1; sbit DIO=P1^2; sbit RS=P2^0; sbit RW=P2^1; sbit E=P2^2; #define LCD_PORT P0 uchar Disp_Buff1[]=" VOLTAGE: 0.00V"; uchar Disp_Buff2[16]; #define delay4us(){_nop_();_nop_(); _nop_();_nop();} void delay_ms(uint x) { uchar t; while(x--); for(t=0;t<120;t++); } bit Read_LCD_Busy_Flag() { uchar result; LCD_PORT=0xFF; RS=0;RW=1; E=1; delay4us(); result=P0; E=0; return(result&0x80)?1:0; } void Write_LCD_Command(uchar cmd) { while(Read_LCD_Busy_Flag()); RS=0; RW=0; E=0; _nop()_; _nop()_; LCD_PORT=cmd; delay4us(); E=1; delay4us(); E=0; } void Write_LCD_Data(uchar dat) { while(Read_LCD_Busy_Flag()); RS=1; RW=0; E=0; LCD_PORT=dat; delay4us(); E=1; delay4us(); E=0; } void LCD_Show_String(uchar r,uchar c,char *s) { uchar i=0; uchar code DDRAM[]={0x80,0xC0}; Write_LCD_Command(DDRAM[r]|c); while(s[i]&&i<16) Write_LCD_Data(s[i++]); } void LCD_Initialise() { Write_LCD_Command(0x38); delay_ms(1); Write_LCD_Command(0x0C); delay_ms(1); Write_LCD_Command(0x06); delay_ms(1); Write_LCD_Command(0x01); delay_ms(1); } uchar Get_AD_Result() { uchar i, dat1=0, dat2=0; CS=0; CLK=0; DIO=1; _nop()_;_nop()_; CLK=1; _nop()_;_nop()_; CLK=0; DIO=1; _nop()_;_nop()_; CLK=1; _nop()_;_nop()_; CLK=0; DIO=0; _nop()_;_nop()_; CLK=1; _nop()_;_nop()_; CLK=0; DIO=1; _nop()_;_nop()_; for(i=0;i<8;i++) { CLK=1; _nop()_;_nop()_; CLK=0; _nop()_;_nop()_; dat1=(dat1<<1)|DIO; } for(i=0;i<8;I++) { dat2=dat2|((uchar)DIO<<i); CLK=1; _nop()_;_nop()_; CLK=0; _nop()_;_nop()_; } CS=1; return(dat1==dat2)?dat1:0x00; } void main() { uchar i,AD;uint d; LCD_Initialise(); while(1) { AD=Get_AD_Result(); d= A/D *500.0*2/511.0; Disp_Buff1[11]d/100 +'0'; Disp_Buff1[13]=d/10%10 +'0'; Disp_Buff1[14]=d%10 +'0'; i=(uint)AD*16/255; memset(Disp_Buff2,'\xFF',i); memset(Disp_Buff2 +i,'\xDB',16-i); LCD_show_String(1,0,Disp_Buff2); } }