Test internal ADC
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
* @brief This is the list of modules to be used in the HAL driver
|
||||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
/*#define HAL_ADC_MODULE_ENABLED */
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||
/*#define HAL_CAN_MODULE_ENABLED */
|
||||
/*#define HAL_CEC_MODULE_ENABLED */
|
||||
|
||||
@@ -52,6 +52,7 @@ void SVC_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
void TIM14_IRQHandler(void);
|
||||
void TIM16_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
123
Core/Src/main.c
123
Core/Src/main.c
@@ -41,12 +41,15 @@
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
ADC_HandleTypeDef hadc;
|
||||
|
||||
SPI_HandleTypeDef hspi1;
|
||||
|
||||
TIM_HandleTypeDef htim14;
|
||||
TIM_HandleTypeDef htim16;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
DS_MAX7219 Display;
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
@@ -54,6 +57,8 @@ void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_SPI1_Init(void);
|
||||
static void MX_TIM14_Init(void);
|
||||
static void MX_ADC_Init(void);
|
||||
static void MX_TIM16_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
@@ -94,12 +99,14 @@ int main(void)
|
||||
MX_GPIO_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_TIM14_Init();
|
||||
MX_ADC_Init();
|
||||
MX_TIM16_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
HAL_TIM_Base_Start_IT(&htim14);
|
||||
//htim14.Instance->ARR = 49;
|
||||
|
||||
DS_Button ButtonUp, ButtonDown;
|
||||
DS_MAX7219 Display;
|
||||
|
||||
|
||||
uint16_t Frequensy = 1;
|
||||
|
||||
@@ -107,13 +114,25 @@ int main(void)
|
||||
DS_ButtonInit(&ButtonDown, Button2_GPIO_Port, Button2_Pin);
|
||||
|
||||
DS_MAX7219_Init(&Display, &hspi1, CS_GPIO_Port, CS_Pin);
|
||||
|
||||
HAL_TIM_Base_Start_IT(&htim16);
|
||||
uint16_t ADCValue;
|
||||
uint64_t TotalDisplay = 0;
|
||||
HAL_ADCEx_Calibration_Start(&hadc);
|
||||
HAL_ADC_Start(&hadc);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
|
||||
HAL_ADC_PollForConversion(&hadc, 100);
|
||||
ADCValue = HAL_ADC_GetValue(&hadc);
|
||||
HAL_ADC_Stop(&hadc);
|
||||
HAL_ADC_Start(&hadc);
|
||||
|
||||
TotalDisplay = 3200 * ADCValue / 4095;
|
||||
|
||||
DS_ButtonUpdate(&ButtonUp);
|
||||
DS_ButtonUpdate(&ButtonDown);
|
||||
|
||||
@@ -126,8 +145,7 @@ int main(void)
|
||||
else
|
||||
Frequensy +=10;
|
||||
|
||||
DS_MAX7219_Print(&Display, Frequensy);
|
||||
DS_MAX7219_UpdataDisplay(&Display);
|
||||
|
||||
|
||||
htim14.Instance->ARR = 1000/Frequensy/2-1;
|
||||
}
|
||||
@@ -139,11 +157,12 @@ int main(void)
|
||||
else
|
||||
Frequensy -=10;
|
||||
|
||||
DS_MAX7219_Print(&Display, Frequensy);
|
||||
DS_MAX7219_UpdataDisplay(&Display);
|
||||
htim14.Instance->ARR = 1000/Frequensy/2-1;
|
||||
}
|
||||
|
||||
TotalDisplay = TotalDisplay * 1000 + Frequensy;
|
||||
DS_MAX7219_Print(&Display, TotalDisplay);
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
@@ -163,9 +182,11 @@ void SystemClock_Config(void)
|
||||
/** Initializes the RCC Oscillators according to the specified parameters
|
||||
* in the RCC_OscInitTypeDef structure.
|
||||
*/
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
|
||||
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
||||
RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
|
||||
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
||||
RCC_OscInitStruct.HSI14CalibrationValue = 16;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||||
{
|
||||
@@ -186,6 +207,60 @@ void SystemClock_Config(void)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_ADC_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN ADC_Init 0 */
|
||||
|
||||
/* USER CODE END ADC_Init 0 */
|
||||
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN ADC_Init 1 */
|
||||
|
||||
/* USER CODE END ADC_Init 1 */
|
||||
|
||||
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
||||
*/
|
||||
hadc.Instance = ADC1;
|
||||
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
|
||||
hadc.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
hadc.Init.LowPowerAutoWait = DISABLE;
|
||||
hadc.Init.LowPowerAutoPowerOff = DISABLE;
|
||||
hadc.Init.ContinuousConvMode = DISABLE;
|
||||
hadc.Init.DiscontinuousConvMode = DISABLE;
|
||||
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
hadc.Init.DMAContinuousRequests = DISABLE;
|
||||
hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
||||
if (HAL_ADC_Init(&hadc) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
/** Configure for the selected ADC regular channel to be converted.
|
||||
*/
|
||||
sConfig.Channel = ADC_CHANNEL_1;
|
||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
|
||||
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN ADC_Init 2 */
|
||||
|
||||
/* USER CODE END ADC_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI1 Initialization Function
|
||||
* @param None
|
||||
@@ -257,6 +332,38 @@ static void MX_TIM14_Init(void)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM16 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM16_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM16_Init 0 */
|
||||
|
||||
/* USER CODE END TIM16_Init 0 */
|
||||
|
||||
/* USER CODE BEGIN TIM16_Init 1 */
|
||||
|
||||
/* USER CODE END TIM16_Init 1 */
|
||||
htim16.Instance = TIM16;
|
||||
htim16.Init.Prescaler = 8000;
|
||||
htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim16.Init.Period = 99;
|
||||
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();
|
||||
}
|
||||
/* USER CODE BEGIN TIM16_Init 2 */
|
||||
|
||||
/* USER CODE END TIM16_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
|
||||
@@ -77,6 +77,67 @@ void HAL_MspInit(void)
|
||||
/* USER CODE END MspInit 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hadc: ADC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(hadc->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**ADC GPIO Configuration
|
||||
PA1 ------> ADC_IN1
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ADC MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param hadc: ADC handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
|
||||
{
|
||||
if(hadc->Instance==ADC1)
|
||||
{
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_ADC1_CLK_DISABLE();
|
||||
|
||||
/**ADC GPIO Configuration
|
||||
PA1 ------> ADC_IN1
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);
|
||||
|
||||
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END ADC1_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SPI MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
@@ -164,6 +225,20 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
/* USER CODE END TIM14_MspInit 1 */
|
||||
}
|
||||
else if(htim_base->Instance==TIM16)
|
||||
{
|
||||
/* USER CODE BEGIN TIM16_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM16_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_TIM16_CLK_ENABLE();
|
||||
/* TIM16 interrupt Init */
|
||||
HAL_NVIC_SetPriority(TIM16_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(TIM16_IRQn);
|
||||
/* USER CODE BEGIN TIM16_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM16_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -189,6 +264,20 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
/* USER CODE END TIM14_MspDeInit 1 */
|
||||
}
|
||||
else if(htim_base->Instance==TIM16)
|
||||
{
|
||||
/* USER CODE BEGIN TIM16_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM16_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM16_CLK_DISABLE();
|
||||
|
||||
/* TIM16 interrupt DeInit */
|
||||
HAL_NVIC_DisableIRQ(TIM16_IRQn);
|
||||
/* USER CODE BEGIN TIM16_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM16_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -22,11 +22,12 @@
|
||||
#include "stm32f0xx_it.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "../../Library/DS_MAX7219/Inc/DS_MAX7219.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN TD */
|
||||
|
||||
extern DS_MAX7219 Display;
|
||||
/* USER CODE END TD */
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
@@ -56,6 +57,7 @@
|
||||
|
||||
/* External variables --------------------------------------------------------*/
|
||||
extern TIM_HandleTypeDef htim14;
|
||||
extern TIM_HandleTypeDef htim16;
|
||||
/* USER CODE BEGIN EV */
|
||||
|
||||
/* USER CODE END EV */
|
||||
@@ -154,6 +156,20 @@ void TIM14_IRQHandler(void)
|
||||
/* USER CODE END TIM14_IRQn 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function handles TIM16 global interrupt.
|
||||
*/
|
||||
void TIM16_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN TIM16_IRQn 0 */
|
||||
DS_MAX7219_UpdataDisplay(&Display);
|
||||
/* USER CODE END TIM16_IRQn 0 */
|
||||
HAL_TIM_IRQHandler(&htim16);
|
||||
/* USER CODE BEGIN TIM16_IRQn 1 */
|
||||
|
||||
/* USER CODE END TIM16_IRQn 1 */
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
Reference in New Issue
Block a user