本设计以STM32F407ZGT6开发板为硬件基础。制作了一个基于红外遥控的多功能智能电风扇控制器,主要实现了以下功能: 1.实现电风扇的无极调速,并可以对电风扇的转速进行设置和转速的实时测试和显示。 2. 电风扇至少产生三类风:“自然风”、“常风”和“睡眠风”。 3. 能实现自带定时开、关机。 4. 具备温度测量功能和省电功能,可以根据温度自动微调风速,当外界温度低于设定温度,电风扇停止转动;外界温度高于设定温度,电风扇可恢复转动测温精度优于1摄氏度。 5. 具有LCD显示功能,能显示日期、时间、温度、风扇转速、运行模式等等信息。 6. 能实现全功能红外遥控;通过红外遥控板实现全部的参数设置与调节。 7.可编程模式,可设置闹钟。
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "usmart.h"
- #include "rtc.h"
- #include "key.h"
- #include "timer.h"
- #include "remote.h"
- #include "ds18b20.h"
- #include "fontupd.h"
- #include "text.h"
- #include "w25qxx.h"
- #include "pwm.h"
- #include "mypid.h"
- #include "wkup.h"
- extern u8 TIM5CH1_CAPTURE_STA; //输入捕获状态
- extern u16 TIM5CH1_CAPTURE_VAL; //输入捕获值
- long long temp,now_ses;
- long long set_in=30;
- long long pwm_out0;
- int t1=31,t2=5,t3=5,t4=5,t5=30;//睡眠风初始时间
- int ton=30; //定时开机时间
- int toff=30; //定时关机时间
- int flag=0,flag0=0,flag1=0;//模式标志
- int flag2=0,flag3=0,flag4=0;//起始标志为0
- short temperature;
- short temperature_set=25;
- int i=0,j=0;
- //RTC实时时钟显示
- void RTC_SHOW(void)
- {
- RTC_TimeTypeDef RTC_TimeStruct;
- RTC_DateTypeDef RTC_DateStruct;
- u8 tbuf[40];
- POINT_COLOR=BLUE;
- RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
- sprintf((char*)tbuf,"%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
- LCD_ShowString(180,120,210,16,24,tbuf);
-
- RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
-
- sprintf((char*)tbuf,"20%02d-%02d-%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date);
- LCD_ShowString(30,120,210,16,24,tbuf);
-
-
- sprintf((char*)tbuf,"%d",RTC_DateStruct.RTC_WeekDay);
- LCD_ShowString(373,120,210,16,24,tbuf);
- }
- //LCD显示
- void LCD_SHOW(void)
- {
- // set_in=30;
- POINT_COLOR=BLUE;
- Show_Str(300,120,400,24,"星期: ",24,0);
- POINT_COLOR=RED;
- Show_Str(100,50,400,24,"多功能智能风扇控制器",24,0);
- POINT_COLOR= DARKBLUE ;
- Show_Str(75,200,120,24,"设定转速: ",24,0);
- LCD_ShowString(350,200,100,4,24,"r/s");
- LCD_ShowxNum(228,200,set_in,3,24,0);
- Show_Str(75,280,200,24,"风扇转速: ",24,0);
- LCD_ShowString(350,280,100,4,24,"r/s");
- LCD_ShowxNum(228,280,now_ses,3,24,0);
- Show_Str(75,360,400,24,"运行模式: ",24,0);
- Show_Str(75,440,400,24,"设置的温度: ",24,0);
- LCD_ShowString(245,440,100,4,24,". C");
- LCD_ShowxNum(228,440,temperature_set,3,24,0);
- Show_Str(75,520,400,24,"当前的温度: ",24,0);
- LCD_ShowString(250,520,100,4,24,". C");
- LCD_ShowxNum(180,600,toff,3,24,0);
- LCD_ShowxNum(335,600,ton,3,24,0);
- Show_Str(75,600,400,24,"定时关机:",24,0);
- Show_Str(230,600,400,24,"定时开机:",24,0);
- Show_Str(350,680,400,24,"flag2= ",24,0);
- LCD_ShowxNum(410,680,flag2,3,24,0);
- Show_Str(75,660,400,24,"睡眠时间: ",24,0);
- Show_Str(75,700,120,24,"t1: ",24,0);
- LCD_ShowxNum(120,700,t1,3,24,0);
- Show_Str(195,700,45,24,"t5: ",24,0);
- LCD_ShowxNum(240,700,t5,3,24,0);
-
- Show_Str(40,760,45,24,"t2:",24,0);
- LCD_ShowxNum(85,760,t2,3,24,0);
- Show_Str(160,760,45,24,"t3:",24,0);
- LCD_ShowxNum(205,760,t3,3,24,0);
- Show_Str(280,760,45,24,"t4: ",24,0);
- LCD_ShowxNum(325,760,t4,3,24,0);
- // Show_Str(75,600,400,24,"占空比: /1000",24,0);
- // LCD_ShowxNum(175,600,pwm_out0,3,24,0);
- }
- //温度显示函数
- void Show_temperature(void)
- {
- u8 t;
- temperature=DS18B20_Get_Temp();
- if(temperature<0)
- {
- LCD_ShowChar(30+40,520,'-',16,0); //显示负号
- temperature=-temperature; //转为正数
- }else LCD_ShowChar(30+40,520,' ',16,0); //去掉负号
- LCD_ShowNum(150+70+8,520,temperature/10,2,24); //显示正数部分
- LCD_ShowNum(150+70+40,520,temperature%10,1,24); //显示小数部分
- }
- int main(void)
- {
- u8 key;
- u8 *str=0;
- u8 t=0;
-
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- delay_init(168); //初始化延时函数
- uart_init(115200); //初始化串口波特率为115200
- usmart_dev.init(168); //初始化USMART
- LED_Init(); //初始化LED
- LCD_Init(); //初始化LCD
- My_RTC_Init(); //初始化RTC
- Remote_Init(); //红外接收初始化
- DS18B20_Init();
- W25QXX_Init(); //初始化W25Q128
- exfuns_init(); //为fatfs相关变量申请内存
- font_init();
- TIM2_Int_Init(4999,8399); //计数0.5s发生一次中断
- TIM3_Int_Init(9999,8399); //计数1s发生一次中断
- TIM7_Int_Init(4999,8399); //计数0.5s发生一次中断
- TIM4_Int_Init(9999,8399); //计数1s发生一次中断
- TIM14_PWM_Init(1000-1,4-1); //84M/4=21Mhz的计数频率,重装载值500,所以PWM频率为 21M/1000=21Khz.
-
- TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1); //以1Mhz的频率计数
-
- RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits,0); //配置WAKE UP中断,1秒钟中断一次
- while(DS18B20_Init()) //DS18B20初始化
- {
- LCD_ShowString(30,130,200,16,16,"DS18B20 Error");
- delay_ms(200);
- }
-
- TIM_SetCompare1(TIM14,550);
- while(1)
- {
- RTC_Set_AlarmA(2,18,37,0);
- RTC_SHOW();
- LCD_SHOW();
- Show_temperature();
- key=Remote_Scan();
-
- // TIM_SetCompare1(TIM14,0);
- if(temperature_set<temperature/10||temperature_set==temperature/10)
- {
- if(j==0)
- {
- LCD_LED=1;
- if(TIM5CH1_CAPTURE_STA&0X80)
- {
- temp=TIM5CH1_CAPTURE_STA&0X3F;
- temp*=0XFFFFFFFF;
- temp+=TIM5CH1_CAPTURE_VAL;
-
- printf("HIGH:%lld us\r\n",temp);
- TIM5CH1_CAPTURE_STA=0x80; //关闭下一次捕获
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,DISABLE);
- now_ses = 1000000 / (temp * 200);
- pwm_out0 = pid_function2(set_in,now_ses);
- TIM_SetCompare1(TIM14,pwm_out0);
-
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
- TIM5CH1_CAPTURE_STA=0; //开启下一次捕获
- }
- }
- }
- else if(temperature_set>temperature/10)
- {
- pwm_out0 =0;now_ses =0;
- TIM_SetCompare1(TIM14,pwm_out0);
- LCD_LED=0;
- j=0;
-
- }
-
- if(key)
- {
- delay_ms(200);
- if(key == Remote_Scan())
- {
- switch(key)
- {
-
- case 98: //调温
- str="UP";
- temperature_set++;
- j=0;
- break;
- case 168:str="DOWN";
- temperature_set--;
- j=0;
- break;
-
-
-
- case 152:str="2"; //调速
- set_in++;
- break;
-
- case 56:str="8";
- set_in--;
- break;
-
-
- case 224:str="VOL-"; //睡眠时间
- if(flag4==0)
- {
- t1--;
- }
- if(flag4==1)
- {
- t5--;
- }
- break;
-
- case 144:str="VOL+";
- if(flag4==0)
- {
- t1++;
- }
- if(flag4==1)
- {
- t5++;
- }
- break;
-
-
- case 162:str="POWER";
- {
- TIM_Cmd(TIM5, DISABLE); //失能TIM5
- TIM_SetCompare1(TIM14,0); //PWM输出高电平
- TIM5CH1_CAPTURE_STA=1;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,DISABLE);
- LCD_LED=0;
- flag3=0;
- j=1;
- break;
- }
- case 226:str="ALIENTEK";
- {
- TIM_Cmd(TIM5, ENABLE); //使能TIM5 (捕获模式)
- TIM_SetCompare1(TIM14,550);
- TIM5CH1_CAPTURE_STA=0;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
- LCD_LED=1;
- j=0;
- flag3=0;
- }
- break;
-
-
- case 176: str="3"; //定时关机
- ton++;
- flag2=1;
- break;
- case 90: str="9";
- ton--;
- flag2=1;
- break;
-
-
- case 104:str="1"; //定时关机
- toff++;
- flag2=0;
- break;
- case 16:str="7";
- toff--;
- flag2=0;
- break;
-
-
- case 48:str="4"; //可编程模式时间设置
- t2++;
- break;
- case 24:str="5";
- t3++;
- break;
- case 122:str="6";
- t4++;
- break;
-
-
- case 2:str="PLAY"; //确认开关机
- {
- if(flag3==0)
- {
- TIM_Cmd(TIM4, ENABLE); //定时开/关机
- }
- if(flag3==1)
- {
- TIM_Cmd(TIM4, DISABLE); //取消定时开/关机
- flag3=0;
- }
- }
- break;
-
-
-
- case 34: //模式调节
- i--;
- str="LEFT";
- if(i==1)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"常风",24,0);
- TIM_Cmd(TIM7,DISABLE);
- }
- if(i==2)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"睡眠风",24,0);
- TIM_Cmd(TIM7,DISABLE);
- flag1=0;
- }
- if(i==3)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"自然风",24,0);
- }
- if(i==4)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"可编程模式",24,0);
- TIM_Cmd(TIM7,DISABLE);
- }
- if(i<=0)
- {
- i=0;
- set_in=30;
- }
- break;
-
- case 194:
- i++;
- str="RIGHT";
- if(i==1)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"常风",24,0);
- TIM_SetCompare1(TIM14,pwm_out0); //失能TIM14
- TIM_Cmd(TIM5, ENABLE); //失能TIM5
- TIM5CH1_CAPTURE_STA=0;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
- }
- if(i==2)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"睡眠风",24,0);
- flag1=0;
- }
- if(i==3)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"自然风",24,0);
- TIM_SetCompare1(TIM14,pwm_out0); //失能TIM14
- TIM_Cmd(TIM5, ENABLE); //失能TIM5
- TIM5CH1_CAPTURE_STA=0;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
- }
- if(i==4)
- {
- LCD_Fill(180,350,600,420,WHITE); //清除之前的显示
- Show_Str(200,360,400,24,"可编程模式",24,0);
- }
- if(i>=5)
- {
- i=0;
- set_in=30;
- }
- break;
- }
- }
- if(i==1) //常风
- {
- TIM_Cmd(TIM3,DISABLE); //失能TIM3 (睡眠风计时)
- TIM_Cmd(TIM7,DISABLE); //失能TIM7 (自然风计时)
- TIM_Cmd(TIM2, DISABLE);
- }
- if(i==2) //睡眠风
- {
- TIM_Cmd(TIM7,DISABLE); //失能TIM7
- TIM_Cmd(TIM3,ENABLE); //使能定时器3
- TIM_Cmd(TIM2, DISABLE);
- }
- if(i==3) //自然风
- {
- TIM_Cmd(TIM3,DISABLE); //失能TIM3
- TIM_Cmd(TIM7,ENABLE); //使能TIM7
- TIM_Cmd(TIM2, DISABLE);
- }
- if(i==4) //可编程
- {
- TIM_Cmd(TIM3,DISABLE); //失能TIM3
- TIM_Cmd(TIM7,DISABLE); //使能TIM7
- TIM_Cmd(TIM2,ENABLE); //使能TIM2
- }
- }
- }
- }
- //定时器3中断服务函数 睡眠风定时
- void TIM3_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
- {
- if(flag1==0)
- {
- t1--;
- if(t1==0) //关机
- {
- flag1=1;
- TIM_SetCompare1(TIM14,0); //失能TIM14
- TIM_Cmd(TIM5, DISABLE); //失能TIM5
- TIM5CH1_CAPTURE_STA=1;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,DISABLE);
- TIM_Cmd(TIM7,DISABLE);
- LCD_LED=0;
- j=1;
- flag4=1;
- }
- }
- if(flag1==1)
- {
- t1++;
- if(t1==t5) //开机
- {
- flag1=0;
- TIM_SetCompare1(TIM14,pwm_out0); //使能TIM14
- TIM_Cmd(TIM5, ENABLE); //使能TIM5
- TIM5CH1_CAPTURE_STA=0;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
- TIM_Cmd(TIM7,ENABLE);
- LCD_LED=1;
- j=0;
- flag4=0;
- }
- }
- }
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
- }
- //定时器7中断服务函数 自然风
- void TIM7_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM7,TIM_IT_Update)==SET) //溢出中断
- {
- if(flag==0)
- {
- set_in--;
- if(set_in<=1)
- {
- flag=1;
- }
- }
- if(flag==1)
- {
- set_in++;
- if(set_in>=55)
- {
- flag=0;
- }
- }
- TIM_ClearITPendingBit(TIM7,TIM_IT_Update); //清除中断标志位
- }
- }
- //定时器2中断服务函数 可编程
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //溢出中断
- {
- if(flag0==0)
- {
- t2--;
- set_in=50;
- if(t2==0)
- {
- TIM_Cmd(TIM7,DISABLE);
- flag0=1;
- t2=5;
- }
- }
-
- if(flag0==1)
- {
- t3--;
- set_in=30;
- if(t3==0)
- {
- TIM_Cmd(TIM7,DISABLE);
- flag0=2;
- t3=5;
- }
- }
-
- if(flag0==2)
- {
- t4--;
- TIM_Cmd(TIM7,ENABLE);
- if(t4==0)
- {
- TIM_Cmd(TIM7,DISABLE);
- TIM_Cmd(TIM5, DISABLE); //失能TIM5
- TIM_SetCompare1(TIM14,0); //PWM输出高电平
- TIM5CH1_CAPTURE_STA=1;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,DISABLE);
- LCD_LED=0;
- j=1;
- flag0=0;
- t4=5;
- TIM_Cmd(TIM2, DISABLE);
- }
- }
- TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
- }
- }
- //定时器4中断服务函数 定时开/关机
- void TIM4_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET) //溢出中断
- {
- if(flag2==0)
- {
- toff--;
- flag3=1;
- if(toff==0)
- {
- TIM_Cmd(TIM4, DISABLE);
- TIM_SetCompare1(TIM14,0); //失能TIM14
- TIM_Cmd(TIM5, DISABLE); //失能TIM5
- TIM5CH1_CAPTURE_STA=1;
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,DISABLE);
- LCD_LED=0;
- j=1;
- }
- }
- if(flag2==1)
- {
- ton--;
- flag3=1;
- if(ton==0)
- {
- TIM_Cmd(TIM4, DISABLE);
- TIM_Cmd(TIM5, ENABLE); //使能TIM5 (捕获模式)
- TIM5CH1_CAPTURE_STA=0;
- TIM_SetCompare1(TIM14,550);
- TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
- LCD_LED=1;
- j=0;
- }
- }
- TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清除中断标志位
- }
- }
复制代码
所有资料51hei提供下载:
智能风扇.rar
(9.11 MB, 下载次数: 446)
|