zoukankan      html  css  js  c++  java
  • nRF52832 SAADC sampling

    I have been able to use SAADC to measure temperature, but my code does not behave in the way I expect it to! In the config file I have the following settings set:

    1 #define SAADC_CONFIG_RESOLUTION    NRF_SAADC_RESOLUTION_10BIT
    2 #define SAADC_CONFIG_OVERSAMPLE      NRF_SAADC_OVERSAMPLE_DISABLED
    3 #define SAADC_CONFIG_IRQ_PRIORITY    APP_IRQ_PRIORITY_LOW

    here is my code:

    #define SAMPLES_IN_BUFFER 5
    
    static nrf_saadc_value_t       m_buffer_pool[2][SAMPLES_IN_BUFFER];
    static int temp_rotor;
    
    void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
    {
        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            int avg_sample = 0;
            float steinhart;
            float sampleAVG;
            int i;
            ret_code_t err_code;
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
            APP_ERROR_CHECK(err_code);
    
            for (i = 0; i < SAMPLES_IN_BUFFER; i++) 
            {
                  avg_sample += p_event->data.done.p_buffer[i]; // take N samples in a row
            }
            avg_sample /= i; // average all the samples out
            printf("Avg_sample: %d
    ", avg_sample);
    
            sampleAVG = 810 / avg_sample - 1;       //VDD = 2.85V
            sampleAVG = SERIESRESISTOR / sampleAVG;
    
            steinhart = 0;
            steinhart = sampleAVG / THERMISTORNOMINAL;   // (R/Ro)
            steinhart = log(steinhart);                  // ln(R/Ro)
            steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
            steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
            steinhart = 1.0 / steinhart;                 // Invert
            steinhart -= 273.15;                         // convert to C
            temp_rotor = (int)steinhart;             //convert float to int
        }
    }
    
    void saadc_init(void)
    {
        ret_code_t err_code;
        nrf_saadc_channel_config_t channel_config =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
        channel_config.gain = NRF_SAADC_GAIN1_6;
    
        err_code = nrf_drv_saadc_init(NULL, saadc_callback);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);
    }
    
    void saadc_sampling_trigger(void)
    {
        ret_code_t err_code;
        //Event handler is called immediately after conversion is finished.
        err_code = nrf_drv_saadc_sample(); // Check error
        APP_ERROR_CHECK(err_code);
    }
    
    int main(void)
    {
        uart_config();
        printf("SAADC Started...
    ");
        saadc_init();
        while (true)
        {
            saadc_sampling_trigger();
            printf("Room Temperature: %d
    ", temp_rotor);
            nrf_delay_ms(1000);       //waiting a second
        }
    
    }

    When I run the code, I get the following text on Termite terminal(each line is printed every 1 second): 

  • 相关阅读:
    AWVS 安全渗透扫描
    Nmon 监控结果分析
    Jmeter 插件图表分析
    Nmon 安装
    子母钟系统(卫星时钟系统)在医院网络中的架设与设计
    从NTP网络授时到社交GPS千奇百怪的应用方式
    NTP网络时钟系统技术参数与详细介绍
    GPS北斗子母钟系统在某机场系统中的应用
    GPS北斗时间频率信号源的定义及简介
    ntp对时服务器同步网络中计算机详细配置
  • 原文地址:https://www.cnblogs.com/zzu-liulei/p/6519141.html
Copyright © 2011-2022 走看看