NRF52832定时器

本文详细介绍了如何在nRF52系列MCU上使用SDK14.0.0的TIMER0模块创建定时器。通过具体的代码示例,包括配置TIMER实例、定义事件处理函数、初始化定时器和设置周期性中断,展示了如何实现LED的闪烁效果。适用于嵌入式开发人员和学习者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以SDK14.0.0为例 ,TIMER0的定时器使用

1.加入文件nrf_drv_timer.c

2.sdk_config.h中的宏打开

#ifndef TIMER_ENABLED
#define TIMER_ENABLED 1
#endif


#ifndef TIMER0_ENABLED
#define TIMER0_ENABLED 1
#endif

3.定义一个TIMER实例 

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

4.定义一个timer事件处理函数
/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
  
    
            nrf_gpio_pin_toggle(31);
         
}

5.初时化timer

int main(void)
{
    uint32_t time_ms = 244; //Time(in miliseconds) between consecutive compare events.
    volatile uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    //Configure all leds on board.
    nrf_gpio_cfg_output(31);   //p0_31 配置成输出模式
 
    //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.frequency = 4;  // 修改分频,频率变成1MHZ
    err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
    APP_ERROR_CHECK(err_code);

   // time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms); //调用此方法,转换成TICK数,如果不调用,则可以实现

                                                                                                            //微秒级定时器

    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ms, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_LED);


       while (1)
    {
        __WFI();
    }
}

 

代码下载地址:https://download.youkuaiyun.com/download/mygod2008ok/10734229

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值