标题:
基于stm32单片机的温控风扇代码
[打印本页]
作者:
3039233296
时间:
2025-7-4 08:48
标题:
基于stm32单片机的温控风扇代码
按键1 用于切换模式
按键2 用于模式2调速
模式1 为温控模式
单片机源程序如下:
#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "Delay.h"
#include "Key.h"
#include "dht11.h"
#include "motor.h"
#include "pwm.h"
#include "encoder.h"
#include "Timer.h"
uint8_t Key_num,Key_down,Key_up,Key_old;//模式控制按键 PB1数为1,PB11数为2
uint8_t jishu = 0;//按键控制电机
u8 temp;//温度
u8 humi;//湿度
u8 mode = 0;//模式 0-关机 1-温控风扇 2-按键控制
u8 check = 0;
u8 i;
u16 t;
u16 Speed;
//void TIM2_IRQHandler(void)
//{
// if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
// {
// Speed = Encoder3_Get();
// TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// }
//}
//电机A4高A5低
int main(void)
{
// PWM2_Init();
Key_Init();//按键初始化
OLED_Init();//显示屏初始化
Motor_Init();//电机初始化
Encoder_Init();
Encoder4_Init();
// Timer_Init();
OLED_ShowString(1,1,"T:");
OLED_ShowString(2,1,"S:");
OLED_ShowString(3,1,"mode:");
OLED_ShowString(4,1,"Gear:");
while(DHT11_Init())//初始化DHT11的IO口 DQ 同时检测DHT11的存在
{
// Delay_ms(200);
OLED_ShowString(3,5,"error");
}
while(1)
{
Key_num = Key_GetNum();
// Key_down = Key_num & (Key_old ^ Key_num);//下降沿输出
// Key_up = Key_num & (Key_old ^ Key_num);//上升沿输出
// Key_old = Key_num;//长按
//
check = DHT11_Read_Data(&temp,&humi);
if(check == 1)
{
OLED_ShowString(2,5,"error");
} //温度范围检查
// t = Encoder3_Get();
Delay_ms(500);
OLED_ShowNum(1,3,temp,2);//温度值显示
OLED_ShowNum(2,3,humi,2);//湿度值显示
OLED_ShowNum(3,6,mode,1);//模式显示
OLED_ShowNum(1,6,Encoder3_Get(),5);
OLED_ShowNum(2,6,Encoder4_Get(),5);
if(Key_num == 1)
{
if(++mode == 3) mode = 0; //模式切换
}
// if(mode == 1)
// Motor_SetSpeed(humi);//温控
// else if(mode == 2)
// {
// Motor_SetSpeed(jishu*10);
//
// if(Key_num == 2)
// {
// if(++jishu == 11) jishu = 1;
// }//按键控制
//// OLED_ShowNum(4,6,jishu,2);//模式二档位
// }
switch(mode)
{
case 0:
jishu = 0;
Motor_SetSpeed(0);
OLED_ShowNum(4,6,jishu,2);
// PWM_SetCompare1(0);
for (i = 0; i <= Encoder3_Get()/10; i++)
{
PWM_SetCompare1(i); //依次将定时器的CCR寄存器设置为0~100,PWM占空比逐渐增大,LED逐渐变亮
Delay_ms(10);
}
for (i = 0; i <= Encoder3_Get()/10; i++)
{
PWM_SetCompare1(100 - i); //依次将定时器的CCR寄存器设置为100~0,PWM占空比逐渐减小,LED逐渐变暗
Delay_ms(10);
}
break; //模式0——关机
case 1:
if(humi<25) Motor_SetSpeed(0);
else if(humi>=25 && humi<100)
{
Motor_SetSpeed(humi);
PWM_SetCompare1(humi);
for (i = 0; i <= (100-humi); i++)
{
PWM_SetCompare1(i); //依次将定时器的CCR寄存器设置为0~100,PWM占空比逐渐增大,LED逐渐变亮
Delay_ms(10); //延时10ms
}
for (i = 0; i <= (100-humi); i++)
{
PWM_SetCompare1(100 - i); //依次将定时器的CCR寄存器设置为100~0,PWM占空比逐渐减小,LED逐渐变暗
Delay_ms(10); //延时10ms
}
}
else OLED_ShowString(2,5,"error");
break;//模式1——温控
case 2:
if(Key_num == 2)
{
if(++jishu == 11) jishu = 0;
}
OLED_ShowNum(4,6,jishu,2);
Motor_SetSpeed(jishu*10);
for (i = 0; i <= (100-jishu*10); i++)
{
PWM_SetCompare1(i); //依次将定时器的CCR寄存器设置为0~100,PWM占空比逐渐增大,LED逐渐变亮
Delay_ms(10); //延时10ms
}
for (i = 0; i <= (100-jishu*10); i++)
{
PWM_SetCompare1(100 - i); //依次将定时器的CCR寄存器设置为100~0,PWM占空比逐渐减小,LED逐渐变暗
Delay_ms(10); //延时10ms
}
break;//模式2——
}
}
}
复制代码
原理图: 无
仿真: 无
代码:
按键完全实现.7z
(187.04 KB, 下载次数: 0)
2025-7-8 18:34 上传
点击文件名下载附件
基于stm32de温控风扇
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1