Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:1df541c54e25, 2018-06-19 (annotated)
- Committer:
- geryyy
- Date:
- Tue Jun 19 08:33:20 2018 +0000
- Revision:
- 0:1df541c54e25
- Child:
- 1:5bceb51fde85
PWM working
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| geryyy | 0:1df541c54e25 | 1 | #include "mbed.h" |
| geryyy | 0:1df541c54e25 | 2 | #include "stm32f7xx_hal.h" |
| geryyy | 0:1df541c54e25 | 3 | |
| geryyy | 0:1df541c54e25 | 4 | #define TIMx TIM3 |
| geryyy | 0:1df541c54e25 | 5 | #define TIMx_CLK_ENABLE() __TIM3_CLK_ENABLE() |
| geryyy | 0:1df541c54e25 | 6 | |
| geryyy | 0:1df541c54e25 | 7 | /* Definition for TIMx Channel Pins */ |
| geryyy | 0:1df541c54e25 | 8 | #define TIMx_CHANNEL_GPIO_PORT() __HAL_RCC_GPIOB_CLK_ENABLE(); |
| geryyy | 0:1df541c54e25 | 9 | #define TIMx_GPIO_PORT_CHANNEL1 GPIOB |
| geryyy | 0:1df541c54e25 | 10 | #define TIMx_GPIO_PORT_CHANNEL2 GPIOB |
| geryyy | 0:1df541c54e25 | 11 | #define TIMx_GPIO_PORT_CHANNEL3 GPIOB |
| geryyy | 0:1df541c54e25 | 12 | #define TIMx_GPIO_PORT_CHANNEL4 GPIOB |
| geryyy | 0:1df541c54e25 | 13 | #define TIMx_GPIO_PIN_CHANNEL1 GPIO_PIN_4 |
| geryyy | 0:1df541c54e25 | 14 | #define TIMx_GPIO_PIN_CHANNEL2 GPIO_PIN_5 |
| geryyy | 0:1df541c54e25 | 15 | #define TIMx_GPIO_AF_CHANNEL1 GPIO_AF2_TIM3 |
| geryyy | 0:1df541c54e25 | 16 | #define TIMx_GPIO_AF_CHANNEL2 GPIO_AF2_TIM3 |
| geryyy | 0:1df541c54e25 | 17 | |
| geryyy | 0:1df541c54e25 | 18 | TIM_HandleTypeDef TimHandle; |
| geryyy | 0:1df541c54e25 | 19 | /* Prescaler value declartion*/ |
| geryyy | 0:1df541c54e25 | 20 | uint32_t uhPrescalerValue = 0; |
| geryyy | 0:1df541c54e25 | 21 | |
| geryyy | 0:1df541c54e25 | 22 | TIM_OC_InitTypeDef sConfig; |
| geryyy | 0:1df541c54e25 | 23 | |
| geryyy | 0:1df541c54e25 | 24 | void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim); |
| geryyy | 0:1df541c54e25 | 25 | void Error_Handler(char *file, int line); |
| geryyy | 0:1df541c54e25 | 26 | |
| geryyy | 0:1df541c54e25 | 27 | |
| geryyy | 0:1df541c54e25 | 28 | DigitalOut myled(LED1); |
| geryyy | 0:1df541c54e25 | 29 | |
| geryyy | 0:1df541c54e25 | 30 | int main() { |
| geryyy | 0:1df541c54e25 | 31 | printf("HV Pulsgenerator V0.1\nGerald Ebmer 2018\n\n"); |
| geryyy | 0:1df541c54e25 | 32 | |
| geryyy | 0:1df541c54e25 | 33 | uhPrescalerValue = (uint32_t)(((SystemCoreClock/ 2) / 10000000) - 1); |
| geryyy | 0:1df541c54e25 | 34 | |
| geryyy | 0:1df541c54e25 | 35 | TimHandle.Instance = TIMx; |
| geryyy | 0:1df541c54e25 | 36 | |
| geryyy | 0:1df541c54e25 | 37 | TimHandle.Init.Prescaler = uhPrescalerValue; |
| geryyy | 0:1df541c54e25 | 38 | TimHandle.Init.Period = 1000; |
| geryyy | 0:1df541c54e25 | 39 | TimHandle.Init.ClockDivision = 0; |
| geryyy | 0:1df541c54e25 | 40 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
| geryyy | 0:1df541c54e25 | 41 | TimHandle.Init.RepetitionCounter = 0; |
| geryyy | 0:1df541c54e25 | 42 | TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
| geryyy | 0:1df541c54e25 | 43 | if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) |
| geryyy | 0:1df541c54e25 | 44 | { |
| geryyy | 0:1df541c54e25 | 45 | /* Initialization Error */ |
| geryyy | 0:1df541c54e25 | 46 | Error_Handler(__FILE__, __LINE__); |
| geryyy | 0:1df541c54e25 | 47 | } |
| geryyy | 0:1df541c54e25 | 48 | |
| geryyy | 0:1df541c54e25 | 49 | /*##-2- Configure the PWM channels #########################################*/ |
| geryyy | 0:1df541c54e25 | 50 | /* Common configuration for all channels */ |
| geryyy | 0:1df541c54e25 | 51 | sConfig.OCMode = TIM_OCMODE_PWM1; |
| geryyy | 0:1df541c54e25 | 52 | sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; |
| geryyy | 0:1df541c54e25 | 53 | sConfig.OCFastMode = TIM_OCFAST_DISABLE; |
| geryyy | 0:1df541c54e25 | 54 | sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; |
| geryyy | 0:1df541c54e25 | 55 | sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET; |
| geryyy | 0:1df541c54e25 | 56 | |
| geryyy | 0:1df541c54e25 | 57 | sConfig.OCIdleState = TIM_OCIDLESTATE_RESET; |
| geryyy | 0:1df541c54e25 | 58 | |
| geryyy | 0:1df541c54e25 | 59 | /* Set the pulse value for channel 1 */ |
| geryyy | 0:1df541c54e25 | 60 | sConfig.Pulse = 100; |
| geryyy | 0:1df541c54e25 | 61 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) |
| geryyy | 0:1df541c54e25 | 62 | { |
| geryyy | 0:1df541c54e25 | 63 | /* Configuration Error */ |
| geryyy | 0:1df541c54e25 | 64 | Error_Handler(__FILE__, __LINE__); |
| geryyy | 0:1df541c54e25 | 65 | } |
| geryyy | 0:1df541c54e25 | 66 | |
| geryyy | 0:1df541c54e25 | 67 | /* Set the pulse value for channel 2 */ |
| geryyy | 0:1df541c54e25 | 68 | sConfig.Pulse = 900; |
| geryyy | 0:1df541c54e25 | 69 | if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) |
| geryyy | 0:1df541c54e25 | 70 | { |
| geryyy | 0:1df541c54e25 | 71 | /* Configuration Error */ |
| geryyy | 0:1df541c54e25 | 72 | Error_Handler(__FILE__, __LINE__); |
| geryyy | 0:1df541c54e25 | 73 | } |
| geryyy | 0:1df541c54e25 | 74 | |
| geryyy | 0:1df541c54e25 | 75 | |
| geryyy | 0:1df541c54e25 | 76 | /*##-3- Start PWM signals generation #######################################*/ |
| geryyy | 0:1df541c54e25 | 77 | /* Start channel 1 */ |
| geryyy | 0:1df541c54e25 | 78 | if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) |
| geryyy | 0:1df541c54e25 | 79 | { |
| geryyy | 0:1df541c54e25 | 80 | /* PWM Generation Error */ |
| geryyy | 0:1df541c54e25 | 81 | Error_Handler(__FILE__, __LINE__); |
| geryyy | 0:1df541c54e25 | 82 | } |
| geryyy | 0:1df541c54e25 | 83 | /* Start channel 2 */ |
| geryyy | 0:1df541c54e25 | 84 | if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) |
| geryyy | 0:1df541c54e25 | 85 | { |
| geryyy | 0:1df541c54e25 | 86 | /* PWM Generation Error */ |
| geryyy | 0:1df541c54e25 | 87 | Error_Handler(__FILE__, __LINE__); |
| geryyy | 0:1df541c54e25 | 88 | } |
| geryyy | 0:1df541c54e25 | 89 | |
| geryyy | 0:1df541c54e25 | 90 | while(1) { |
| geryyy | 0:1df541c54e25 | 91 | myled = !myled; |
| geryyy | 0:1df541c54e25 | 92 | wait(1); |
| geryyy | 0:1df541c54e25 | 93 | } |
| geryyy | 0:1df541c54e25 | 94 | } |
| geryyy | 0:1df541c54e25 | 95 | |
| geryyy | 0:1df541c54e25 | 96 | void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) |
| geryyy | 0:1df541c54e25 | 97 | { |
| geryyy | 0:1df541c54e25 | 98 | GPIO_InitTypeDef GPIO_InitStruct; |
| geryyy | 0:1df541c54e25 | 99 | /*##-1- Enable peripherals and GPIO Clocks #################################*/ |
| geryyy | 0:1df541c54e25 | 100 | /* TIMx Peripheral clock enable */ |
| geryyy | 0:1df541c54e25 | 101 | TIMx_CLK_ENABLE(); |
| geryyy | 0:1df541c54e25 | 102 | |
| geryyy | 0:1df541c54e25 | 103 | /* Enable all GPIO Channels Clock requested */ |
| geryyy | 0:1df541c54e25 | 104 | TIMx_CHANNEL_GPIO_PORT(); |
| geryyy | 0:1df541c54e25 | 105 | |
| geryyy | 0:1df541c54e25 | 106 | /* Configure PB.04 (pin 19 in CN7 connector) (TIM3_Channel1), PB.05 (pin 13 in CN7 connector) (TIM3_Channel2), PB.00 (pin 31 in CN10 connector) (TIM3_Channel3), |
| geryyy | 0:1df541c54e25 | 107 | PB.01 (pin 7 in CN10 connector) (TIM3_Channel4) in output, push-pull, alternate function mode |
| geryyy | 0:1df541c54e25 | 108 | */ |
| geryyy | 0:1df541c54e25 | 109 | /* Common configuration for all channels */ |
| geryyy | 0:1df541c54e25 | 110 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| geryyy | 0:1df541c54e25 | 111 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
| geryyy | 0:1df541c54e25 | 112 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| geryyy | 0:1df541c54e25 | 113 | |
| geryyy | 0:1df541c54e25 | 114 | GPIO_InitStruct.Alternate = TIMx_GPIO_AF_CHANNEL1; |
| geryyy | 0:1df541c54e25 | 115 | GPIO_InitStruct.Pin = TIMx_GPIO_PIN_CHANNEL1; |
| geryyy | 0:1df541c54e25 | 116 | HAL_GPIO_Init(TIMx_GPIO_PORT_CHANNEL1, &GPIO_InitStruct); |
| geryyy | 0:1df541c54e25 | 117 | |
| geryyy | 0:1df541c54e25 | 118 | GPIO_InitStruct.Alternate = TIMx_GPIO_AF_CHANNEL2; |
| geryyy | 0:1df541c54e25 | 119 | GPIO_InitStruct.Pin = TIMx_GPIO_PIN_CHANNEL2; |
| geryyy | 0:1df541c54e25 | 120 | HAL_GPIO_Init(TIMx_GPIO_PORT_CHANNEL2, &GPIO_InitStruct); |
| geryyy | 0:1df541c54e25 | 121 | } |
| geryyy | 0:1df541c54e25 | 122 | |
| geryyy | 0:1df541c54e25 | 123 | |
| geryyy | 0:1df541c54e25 | 124 | /** |
| geryyy | 0:1df541c54e25 | 125 | * @brief This function is executed in case of error occurrence. |
| geryyy | 0:1df541c54e25 | 126 | * @param file: The file name as string. |
| geryyy | 0:1df541c54e25 | 127 | * @param line: The line in file as a number. |
| geryyy | 0:1df541c54e25 | 128 | * @retval None |
| geryyy | 0:1df541c54e25 | 129 | */ |
| geryyy | 0:1df541c54e25 | 130 | void Error_Handler(char *file, int line) |
| geryyy | 0:1df541c54e25 | 131 | { |
| geryyy | 0:1df541c54e25 | 132 | fprintf(stderr, "Error in file %s linenr %d!\n",file,line); |
| geryyy | 0:1df541c54e25 | 133 | /* USER CODE BEGIN Error_Handler_Debug */ |
| geryyy | 0:1df541c54e25 | 134 | /* User can add his own implementation to report the HAL error return state */ |
| geryyy | 0:1df541c54e25 | 135 | while(1) |
| geryyy | 0:1df541c54e25 | 136 | { |
| geryyy | 0:1df541c54e25 | 137 | } |
| geryyy | 0:1df541c54e25 | 138 | /* USER CODE END Error_Handler_Debug */ |
| geryyy | 0:1df541c54e25 | 139 | } |