PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gnu_simple_examples/gnu_blink/bsp/STM32F2xx_StdPeriph_Lib_V1.1.0/Project/STM32F2xx_StdPeriph_Examples/TIM/OCToggle/main.c

https://gitlab.com/willemwouters/STM32F
C | 227 lines | 70 code | 37 blank | 120 comment | 2 complexity | 4f50559c2ede33f5975015e5f0eca6dc MD5 | raw file
  1. /**
  2. ******************************************************************************
  3. * @file TIM/OCToggle/main.c
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 13-April-2012
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f2xx.h"
  29. /** @addtogroup STM32F2xx_StdPeriph_Examples
  30. * @{
  31. */
  32. /** @addtogroup TIM_OCToggle
  33. * @{
  34. */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  40. TIM_OCInitTypeDef TIM_OCInitStructure;
  41. __IO uint16_t CCR1_Val = 40961;
  42. __IO uint16_t CCR2_Val = 20480;
  43. __IO uint16_t CCR3_Val = 10240;
  44. __IO uint16_t CCR4_Val = 5120;
  45. uint16_t PrescalerValue = 0;
  46. /* Private function prototypes -----------------------------------------------*/
  47. void TIM_Config(void);
  48. /* Private functions ---------------------------------------------------------*/
  49. /**
  50. * @brief Main program
  51. * @param None
  52. * @retval None
  53. */
  54. int main(void)
  55. {
  56. /*!< At this stage the microcontroller clock setting is already configured,
  57. this is done through SystemInit() function which is called from startup
  58. file (startup_stm32f2xx.s) before to branch to application main.
  59. To reconfigure the default setting of SystemInit() function, refer to
  60. system_stm32f2xx.c file
  61. */
  62. /* TIM Configuration */
  63. TIM_Config();
  64. /* ---------------------------------------------------------------------------
  65. TIM3 Configuration: Output Compare Toggle Mode:
  66. In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),
  67. since APB1 prescaler is different from 1.
  68. TIM3CLK = 2 * PCLK1
  69. PCLK1 = HCLK / 4
  70. => TIM3CLK = HCLK / 2 = SystemCoreClock /2
  71. To get TIM3 counter clock at 15 MHz, the prescaler is computed as follows:
  72. Prescaler = (TIM3CLK / TIM3 counter clock) - 1
  73. Prescaler = ((SystemCoreClock /2) /15 MHz) - 1
  74. CC1 update rate = TIM3 counter clock / CCR1_Val = 366.2 Hz
  75. ==> So the TIM3 Channel 1 generates a periodic signal with a
  76. frequency equal to 183.1 Hz.
  77. CC2 update rate = TIM3 counter clock / CCR2_Val = 732.4 Hz
  78. ==> So the TIM3 Channel 2 generates a periodic signal with a
  79. frequency equal to 366.3 Hz.
  80. CC3 update rate = TIM3 counter clock / CCR3_Val = 1464.8 Hz
  81. ==> So the TIM3 Channel 3 generates a periodic signal with a
  82. frequency equal to 732.4 Hz.
  83. CC4 update rate = TIM3 counter clock / CCR4_Val = 2929.6 Hz
  84. ==> So the TIM3 Channel 4 generates a periodic signal with a
  85. frequency equal to 1464.8 Hz.
  86. Note:
  87. SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.c file.
  88. Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  89. function to update SystemCoreClock variable value. Otherwise, any configuration
  90. based on this variable will be incorrect.
  91. --------------------------------------------------------------------------- */
  92. /* Compute the prescaler value */
  93. PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 15000000) - 1;
  94. /* Time base configuration */
  95. TIM_TimeBaseStructure.TIM_Period = 65535;
  96. TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  97. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  98. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  99. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  100. /* Output Compare Toggle Mode configuration: Channel1 */
  101. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  102. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  103. TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  104. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  105. TIM_OC1Init(TIM3, &TIM_OCInitStructure);
  106. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
  107. /* Output Compare Toggle Mode configuration: Channel2 */
  108. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  109. TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
  110. TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  111. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
  112. /* Output Compare Toggle Mode configuration: Channel3 */
  113. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  114. TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  115. TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  116. TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
  117. /* Output Compare Toggle Mode configuration: Channel4 */
  118. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  119. TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
  120. TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  121. TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
  122. /* TIM enable counter */
  123. TIM_Cmd(TIM3, ENABLE);
  124. /* TIM IT enable */
  125. TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
  126. while (1)
  127. {}
  128. }
  129. /**
  130. * @brief Configure the TIM3 Pins.
  131. * @param None
  132. * @retval None
  133. */
  134. void TIM_Config(void)
  135. {
  136. GPIO_InitTypeDef GPIO_InitStructure;
  137. NVIC_InitTypeDef NVIC_InitStructure;
  138. /* TIM3 clock enable */
  139. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  140. /* GPIOC clock enable */
  141. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  142. /* GPIOC Configuration: TIM3 CH1 (PC6), TIM3 CH2 (PC7), TIM3 CH2 (PC8) and TIM3 CH4 (PC9) */
  143. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  144. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  145. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  146. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  147. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  148. GPIO_Init(GPIOC, &GPIO_InitStructure);
  149. /* Connect TIM Channels to AF2 */
  150. GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
  151. GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
  152. GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
  153. GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);
  154. /* Enable the TIM3 global Interrupt */
  155. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  156. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  157. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  158. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  159. NVIC_Init(&NVIC_InitStructure);
  160. }
  161. #ifdef USE_FULL_ASSERT
  162. /**
  163. * @brief Reports the name of the source file and the source line number
  164. * where the assert_param error has occurred.
  165. * @param file: pointer to the source file name
  166. * @param line: assert_param error line source number
  167. * @retval None
  168. */
  169. void assert_failed(uint8_t* file, uint32_t line)
  170. {
  171. /* User can add his own implementation to report the file name and line number,
  172. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  173. while (1)
  174. {}
  175. }
  176. #endif
  177. /**
  178. * @}
  179. */
  180. /**
  181. * @}
  182. */
  183. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/