LCD1602+ADC0832數(shù)字電壓表(asm/c51)
rs bit P2.4
本文引用地址:http://m.butianyuan.cn/article/201611/317452.htmrw bit P2.5
e bit P2.6
clk bit P1.3
do bit P1.4
di bit P1.5
cs bit P1.6
com data 60h
dat data 61h
adcb data 62h
adcs data 63h
adcg data 64h
adc data 65h
chx data 66h
org 0000h
sjmp main
org 0030h
main:mov sp,#30h
clr f0
lcall init
s1:lcall adcx
lcall hdc
lcall top
lcall bot
ljmp s1
init:mov com,#00111000b
lcall write_com
mov com,#00001110b
lcall write_com
mov com,#00000110b
lcall write_com
mov com,#00000001b
lcall write_com
ret
adcx: clr a
setb cs
clr clk
clr cs
setb di
setb clk;第一個脈沖
clr clk
setb clk;第二個脈沖
clr clk
jb p3.0,a1
cpl f0
jnb p3.0,$
a1:mov c,f0
mov di,c
setb clk;第三個脈沖
clr clk
setb clk
setb do
mov r1,#08h
a2:clr clk
nop
mov c,do
rlc a
setb clk
nop
djnz r1,a2
mov adc,a
setb cs
ret
hdc: mov a,adc
mov b,#51
div ab
mov adcb,a
mov a,b
clr c
subb a,#26
mov 00h,c
mov a,b
mov b,#10
mul ab
mov b,#51
div ab
jb 00h,h1
add a,#5
h1:mov adcs,a
mov a,b
clr c
subb a,#26
mov 00h,c
mov a,b
mov b,#10
mul ab
mov b,#51
div ab
jb 00h,h2
add a,#5
h2:mov adcg,a
ret
write_com:clr rs
clr rw
mov p0,com
lcall delay
clr e
setb e
clr e
ret
write_dat:setb rs
clr rw
mov p0,dat
lcall delay
clr e
setb e
clr e
ret
top: mov com,#80h
lcall write_com
mov dptr,#tab1
mov r0,#14
t4:clr a
movc a,@a+dptr
mov dat,a
lcall write_dat
inc dptr
djnz r0,t4
mov dptr,#tab4
mov com,#8eh
lcall write_com
jb f0,t2
mov chx,#00h
sjmp t3
t2:mov chx,#01h
t3:mov a,chx
movc a,@a+dptr
mov dat,a
lcall write_dat
ret
bot: mov com,#0c0h
lcall write_com
mov dptr,#tab2
mov r0,#10
b1:clr a
movc a,@a+dptr
mov dat,a
lcall write_dat
inc dptr
djnz r0,b1
mov com,#0cah
lcall write_com
mov dptr,#tab4
mov a,adcb
movc a,@a+dptr
mov dat,a
lcall write_dat
mov a,#10
movc a,@a+dptr
mov dat,a
lcall write_dat
mov a,adcs
movc a,@a+dptr
mov dat,a
lcall write_dat
mov a,adcg
movc a,@a+dptr
mov dat,a
lcall write_dat
ret
tab1: db "INPUT CHANNEL "
tab2: db " VOLTAGE: "
tab4: db "0123456789."
DELAY: ;誤差 0us
MOV R6,#13H
DL0:
MOV R5,#82H
DJNZ R5,$
DJNZ R6,DL0
RET
end
...................................LCD1602+ADC0832數(shù)字電壓表(已調(diào)試通過).................................
#include
#include
#define uchar unsigned char
#define uint unsigned int;
sbit rs=P2^4;
sbit rw=P2^5;
sbit e=P2^6; //LCD1602控制端
sbit cs=P1^6;
sbit di=P1^5;
sbit dox=P1^4;
sbit clk=P1^3;//ADC0832控制端
sbit key=P3^0;//測量通道選擇按鍵
uchar code table1[]="INPUT CHANNEL "; //顯示數(shù)據(jù)表
uchar code table2[]=" VOLTAGE: ";
uchar code table3[]="0123456789.";
uchar ge,shi,bai; //顯示值存儲變量
uchar adc_data; //A/D轉(zhuǎn)換值存儲變量
void delay() //誤差 0us 5ms延時
{
uchar a,b;
for(b=19;b>0;b--)
for(a=130;a>0;a--);
}
void write_dat(uchar dat) //LCD1602寫數(shù)據(jù)
{
rs=1;
rw=0;
P0=dat;
delay();//待數(shù)據(jù)穩(wěn)定,待LCD1602響應(yīng)完畢,否則LCD不顯示.
e=0;
e=1;
e=0;
}
void write_com(uchar com)//LCD1602寫指令
{
rs=0;
rw=0;
P0=com;
delay();
e=0;
e=1;
e=0;
}
void top()//LCD1602顯示第一行
{
uchar i;
write_com(0x80);
for(i=0;i<14;i++)
{
write_dat(table1[i]);
}
write_dat(table3[(uchar)(F0)]);
}
void bot()//LCD1602顯示第二行
{
uchar i;
write_com(0xc0);
for(i=0;i<10;i++)
{
write_dat(table2[i]);
}
write_dat(table3[(uchar)(bai)]);
write_dat(table3[10]); //顯示小數(shù)點
write_dat(table3[(uchar)(shi)]);
write_dat(table3[(uchar)(ge)]);
}
void init() //LCD1602初始化
{
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
}
void adc()//ADC0832轉(zhuǎn)換
{
uchar i;
cs=1;
clk=0;
cs=0;
di=1;
clk=1;//第一個脈沖
clk=0;
clk=1;//第二個脈沖
if(key!=1) //測量通道轉(zhuǎn)換
{
F0=!F0;
while(key!=1);
}
clk=0;
di=F0;
clk=1;//第三個脈沖
clk=0;
clk=1;
for(i=0;i<8;i++)
{
clk=0;
adc_data<<=1;
adc_data|=(uchar)(dox);
clk=1;
}
cs=1;
}
void adx() //ADC0832數(shù)制轉(zhuǎn)換
{
bai=adc_data/51;
shi=((adc_data%51)*10)/51;
ge=((((adc_data%51)*10)%51)*10)/51;
}
void main()
{
F0=0;
SP=0x30;
init();
while(1)
{
adc();
adx();
top();
bot();
delay();
}
}
評論