標(biāo)題:
單片機(jī)+adc0808與1602測電壓,只能測一半?
[打印本頁]
作者:
longzhihao
時間:
2021-4-18 20:44
標(biāo)題:
單片機(jī)+adc0808與1602測電壓,只能測一半?
ADC0808 轉(zhuǎn)換測電壓,當(dāng)滑動變阻器在百分之50以下就是2.5v以下時很準(zhǔn),超過2.5v就從0開始記。求助。。
另外,我還想顯示電流和電功率,請問該如何做呢?
單片機(jī)源程序如下:
#include <reg51.h>
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
sbit CLOCK = P3^1;
sbit EOC = P3^2;
sbit START = P3^3;
sbit OE = P3^4;
uchar LCDDSY1[]={"V: . I: . "}; //第一行顯示數(shù)據(jù)//
uchar LCDDSY2[]={"P: . "}; //第二行顯示數(shù)據(jù)//
uchar n;//adc轉(zhuǎn)換數(shù)據(jù)
void DelayMS(uint ms) //延時子程序//
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
uchar Read_LCD_State()
{
uchar state;
RS=0;RW=1;EN=1;DelayMS(1);
state=P0;
EN = 0;DelayMS(1);
return state;
}
void LCD_Busy_Wait()
{
while((Read_LCD_State()&0x80)==0x80);//DB7=1時,忙
DelayMS(5);
}
void Write_LCD_Data(uchar dat)
{
LCD_Busy_Wait();
RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0; //en使能端,給高脈沖為寫操作
}
void Write_LCD_Command(uchar cmd)
{
LCD_Busy_Wait();
RS=0;RW=0;EN=0;P0=cmd;EN=1;DelayMS(1);EN=0;
}
void Init_LCD()
{
Write_LCD_Command(0x38); // 兩行顯示,5*8點(diǎn)陣
DelayMS(1);
Write_LCD_Command(0x01); //清屏
DelayMS(1);
Write_LCD_Command(0x06); //數(shù)據(jù)讀寫操作后,AC自動加一,畫面不動
DelayMS(1);
Write_LCD_Command(0x0c); // 顯示開,光標(biāo)關(guān),閃爍關(guān)
DelayMS(1);
}
void Set_LCD_POS(uchar p)
{
Write_LCD_Command(p|0x80);
}
void change()
{ uint a;
a=n*500/255;
LCDDSY1[2]=a/100+0x30;
LCDDSY1[4]=a%100/10+0x30;
LCDDSY1[5]=a%10+0x30;
}
void display()
{ uchar i=0;
change();
LCDDSY1[6]='v';
Set_LCD_POS(0x02);i=0;
while(LCDDSY1[i]!='\0')
{
Write_LCD_Data(LCDDSY1[i]);
i++;
}
}
void timer0() interrupt 1
{
CLOCK=~CLOCK;
}
uchar ADC()
{
EOC=1;TR0=1;START=0;OE=0;DelayMS(50);
START=1;DelayMS(50);
START=0;
while(EOC==0);
OE=1;
n=P1;
DelayMS(1);
OE=0;
TR0=0;
return n;
}
void main()
{
TMOD=0x02;
TH0=0x14;
TL0=0x00;
IE=0x82;
TR0=1;
Init_LCD();DelayMS(1);
while(1)
{ ADC();
display();
}
}
復(fù)制代碼
作者:
longzhihao
時間:
2021-4-18 20:51
已經(jīng)弄好了,500換成500.0就ok了 但是不知道為什么。。。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1