#include "main.h" // Inclure le fichier d'en-tête principal
// Définir les broches pour les sorties
#define LED_PIN_1 GPIO_PIN_5 // Broche pour la première LED (ou autre périphérique)
#define LED_PIN_2 GPIO_PIN_6 // Broche pour la deuxième LED (ou autre périphérique)
// Déclaration de la structure pour l'ADC
ADC_HandleTypeDef hadc1; // Instance de l'ADC
// Prototypes de fonctions
void SystemClock_Config(void); // Fonction pour configurer l'horloge système
static void MX_GPIO_Init(void); // Fonction pour initialiser les GPIO
static void MX_ADC1_Init(void); // Fonction pour initialiser l'ADC
uint16_t adcValue; // Variable pour stocker la valeur lue de l'ADC
float temperature; // Variable pour stocker la température calculée
int main(void)
{
// Initialiser la bibliothèque HAL
HAL_Init();
// Configurer l'horloge système
SystemClock_Config();
// Initialiser les GPIO
MX_GPIO_Init();
// Initialiser l'ADC
MX_ADC1_Init();
// Boucle principale
while (1)
{
HAL_ADC_Start(&hadc1); // Démarrer la conversion ADC
HAL_ADC_PollForConversion(&hadc1, 100); // Attendre la fin de la conversion, avec un timeout de 100 ms
// Lire la valeur convertie de l'ADC
adcValue = HAL_ADC_GetValue(&hadc1);
// Convertir la valeur ADC en température (en Celsius)
// Supposons que la référence de tension est 3.3V et que le LM35 fournit 10mV/°C
temperature = (adcValue * 3.3 / 1024); // Conversion
// Arrêter la conversion ADC
HAL_ADC_Stop(&hadc1);
// Logique de contrôle basée sur la température
if (temperature < 110.0f)
{
HAL_GPIO_WritePin(GPIOA, LED_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, LED_PIN_2, GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOA, LED_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, LED_PIN_1, GPIO_PIN_RESET);
}
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = LED_PIN_1 | LED_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV4;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_10B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_0; // Assurez-vous que c'est le bon canal pour le capteur de température
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
// L'utilisateur peut ajouter du code pour indiquer une erreur, par exemple, allumer une LED ou une sortie
}
/* USER CODE END Error_Handler_Debug */
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6