hanfei08131106的笔记 https://bbs.21ic.com/?628143 [收藏] [复制] [RSS]

日志

A/D转换实现0~5V测量并控制显示与报警

已有 2585 次阅读2010-6-25 15:16 |系统分类:单片机| A/D转换, 显示, 报警


A/D转换实现0~5V测量


0~1V数码管显示数字量与模拟量;
1~2V显示学号;
2~3V显示HAPPY!!!;
3~4V从右至左滚动显示HAPPY!!!;
4.5V~ 报警



#include  <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long


sfr P1ASF=0x9d;
sfr ADC_CONTR=0xbc;    //power  speed1 speed0 flag  st chs2 chs1 chs0
sfr ADC_RES=0xbd;
sfr ADC_RESL=0xbe;
sbit ser=P2^1;            //LED显示 595数据输入
sbit srclk1=P2^2; 
sbit rclk1=P2^3;  
sbit speak=P3^2;
                                                                          //      H    A     P    Y    !
uchar idata  LED[ ]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xfe,0xbf,0x89,0x88,0x8c,0x99,0x7d,0xff};//共阳极段码表说明
uchar idata  bb[]={0,0,10,0,11,11,11,11},cc[]={0,0,10,0,11,11,11,11},dd[]={0,0,10,0,11,11,11,11},jj[]={0,0,10,0,11,11,11,11};
//--------------------------------------------------------------------
void delayXms(uint n)
{
    uint i,j;
    for(i=0;i<n;i++)
       for(j=0;j<125;j++);
}
//--------------------------------------------------------------------
void outbyte(uchar a,b)
 {
 uchar j;
   for(j=0;j<8;j++)   //位码移位串行输出
   {     if(a&0x80) ser=1;  else  ser=0;
            a=a<<1;
            srclk1=0;
            srclk1=1;


    }
  for(j=0;j<8;j++)   //duan码移位串行输出
   {     if(b&0x80) ser=1;  else  ser=0;
            b=b<<1;
            srclk1=0;
            srclk1=1;


    }
   rclk1=0;   //595锁存脉冲
      rclk1=1;


  }
//--------------------------------------------------------------------
uint GET_AD_Result()//启动AD转换并返回转换值
{
 uchar temp;
 uint data_temp;
 data_temp=0;
 ADC_RES=0;
 ADC_RESL=0;
 ADC_CONTR|=0x08;//启动AD
re: temp=0x10;
 temp&=ADC_CONTR;//查询ADC_FLAG,忙标志,转换完否
 if(temp==0)
  goto re;
 ADC_CONTR&=0xe0;//P1.0为AD输入口
 data_temp=ADC_RES;
 data_temp<<=2;
 data_temp+=ADC_RESL;
 return data_temp;
}
//--------------------------------------------------------------------
void main(void) using 0
{
        uint result,temp,i,a,c,d,e;
        uchar aa[3]={0,0,0};
 P1ASF=0x01;
 ADC_CONTR=0xe0;                   //P1.0为AD输入口
        while(1)
        {
  result=GET_AD_Result();
  aa[2]=result*5/1024;     //模拟量显示0-5v   数学转换
  temp=result*5%1024;
  temp=temp*10;
         aa[1]=temp/1024;
  temp=temp%1024;
  temp=temp*10;
  aa[0]=temp/1024;
                bb[0]=result/1000;         //数字量显示1024   数学转换
                bb[1]=(result%1000)/100;
                bb[2]=(result%100)/10;
                bb[3]=result%10;


                dd[0]=6;
                dd[1]=0;
                dd[2]=1;
                dd[3]=1;
                dd[4]=3;
                dd[5]=1;
                dd[6]=8;
                dd[7]=0;


                cc[0]=16;
                cc[1]=16;
                cc[2]=16;
                cc[3]=15;
                cc[4]=14;
                cc[5]=14;
                cc[6]=13;
                cc[7]=12;
               
                jj[0]=17;
                jj[1]=17;
                jj[2]=17;
                jj[3]=17;
                jj[4]=17;
                jj[5]=17;
                jj[6]=17;
                jj[7]=17;


                e=0xfe;
                switch(aa[2]%10)
                {
                      case 0:
                             e=0x7f;
               a=0xfe;
               for(i=0;i<3;i++)      //模拟量显示0-5v
                  {
                     c=LED[aa];
                     if(i==2) c=c&0x7f;
                         outbyte(a,c);
                     a=(a<<1)|0x01;        //a数字量显示位码
                  }
                             for(i=0;i<4;i++)      //数字量显示1024
                  {
                     d=LED[bb];
                     if(i==2) c=c&0x7f;
                         outbyte(e,d);
                     e=(e>>1)|0x80;        //e数字量显示位码
                  } break;
                      case 1:
                             for(i=0;i<8;i++)
                  {
                     d=LED[dd];
                     if(i==2) c=c&0x7f;
                         outbyte(e,d);
                     e=(e<<1)|0x01;
                  } break;


                      case 2:
                             for(i=0;i<8;i++)
                  {
                     d=LED[cc];
                     if(i==2) c=c&0x7f;
                         outbyte(e,d);
                     e=(e<<1)|0x01;
                  } break;
                      case 3:
                             for(i=0;i<8;i++)
                  {
                     d=LED[cc];
                         outbyte(e,d);
                                   delayXms(1000);
                                   d=LED[jj];
                     if(i==2) c=c&0x7f;
                         outbyte(e,d);
                     e=(e<<1)|0x01;
                  }break;
                      case 4:
                             if(aa[1]>=5)
                                 {
                                     speak=0;
                                     delayXms(1000);
                                     speak=1;
                                     delayXms(1000);
                                 }
                   break;
                }
 }
}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)