分享一个自己做的单片机设计
智能风扇控制系统仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
功能非常多多,带三挡风速调节,还有正常,睡眠,自然3个模式,是按键控制的。
单片机风扇源程序如下:
- //数码管低电平有效
- #include <reg51.h>
- #include <absacc.h>
- #include <ctype.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include "set.h"
- #include "myinit.h"
- #include "function.h"
- uint signal;
- uint q; //用于DS18B20 j秒读取一次数据
- uchar code a[]={ //定义数组,用于数码管显示
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x40
- };
- /*******************************************************************************/
- uchar TH=50,TL=10; //定义温度上下限
- uchar t[2]={20,20},*pt; //用来存放温度值,测温程序就是通过这个数组与主函数通信的
- uchar TempBuffer1[9]={0x2b,0x31,0x32,0x32,0x2e,0x30,0x30,0x43,'\0'};
- //显示实时温度,上电时显示+125.00C
- uchar TempBuffer0[17]={0x54,0x48,0x3a,0x2b,0x31,0x32,0x35,0x20,
- 0x54,0x4c,0x3a,0x2b,0x31,0x32,0x34,0x43,'\0'};
- //显示温度上下限,上电时显示TH:+125 TL:+124C
- uchar code dotcode[4]={0,25,50,75};
- /*******************************************************************************/
- void main()
- {
- // unsigned char TH=50,TL=10; //定义温度上下限
- init_timer(); //初始化
- init(); //初始化
- pt=ReadTemperature(TH,TL,0x3f); //上限温度-22,下限-24,分辨率10位,也就是0.25C
- //读取温度,温度值存放在一个两个字节的数组
- covert1(); //将温度转换为LCD显示的据数
- while(1) //等待启动
- {
- scan();
- if(flag_modle==1)
- {
- led_normal=1;
- led_style=1;
- break;
- }
- }
- close_motor=1; //打开电机电源
- while(1) //启动后进入循环扫面状态
- {
- scan();
- switch(flag_modle)
- {
- case 1:
- choice_speed();
- break;
- case 2:
- choice_modle();
- flag_modle=1;
- break;
- case 3:
- stop_operation();
- break;
- }
- /* if(q>60) //3S读取一次温度
- {
- pt=ReadTemperature(TH,TL,0x3f); //上限温度-22,下限-24,分辨率10位,也就是0.25C
- //读取温度,温度值存放在一个两个字节的数组
- covert1();
- } */
- }
- }
- //中断函数
- void timer0() interrupt 1
- {
- if(i>=20) //定时1S
- {
- i=0;
- time--;
- if(time<=0)
- time=0;
- }
- if(q>20) //定时3S,3S测一次温度
- {
- q=0;
- pt=ReadTemperature(TH,TL,0x3f);
- covert1();
- }
- if((t[1]>TH)|(t[1]<TL)) //温度 TL<t[1]<TH 时,关闭电机
- {
- close_motor=0; //关闭电机电源
- init(); //初始化
- }
- else
- close_motor=1; //打开电机电源
- i++,q++;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- }
复制代码- #include "function.h"
- #include "set.h"
- //按键扫描函数
- void scan(void)
- {
- if(speed==1)//风速
- {
- delay(10);
- if(speed==1)
- {
- while(speed);
- flag_modle=1; //模式标志位
- flag_speed++;
- signal=1; //按键信号标志,用于选择模式
- if(flag_speed>4)
- flag_speed=1;
- }
- }
- if(style==1)//模式
- {
- delay(10);
- if(style==1)
- {
- while(style);
- flag_modle=2; //模式标志位
- flag_style++;
- signal=1; //按键信号标志,用于选择模式
- if(flag_style>3)
- flag_style=1;
- }
- }
- if(stop==1)//停止
- {
- delay(10);
- if(stop==1)
- {
- while(stop);
- flag_modle=3; //模式标志位
- signal=1; //按键信号标志,用于选择模式
- }
- }
- }
复制代码
调试中的代码与Proteus7.8版本的仿真下载(代码存在一些Keil版本兼容性问题):
STC89C51_风扇控制系统.zip
(433.14 KB, 下载次数: 284)
最终代码下载:经过2楼提醒后 修复某些Keil会编译错误的问题
修复后的代码.7z
(37.51 KB, 下载次数: 77)
|