找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3118|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM8L151實(shí)現(xiàn)AD測量程序_寄存器版調(diào)試成功

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:600674 發(fā)表于 2019-8-16 11:18 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
#include "ADC.h"
#include "iostm8l151f2.h"
#include "stdbool.h"
#include "stdint.h"
uint16_t Vbat1=0;
uint16_t Vbat0=0;
uint16_t Vusbin=0;
uint16_t Vbutton0=0;
uint16_t Vbat=0;
static uint16_t temp=0;

void delay_500ms(void)    //粗略延時程序

{

  uint16_t i,j;

  for(i=0;i<1000;i++)//2*1000個指令周期

    for(j=0;j<500;j++);//2*500個指令周期

  //1000*2*500+2*500=1.001M,延遲1/16s=62.5ms

  //16M 一個指令周期為1/16us

}

void ADC1_Init()
{

   CLK_PCKENR2_PCKEN20=1; //開啟ADC1外設(shè)時鐘

   ADC1_CR1_ADON=1;  //wakes up the ADC from Power down mode

   ADC1_CR1_CONT=0; //單次轉(zhuǎn)換模式

   ADC1_CR2_PRESC=1;//Select a sample time of 1μs  //   0: f(ADC_CLK) = CK

                     //   1: f(ADC_CLK) = CK/2  //系統(tǒng)時鐘此時為默認(rèn)的16M/8=2M,這里分頻后,fadc=2M/2=1M

   ADC1_SQR1_DMAOFF=1;    //關(guān)閉DMA通道

   //ADC1_CR3_SMTP2=0x06;  //設(shè)置內(nèi)部參考電壓通道的采樣時間

   ADC1_CR2_SMTP1=0x03;//外部0-23通道的采樣時間為24 ADC clock cycles,也就是24*1us=24us


//  ADC1_CR1_EOCIE=1; //中斷

}
//#pragma vector=COMP_EF2_vector

//__interrupt void ADC1_ISR(void

//測量VDD
uint16_t get_VDD()
{
   Vbat=0;

   ADC1_SQR1_CHSEL_S28=1;  //選擇ADC通道28,內(nèi)部參考電壓通道

   ADC1_TRIGR1_VREFINTON=1;//打開內(nèi)部參考電壓源1.225v


   for(char j=0;j<10;j++)
    {

        ADC1_CR1_START=1 ;//開始轉(zhuǎn)換
        while(!(ADC1_SR_EOC==1));//等待轉(zhuǎn)換結(jié)束,清除標(biāo)志位
        temp = ADC1_DRH;
        temp = (temp<<8)+ADC1_DRL;
        temp=(uint32_t)2400*4096/temp;   //由于VREF與VDD接在同一引腳上,內(nèi)部參考電壓輸出通道實(shí)際電壓不知,
                                        //因此無法采用內(nèi)部參考電壓測量VDD,用電壓表多點(diǎn)測量的方法測得實(shí)際VDD=1.790V
        Vbat+=temp;
    }   
   Vbat = Vbat/10;

   asm("nop");

   ADC1_SQR1_CHSEL_S28=0; //關(guān)閉通道

   return Vbat;


}

uint16_t get_Vbat1()
{
   Vbat1=0;

   static uint16_t temp1=0;

   ADC1_SQR3_CHSEL_S11=1;  //選擇ADC通道11

   ADC1_TRIGR3_TRIG11=1;  //關(guān)閉通道11的施密特觸發(fā)器

   for(char j=0;j<10;j++)
    {

        ADC1_CR1_START=1 ;//開始轉(zhuǎn)換
        while(!(ADC1_SR_EOC==1));
        temp1 = ADC1_DRH;
        temp1 = (temp1<<8)+ADC1_DRL;
        temp1=(uint32_t)1800*temp1/4096;   

        Vbat1+=temp1;
    }   
    Vbat1 = Vbat1/10;

    Vbat1=3*Vbat1;//實(shí)際電壓值為3倍的AD電壓值

    ADC1_SQR3_CHSEL_S11=0;
   asm("nop");
     return Vbat1;

}

uint16_t get_Vbat0()
{
   Vbat0=0;

   static uint16_t temp0=0;

   ADC1_SQR3_CHSEL_S12=1;  //打開ADC通道12

   ADC1_TRIGR3_TRIG12=1;  //關(guān)閉通道12的施密特觸發(fā)器

  for(char k=0;k<10;k++)
    {
        ADC1_CR1_START=1 ;//開始轉(zhuǎn)換
        while(!(ADC1_SR_EOC==1));
        temp0 = ADC1_DRH;
        temp0 = (temp0<<8)+ADC1_DRL;
        temp0=(uint32_t)1800*temp0/4096;
        Vbat0+=temp0;
    }

    Vbat0 = Vbat0/10;

    Vbat0=3*Vbat0;
     asm("nop");
      ADC1_SQR3_CHSEL_S12=0;

     return Vbat0;

}

uint16_t get_Vusbin()

{

   Vusbin=0;

   static uint16_t temp2=0;

   ADC1_SQR4_CHSEL_S4=1;  //打開ADC通道4

   ADC1_TRIGR4_TRIG4=1;  //關(guān)閉通道4的施密特觸發(fā)器

  for(char k=0;k<10;k++)
    {
        ADC1_CR1_START=1 ;//開始轉(zhuǎn)換

        while(!(ADC1_SR_EOC==1));//等待轉(zhuǎn)換完成

        temp2 = ADC1_DRH;   //先存MSB

        temp2 = (temp2<<8)+ADC1_DRL; //(MSB)*256+LSB

        temp2=(uint32_t)1800*temp2/4096;  //   VDD/4096=Vx/AD值

        Vusbin+=temp2;
    }

      Vusbin = Vusbin/10;

      Vusbin =3*Vusbin;//USB輸入電壓
      asm("nop");

     ADC1_SQR4_CHSEL_S4=0;

     return Vusbin;

}

uint16_t get_Vbutton0()
{

     Vbutton0=0;

   static uint16_t temp3=0;

   ADC1_SQR3_CHSEL_S13=1;  //打開ADC通道13

   ADC1_TRIGR3_TRIG13=1;  //關(guān)閉通道13的施密特觸發(fā)器

  for(char k=0;k<10;k++)
    {
        ADC1_CR1_START=1 ;//開始轉(zhuǎn)換
        while(!(ADC1_SR_EOC==1));
        temp3= ADC1_DRH;
        temp3 = (temp3<<8)+ADC1_DRL;
        temp3=(uint32_t)1800*temp3/4096;
        Vbutton0+=temp3;
    }

        Vbutton0 =Vbutton0/10;  //button0對應(yīng)的電壓值
        asm("nop");
        ADC1_SQR3_CHSEL_S13=0; //關(guān)閉通道
        return Vbutton0;


}


/*
void main()
{
  // CLK_Init();
  ADC1_Init();

  delay_500ms();

  delay_500ms();

  delay_500ms();//延時等待ADC穩(wěn)定之后再進(jìn)行轉(zhuǎn)換,能夠準(zhǔn)確讀取對應(yīng)通道的電壓

  get_Vusbin();

  delay_500ms();

  delay_500ms();

  get_Vbat0();

  delay_500ms();

  get_Vbutton0();

  delay_500ms();

  get_Vbat1();

while(1)
{

}

}
*/










評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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