PageRenderTime 34ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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