找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 3382|回復(fù): 14
打印 上一主題 下一主題
收起左側(cè)

為什么STC12C5A60S2單片機(jī)的AD值不能轉(zhuǎn)換成電壓值?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
弄了好久都解決不了,AD值都正常,但是轉(zhuǎn)換成電壓值就不正常了,會(huì)不會(huì)是軟件的原因(使用KEIL3)?



#include<reg52.h>
#include<intrins.h>
#define uint unsigned int  
#define uchar unsigned char
sfr        P1ASF=0X9D;           //P1口模擬功能控制寄存器
sfr        ADC_CONTR=0XBC;      //ADC控制寄存器
sfr        ADC_RES=0XBD;        //AD轉(zhuǎn)換結(jié)果存放
sfr        ADC_RESL=0XBE;       //AD轉(zhuǎn)換結(jié)果存放
sfr        AUXR1=0XA2;               
sbit SCL=P0^0;
sbit SDA=P0^1;
uchar a,b,c,d;
uint j,k,l;
unsigned  long g;

unsigned char code Hzk[][32]={

{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",0*/

{0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00},/*"1",1*/

{0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",2*/

{0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00},/*"3",3*/

{0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24},/*"4",4*/

{0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00},/*"5",5*/

{0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00},/*"6",6*/

{0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00},/*"7",7*/

{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},/*"8",8*/

{0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00},/*"9",9*/

{0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20},/*"A",10*/

{0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00},/*"B",11*/

{0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00},/*"C",12*/

{0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00},/*"D",13*/

{0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00},/*"E",14*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"空白",15*/

{0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00},/*">",16*/

{0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00},/*"<",17*/

{0x00,0x40,0xC0,0x78,0x40,0xC0,0x78,0x00,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x00},/*"#",18*/

{0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},/*"*",19*/

{0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00},/*"%",20*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00},/*".",21*/

};
/**********************************************
//IIC Start
**********************************************/
void IIC_Start()                                                                                   //開始信號(hào)
{
   SCL = 1;

   SDA = 1;

   SDA = 0;

   SCL = 0;

}

/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()                                                                                         //停止信號(hào)
{
   SCL = 0;

   SDA = 0;

   SCL = 1;
   //Delay_us(1);
   SDA = 1;
   //Delay_us(1);
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)                        //寫1個(gè)字節(jié)
{
        unsigned char i;
        for(i=0;i<8;i++)               
        {
                if(IIC_Byte & 0x80)                //1?0?
                SDA=1;
                else
                SDA=0;
                //Delay_us(1);
                SCL=1;
                //Delay_us(1);
                SCL=0;
                //Delay_us(1);
                IIC_Byte<<=1;                        //loop
        }
        SDA=1;
        //Delay_us(1);
        SCL=1;
        //Delay_us(1);
        SCL=0;
        //Delay_us(1);
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
   IIC_Start();
   Write_IIC_Byte(0x78);            //Slave address,SA0=0
   Write_IIC_Byte(0x00);                        //write command
   Write_IIC_Byte(IIC_Command);
   IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
   IIC_Start();
   Write_IIC_Byte(0x78);                       
   Write_IIC_Byte(0x40);                        //write data
   Write_IIC_Byte(IIC_Data);
   IIC_Stop();
}

void picture_1(uint y)  
{
  unsigned char x;
      Write_IIC_Command(0xb4);
      Write_IIC_Command(0x00);
      Write_IIC_Command(0x10);
      for(x=0;x<8;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

          Write_IIC_Command(0xb5);
      Write_IIC_Command(0x00);
      Write_IIC_Command(0x10);
      for(x=8;x<16;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

}
void picture_2(uint y)  
{
  unsigned char x;
      Write_IIC_Command(0xb4);
      Write_IIC_Command(0x08);
      Write_IIC_Command(0x10);
      for(x=0;x<8;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

          Write_IIC_Command(0xb5);
      Write_IIC_Command(0x08);
      Write_IIC_Command(0x10);
      for(x=8;x<16;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

}
void picture_3(uint y)  
{
  unsigned char x;
      Write_IIC_Command(0xb4);
      Write_IIC_Command(0x00);
      Write_IIC_Command(0x11);
      for(x=0;x<8;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

          Write_IIC_Command(0xb5);
      Write_IIC_Command(0x00);
      Write_IIC_Command(0x11);
      for(x=8;x<16;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

}
void picture_4(uint y)  
{
  unsigned char x;
      Write_IIC_Command(0xb4);
      Write_IIC_Command(0x08);
      Write_IIC_Command(0x11);
      for(x=0;x<8;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

          Write_IIC_Command(0xb5);
      Write_IIC_Command(0x08);
      Write_IIC_Command(0x11);
      for(x=8;x<16;x++)
        {
          Write_IIC_Data(Hzk[y][x]);
        }

}
void fill_picture(unsigned char fill_Data)          //清屏
{
        unsigned char m,n;
        for(m=0;m<8;m++)
        {
                Write_IIC_Command(0xb0+m);               
                Write_IIC_Command(0x00);               
                Write_IIC_Command(0x10);               
                for(n=0;n<128;n++)
                        {
                         Write_IIC_Data(fill_Data);
                        }
        }
}
void Initial_LY096BG30()
{
        Write_IIC_Command(0xAE);   //display off
        Write_IIC_Command(0x20);        //Set Memory Addressing Mode      
        Write_IIC_Command(0x10);        //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
        Write_IIC_Command(0xb0);        //Set Page Start Address for Page Addressing Mode,0-7
        Write_IIC_Command(0xc8);        //Set COM Output Scan Direction
        Write_IIC_Command(0x00);//---set low column address
        Write_IIC_Command(0x10);//---set high column address
        Write_IIC_Command(0x40);//--set start line address
        Write_IIC_Command(0x81);//--set contrast control register
        Write_IIC_Command(0x7f);
        Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
        Write_IIC_Command(0xa6);//--set normal display
        Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
        Write_IIC_Command(0x3F);//
        Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
        Write_IIC_Command(0xd3);//-set display offset
        Write_IIC_Command(0x00);//-not offset
        Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
        Write_IIC_Command(0xf0);//--set divide ratio
        Write_IIC_Command(0xd9);//--set pre-charge period
        Write_IIC_Command(0x22); //
        Write_IIC_Command(0xda);//--set com pins hardware configuration
        Write_IIC_Command(0x12);
        Write_IIC_Command(0xdb);//--set vcomh
        Write_IIC_Command(0x20);//0x20,0.77xVcc
        Write_IIC_Command(0x8d);//--set DC-DC enable
        Write_IIC_Command(0x14);//
        Write_IIC_Command(0xaf);//--turn on oled panel
}
/***********************Delay****************************************/
void Delay_50ms(unsigned int Del_50ms)                //   延時(shí)
{
        unsigned int m;
        for(;Del_50ms>0;Del_50ms--)
        for(m=6245;m>0;m--);
}


void delay1ms(uint z)   //誤差 0us
{
    unsigned char a,b,c;
    for(c=z;c>0;c--)
        for(b=142;b>0;b--)
            for(a=2;a>0;a--);
}

void ADinit()
{
   P1ASF=0x01;        //P1^0口作為AD輸入口
   ADC_CONTR=0x20;    //P1^0口作為AD輸入口,360個(gè)時(shí)鐘周期轉(zhuǎn)換一次
   AUXR1=0x00;        //高八位存放于ADC_RES,低二位存放于ADC_RESL
   IE=0XA0;           //開總中斷允許AD中斷
}
void ADser() interrupt 5
{
         _nop_();
         _nop_();
         _nop_();         
         _nop_();              //延時(shí)4個(gè)時(shí)鐘周期,使其趨于穩(wěn)定

     ADC_CONTR=0xC0;           //轉(zhuǎn)換完成,將ADC_FLAG清零
     j=ADC_RES;                    //AD高位數(shù)據(jù)
         k=ADC_RESL;                   //AD低位數(shù)據(jù)
         l=(j<<2)+k;
         
}
void main()
{
        ADinit();
                Initial_LY096BG30();
                delay1ms(1);
                fill_picture(0x00);        
                Delay_50ms(50);
        while(1)
        {
           ADC_CONTR=0XA8;//開始AD轉(zhuǎn)換
           delay1ms(1);  
           ADC_CONTR=0X20;//關(guān)閉AD轉(zhuǎn)
                g=l*50/1023*100;       //這里的轉(zhuǎn)換值不正確,不知道是不是軟件原因還是計(jì)算錯(cuò)誤?   
                     d=g/1%10;                    //個(gè)位
                c=g/10%10;                  //十位
                b=g/100%10;                //百位
                a=g/1000%10;              //千位
                  picture_1(a);
                  picture_2(b);
                  picture_3(c);
                  picture_4(d);
        }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:213173 發(fā)表于 2020-11-6 07:44 | 只看該作者
是樓主計(jì)算方法不當(dāng)

#define VCC_V  5//宏定義電源電壓5V
unsigned int ADC_RESX;//10位ADC轉(zhuǎn)換結(jié)果
unsigned int ADC_V;        //ADC mV
//強(qiáng)制轉(zhuǎn)換數(shù)據(jù)類型(32位)運(yùn)算,得到(4舍5入)mV
ADC_V=(VCC_V*(long)ADC_RESX*10000/1024+5)/10;
回復(fù)

使用道具 舉報(bào)

板凳
ID:420836 發(fā)表于 2020-11-6 09:22 | 只看該作者
模數(shù)轉(zhuǎn)換的參考電壓為VREF,在任何電壓V下,ADC結(jié)果為n,滿量程ADC結(jié)果為1023,則V = VREF x(n / 1023),將V轉(zhuǎn)換為mV:VmV = V
回復(fù)

使用道具 舉報(bào)

地板
ID:65956 發(fā)表于 2020-11-6 09:27 | 只看該作者
首先要根據(jù)你的分壓電阻比例關(guān)系,其次是要知道你單片機(jī)工作電源電壓,即為基準(zhǔn)源,這兩個(gè)參數(shù)很關(guān)鍵,接下來就是:基準(zhǔn)源乘以采集到的ADC值,再除以10位AD即1024,這樣計(jì)算結(jié)果就是你的ADC實(shí)際電壓值了
回復(fù)

使用道具 舉報(bào)

5#
ID:382699 發(fā)表于 2020-11-6 10:06 來自觸屏版 | 只看該作者
aking991 發(fā)表于 2020-11-6 09:27
首先要根據(jù)你的分壓電阻比例關(guān)系,其次是要知道你單片機(jī)工作電源電壓,即為基準(zhǔn)源,這兩個(gè)參數(shù)很關(guān)鍵,接下 ...

就是按照這個(gè)方法,計(jì)算出結(jié)果不正常!
回復(fù)

使用道具 舉報(bào)

6#
ID:382699 發(fā)表于 2020-11-6 10:07 來自觸屏版 | 只看該作者
TTQ001 發(fā)表于 2020-11-6 09:22
模數(shù)轉(zhuǎn)換的參考電壓為VREF,在任何電壓V下,ADC結(jié)果為n,滿量程ADC結(jié)果為1023,則V = VREF x(n / 1023), ...

這個(gè)方法不行的,出現(xiàn)錯(cuò)誤
回復(fù)

使用道具 舉報(bào)

7#
ID:382699 發(fā)表于 2020-11-6 10:09 來自觸屏版 | 只看該作者
wulin 發(fā)表于 2020-11-6 07:44
是樓主計(jì)算方法不當(dāng)

#define VCC_V  5//宏定義電源電壓5V

為什么要加5呢?
回復(fù)

使用道具 舉報(bào)

8#
ID:213173 發(fā)表于 2020-11-6 11:17 | 只看該作者

+5/10是為了4舍5入
回復(fù)

使用道具 舉報(bào)

9#
ID:235954 發(fā)表于 2020-11-6 15:34 | 只看該作者
轉(zhuǎn)換成浮點(diǎn)型看看,除的數(shù)沒有小數(shù)點(diǎn)的
回復(fù)

使用道具 舉報(bào)

10#
ID:394367 發(fā)表于 2020-11-6 16:46 | 只看該作者
gjkl你定義為浮點(diǎn)型,計(jì)算電壓公式   g=l*5.00/1024*1000; 然后看看
回復(fù)

使用道具 舉報(bào)

11#
ID:382699 發(fā)表于 2020-11-7 22:18 | 只看該作者
梁廷明 發(fā)表于 2020-11-6 15:34
轉(zhuǎn)換成浮點(diǎn)型看看,除的數(shù)沒有小數(shù)點(diǎn)的

改成 g=l*50/1023*100;
輸出0——50,只有2位,不知道什么原因?
回復(fù)

使用道具 舉報(bào)

12#
ID:382699 發(fā)表于 2020-11-7 22:20 | 只看該作者
FJ2018 發(fā)表于 2020-11-6 16:46
gjkl你定義為浮點(diǎn)型,計(jì)算電壓公式   g=l*5.00/1024*1000; 然后看看

改成unsigned long
改成 g=l*50/1023*100;
輸出0——50,只有2位,不知道什么原因,正常應(yīng)該有四位吧?
回復(fù)

使用道具 舉報(bào)

13#
ID:382699 發(fā)表于 2020-11-10 15:53 來自觸屏版 | 只看該作者
問題已解決,是數(shù)據(jù)類型設(shè)置不對(duì)造成的!
回復(fù)

使用道具 舉報(bào)

14#
ID:852655 發(fā)表于 2020-12-10 13:55 | 只看該作者
分立元件 發(fā)表于 2020-11-10 15:53
問題已解決,是數(shù)據(jù)類型設(shè)置不對(duì)造成的!

應(yīng)該怎么設(shè)置呢?
回復(fù)

使用道具 舉報(bào)

15#
ID:93341 發(fā)表于 2021-7-15 16:16 | 只看該作者
標(biāo)定不對(duì)。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表