Skip to content

Commit

Permalink
add adc current
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Mar 12, 2024
1 parent 3498456 commit 4add96b
Show file tree
Hide file tree
Showing 9 changed files with 370 additions and 34 deletions.
Binary file added Assets/adc1_mode_and_configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/stm32cubemx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void Error_Handler(void);
#define ADC_VIN_GPIO_Port GPIOA
#define ADC_5V_Pin GPIO_PIN_1
#define ADC_5V_GPIO_Port GPIOA
#define ADC_CURRENT_Pin GPIO_PIN_3
#define ADC_CURRENT_GPIO_Port GPIOA
#define INT_RGB_LED_BLUE_Pin GPIO_PIN_4
#define INT_RGB_LED_BLUE_GPIO_Port GPIOA
#define ADC_VERSION_Pin GPIO_PIN_1
Expand All @@ -77,6 +79,7 @@ void Error_Handler(void);
#define PWM2_TIM4_CH1_GPIO_Port GPIOB
#define PWM1_TIM4_CH2_Pin GPIO_PIN_7
#define PWM1_TIM4_CH2_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */
Expand Down
21 changes: 16 additions & 5 deletions Core/Src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void MX_ADC1_Init(void)
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 4;
hadc1.Init.NbrOfConversion = 5;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
Expand All @@ -76,7 +76,7 @@ void MX_ADC1_Init(void)

/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Expand All @@ -85,12 +85,21 @@ void MX_ADC1_Init(void)

/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Rank = ADC_REGULAR_RANK_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */

/* USER CODE END ADC1_Init 2 */
Expand All @@ -114,9 +123,10 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
/**ADC1 GPIO Configuration
PA0-WKUP ------> ADC1_IN0
PA1 ------> ADC1_IN1
PA3 ------> ADC1_IN3
PB1 ------> ADC1_IN9
*/
GPIO_InitStruct.Pin = ADC_VIN_Pin|ADC_5V_Pin;
GPIO_InitStruct.Pin = ADC_VIN_Pin|ADC_5V_Pin|ADC_CURRENT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

Expand Down Expand Up @@ -161,9 +171,10 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
/**ADC1 GPIO Configuration
PA0-WKUP ------> ADC1_IN0
PA1 ------> ADC1_IN1
PA3 ------> ADC1_IN3
PB1 ------> ADC1_IN9
*/
HAL_GPIO_DeInit(GPIOA, ADC_VIN_Pin|ADC_5V_Pin);
HAL_GPIO_DeInit(GPIOA, ADC_VIN_Pin|ADC_5V_Pin|ADC_CURRENT_Pin);

HAL_GPIO_DeInit(ADC_VERSION_GPIO_Port, ADC_VERSION_Pin);

Expand Down
1 change: 1 addition & 0 deletions Core/Src/stm32f1xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */
Expand Down
3 changes: 2 additions & 1 deletion Core/Src/stm32f1xx_hal_timebase_tim.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);

/* Compute TIM1 clock */
uwTimclock = HAL_RCC_GetPCLK2Freq();
uwTimclock = HAL_RCC_GetPCLK2Freq();

/* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
Expand All @@ -64,6 +64,7 @@ HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
htim1.Instance = TIM1;

/* Initialize TIMx peripheral as follow:
+ Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base.
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
+ ClockDivision = 0
Expand Down
Loading

0 comments on commit 4add96b

Please sign in to comment.