Advertisement
dtung

pwm

Sep 27th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include "stm32f10x.h"
  2.  
  3. void TIM2_IRQHandler(void);
  4.  
  5. static void gpio_init() {
  6. // enable PORTA GPIO0 to alt. function push-pull with max_speed 50MHz
  7. RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
  8. RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
  9. GPIOA->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_MODE0);
  10. GPIOA->CRL |= GPIO_CRL_CNF0_1|GPIO_CRL_MODE0;
  11. }
  12.  
  13. static void pwm_init(uint16_t psc, uint16_t arr, uint16_t ccr) {
  14. // timer init
  15. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  16. TIM2->SR = 0;
  17. TIM2->PSC = psc;
  18. TIM2->ARR = arr;
  19. TIM2->CCR1= ccr;
  20. // set up interrupt
  21. TIM2->DIER = TIM_DIER_UIE|TIM_DIER_CC1IE;
  22. NVIC->ISER[0] = NVIC_ISER_SETENA_28;
  23. // set output to PWM1 mode
  24. TIM2->CCMR1 = TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1M_1;
  25. TIM2->CCER = TIM_CCER_CC1E;
  26. }
  27.  
  28. void TIM2_IRQHandler() {
  29. TIM2->SR ^= TIM_SR_CC1IF|TIM_SR_UIF;
  30. }
  31.  
  32. int main() {
  33. gpio_init();
  34. pwm_init(1, 1, 1);
  35. // enable timer2
  36. TIM2->CR1 |= TIM_CR1_CEN;
  37. for (;;);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement