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\r\n", 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...\r\n");
    saadc_init();
    while (true)
    {
        saadc_sampling_trigger();
        printf("Room Temperature: %d\r\n", 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): 

 

转载于:https://www.cnblogs.com/zzu-liulei/p/6519141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值