Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
Diff: HardwareSetup.cpp
- Revision:
- 2:30503be9a375
- Parent:
- 1:4c3c2b7337a6
- Child:
- 3:2f46953e7c8b
--- a/HardwareSetup.cpp Tue Aug 25 23:38:26 2020 +0000 +++ b/HardwareSetup.cpp Wed Aug 26 00:24:51 2020 +0000 @@ -12,6 +12,11 @@ void SystemClock_Config(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); static void MX_TIM12_Init(void); +static void MX_TIM13_Init(void); +static void MX_TIM14_Init(void); +static void MX_TIM15_Init(void); +static void MX_TIM16_Init(void); +static void MX_TIM17_Init(void); void initHardware(){ /* Initialise the HAL Layer */ @@ -22,24 +27,29 @@ /* Initialize all configured peripherals */ MX_TIM12_Init(); - + MX_TIM13_Init(); + MX_TIM14_Init(); + MX_TIM15_Init(); + MX_TIM16_Init(); + MX_TIM17_Init(); + //HAL_TIM_Base_Start(&htim15); + HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_2); // start pwm generation + HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); // start pwm generation + HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); // start pwm generation + HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); // start pwm generation + HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2); // start pwm generation + HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); // start pwm generation + HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1); // start pwm generation + + } static void MX_TIM12_Init(void) { - - /* USER CODE BEGIN TIM12_Init 0 */ - - /* USER CODE END TIM12_Init 0 */ - __HAL_RCC_TIM12_CLK_ENABLE(); TIM_OC_InitTypeDef sConfigOC = {0}; - - /* USER CODE BEGIN TIM12_Init 1 */ - - /* USER CODE END TIM12_Init 1 */ htim12.Instance = TIM12; htim12.Init.Prescaler = 0; htim12.Init.CounterMode = TIM_COUNTERMODE_UP; @@ -62,15 +72,218 @@ { //Error_Handler(); } - /* USER CODE BEGIN TIM12_Init 2 */ + HAL_TIM_MspPostInit(&htim12); +} + +static void MX_TIM13_Init(void) +{ + __HAL_RCC_TIM13_CLK_ENABLE(); + TIM_OC_InitTypeDef sConfigOC = {0}; + htim13.Instance = TIM13; + htim13.Init.Prescaler = 0; + htim13.Init.CounterMode = TIM_COUNTERMODE_UP; + htim13.Init.Period = PWM_PERIOD; + htim13.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim13.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim13) != HAL_OK) + { + //Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim13) != HAL_OK) + { + //Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim13, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + //Error_Handler(); + } + HAL_TIM_MspPostInit(&htim13); +} + +static void MX_TIM14_Init(void) +{ + __HAL_RCC_TIM14_CLK_ENABLE(); + TIM_OC_InitTypeDef sConfigOC = {0}; + htim14.Instance = TIM14; + htim14.Init.Prescaler = 0; + htim14.Init.CounterMode = TIM_COUNTERMODE_UP; + htim14.Init.Period = PWM_PERIOD; + htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim14) != HAL_OK) + { + //Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim14) != HAL_OK) + { + //Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim14, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + //Error_Handler(); + } + HAL_TIM_MspPostInit(&htim14); +} - /* USER CODE END TIM12_Init 2 */ - HAL_TIM_MspPostInit(&htim12); +static void MX_TIM15_Init(void) +{ + __HAL_RCC_TIM15_CLK_ENABLE(); + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; + htim15.Instance = TIM15; + htim15.Init.Prescaler = 0; + htim15.Init.CounterMode = TIM_COUNTERMODE_UP; + htim15.Init.Period = PWM_PERIOD; + htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim15.Init.RepetitionCounter = 0; + htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_PWM_Init(&htim15) != HAL_OK) + { + //Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK) + { + //Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + //Error_Handler(); + } + if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK) + { + //Error_Handler(); + } + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK) + { + //Error_Handler(); + } + HAL_TIM_MspPostInit(&htim15); + +} + +static void MX_TIM16_Init(void) +{ + __HAL_RCC_TIM16_CLK_ENABLE(); + TIM_OC_InitTypeDef sConfigOC = {0}; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; + htim16.Instance = TIM16; + htim16.Init.Prescaler = 0; + htim16.Init.CounterMode = TIM_COUNTERMODE_UP; + htim16.Init.Period = PWM_PERIOD; + htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim16.Init.RepetitionCounter = 0; + htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim16) != HAL_OK) + { + //Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim16) != HAL_OK) + { + //Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + if (HAL_TIM_PWM_ConfigChannel(&htim16, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + //Error_Handler(); + } + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if (HAL_TIMEx_ConfigBreakDeadTime(&htim16, &sBreakDeadTimeConfig) != HAL_OK) + { + //Error_Handler(); + } + HAL_TIM_MspPostInit(&htim16); + +} + +static void MX_TIM17_Init(void) +{ + __HAL_RCC_TIM17_CLK_ENABLE(); + TIM_OC_InitTypeDef sConfigOC = {0}; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; + htim17.Instance = TIM17; + htim17.Init.Prescaler = 0; + htim17.Init.CounterMode = TIM_COUNTERMODE_UP; + htim17.Init.Period = PWM_PERIOD; + htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim17.Init.RepetitionCounter = 0; + htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim17) != HAL_OK) + { + //Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim17) != HAL_OK) + { + //Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 0; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + if (HAL_TIM_PWM_ConfigChannel(&htim17, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + { + //Error_Handler(); + } + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + if (HAL_TIMEx_ConfigBreakDeadTime(&htim17, &sBreakDeadTimeConfig) != HAL_OK) + { + //Error_Handler(); + } + HAL_TIM_MspPostInit(&htim17); + } void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) { - /* GPIO Ports Clock Enable */ + /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOE_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); @@ -94,18 +307,9 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM12; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM12_MspPostInit 1 */ - - /* USER CODE END TIM12_MspPostInit 1 */ } else if(htim->Instance==TIM13) { - /* USER CODE BEGIN TIM13_MspPostInit 0 */ - - /* USER CODE END TIM13_MspPostInit 0 */ - - __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM13 GPIO Configuration PA6 ------> TIM13_CH1 */ @@ -115,18 +319,9 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM13; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM13_MspPostInit 1 */ - - /* USER CODE END TIM13_MspPostInit 1 */ } else if(htim->Instance==TIM14) { - /* USER CODE BEGIN TIM14_MspPostInit 0 */ - - /* USER CODE END TIM14_MspPostInit 0 */ - - __HAL_RCC_GPIOF_CLK_ENABLE(); /**TIM14 GPIO Configuration PF9 ------> TIM14_CH1 */ @@ -136,18 +331,9 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF9_TIM14; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM14_MspPostInit 1 */ - - /* USER CODE END TIM14_MspPostInit 1 */ } else if(htim->Instance==TIM15) { - /* USER CODE BEGIN TIM15_MspPostInit 0 */ - - /* USER CODE END TIM15_MspPostInit 0 */ - - __HAL_RCC_GPIOE_CLK_ENABLE(); /**TIM15 GPIO Configuration PE5 ------> TIM15_CH1 PE6 ------> TIM15_CH2 @@ -158,18 +344,9 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_TIM15; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM15_MspPostInit 1 */ - - /* USER CODE END TIM15_MspPostInit 1 */ } else if(htim->Instance==TIM16) { - /* USER CODE BEGIN TIM16_MspPostInit 0 */ - - /* USER CODE END TIM16_MspPostInit 0 */ - - __HAL_RCC_GPIOF_CLK_ENABLE(); /**TIM16 GPIO Configuration PF6 ------> TIM16_CH1 */ @@ -179,18 +356,9 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM16; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM16_MspPostInit 1 */ - - /* USER CODE END TIM16_MspPostInit 1 */ } else if(htim->Instance==TIM17) { - /* USER CODE BEGIN TIM17_MspPostInit 0 */ - - /* USER CODE END TIM17_MspPostInit 0 */ - - __HAL_RCC_GPIOF_CLK_ENABLE(); /**TIM17 GPIO Configuration PF7 ------> TIM17_CH1 */ @@ -200,10 +368,6 @@ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM17; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct); - - /* USER CODE BEGIN TIM17_MspPostInit 1 */ - - /* USER CODE END TIM17_MspPostInit 1 */ } }