51单片机液晶1602 ADC0832 proteus仿真源程序

来源:互联网 发布:启点cms平台 编辑:程序博客网 时间:2024/05/16 16:02

基于网上的资料,整理而成。


/*
51单片机 1602+ADC0832显示程序,用proteus 7.8仿真通过。
*/


#include <reg52.h>
#include<intrins.h>
#include<math.h>
#include <stdio.h>


char data str[]="                ";
/**********************************/
/**********LCD1602接口程序**********/


#define DD P2
sbit Rs=P3^0;
sbit Rw=P3^1;
sbit E=P3^2;


/********************************/
void delay_1ms(unsigned char i)   //最小延时1ms

unsigned char j;
while(i--)
for(j=0;j<125; j++);
}
void delay_10us(unsigned char i) //最小延时10us

unsigned char j;
while(i--)
for(j=0;j<10; j++);
}


void write_com(unsigned char com)   //写指令
{
delay_10us(5);
E=0;
Rs=0;
Rw=0; 
DD=com;
delay_10us(50); //>40us
E=1; 
delay_1ms(2); //>150us
E=0;
delay_10us(4); //>25+10us 
}


void write_data(unsigned char DATA)   //写数据

delay_10us(50);
E=0;
Rs=1;
Rw=0; 
DD=DATA; 
delay_10us(50);
E=1;
delay_10us(50);
E=0;
delay_10us(4);
}




void addr_x_y(unsigned char x,bit y)   //写坐标,定位置

unsigned char temp=0x80;//默认最高位:D7为1 即以0x80开始。  
if(y) //y :0为第一行  1为第二行
  {
  temp|=0x40;
  }
  temp|=x;
write_com(temp);
}




void Show_Char(unsigned char x,bit y,unsigned char p) 


//在指定位置显示一个字符。

addr_x_y(x,y);
write_data(p);
}


void Show_String(unsigned char x,bit y,char *ptr)
{
  unsigned char i;
for (i=0;i<16;i++)
  Show_Char(x++,y,*(ptr+i));//循环显示16个字符
}




void init(void) //1602初始化代码
{
delay_1ms(1500);
write_com(0x38); 
delay_1ms(5);
write_com(0x38); 
delay_1ms(5); 
write_com(0x38); 
delay_1ms(5);
write_com(0x38); 
write_com(0x08); 
write_com(0x06); 
write_com(0x0c); 
write_com(0x01); 
}
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)//这里没有break,因此从标识匹配的入口直接执行到最后,完成整数各位的提取到数组。
  {
  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=0;i<6;i++)
  {
  if(i==1)Show_Char(i,1,'.');//加入小数点,缩小了10000倍,因此AD采样后的值需要乘上10000*5V/256=196(V)
       else Show_Char(i,t,0x30+huancun[6-i-1]); 

Show_Char(6,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<j;i++); //延时,脉冲一位持续的时间
}


unsigned char ADC0832(void) //把模拟电压值转换成8位二进制数并返回

unsigned char i,data_c;
data_c=0;
ADC_CS=0;
ADC_CLK=1;
ADC_DO=0;//片选,DO为高阻态
//ADC_DI=0; //先拉低

for(i=0;i<10;i++)
    {;}

ADC_CLK=0;
Delay(2);
ADC_CLK=1; 
ADC_DI=1; //启始位 
Delay(2); 

ADC_CLK=0; //第一个脉冲下降沿,DI=1为起始位 
Delay(2); 
ADC_CLK=1;
ADC_DI=1;   //1
Delay(2); 

ADC_CLK=0;  //第二个脉冲下降沿,DI=1
Delay(2); 
ADC_DI=1;  //1若为0则选择CH0
ADC_CLK=1;
Delay(2); 

ADC_CLK=0; //第三个脉冲下降沿,DI=1,选择 ADC0832 的CH1(1 1)
Delay(2); 
ADC_DI=1;   //已不关心
ADC_DO=1; //高阻
ADC_CLK=1;
Delay(2); 

for (i=0; i<8; i++)
   { 
ADC_CLK=0; //第四个开始读数据
Delay(2);
   data_c=(data_c<<1)|ADC_DO;//在每个脉冲的下降沿DO输出一位数据,最终ch为8位二进制数
ADC_CLK=1; 
Delay(2); 
  }
ADC_CS=1;//取消片选,一个转换周期结束
return(data_c);//把转换结果返回
}


void main(void)

unsigned int data_temp=0; 
while(1)

data_temp=ADC0832();
sprintf(str,"The voltage is:"); //the first line
    init();
Show_String(0,0,str);
xs_int(196*data_temp,1);  //10000*5V/256=196(V),把采样值放大10000倍后再处理,移动小数点4位。 4位有效数字,其实用不到,因为8位采样精度为:0.019V
delay_1ms(1500);
   }
}








proteus仿真原理图如下:


原创粉丝点击