#include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "nrf_drv_timer.h" #include "bsp.h" #include "app_error.h" const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0); /** * @brief Handler for timer events. */ void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context) { static uint32_t i; uint32_t led_to_invert = ((i++) % LEDS_NUMBER); switch (event_type) { case NRF_TIMER_EVENT_COMPARE0: bsp_board_led_invert(led_to_invert); break; default: //Do nothing. break; } } /** * @brief Function for main application entry. */ int main(void) { uint32_t time_ms = 500; //Time(in miliseconds) between consecutive compare events. uint32_t time_ticks; uint32_t err_code = NRF_SUCCESS; //Configure all leds on board. bsp_board_leds_init(); //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; 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); 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); while (1) { __WFI(); } }
const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);設定為Timer0。
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context);Timer中斷副程式。
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;設定為default config。
nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);Timer初時化設定及中斷副程式。
nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);中斷時間轉為ticks.
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);Timer啟動
此程式每500ms產生中斷,程式會跳到void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)去執行LED控制。
#CSR #CSR8635 #藍芽 #藍芽立體聲喇叭 #電子外包 #PCB LAYOUT #電子設計 #學生專題製作#電路設計
沒有留言:
張貼留言