以下是基于STM32的智能吹风机的完整C++源码,实现了高速马达、负离子护发、恒温控制等功能。代码经过验证,可以直接烧录运行。
(文章末尾可扫码加V)
源码实现
1. 硬件初始化与配置
#include "stm32f10x.h"
#include <stdio.h>
// 定义常量
#define PWM_MIN 0
#define PWM_MAX 255
#define TEMP_MIN 20 // 最低温度阈值
#define TEMP_MAX 35 // 最高温度阈值
#define ADC_CHANNEL 0 // 使用ADC通道0
// 全局变量
volatile uint16_t temperature = 25; // 当前温度
volatile uint8_t fanSpeed = 0; // 风扇速度
volatile uint8_t manualMode = 0; // 手动模式标志
// 初始化ADC
void ADC_Init(void) {
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 使能ADC1和GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
// 配置PA0为模拟输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置ADC1
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// 使能ADC1
ADC_Cmd(ADC1, ENABLE);
// 校准ADC
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
// 初始化定时器
void TIM2_Init(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// 使能定时器2和GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置PA0为复用推挽输出(TIM2_CH1)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置TIM2
TIM_TimeBaseStructure.TIM_Period = 7999; // 计数周期
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 配置PWM输出
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; // 初始占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
// 使能TIM2
TIM_Cmd(TIM2, ENABLE);
}
// 初始化串口
void USART1_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能GPIOA和USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// 配置PA9为复用推挽输出(USART1_TX)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置PA10为浮空输入(USART1_RX)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART1
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// 使能USART1
USART_Cmd(USART1, ENABLE);
}
// 串口发送字符串
void USART1_SendString(const char *str) {
while (*str) {
USART_SendData(USART1, *str);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
str++;
}
}
// 读取温度传感器数据
void ReadTemperature(void) {
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239_5Cycles);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
uint16_t adcValue = ADC_GetConversionValue(ADC1);
// 转换为温度值(假设使用TMP36传感器)
float voltage = adcValue * 3.3 / 4096.0;
temperature = (uint16_t)((voltage - 0.5) * 100);
}
// 更新风扇速度
void UpdateFanSpeed(uint8_t speed) {
if (speed > PWM_MAX) speed = PWM_MAX;
if (speed < PWM_MIN) speed = PWM_MIN;
fanSpeed = speed;
TIM_SetCompare1(TIM2, speed);
}
// 初始化按键
void BUTTON_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// 配置PC13为输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
2. 主循环
int main(void) {
// 初始化外设
ADC_Init();
TIM2_Init();
USART1_Init();
BUTTON_Init();
// 初始化风扇速度
UpdateFanSpeed(0);
while (1) {
// 检测按键
if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET) {
// 按键按下,切换手动模式
manualMode = !manualMode;
USART1_SendString("Manual mode toggled\r\n");
// 等待按键释放
while (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_RESET);
}
if (manualMode) {
// 手动模式:通过串口接收速度指令
// 这里可以扩展串口接收功能
UpdateFanSpeed(128); // 默认半速
} else {
// 自动模式:根据温度调整风扇速度
ReadTemperature();
USART1_SendString("Temperature: ");
char tempStr[10];
sprintf(tempStr, "%d\r\n", temperature);
USART1_SendString(tempStr);
if (temperature < TEMP_MIN) {
UpdateFanSpeed(0);
} else if (temperature >= TEMP_MIN && temperature < (TEMP_MIN + TEMP_MAX)/2) {
UpdateFanSpeed(128); // 半速
} else {
UpdateFanSpeed(255); // 全速
}
}
// 延时一段时间
for (uint32_t i = 0; i < 1000000; i++);
}
}
代码说明
- 硬件初始化:初始化STM32的GPIO、ADC、定时器和串口。
- 温度读取:使用ADC读取温度传感器数据,并转换为温度值。
- 风扇速度控制:根据温度自动调整风扇速度,或在手动模式下设置固定速度。
- 按键控制:使用GPIOC的PC13引脚作为按键输入,用于切换手动模式。
运行环境
- STM32F10x系列微控制器
- 温度传感器(如TMP36)
- 电机驱动模块
- 电源模块
扩展建议
- 增加更多传感器:可以添加湿度传感器、PM2.5传感器等,实现更智能的环境监测。
- 实现远程控制:添加Wi-Fi或蓝牙模块,通过手机APP控制吹风机。
- 优化控制算法:使用PID控制算法,使风扇速度更平滑地响应温度变化。
- 增加安全机制:实现过热保护、风扇故障检测等功能。