学习STM32的水位传感器
·
以下是一个使用STM32库函数编写的水位传感器的代码案例:
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
// 定义GPIO引脚
#define TRIG_PIN GPIO_Pin_0
#define ECHO_PIN GPIO_Pin_1
#define GPIO_PORT GPIOA
// 定义水位传感器的工作状态
typedef enum {
IDLE, // 空闲状态
MEASURING, // 测量中
COMPLETED // 测量完成
} SensorState;
// 定义全局变量
volatile SensorState state = IDLE;
volatile uint32_t start_time = 0;
volatile uint32_t stop_time = 0;
volatile uint32_t distance = 0;
// 函数声明
void GPIO_Configuration(void);
void TIM_Configuration(void);
void EXTI_Configuration(void);
void delay_us(uint32_t us);
void delay_ms(uint32_t ms);
int main(void)
{
// 初始化GPIO、定时器和外部中断
GPIO_Configuration();
TIM_Configuration();
EXTI_Configuration();
while (1)
{
if (state == IDLE)
{
// 发送触发信号
GPIO_SetBits(GPIO_PORT, TRIG_PIN);
delay_us(10);
GPIO_ResetBits(GPIO_PORT, TRIG_PIN);
state = MEASURING;
}
else if (state == COMPLETED)
{
// 计算距离并输出
uint32_t pulse_width = stop_time - start_time;
distance = pulse_width * 1.7 / 100; // 声速为340m/s,计算距离需要除以2,再除以10000得到厘米单位
printf("Distance: %d cm\n", distance);
state = IDLE;
}
}
}
// 配置GPIO
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// 配置TRIG引脚为输出
GPIO_InitStructure.GPIO_Pin = TRIG_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_PORT, &GPIO_InitStructure);
// 配置ECHO引脚为输入
GPIO_InitStructure.GPIO_Pin = ECHO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_PORT, &GPIO_InitStructure);
}
// 配置定时器
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// 配置定时器时钟
TIM_BaseStruct.TIM_Prescaler = (uint16_t)((SystemCoreClock / 1000000) - 1); // 设置定时器时钟为1MHz
TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseStruct.TIM_Period = 0xFFFF;
TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_BaseStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_BaseStruct);
// 配置中断
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 配置输入捕获通道
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
// 配置外部中断
void EXTI_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// 配置ECHO引脚为中断输入
GPIO_InitStructure.GPIO_Pin = ECHO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIO_PORT, &GPIO_InitStructure);
// 配置外部中断线路
EXTI_InitStruct.EXTI_Line = EXTI_Line1;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
// 配置外部中断优先级
NVIC_InitStruct.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
// 延时指定时间(微秒)
void delay_us(uint32_t us)
{
uint32_t ticks = us * (SystemCoreClock / 1000000);
while (ticks--)
{
__NOP();
}
}
// 延时指定时间(毫秒)
void delay_ms(uint32_t ms)
{
uint32_t ticks = ms * (SystemCoreClock / 1000);
while (ticks--)
{
__NOP();
}
}
// 定时器中断处理函数
void TIM2_IRQHandler()
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
{
if (state == MEASURING)
{
if (GPIO_ReadInputDataBit(GPIO_PORT, ECHO_PIN))
{
start_time = TIM_GetCapture2(TIM2);
}
else
{
stop_time = TIM_GetCapture2(TIM2);
state = COMPLETED;
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}
}
// 外部中断处理函数
void EXTI1_IRQHandler()
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
if (state == MEASURING)
{
if (GPIO_ReadInputDataBit(GPIO_PORT, ECHO_PIN))
{
start_time = TIM_GetCounter(TIM2);
}
else
{
stop_time = TIM_GetCounter(TIM2);
state = COMPLETED;
}
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
代码中使用了定时器和外部中断来计算脉冲宽度,进而计算出距离。在主循环中,程序首先将传感器设置为测量状态,然后发送一个触发信号用于启动测量。当测量完成时,计算脉冲宽度并将距离输出。
主要的函数包括:
GPIO_Configuration():配置GPIO引脚为输入输出模式。TIM_Configuration():配置定时器用于测量脉冲宽度。EXTI_Configuration():配置外部中断用于检测脉冲边沿。delay_us()和delay_ms():提供微秒级和毫秒级延时函数。TIM2_IRQHandler()和EXTI1_IRQHandler():处理定时器和外部中断的中断事件。
该代码案例是一个基础的水位传感器实现,可以通过修改相关参数和接口来适应不同的传感器和硬件配置。
更多推荐


所有评论(0)