Created
October 11, 2023 22:00
-
-
Save ShawonAshraf/68354ff744eed5bb3ac3ec5726e105c1 to your computer and use it in GitHub Desktop.
STM32 Phase Change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stm32f4xx.h" | |
#include "stm32f4xx_hal.h" | |
ADC_HandleTypeDef hadc1; | |
void SystemClock_Config(void); | |
static void MX_GPIO_Init(void); | |
static void MX_ADC1_Init(void); | |
int main(void) { | |
HAL_Init(); | |
SystemClock_Config(); | |
MX_GPIO_Init(); | |
MX_ADC1_Init(); | |
HAL_ADC_Start(&hadc1); | |
uint32_t previousPhase = 0; | |
while (1) { | |
if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK) { | |
uint32_t phase = HAL_ADC_GetValue(&hadc1); | |
if (phase != previousPhase) { | |
// Phase has changed | |
// You can observe the phase change here, e.g., print it to a display or send it via UART | |
previousPhase = phase; | |
} | |
} | |
} | |
} | |
void SystemClock_Config(void) { | |
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; | |
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; | |
HAL_RCC_DeInit(); | |
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; | |
RCC_OscInitStruct.HSEState = RCC_HSE_ON; | |
RCC_OscInitStruct.HSIState = RCC_HSI_OFF; | |
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | |
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; | |
RCC_OscInitStruct.PLL.PLLM = 8; | |
RCC_OscInitStruct.PLL.PLLN = 336; | |
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; | |
RCC_OscInitStruct.PLL.PLLQ = 4; | |
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { | |
Error_Handler(); | |
} | |
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; | |
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | |
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | |
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; | |
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; | |
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { | |
Error_Handler(); | |
} | |
} | |
static void MX_ADC1_Init(void) { | |
ADC_ChannelConfTypeDef sConfig = {0}; | |
__HAL_RCC_ADC1_CLK_ENABLE(); | |
hadc1.Instance = ADC1; | |
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4; | |
hadc1.Init.Resolution = ADC_RESOLUTION_12B; | |
hadc1.Init.ScanConvMode = DISABLE; | |
hadc1.Init.ContinuousConvMode = ENABLE; | |
hadc1.Init.DiscontinuousConvMode = DISABLE; | |
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; | |
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; | |
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; | |
hadc1.Init.NbrOfConversion = 1; | |
if (HAL_ADC_Init(&hadc1) != HAL_OK) { | |
Error_Handler(); | |
} | |
sConfig.Channel = ADC_CHANNEL_0; // Replace with your specific ADC channel | |
sConfig.Rank = 1; | |
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; | |
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { | |
Error_Handler(); | |
} | |
} | |
static void MX_GPIO_Init(void) { | |
__HAL_RCC_GPIOA_CLK_ENABLE(); | |
} | |
void Error_Handler(void) { | |
while (1) { | |
// Error occurred, stay in this loop | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code initializes the ADC to continuously sample a specific channel (ADC_CHANNEL_0 in this example) and checks for changes in the phase value. When the phase changes, you can observe it as desired (e.g., print it to a display or send it via UART).
Make sure to adapt the code to your specific STM32 microcontroller model and ADC configuration as needed.