51單片機(jī)ADC0832電壓測(cè)量液晶1602顯示的C程序與proteus仿真
很早對(duì)AD轉(zhuǎn)換感興趣,也想自己業(yè)余做塊單片機(jī)開(kāi)發(fā)板,讓廣大的電子設(shè)計(jì)愛(ài)好者學(xué)習(xí)使用單片機(jī)。
本文引用地址:http://cafeforensic.com/article/201609/310575.htm今天通過(guò)搜索,整理了proteus 7可以仿真通過(guò)的基于51單片機(jī)+ADC0832電壓采集并通過(guò)液晶1602顯示電壓的C程序及電路連接圖,希望對(duì)大家有所幫助。
程序是誰(shuí)寫(xiě)的誰(shuí)修改的并不主要,主要的是學(xué)會(huì)并使用單片機(jī)AD轉(zhuǎn)換,這才是王道。
電路連接圖如下:
C程序如下:
#include
#include
#include
/**********************************8/
/**********LCD1602接口程序**********/
#define DD P2
sbit Rs=P3^0;
sbit Rw=P3^1;
sbit E=P3^2;
sbit busy_p=ACC^7;
/********************************/
void delay_1ms(unsigned char i) //最小延時(shí)1ms
{ unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void delay_10ns(unsigned char i) //最小延時(shí)10ns
{ unsigned char j;
while(i--)
for(j=0;j<10; j++);
}
void write_com(unsigned char com,bit p) //寫(xiě)指令
{if(p)
delay_10ns(5);
E=0;
Rs=0;
Rw=0;
DD=com;
delay_10ns(50); //>40ns
E=1;
delay_1ms(2); //>150ns
E=0;
delay_10ns(4); //>25+10ns
}
void write_date(unsigned char DATA) //寫(xiě)數(shù)據(jù)
{
delay_10ns(50);
E=0;
Rs=1;
Rw=0;
DD=DATA;
delay_10ns(50);
E=1;
delay_10ns(50);
E=0;
delay_10ns(4);
}
void addr_x_y(unsigned char x,bit y) //寫(xiě)坐標(biāo),定位置
{ unsigned char temp=0x80;
if(y)
{temp|=0x40;}
temp|=x;
write_com(temp,0);
}
void desplay_char(unsigned char x,bit y,unsigned char p)
//在指定位置顯示一個(gè)字符。
{ addr_x_y(x,y);
write_date(p);
}
void init(void)
{delay_1ms(15);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,1);
write_com(0x08,1);
write_com(0x01,1);
write_com(0x06,1);
write_com(0x0c,1);
}
void xs_int(unsigned int shuju,bit t) //顯示一個(gè)數(shù)字
{unsigned char huancun[6]={0};
unsigned char biaozhi=0,i;
if (shuju < 10) biaozhi = 1;
else if(shuju < 100) biaozhi = 2;
else if(shuju < 1000) biaozhi = 3;
else if(shuju < 10000) biaozhi = 4;
else if(shuju < 65535) biaozhi = 5;
switch(biaozhi)
{case 5:huancun[5] = shuju/10000;
case 4:huancun[3] = shuju%10000/1000;
case 3:huancun[2] = shuju%1000/100;
case 2:huancun[1] = shuju%100/10;
case 1:huancun[0] = shuju%10;
break;
default:break;
}
for(i=6;i>1;i--)
{if(i==5)desplay_char(10,1,'.');
else desplay_char(15-i,t,0x30+huancun[i-1]); }
desplay_char(15,t,'V');
}
/************************************************************/
/**********ADC0832接口程序************************************/
sbit ADC_CS =P3^4;
sbit ADC_CLK=P3^5;
sbit ADC_DO =P3^6;
sbit ADC_DI =P3^7;
/*******************************************************************/
void Delay(unsigned char j)
{
unsigned char i;
for(i=0;i
}
unsigned char ADC0832(void) //把模擬電壓值轉(zhuǎn)換成8位二進(jìn)制數(shù)并返回
{
unsigned char i,data_c;
data_c=0;
ADC_CS=0;
ADC_DO=0;//片選,DO為高阻態(tài)
for(i=0;i<10;i++)
{;}
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第一個(gè)脈沖,起始位
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第二個(gè)脈沖,DI=1表示雙通道單極性輸入
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第三個(gè)脈沖,DI=1表示選擇通道1(CH2)
ADC_DI=0;
ADC_DO=1;//DI轉(zhuǎn)為高阻態(tài),DO脫離高阻態(tài)為輸出數(shù)據(jù)作準(zhǔn)備
ADC_CLK=1;
Delay(2);
ADC_CLK=0;
Delay(2);//經(jīng)實(shí)驗(yàn),這里加一個(gè)脈沖AD便能正確讀出數(shù)據(jù),
//不加的話讀出的數(shù)據(jù)少一位(最低位d0讀不出)
for (i=0; i<8; i++)
{
ADC_CLK=1;
Delay(2);
ADC_CLK=0;
Delay(2);
data_c=(data_c<<1)|ADC_DO;//在每個(gè)脈沖的下降沿DO輸出一位數(shù)據(jù),最終ch為8位二進(jìn)制數(shù)
}
ADC_CS=1;//取消片選,一個(gè)轉(zhuǎn)換周期結(jié)束
return(data_c);//把轉(zhuǎn)換結(jié)果返回
}
void main(void)
{
unsigned int data_temp=0;
while(1)
{ data_temp=ADC0832();
init();
xs_int(196*data_temp,1);
}
}
以上經(jīng)過(guò)仿真測(cè)試通過(guò)。
評(píng)論