|
//-----------------------------------------------------------------------------------
// 說(shuō)明:調(diào)整VR1時(shí),adc0832將模擬電壓轉(zhuǎn)換為數(shù)字電壓顯示在1602液晶
// 屏第0行,液晶屏第一行同時(shí)以進(jìn)程條方式顯示當(dāng)前電壓大小。
//-----------------------------------------------------------------------------------
#include<reg51.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
//adc0832引腳定義
sbit CS = P2^0; //片選線
sbit CLK = P2^1;
sbit DIO = P2^2;
sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
#define LCD_PORT P0
uchar Disp_Buffer1[] = "VOLTAGE: 0.00V";
uchar Disp_Buffer2[16];
#define delay4us() {_nop_();_nop_();_nop_();_nop_();}
//--------------------------
//
//-------------------------
void delay_ms(uint x)
{
uchar t;
while(x--)
for(t = 0; t < 120 ;t++);
}
//----------------------------
// du mang
//-----------------------------
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;
}
//-----------------
//xie lcd command
//-----------------
void Write_LCD_Command(uchar cmd)
{
while(Read_LCD_Busy_Flag() );
RS = 0;
RW = 0;
E = 0;
_nop_();
LCD_PORT = cmd;
delay4us();
E = 1;
delay4us();
E = 0;
}
//-------------------
//xie lcd data
//------------------------
void Write_LCD_Data(uchar dat)
{
while (Read_LCD_Busy_Flag() );
RS = 1;
RW = 0;
E = 0;
_nop_();
LCD_PORT = dat;
delay4us();
E = 1;
delay4us();
E = 0;
}
//------------------------------------------------
//在lcd制定行列顯示字符串
//-----------------------------------------------
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++]);
}
//-------------------------------------------------
//LCD初始化
//--------------------------------------
void LCD_Init()
{
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);
}
//獲取ad轉(zhuǎn)換結(jié)果
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_();
dat1=(dat1<<1)|DIO;
}
CS= 1;
return(dat1 = dat2)?dat1:0x00;
}
//-----------
//串口輸出字符串
//------------------
void Putstr(char *s)
{
uchar i=0;
while(s[i])
{
SBUF = s[i++];
while(TI == 0);
TI = 0;
}
}
//------------------------------------------
//------主程序
//-----------------------------------------
void main()
{
uchar i,AD;
uint d;
LCD_Init();
SCON = 0X50;
TMOD = 0X20;
PCON = 0X00;
TH1=TL1=0XFD;
IE = 0X91;
IT0 = 1;
TR1 = 1;
while(1)
{
AD = Get_AD_Result();
d = AD*500.0*2/511.0;
Disp_Buffer1[11]=d/100+'0';
Disp_Buffer1[13]=d/10%10+'0';
Disp_Buffer1[11]=d%10+'0';
LCD_Show_String(0,0,Disp_Buffer1);
Putstr(strcat((char*)Disp_Buffer1,"\r\n"));
delay_ms(50);
i=(uint)AD*16/255;
memset(Disp_Buffer2,'\xff',i);
memset(Disp_Buffer2+i,'\xdb',16-i);
LCD_Show_String(1,0,Disp_Buffer2); //memset是以字節(jié)為單位,初始化內(nèi)存塊。
}
}
|
評(píng)分
-
查看全部評(píng)分
|