main.c 1.3 KB

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