原创 【灵动微电子 L0136 温控器/遥控器应用】+温控器应用外加PT1000热敏电阻

2023-1-10 14:33 791 0 分类: 智能硬件

运用开发板例程中ADC采集程序,用PA1口作为AD输入口,用pt1000电路,读出AD值算出pt1000电阻值,得出温度值,用串口输出。调试阶段为了电阻范围增大,运用电阻箱测试。具体程序如下所示:

/*

 * Copyright 2021 MindMotion Microelectronics Co., Ltd.

 * All rights reserved.

 *

 * SPDX-License-Identifier: BSD-3-Clause

 */


#include

#include


#include "board_init.h"



/*

 * Definitions.

 */


/*

 * Variables.

 */

uint32_t conv_val;

void app_adc_init(void);

volatile bool app_adc_compare_done = false;

const uint16_t PT1000_tab[100]= //温度对应的阻值表格 跨度为0--100°

{//0

10000,10040,10079,10119,10159,10198,10238,10278,10317,10357,

//10

10396,10436,10475,10515,10554,10594,10633,10673,10712,10752,

//20

 10791,10831,10870,10910,10949,10988,11028,11067,11107,11146,

//30

 11185, 11225,11264,11303,11343,11382,11421,11460, 11500,11539,

//40

 11578,11617,11657,11696,11735,11774,11813,11852,11891,11931,

//50

 11970,12009,12048,12087,12126,12165,12204,12243,12282,12321,

//60

 12360, 12399,12438,12477,12516,12555,12594,12633,12672,12710,

//70

 12749,12788,12827,12866,12905,12944,12982,13021,13060,13099,

//80

 13137,13176,13215,13254,13292,13331,13370,13408,13447,13488,

//90

 13524,13563,13602,13640,13679,13717,13756,13794,13832,13872

};

/*

 * Declerations.

 */



/*

 * Functions.

 */

uint16_t Temp(void);

int main(void)

{


    BOARD_Init();

    printf("adc_awdg example. \r\n");


    /* setup adc with dma. */

    app_adc_init();

    printf("press any key to start the conversion.\r\n");


    while(1)

    {

        /* type any key into the terminal to trigger the conversion. */

        getchar();


        /* start the converison. */

        printf("adc conversion start...\r\n");


        ADC_DoSwTrigger(BOARD_ADC_PORT, true);

        printf("Wait for the comparison condition to match...\r\n");


        /* wait for the compare match done. */

        while (!app_adc_compare_done)

        { }


        app_adc_compare_done = false;

//        printf("value=%u\n", (unsigned)(conv_val & 0xFFF));

printf("Temp=%.2f\n", (float)(Temp()/100));

        printf("comparison condition match...\r\n");

    }

}


/* setup the adc converter. */

void app_adc_init(void)

{

    /* pins and clock are already in the pin_init.c and clock_init.c. */


    /* setup the converter. */

    ADC_Init_Type adc_init;


    adc_init.Resolution = ADC_Resolution_12b;

    adc_init.ClockDiv = ADC_ClockDiv_16;

    adc_init.ConvMode = ADC_ConvMode_SeqContinues;

    adc_init.Align = ADC_Align_Right;

    ADC_Init(BOARD_ADC_PORT, &adc_init);


    ADC_Enable(BOARD_ADC_PORT, true); /* power on the converter. */


    /* setup one regular channel. */

    ADC_RegSeqConf_Type regseq_conf;


    regseq_conf.SeqSlots = 1u << BOARD_ADC_HW_COMP_CHN_NUM;

    regseq_conf.SeqDirection = ADC_RegSeqDirection_LowFirst;

    ADC_EnableRegSeq(BOARD_ADC_PORT, ®seq_conf);


    /* set channel sample time. */

    ADC_SetChnSampleTime(BOARD_ADC_PORT, BOARD_ADC_HW_COMP_CHN_NUM, ADC_SampleTime_Alt7);


    /* setup hardware compare. the conversion would be pompt when the conv value is out of windows. */

    ADC_HwCompConf_Type comp_init;

    comp_init.ChnNum    = BOARD_ADC_HW_COMP_CHN_NUM;

    comp_init.HighLimit = BOARD_ADC_HW_COMP_HIGH_LIMIT;

    comp_init.LowLimit  = BOARD_ADC_HW_COMP_LOW_LIMIT;

    ADC_EnableHwComp(BOARD_ADC_PORT, &comp_init);


    /* enable windows compare interrupt. */

    ADC_EnableInterrupts(BOARD_ADC_PORT, ADC_INT_COMPARE_DONE, true);

    NVIC_EnableIRQ(BOARD_ADC_IRQn);

}


/* entry to adc compare done interrupt. */

void BOARD_ADC_IRQHandler(void)

{

    uint32_t conv_flags;

    uint32_t adc_channel; /* keep the actual hardware conversion channel number. */

    uint32_t flags = ADC_GetStatus(BOARD_ADC_PORT);


    if ( 0u != (flags & ADC_STATUS_COMPARE_DONE) )

    {

        app_adc_compare_done = true;

        conv_val = ADC_GetConvResult(BOARD_ADC_PORT, &adc_channel, &conv_flags);

    }


    ADC_ClearStatus(BOARD_ADC_PORT, flags);

}

float AppAdcColTemp(void)

{

float Vout,Vpt,Rx;

 

Vout = (float)(conv_val*(3300.0f/4096.0f));  //AD值

Vpt = Vout*1690.0f/56000.0f;   //VPT_A

Rx = ((300.0f+Vpt)*9090.0f/(3000.0f-Vpt));//100.0f是补偿

return  Rx;

}

uint16_t  PT1000_Temp(float rtn) 

{    

    uint16_t index=0; //索引值

    uint16_t seg=0; //横轴差

    uint16_t temp=0; //温度值

    if(rtn<10000)  //0度时t=10000

    { 

        rtn=10000; 

    }

  else if( rtn >13872) //99度时t=13872

    { 

        rtn=13872; 

    } 

    index = (rtn-10000)/39;//(13872-10000)/100=38.72大致求出指针范围 

    if(index > 99) //限制指针范围index =99指最后一项 

    { 

        index = 99; 

    } 

    do{ 

        if(rtn>=PT1000_tab[index]&&rtn<=PT1000_tab[index+1]) 

        { 

            break; 

        }

else if(rtn

        {     

            if(index >0 ) 

            { 

                index--; 

                continue; 

            }

else

              break; 

            } 

        }

else if(rtn>PT1000_tab[index+1]) 

        { 

            if(index < 99 ) 

            { 

                index++; 

                continue; 

            }

else

               break; 

            } 

        } 

    }while(1); 

    seg = PT1000_tab[index+1]-PT1000_tab[index]; //横坐标

    temp =(index)*10+(uint8_t)((((rtn-PT1000_tab[index])*1)/seg)*10);//每隔1°求值故乘以 1

return temp;

}

uint16_t Temp(void)

{

  uint16_t  u16Temp;//温度值

float Rx;

Rx = AppAdcColTemp()*10;

// TRx = Rx*10;

  u16Temp = PT1000_Temp(Rx)*10;

return u16Temp;

}

/* EOF. */


作者: cxytj, 来源:面包板社区

链接: https://mbb.eet-china.com/blog/uid-me-4026976.html

版权声明:本文为博主原创,未经本人允许,禁止转载!

给作者打赏,鼓励TA抓紧创作!

赞赏支持
点赞 0
赞赏0

文章评论0条评论)

登录后参与讨论
我要评论
0
0
1
2
3
4
5
6
7
8
9
0
关闭 热点推荐上一条 /3 下一条