Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. void PWM_Setup(void){
  2. GPIO_InitTypeDef GPIO_InitStructure;
  3. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  4. TIM_OCInitTypeDef TIM_OCInitStructure;
  5. NVIC_InitTypeDef NVIC_InitStructure;
  6. DAC_InitTypeDef DAC_InitStructure;
  7.  
  8. uint16_t TIM2_period = 6000;
  9. uint16_t TIM14_period = 800;
  10.  
  11. //[..] To use the Timer in Output Compare mode, the following steps are mandatory:
  12.  
  13. //(#) Enable TIM clock using
  14. // RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function.
  15. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  16. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE);
  17. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
  18. //(#) Configure the TIM pins by configuring the corresponding GPIO pins
  19. // This is LED3 on STM32F0-Discovery
  20. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  21. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  22. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  23. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24. GPIO_Init(GPIOA, &GPIO_InitStructure);
  25. GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_4);
  26.  
  27. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  28. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  29. TIM_TimeBaseStructure.TIM_Period = TIM2_period;
  30. TIM_TimeBaseStructure.TIM_Prescaler = 0;
  31.  
  32. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  33.  
  34. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  35. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  36. TIM_OCInitStructure.TIM_Pulse = TIM2_period-1;
  37. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  38.  
  39. //Enable interrupt on compare for timer2
  40. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  41. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  42. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  43. NVIC_Init(&NVIC_InitStructure);
  44.  
  45. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  46. NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  47. NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  48. NVIC_Init(&NVIC_InitStructure);
  49.  
  50. DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  51. DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  52. DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  53.  
  54. TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  55. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  56. TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
  57. TIM_Cmd(TIM2, ENABLE);
  58. TIM_Cmd(TIM3, DISABLE);
  59. DAC_Cmd(DAC_Channel_1, DISABLE);
  60.  
  61. //(#) Configure the Time base unit as described in the first part of this
  62. // driver, if needed, else the Timer will run with the default
  63. // configuration:
  64. // (++) Autoreload value = 0xFFFF.
  65. // (++) Prescaler value = 0x0000.
  66. // (++) Counter mode = Up counting.
  67. // (++) Clock Division = TIM_CKD_DIV1.
  68. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  69. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  70. TIM_TimeBaseStructure.TIM_Period = TIM14_period;
  71. TIM_TimeBaseStructure.TIM_Prescaler = 0;
  72. TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
  73.  
  74. //(#) Fill the TIM_OCInitStruct with the desired parameters including:
  75. // (++) The TIM Output Compare mode: TIM_OCMode.
  76. // (++) TIM Output State: TIM_OutputState.
  77. // (++) TIM Pulse value: TIM_Pulse.
  78. // (++) TIM Output Compare Polarity : TIM_OCPolarity.
  79. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  80. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  81. TIM_OCInitStructure.TIM_Pulse = TIM14_period-1;
  82. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  83.  
  84. //(#) Call TIM_OCxInit(TIMx, &TIM_OCInitStruct) to configure the desired
  85. // channel with the corresponding configuration.
  86. TIM_OC1Init(TIM14, &TIM_OCInitStructure);
  87.  
  88. //(#) Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  89. TIM_Cmd(TIM14, ENABLE);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement