学习STM32的光敏传感器
光敏传感器,也被称为光敏电阻器或光敏电子元件,是一种可以感知光线强度的传感器。在STM32微控制器上,我们可以利用ADC(模数转换器)来读取光敏传感器的模拟输出值,并通过相应的程序进行处理和控制。
本文将通过代码案例来介绍如何在STM32上使用光敏传感器,并进行光线强度的测量和控制。代码案例将基于STM32CubeIDE开发环境和HAL库进行编写。
- 硬件准备 在开始编写代码之前,我们需要准备以下硬件材料:
-
硬件连接 将光敏传感器的一个引脚连接到STM32开发板的一个ADC输入引脚,例如PA0。
-
创建新项目 在STM32CubeIDE中创建一个新的STM32项目,并根据具体的开发板选择正确的目标芯片和时钟设置。
-
配置ADC 在STM32CubeIDE的“配置”选项卡中,选择“ADC1”,并进行如下配置:
- 编写初始化代码 打开生成的主函数文件(通常是main.c),在
main()
函数之前编写以下初始化代码:
/* ADC handle structure */
ADC_HandleTypeDef hadc1;
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
int main(void)
{
/* MCU Configuration */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
/* Infinite loop */
while (1)
{
/* Read ADC value */
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
/* Perform processing and control based on ADC value */
// TODO: Add your code here
}
}
- 编写ADC初始化代码 在生成的主函数文件中的
MX_ADC1_Init()
函数中添加以下代码来初始化ADC1:
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
/* Configure the global features of the ADC */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* Configure the ADC channel */
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
- 编写光敏传感器代码 在无限循环的
while (1)
循环中,我们可以读取光敏传感器的模拟输出值,然后根据需要进行进一步处理和控制。
/* Read ADC value */
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
/* Perform processing and control based on ADC value */
if (adc_value > 2048)
{
// Light intensity is high
// TODO: Add your code here
}
else
{
// Light intensity is low
// TODO: Add your code here
}
- 添加延时 为了避免频繁读取光敏传感器的值,我们可以在每次读取后添加适当的延时,例如100ms。在
while (1)
循环中添加以下代码:
/* Delay for 100ms */
HAL_Delay(100);
- 完整代码示例 下面是一个完整的代码示例,包括初始化和光敏传感器控制:
/* ADC handle structure */
ADC_HandleTypeDef hadc1;
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
int main(void)
{
/* MCU Configuration */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
/* Infinite loop */
while (1)
{
/* Read ADC value */
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
/* Perform processing and control based on ADC value */
if (adc_value > 2048)
{
// Light intensity is high
// TODO: Add your code here
}
else
{
// Light intensity is low
// TODO: Add your code here
}
/* Delay for 100ms */
HAL_Delay(100);
}
}
/* ADC1 init function */
static void MX_ADC1_Init(void)
{
/* Configure the global features of the ADC */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* Configure the ADC channel */
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure. */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 400;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the
作者:xiaoalla