• 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): 

  • 相关阅读:
    JVM理论:(一/2)OutOfMemoryError异常
    JVM理论:(一/1)对象的创建过程
    JVM理论:(一)JVM内存模型
    MySQL优化(6):Mysql锁机制
    MySQL优化(5):索引失效分析、in与exists使用场合
    MySQL优化(4):explain分析
    MySQL优化(3):慢SQL分析
    MySQL优化(2):索引简述
    MySQL优化(1):Mysql简述
    MySQL基础(4):事务控制
  • 原文地址:https://www.cnblogs.com/zzu-liulei/p/6519141.html
Copyright © 2020-2023  润新知