×
嵌入式 > 技术百科 > 详情

数码管显示的ADC0831电压表程序

发布时间:2020-06-16 发布时间:
|
 /*
程序效果:数码管显示0.00-5.00U电压,调节电位器,得到
       ADC0831的2脚电压值。
注:测量时先把电位器调节到中间,也就是2.5U,但切记
   所测的引脚的电压值不能超过5U,否则会烧坏ADC0831
   芯片和单片机,小心哦。  
程序版权所有:http://www.51hei.com,如无法编译,请去掉所有前导空白。
*/
#include
#include
#define uchar unsigned char
#define uint  unsigned int
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,//共阴的数码管的显示值为0-9
0x8f,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,//显示值为:带有小数点的0-9
0x40,0x3e,0x00}; //分别显示“-”,U(伏的单位),空表
sbit scl1=P1^3;
sbit sda1=P1^4;
sbit px1=P1^5;
sbit cs1=P1^6;
uchar tmpdata[]={0,0,0,0};
uchar readad0831();
void display(uchar *lp,uchar lc);
void delay();
void main()
{
   uint i=0,tmp;  //这要定位为整型,防止数据溢出
   px1=0;
   while(1)
   {
      i++;
      if(i==255)
      {
         i=0;
         tmp=readad0831()*100; //这里乘以100,是保留两位小数的意思
         tmp=tmp/51;
         tmpdata[0]=tmp/100; //得到百位数,其实是原来的个位数
         tmpdata[0]+=10;   //这里加上10,是因为带小数点的数字在后面十位
         tmp=tmp%100;
         tmpdata[1]=tmp/10; //得到十位,即小数点的后一位
         tmpdata[2]=tmp%10; //得到各位,即小数点的第二位
         tmpdata[3]=21;   //加上单位U,即伏
      }
      display(tmpdata,4);   //调用显示子函数
   }
}
void display(uchar *lp,uchar lc)
{
   uchar i;
   P2=0;
   P1=P1&0xf8;   //防止改变后五位数
   for(i=0;i   {
      P2=table[lp[i]];
      delay();
      if(i==7)
         break;
      P2=0;
      P1++;
   }
}
uchar readad0831()//根据ADC0831协议编写的语句
{
   uchar i=0,tmp=0;
   sda1=1;
   cs1=0;
   _nop_();
   _nop_();
   scl1=0;
   _nop_();
   _nop_();
   scl1=1;
   _nop_();
   _nop_();
   scl1=0;
   _nop_();
   _nop_();
   scl1=1;
   _nop_();
   _nop_();
   scl1=0;
   _nop_();
   _nop_();
   for(i=0;i<8;i++)
   {
      tmp=_crol_(tmp,1);
      if(sda1)
         tmp++;
      scl1=1;
      _nop_();
      _nop_();
      scl1=0;
      _nop_();
      _nop_();
   }
   cs1=1;
   return tmp;
}
void delay()   //延时子函数
{
   _nop_();_nop_();_nop_();_nop_();_nop_();
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
分拣机器人的工作原理是什么