/nucleo-l476RG/src/ch11/main-ex9.c
C | 118 lines | 67 code | 25 blank | 26 comment | 4 complexity | ef9a88dcc0f5ab161f0d68b8f1a4760e MD5 | raw file
Possible License(s): GPL-3.0
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l4xx_hal.h"
- #include <nucleo_hal_bsp.h>
- #include <string.h>
- #include <math.h>
-
- /* Private variables ---------------------------------------------------------*/
- TIM_HandleTypeDef htim2;
-
- void MX_TIM2_Init(void);
-
- DMA_HandleTypeDef hdma_tim2_ch1;
-
- #define PI 3.14159
- #define ASR 1.8 //360 / 200 = 1.8
-
- int main(void) {
- uint32_t IV[200];
- float angle;
-
- HAL_Init();
-
- Nucleo_BSP_Init();
- MX_TIM2_Init();
-
- for (uint8_t i = 0; i < 200; i++) {
- angle = ASR*(float)i;
- IV[i] = (uint16_t) rint(100 + 99*sinf(angle*(PI/180)));
- }
-
- HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, (uint32_t *)IV, 200);
-
- while (1);
- }
-
- /* TIM2 init function */
- void MX_TIM2_Init(void) {
- TIM_OC_InitTypeDef sConfigOC;
- TIM_ClockConfigTypeDef sClockSourceConfig;
-
- htim2.Instance = TIM2;
- htim2.Init.Prescaler = 39;
- htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim2.Init.Period = 199;
- htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- HAL_TIM_PWM_Init(&htim2);
-
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
-
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 0;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
-
- hdma_tim2_ch1.Instance = DMA1_Channel5;
- hdma_tim2_ch1.Init.Request = DMA_REQUEST_4;
- hdma_tim2_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_tim2_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_tim2_ch1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_tim2_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_tim2_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_tim2_ch1.Init.Mode = DMA_CIRCULAR;
- hdma_tim2_ch1.Init.Priority = DMA_PRIORITY_LOW;
- HAL_DMA_Init(&hdma_tim2_ch1);
-
- /* Several peripheral DMA handle pointers point to the same DMA handle.
- Be aware that there is only one channel to perform all the requested DMAs. */
- __HAL_LINKDMA(&htim2, hdma[TIM_DMA_ID_CC1],hdma_tim2_ch1);
- }
-
- void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_base) {
- GPIO_InitTypeDef GPIO_InitStruct;
- if (htim_base->Instance == TIM2) {
- __TIM2_CLK_ENABLE();
-
- /**TIM2 GPIO Configuration
- PA6 ------> TIM3_CH1
- */
- GPIO_InitStruct.Pin = GPIO_PIN_5;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- }
-
- #ifdef USE_FULL_ASSERT
-
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
-
- }
-
- #endif
-
- /**
- * @}
- */
-
- /**
- * @}
- */
-
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/