PageRenderTime 26ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/nucleo-l476RG/src/ch11/main-ex9.c

https://bitbucket.org/teknohan/mastering-stm32-book-example
C | 118 lines | 67 code | 25 blank | 26 comment | 4 complexity | ef9a88dcc0f5ab161f0d68b8f1a4760e MD5 | raw file
Possible License(s): GPL-3.0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32l4xx_hal.h"
  3. #include <nucleo_hal_bsp.h>
  4. #include <string.h>
  5. #include <math.h>
  6. /* Private variables ---------------------------------------------------------*/
  7. TIM_HandleTypeDef htim2;
  8. void MX_TIM2_Init(void);
  9. DMA_HandleTypeDef hdma_tim2_ch1;
  10. #define PI 3.14159
  11. #define ASR 1.8 //360 / 200 = 1.8
  12. int main(void) {
  13. uint32_t IV[200];
  14. float angle;
  15. HAL_Init();
  16. Nucleo_BSP_Init();
  17. MX_TIM2_Init();
  18. for (uint8_t i = 0; i < 200; i++) {
  19. angle = ASR*(float)i;
  20. IV[i] = (uint16_t) rint(100 + 99*sinf(angle*(PI/180)));
  21. }
  22. HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, (uint32_t *)IV, 200);
  23. while (1);
  24. }
  25. /* TIM2 init function */
  26. void MX_TIM2_Init(void) {
  27. TIM_OC_InitTypeDef sConfigOC;
  28. TIM_ClockConfigTypeDef sClockSourceConfig;
  29. htim2.Instance = TIM2;
  30. htim2.Init.Prescaler = 39;
  31. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  32. htim2.Init.Period = 199;
  33. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  34. HAL_TIM_PWM_Init(&htim2);
  35. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  36. HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  37. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  38. sConfigOC.Pulse = 0;
  39. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  40. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  41. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  42. hdma_tim2_ch1.Instance = DMA1_Channel5;
  43. hdma_tim2_ch1.Init.Request = DMA_REQUEST_4;
  44. hdma_tim2_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
  45. hdma_tim2_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
  46. hdma_tim2_ch1.Init.MemInc = DMA_MINC_ENABLE;
  47. hdma_tim2_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  48. hdma_tim2_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  49. hdma_tim2_ch1.Init.Mode = DMA_CIRCULAR;
  50. hdma_tim2_ch1.Init.Priority = DMA_PRIORITY_LOW;
  51. HAL_DMA_Init(&hdma_tim2_ch1);
  52. /* Several peripheral DMA handle pointers point to the same DMA handle.
  53. Be aware that there is only one channel to perform all the requested DMAs. */
  54. __HAL_LINKDMA(&htim2, hdma[TIM_DMA_ID_CC1],hdma_tim2_ch1);
  55. }
  56. void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_base) {
  57. GPIO_InitTypeDef GPIO_InitStruct;
  58. if (htim_base->Instance == TIM2) {
  59. __TIM2_CLK_ENABLE();
  60. /**TIM2 GPIO Configuration
  61. PA6 ------> TIM3_CH1
  62. */
  63. GPIO_InitStruct.Pin = GPIO_PIN_5;
  64. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  65. GPIO_InitStruct.Pull = GPIO_NOPULL;
  66. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  67. GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
  68. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  69. }
  70. }
  71. #ifdef USE_FULL_ASSERT
  72. /**
  73. * @brief Reports the name of the source file and the source line number
  74. * where the assert_param error has occurred.
  75. * @param file: pointer to the source file name
  76. * @param line: assert_param error line source number
  77. * @retval None
  78. */
  79. void assert_failed(uint8_t* file, uint32_t line)
  80. {
  81. /* USER CODE BEGIN 6 */
  82. /* User can add his own implementation to report the file name and line number,
  83. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  84. /* USER CODE END 6 */
  85. }
  86. #endif
  87. /**
  88. * @}
  89. */
  90. /**
  91. * @}
  92. */
  93. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/