nrf52832 之 timer

nrf52832 有五个定时器,timer0--timer4 。因为SDK已经使用了timer0,所以如果使用了SDK,就不能再使用timer0,只能使用timer1--timer4。


开发环境:在ble_app_hrs   demo上添加定时器任务      参考timer这个demo。


本例使用timer1,并且修改为32bit模式



添加步骤:


1、添加驱动文件    nrf_drv_timer.c      在协议栈中的目录位置 :   nRF52_SDK\components\drivers_nrf\timer文件夹下


2、mian.c中添加头文件nrf_drv_timer.h

#include "nrf_drv_timer.h"

3、main.c中添加timer结构体变量:

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

4、在main.c中添加timer1 初始化函数以及定时任务处理函数:

/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
   // uint32_t led_to_invert = (1 << leds_list[(i++) % LEDS_NUMBER]);
   // printf("6\n");
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
          //  LEDS_INVERT(led_to_invert);
		  		ledFlash();
			
            break;
        
        default:
            //Do nothing.
            break;
    }    
}


void myTimerInit()
{
	uint32_t time_ms = 1000; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
    
    //Configure all leds on board.
 
    
    //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    err_code = nrf_drv_timer_init(&TIMER_LED, NULL, timer_led_event_handler);
    APP_ERROR_CHECK(err_code);
	
    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);
    
    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
    nrf_drv_timer_enable(&TIMER_LED);
}



5、main函数中添加timer初始化调用:

	myTimerInit();

6、在nrf_driver_config.c中修改timer1的定义:

#define TIMER1_ENABLED 1

#if (TIMER1_ENABLED == 1)
#define TIMER1_CONFIG_FREQUENCY    NRF_TIMER_FREQ_16MHz
#define TIMER1_CONFIG_MODE         TIMER_MODE_MODE_Timer
#define TIMER1_CONFIG_BIT_WIDTH    TIMER_BITMODE_BITMODE_32Bit
#define TIMER1_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW



  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是nrf52832做蓝牙麦克风的代码: ```c #include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "nrf_drv_saadc.h" #include "nrf_delay.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h" #include "app_error.h" #include "app_util_platform.h" #include "boards.h" #define SAMPLES_IN_BUFFER 8 static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0); static nrf_saadc_value_t m_buffer_pool[SAMPLES_IN_BUFFER]; static uint32_t m_adc_evt_counter; static void timer_handler(nrf_timer_event_t event_type, void * p_context) { } void saadc_callback(nrf_drv_saadc_evt_t const * p_event) { if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { 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); uint32_t sum = 0; for (int i = 0; i < SAMPLES_IN_BUFFER; i++) { sum += p_event->data.done.p_buffer[i]; } uint16_t average = sum / SAMPLES_IN_BUFFER; uint8_t battery_level = average * 100 / 2047; // TODO: 发送蓝牙数据,可以使用Nordic的nRF Connect应用程序测试接收到的数据 m_adc_evt_counter++; } } 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); 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, SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); } void timer_init(void) { ret_code_t err_code; nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); APP_ERROR_CHECK(err_code); uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 1000); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); nrf_drv_timer_enable(&m_timer); } int main(void) { saadc_init(); timer_init(); while (true) { nrf_delay_ms(1000); } } ``` 代码中使用了nrf_drv_saadc和nrf_drv_timer驱动程序库,需要在项目配置中添加相应的库文件。在代码中,使用了TIMER0定时器,每1秒钟触发一次ADC采样,并发送蓝牙数据。在saadc_callback()函数中,计算了采样数据的平均值,并将其转换成电池电压百分比。在发送蓝牙数据之前,需要添加相应的蓝牙协议栈代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路人 假

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值