Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include "em_device.h"
  2. #include "em_cmu.h"
  3. #include "em_gpio.h"
  4. #include "em_system.h"
  5. #include "em_timer.h"
  6. #include "em_chip.h"
  7.  
  8. #define LED_PORT gpioPortE
  9. #define LED_PIN 2
  10.  
  11. uint16_t ms_counter = 0;
  12.  
  13. void TIMER0_IRQHandler(void)
  14. {
  15. TIMER_IntClear(TIMER0, TIMER_IF_OF); // Clear overflow flag
  16. ms_counter++; // Increment counter
  17. }
  18.  
  19. int main() {
  20.  
  21. CHIP_Init(); // This function addresses some chip errata and should be called at the start of every EFM32 application (need em_system.c)
  22.  
  23. CMU_HFRCOBandSet(cmuHFRCOBand_1MHz); // Set High Freq. RC Osc. to 1 MHz
  24. CMU_ClockEnable(cmuClock_GPIO, true); // Enable GPIO peripheral clock
  25. CMU_ClockEnable(cmuClock_TIMER0, true); // Enable TIMER0 peripheral clock
  26.  
  27. GPIO_PinModeSet(LED_PORT, LED_PIN, gpioModePushPullDrive, 0); // Configure LED0 pin as digital output (push-pull)
  28.  
  29. TIMER_TopSet(TIMER0, 1000); // Set timer TOP value
  30. TIMER_Init_TypeDef timerInit = // Setup Timer initialization
  31. {
  32. .enable = true, // Start timer upon configuration
  33. .debugRun = true, // Keep timer running even on debug halt
  34. .prescale = timerPrescale1, // Use /1 prescaler...timer clock = HF clock = 1 MHz
  35. .clkSel = timerClkSelHFPerClk, // Set HF peripheral clock as clock source
  36. .fallAction = timerInputActionNone, // No action on falling edge
  37. .riseAction = timerInputActionNone, // No action on rising edge
  38. .mode = timerModeUp, // Use up-count mode
  39. .dmaClrAct = false, // Not using DMA
  40. .quadModeX4 = false, // Not using quad decoder
  41. .oneShot = false, // Using continuous, not one-shot
  42. .sync = false, // Not synchronizing timer operation off of other timers
  43. };
  44. TIMER_IntEnable(TIMER0, TIMER_IF_OF); // Enable Timer0 overflow interrupt
  45. NVIC_EnableIRQ(TIMER0_IRQn); // Enable TIMER0 interrupt vector in NVIC
  46. TIMER_Init(TIMER0, &timerInit); // Configure and start Timer0
  47.  
  48. while(1)
  49. {
  50. if(ms_counter == 500) {
  51. GPIO_PinOutToggle(LED_PORT, LED_PIN); // Toggle LED
  52. ms_counter = 0; // Reset counter
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement