main.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <stm32f10x.h>
  2. void initGPIO()
  3. {
  4. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  5. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  6. GPIO_InitTypeDef GPIO_Config;
  7. GPIO_Config.GPIO_Pin = GPIO_Pin_6;
  8. GPIO_Config.GPIO_Mode = GPIO_Mode_AF_PP;
  9. GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
  10. GPIO_Init(GPIOA, &GPIO_Config);
  11. }
  12. void initTimers()
  13. {
  14. TIM_TimeBaseInitTypeDef TIM_BaseConfig;
  15. TIM_OCInitTypeDef TIM_OCConfig;
  16. // 0 kHz timer.
  17. TIM_BaseConfig.TIM_Prescaler = (uint16_t)(SystemCoreClock / 10000) - 1;
  18. // 10000 / 5000 = 2 Hz blinking.
  19. TIM_BaseConfig.TIM_Period = 5000;
  20. TIM_BaseConfig.TIM_ClockDivision = 0;
  21. TIM_BaseConfig.TIM_CounterMode = TIM_CounterMode_Up;
  22. TIM_TimeBaseInit(TIM3, &TIM_BaseConfig);
  23. TIM_OCConfig.TIM_OCMode = TIM_OCMode_PWM1;
  24. TIM_OCConfig.TIM_OutputState = TIM_OutputState_Enable;
  25. // 2500 / 5000 = 50% duty cycle.
  26. TIM_OCConfig.TIM_Pulse = 2499;
  27. TIM_OCConfig.TIM_OCPolarity = TIM_OCPolarity_High;
  28. TIM_OC1Init(TIM3, &TIM_OCConfig);
  29. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
  30. TIM_ARRPreloadConfig(TIM3, ENABLE);
  31. TIM_Cmd(TIM3, ENABLE);
  32. }
  33. void initAll(void)
  34. {
  35. initGPIO();
  36. initTimers();
  37. }
  38. int main(void)
  39. {
  40. initAll();
  41. for (;;);
  42. return 0;
  43. }