很早对AD转化感兴趣,也想自己业余做块单片机开发板,让广阔的电子设计爱好者学习运用单片机。
今日通过查找,整理了proteus 7能够仿真通过的根据51单片机+ADC0832电压收集并通过液晶1602显现电压的C程序及电路衔接图,期望对我们有所协助。
程序是谁写的谁修正的并不首要,首要的是学会并运用单片机AD转化,这才是王道。
电路衔接图如下:
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) //最小延时1ms
{ unsigned char j;
while(i–)
for(j=0;j<125; j++);
}
void delay_10ns(unsigned char i) //最小延时10ns
{ unsigned char j;
while(i–)
for(j=0;j<10; j++);
}
void write_com(unsigned char com,bit p) //写指令
{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 DA
{
delay_10ns(50);
E=0;
Rs=1;
Rw=0;
DD=DA
delay_10ns(50);
E=1;
delay_10ns(50);
E=0;
delay_10ns(4);
}
void addr_x_y(unsigned char x,bit y) //写坐标,定方位
{ 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)
//在指定方位显现一个字符。
{ 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) //显现一个数字
{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) //把模仿电压值转化成8位二进制数并回来 { unsigned char i,da da ADC_CS=0; ADC_DO=0;//片选,DO为高阻态 for(i=0;i<10;i++) {;} ADC_CLK=0; Delay(2); ADC_DI=1; ADC_CLK=1; Delay(2); //第一个脉冲,开始位 ADC_CLK=0; Delay(2); ADC_DI=1; ADC_CLK=1; Delay(2); //第二个脉冲,DI=1表明双通道单极性输入 ADC_CLK=0; Delay(2); ADC_DI=1; ADC_CLK=1; Delay(2); //第三个脉冲,DI=1表明挑选通道1(CH2) ADC_DI=0; ADC_DO=1;//DI转为高阻态,DO脱离高阻态为输出数据作预备 ADC_CLK=1; Delay(2); ADC_CLK=0; Delay(2);//经试验,这里加一个脉冲AD便能正确读出数据, //不加的话读出的数据少一位(最低位d0读不出) for (i=0; i<8; i++) { ADC_CLK=1; Delay(2); ADC_CLK=0; Delay(2); da } ADC_CS=1;//撤销片选,一个转化周期完毕 return(da void main(void) } 以上通过仿真测试通过。
}
{
unsigned int da
while(1)
{ da
init();
xs_int(196*da
}