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:
- 6:417655779dc5
- Parent:
- 5:d2dffc88e94d
- Child:
- 7:e3a2ade56b79
diff -r d2dffc88e94d -r 417655779dc5 HardwareSetup.cpp --- a/HardwareSetup.cpp Wed Aug 26 14:37:16 2020 +0000 +++ b/HardwareSetup.cpp Wed Aug 26 19:35:02 2020 +0000 @@ -8,6 +8,8 @@ TIM_HandleTypeDef htim14; TIM_HandleTypeDef htim16; TIM_HandleTypeDef htim17; +ADC_HandleTypeDef hadc1; +ADC_HandleTypeDef hadc2; int PWM_PERIOD; void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim); @@ -17,6 +19,18 @@ static void MX_TIM15_Init(void); static void MX_TIM16_Init(void); static void MX_TIM17_Init(void); +static void MX_ADC1_Init(void); +static void MX_ADC2_Init(void); + +uint16_t Current_A; +uint16_t Current_B; +uint16_t Current_C; +uint16_t Current_D; + +AnalogIn currentA(PF_12); //Enable ADC pin +AnalogIn currentB(PF_11); //Enable ADC pin +AnalogIn currentC(PF_13); //Enable ADC pin +AnalogIn currentD(PA_4); //Enable ADC pin void initHardware(int periodTicks){ PWM_PERIOD = periodTicks; @@ -30,6 +44,8 @@ MX_TIM15_Init(); MX_TIM16_Init(); MX_TIM17_Init(); + MX_ADC1_Init(); + MX_ADC2_Init(); //HAL_TIM_Base_Start(&htim15); HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation @@ -40,10 +56,165 @@ 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 - } + + +uint16_t readADC1(int channel){ + + HAL_ADC_Start(&hadc1); + HAL_ADC_PollForConversion(&hadc1,20); + Current_A = HAL_ADC_GetValue(&hadc1); + HAL_ADC_PollForConversion(&hadc1,20); + Current_B = HAL_ADC_GetValue(&hadc1); + HAL_ADC_Stop (&hadc1); + if (channel){ + + return Current_A; + } + else + return Current_B; +} + +uint16_t readADC2(int channel){ + + HAL_ADC_Start(&hadc2); + HAL_ADC_PollForConversion(&hadc2,20); + Current_C = HAL_ADC_GetValue(&hadc2); + HAL_ADC_PollForConversion(&hadc2,20); + Current_D = HAL_ADC_GetValue(&hadc2); + HAL_ADC_Stop (&hadc2); + if (channel){ + + return Current_C; + } + else + return Current_D; +} + + +static void MX_ADC1_Init(void) +{ + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + /** Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc1.Init.Resolution = ADC_RESOLUTION_16B; + hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = ENABLE; + hadc1.Init.NbrOfConversion = 2; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; + hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; + hadc1.Init.OversamplingMode = DISABLE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) + { + //Error_Handler(); + } + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) + { + //Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_2; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + //Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_6; + sConfig.Rank = ADC_REGULAR_RANK_2; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + //Error_Handler(); + } +} + +/** + * @brief ADC2 Initialization Function + * @param None + * @retval None + */ +static void MX_ADC2_Init(void) +{ + ADC_MultiModeTypeDef multimode = {0}; + ADC_ChannelConfTypeDef sConfig = {0}; + /** Common config + */ + hadc2.Instance = ADC2; + hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc2.Init.Resolution = ADC_RESOLUTION_16B; + hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc2.Init.LowPowerAutoWait = DISABLE; + hadc2.Init.ContinuousConvMode = ENABLE; + hadc2.Init.NbrOfConversion = 2; + hadc2.Init.DiscontinuousConvMode = DISABLE; + hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; + hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; + hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; + hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED; + hadc2.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; + hadc2.Init.OversamplingMode = DISABLE; + if (HAL_ADC_Init(&hadc2) != HAL_OK) + { + //Error_Handler(); + } + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc2, &multimode) != HAL_OK) + { + //Error_Handler(); + } + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_2; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) + { + //Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_18; + sConfig.Rank = ADC_REGULAR_RANK_2; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) + { + //Error_Handler(); + } + +} + + static void MX_TIM12_Init(void) { __HAL_RCC_TIM12_CLK_ENABLE();