EFM32片内外设Timer之基本操作

硬件环境:TG STK

实现的效果:LED灯每1秒闪烁一次 .Timer采用基本定时方式工作。

 

#include
#include
#include "efm32.h"
#include "efm32_chip.h"
#include "efm32_cmu.h"
#include "efm32_timer.h"
#include "efm32_gpio.h"


//13761 Hz -> 14Mhz (clock frequency) / 1024 (prescaler)
//Setting TOP to 27342 results in an overflow each 2 seconds

#define TOP (13761/2)

void TimerConfig(void)
{
    /* Enable clock for TIMER0 module */
    CMU_ClockEnable(cmuClock_TIMER0, true);
   
    /* Select TIMER0 parameters */ 
    TIMER_Init_TypeDef timerInit =
    {
        .enable     = true,
        .debugRun   = true,
        .prescale   = timerPrescale1024,
        .clkSel     = timerClkSelHFPerClk,
        .fallAction = timerInputActionNone,
        .riseAction = timerInputActionNone,
        .mode       = timerModeUp,
        .dmaClrAct  = false,
        .quadModeX4 = false,
        .oneShot    = false,
        .sync       = false,
    };
   
    /* Enable overflow interrupt */
    TIMER_IntEnable(TIMER0, TIMER_IF_OF);
   
    /* Enable TIMER0 interrupt vector in NVIC */
    NVIC_EnableIRQ(TIMER0_IRQn);
   
    /* Set TIMER Top value */
    TIMER_TopSet(TIMER0, TOP);
   
    /* Configure TIMER */
    TIMER_Init(TIMER0, &timerInit);
}

//中断响应函数
void TIMER0_IRQHandler(void)
{
    /* Clear flag for TIMER0 overflow interrupt */
    TIMER_IntClear(TIMER0, TIMER_IF_OF);
   
    GPIO_PinOutToggle(gpioPortD, 7);
}

/**************************************************************************//**
 * @brief  Main function
 *****************************************************************************/
int main(void)
{
  /* Chip errata */
  CHIP_Init();
  CMU_ClockEnable(cmuClock_GPIO, true);
  TimerConfig();
 
  GPIO_PinModeSet(gpioPortD, 7, gpioModePushPull, 0);
 
  while(1);
}

 

你可能感兴趣的:(EFM32片内外设Timer之基本操作)