Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. /**
  2. -----------------------------------------------------------------*/
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_rcc.h"
  5. #include "stm32f10x_tim.h"
  6.  
  7.  
  8.  
  9. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  10. TIM_OCInitTypeDef TIM_OCInitStructure;
  11. uint16_t CCR1_Val = 333;
  12. uint16_t CCR2_Val = 249;
  13. uint16_t CCR3_Val = 166;
  14. uint16_t CCR4_Val = 83;
  15. uint16_t PrescalerValue ;
  16.  
  17. /* Private function prototypes -----------------------------------------------*/
  18. void RCC_Configuration(void);
  19. void GPIO_Configuration(void);
  20.  
  21. /* Private functions ---------------------------------------------------------*/
  22.  
  23. /**
  24.  
  25. int main(void)
  26. {
  27.  
  28. RCC_Configuration();
  29.  
  30. /*GPIO Configuration */
  31. GPIO_Configuration();
  32.  
  33.  
  34. PrescalerValue =(72000000 / 24000000) - 1;
  35. /* Time base configuration */
  36. TIM_TimeBaseStructure.TIM_Period = 665;
  37. TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  38. TIM_TimeBaseStructure.TIM_ClockDivision = 4;
  39. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  40.  
  41. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  42.  
  43. /* PWM1 Mode configuration: Channel1 */
  44. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  45. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  46. TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  47. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  48.  
  49. TIM_OC1Init(TIM3, &TIM_OCInitStructure);
  50.  
  51. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
  52.  
  53. /* PWM1 Mode configuration: Channel2 */
  54. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  55. TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
  56.  
  57. TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  58.  
  59. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
  60.  
  61. /* PWM1 Mode configuration: Channel3 */
  62. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  63. TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  64.  
  65. TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  66.  
  67. TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
  68.  
  69.  
  70. /* TIM3 enable counter */
  71. TIM_Cmd(TIM3, ENABLE);
  72.  
  73. while (1)
  74. {}
  75. }
  76.  
  77.  
  78. void RCC_Configuration(void)
  79. {
  80. /* TIM3 clock enable */
  81. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  82.  
  83. /* GPIOA and GPIOB clock enable */
  84. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  85. }
  86.  
  87.  
  88. void GPIO_Configuration(void)
  89. {
  90. GPIO_InitTypeDef GPIO_InitStructure;
  91. /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14|GPIO_Pin_15 ;
  93. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  94. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  95.  
  96. GPIO_Init(GPIOB, &GPIO_InitStructure);
  97.  
  98. GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement