目录
一,前言
当今生活中,风扇已成为人们解暑的重要工具,然而使用风扇缓解夏日酷热的同时也存在着一些问题,传统风扇只能根据选择的档位来设置转速,而不能根据周围环境温度的变化而自动调节转速。基于以上设计了自动调速风扇系统。
二,系统方案选择
本系统由按键控制和液晶显示两部分组成。工作区内由单片机stm32F103C8热敏电阻传感器,当温度达到设定阈值时,从而通过PWM控制电机做相应动作。
环境温度采集模块:采用热敏电阻,可满足 40 摄氏度至 90 摄氏度测量范围
电机模块:采用舵机同直流电机搭配,舵机具有扭力大容易控制。小型直流减速电机,减速电机控制精度低,且速度均匀性好,控制简单,电源要求低,易于实现。
T86612FNG电机驱动 ,直流电机130
显示模块:OLED显示屏,快速插接小巧设计,是内容显示和程序调试的利器,从此程序清晰可见。
舵机:SG90
如图2为STM32F103单片机最小系统电路原理图。该最小系统电路主要包括时钟、按键复位和供电电路三部分。
三,实体展示
工程分类
四,相关代码
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init1(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //开启定时器2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1 PA2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
TIM_InternalClockConfig(TIM2);//TIM的时基单元由内部时钟控制
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //ARR 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC 预分频器的值 对72M(720000000)进行 7200分频 即10K的频率下 计10000个数 1s的时间
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器的值 CCR
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性选择
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出状态使能
TIM_OCInitStructure.TIM_Pulse = 20;//CCR,即占空比为 10%
TIM_OC2Init(TIM2,&TIM_OCInitStructure);//OC编号要与通道编号对应
TIM_Cmd(TIM2,ENABLE);
}
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //开启定时器3
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
TIM_InternalClockConfig(TIM3); //内部时钟配置
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC 预分频器的值
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器的值 CCR
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性选择
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出状态使能
TIM_OCInitStructure.TIM_Pulse = 50;//CCR,即占空比为 10%
TIM_OC3Init(TIM3,&TIM_OCInitStructure);//OC编号要与通道编号对应
//TIM_OC2Init(TIM3,&TIM_OCInitStructure);//OC编号要与通道编号对应
TIM_Cmd(TIM3,ENABLE);
}
void PWM_SetCompare_Servo(uint16_t Compare) //舵机
{
TIM_SetCompare2(TIM2, Compare);
}
void PWM_SetCompare_Motor(uint16_t Compare) //风扇
{
TIM_SetCompare3(TIM3, Compare);
}
PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_Init1(void);
void PWM_SetCompare_Motor(uint16_t Compare);
void PWM_SetCompare_Servo(uint16_t Compare);
#endif
AD.c
#include "stm32f10x.h" // Device header
void AD_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1) == SET);
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) == SET);
}
uint16_t AD_GetValue(void)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);
}
AD.h
#ifndef __AD_H
#define __AD_H
void AD_Init(void);
uint16_t AD_GetValue(void);
#endif
电机驱动程序
#include "stm32f10x.h" // Device header
#include "PWM.h"
#include "Delay2.h"
#include "OLED.h"
#include "LED1.h"
#include "Key.h"
float Speed=0;
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
PWM_Init1();
}
void Motor_SetSpeed(int8_t Speed)
{
if (Speed >= 0)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare_Motor(Speed);
}
else
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
PWM_SetCompare_Motor(-Speed);
}
}
void Motor_Speed_Set(uint8_t KeyNum)
{
//Key_Init1();
//Key_Init();
//KeyNum = Key_GetNum();
if(KeyNum == 1)
{
Speed += 20;
if(Speed > 80)
{
Speed =0;
}
}
void LED11_OFF(void);
void LED22_ON(void);
OLED_ShowNum(3,7,Speed,3);
OLED_ShowNum(2,8,KeyNum,1);
Motor_SetSpeed(Speed);
}
舵机驱动
#include "stm32f10x.h" // Device header
#include "PWM.h"
#include "OLED.h"
#include "Key.h"
#include "LED1.h"
float Angle;
void Servo_Init(void)
{
PWM_Init();
}
void Servo_SetAngle(float Angle)
{
PWM_SetCompare_Servo(Angle / 180 * 2000 + 500);
}
void Servo_Turn(uint8_t KeyNum)
{
KeyNum = Key_GetNum();
if (KeyNum == 2)
{
Angle += 30;
if (Angle > 180)
{
Angle = 0;
}
}
Servo_SetAngle(Angle);
OLED_ShowNum(2,8,KeyNum,1);
OLED_ShowNum(1, 7, Angle, 3);
}