STM32PWM控制智能风扇

设计思路:

这个是一个STM32通过定时器产生PWM波控制小风扇的设计,首先STM32驱动ds18b20温度传感器采集环境温度,然后通过按键设置温度的阈值,不同的温度范围定时器产生的PWM波不同,相应的小风扇的转速也会不一样,温度越高,小风扇的转速越大。LCD1602显示屏显示当前温度,和设置的阈值,同时按键支持连按,也支持掉电数据保存。希望能够帮助到需要的朋友。需要参考的朋友可以在文章底部点击下载源码和原理图。

下面是main.c文件的代码

#include "stm32f10x.h"
#include "lcd.h"
#include 
#include "led.h"
#include "deputy.h"
#include "sys.h"
#include "delay.h"
#include "stmflash.h"
#include "ds18b20.h"
#include "LCD1602.h"

void KEY_Scan(void);
u8   KEY_scan(u8 mode);
void display_tem(void);
unsigned char Uart1_Buff[1024];   //串口1缓冲数组
unsigned char Uart1_Count=0;      //串口1累加变量
u8 right=0,Tim_ms=0,state=0,s0=0,sec1=0,sec2=0,memory_flag=0;
float result=0.0000;
float table = 0.70;
int pm25Value;
float High,Low;

u8 Tem_L[20]={0};
u8 Tem_H[20]={0};
u8 showBuffer[20] = {0};
uint8_t key1_flag=0,key2_flag=0,key3_flag=0;

int main(void)
{
	delay_init();	
	GPIO_init();
	LCD1602_Init();
	TIM2_Int_Init(350,7199);	//10Khz,计数到5000是500ms
	ds18b20_init_x();
	GPIO_ResetBits(GPIOB,GPIO_Pin_10);
	GPIO_SetBits(GPIOB,GPIO_Pin_11);
	LCD1602_ClearScreen();
//	Test_Write(0x08009100,(float)30.1);
//	Test_Write(0x08009104,(float)19.2);
	High=STMFLASH_ReadHalfWord(0x08009100)/16.;
	Low=STMFLASH_ReadHalfWord(0x08009104)/16.;		
	while(1)
	{	
		display_tem();
		if(memory_flag)
		{
			memory_flag=0;
			Test_Write(0x08009100,High*16.);//float类型扩大十倍,方便存储数据
			Test_Write(0x08009104,Low*16.);
		}
	}	
}

void display_tem(void)
{
	float tem;
	u16 z = 0;
	z	= ds18b20_read(); 	
	tem = z/16. ;
	LCD1602_Show_Str(0, 0, (unsigned char *)showBuffer);
	snprintf((char *)showBuffer, sizeof(showBuffer), "Cur_Tem:  %2.1f", tem);
	LCD1602_Write_Dat(0xDF);
	LCD1602_Write_Dat(0x43);
	 
	
	snprintf((char *)Tem_H, sizeof(Tem_H), "H:%2.1f   L:%2.1f", High,Low);
	LCD1602_Show_Str(0, 1, (unsigned char *)Tem_H);	
	LCD1602_Write_Dat(0xDF);
	LCD1602_Write_Dat(0x43);
	if(tem>High)
	{
		TIM3_PWM_Config(9999);
	}else if(tem>=Low&&tem0)&&(High>=Low)) 
					High+=0.1;
				else
					High=Low+0.1;
			}else if(state==2)
			{
				if((Low>0)&&(Low<=High))
				Low+=0.1;
				else 
					Low=High-0.1;
			}
		}
		if(sec1==0)
		{
			if(state==1)
			{
				 if((High>0)&&(High>=Low)) 
					High+=0.1;
				 else
					High=Low+0.1;
			}else if(state==2)
			{
				if((Low>0)&&(Low<=High))
				Low+=0.1;
				else 
					Low=High-0.1;
			}
		}
	}
	else
	{
		sec1=7;
		if(key2_flag==0)
		{
			key2_flag=1;
			memory_flag=1;			
		}			
	}
	if(KEY3==0)
	{
		if(key3_flag==1)
		{
			key3_flag=0;
			if(state==1)
			{
				if((High>0)&&(High>=Low)) 
					High-=0.1;
				else
					High=Low+0.1;
			}else if(state==2)
			{
				if((Low>0)&&(Low<=High))
				Low-=0.1;
				else 
					Low=High-0.1;
			}		
		}
		if(sec2==0)
		{
			if(state==1)
			{
				if((High>0)&&(High>=Low)) 
					High-=0.1;
				else
					High=Low+0.1;
			}else if(state==2)
			{
				if((Low>0)&&(Low<=High))
				Low-=0.1;
				else 
					Low=High-0.1;
			}
		}
	}
	else
	{
		sec2=7;
		if(key3_flag==0)
		{
			key3_flag=1;
			memory_flag=1;			
		}				
	}
}

void TIM2_IRQHandler(void)   //TIM2中断
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)  //检查TIM2更新中断发生与否
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);  //清除TIM2更新中断标志	
		Tim_ms++;
		if(Tim_ms%6==0)
		{
			if(s0==1) 
				s0=0;
			else 
				s0=1;
		}
		KEY_Scan();
		if(state==1&&s0)
		{
			snprintf((char *)Tem_H, sizeof(Tem_H), "H:      L:%2.1f", Low);
			LCD1602_Show_Str(0, 1, (unsigned char *)Tem_H);	
			LCD1602_Write_Dat(0xDF);
			LCD1602_Write_Dat(0x43);
		}else if(state==2&&s0)
		{
			snprintf((char *)Tem_H, sizeof(Tem_H), "H:%2.1f  L:    ",High);
			LCD1602_Show_Str(0, 1, (unsigned char *)Tem_H);	
			LCD1602_Write_Dat(0xDF);
			LCD1602_Write_Dat(0x43);
		}else
		{
			snprintf((char *)Tem_H, sizeof(Tem_H), "H:%2.1f  L:%2.1f", High,Low);
			LCD1602_Show_Str(0, 1, (unsigned char *)Tem_H);	
			LCD1602_Write_Dat(0xDF);
			LCD1602_Write_Dat(0x43);			
		}
		
	}
	if(sec1!=0) sec1--;
	if(sec2!=0) sec2--;
}

需要原理图和全部源码的朋友可以通过下面的链接下载:
源码和原理图

你可能感兴趣的:(STM32开发)