找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2449|回复: 0
打印 上一主题 下一主题
收起左侧

led亮度温控风扇系统源程序

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:
  1. #include "ds18b20.h"
  2. #include "smg.h"
  3. #include "eepom52.h"
  4. #include"motor_pwm.h"

  5. sbit Reduc  = P3^3;         //减
  6. sbit Add = P3^4;         //加
  7. sbit Mode = P3^5;         //设置

  8. sbit fs = P3^6;
  9. uchar m=0;
  10. uint time_when=0,time_points=0,time_seconds=0,time_100ms=0;
  11. void Timer0Init(void)                //1000微秒@12.000MHz
  12. {
  13.         TMOD |= 0x01;                //设置定时器模式
  14.         TL0 = 0x80;                //设置定时初值
  15.         TH0 = 0xD8;                //设置定时初值
  16.     TR0 = 1;                        //timer0 start running
  17.     ET0 = 1;                        //enable timer0 interrupt
  18.     EA = 1;                         //open global interrupt switch
  19. }
  20. void tm0_isr() interrupt 1 using 1
  21. {
  22.         TL0 = 0x80;                //设置定时初值
  23.         TH0 = 0xD8;                //设置定时初值
  24.         time_100ms++;
  25.         if (time_100ms==99)
  26.     {  
  27.                 time_100ms = 0;
  28.                 time_seconds++;
  29.                 m=~m;      
  30.                 if (time_seconds==60)
  31.             {                       
  32.                         time_seconds=0;
  33.                 time_points++;      
  34.                         if (time_points==60)
  35.                     {
  36.                         time_points=0;
  37.                         time_when++;      
  38.                                 if (time_when==24)
  39.                             {
  40.                                 TR0 = 0;
  41.                             }
  42.                     }
  43.             }     
  44.     }       
  45. }


  46. void main(void)
  47. {       
  48.         uint T_tada;//温度数据变量
  49.         char T_L=2,T_H=0;
  50.         uchar Mode_project=1,key_count=0;//设置变量
  51.         uint disp_count=300;//显示300次采集一次温度数据,解决采集时闪烁               
  52.         uchar temp_disp[4];//数据缓存
  53.         /****DS18B20检测,没有检测到数码管显示四个‘----’****/                
  54.         while(reset())
  55.         {
  56.                 disp_count=100;while(disp_count--){SMG_setxy(1,11,0);SMG_setxy(2,11,0);SMG_setxy(3,11,0);SMG_setxy(4,11,0);}
  57.                 disp_count=100;while(disp_count--){SMG_setxy(1,10,0);SMG_setxy(2,10,0);SMG_setxy(3,10,0);SMG_setxy(4,10,0);}
  58.         }
  59.         /****开机采集温度,避免第一次读取温度为85度误报警****/
  60.         disp_count=30;
  61.         while(disp_count--)
  62.         {

  63.                 T_tada=read_temp();
  64.         }
  65.         /***开机把数据从单片机内部eepom中读出来***/
  66.         if(byte_read(0x2050)!=0)
  67.         {               
  68.                 SectorErase(0x2000);
  69.                 byte_write(0x2000,T_L);
  70.                 byte_write(0x2001,T_H);
  71.                 byte_write(0x2050,1);               
  72.         }
  73.         if(byte_read(0x2050)==1)
  74.         {
  75.                 T_L=byte_read(0x2000);
  76.                 T_H=byte_read(0x2001);
  77.         }
  78.         Timer0Init();
  79.         pwm_init();       
  80.         while(1)
  81.         {
  82.                 temp_disp[0]=time_when/10;
  83.                 temp_disp[1]=time_when%10;

  84.                 temp_disp[2]=time_points/10;
  85.                 temp_disp[3]=time_points%10;

  86.                 /*******加速*******/
  87.                 if(Add==0)
  88.                 {       
  89.                         //利用显示延时
  90.                         key_count=60;
  91.                         while(key_count--)
  92.                         {
  93.                                 SMG_setxy(1,temp_disp[0],0);
  94.                                 SMG_setxy(2,temp_disp[1],m);
  95.                                 SMG_setxy(3,temp_disp[2],0);
  96.                                 SMG_setxy(4,temp_disp[3],0);
  97.                         }
  98.                         speed+=5;
  99.                         if(speed>=100)speed=100;
  100.                 }
  101.                 /*******减速*******/
  102.                 if(Reduc==0)
  103.                 {
  104.                         //利用显示延时
  105.                         key_count=60;
  106.                         while(key_count--)
  107.                         {
  108.                                 SMG_setxy(1,temp_disp[0],0);
  109.                                 SMG_setxy(2,temp_disp[1],m);
  110.                                 SMG_setxy(3,temp_disp[2],0);
  111.                                 SMG_setxy(4,temp_disp[3],0);
  112.                         }
  113.                         speed-=5;
  114.                         if(speed<=0)speed=0;
  115.                 }
  116.                 if(T_tada>3500)//温度大于35度,打开风扇
  117.                 {
  118.                         fs=1;
  119.                 }
  120.                 else
  121.                 {
  122.                         fs=0;
  123.                 }

  124.                 if(T_H==time_when && T_L==time_points && time_seconds<0x01)
  125.                 {
  126.                         EA=0;
  127.                         M1=0;       
  128.                 }
  129. //                temp_disp[2]=time_seconds/10;
  130. //                temp_disp[3]=time_seconds%10;

  131.                 /****温度数据采集****/
  132.                 loop:
  133.                 if(++disp_count>=3000)
  134.                 {
  135.                         disp_count=0;
  136.                         TR1=0;T_tada=read_temp();if(speed==0)TR1=1;
  137. //                        if(T_tada>20000)//负数
  138. //                        {
  139. //                                temp_disp[0]=11;
  140. //                                T_tada-=20000;
  141. //                        }
  142. //                        else temp_disp[0]=T_tada/10000; //正数温度百位        
  143. //                                       
  144. //                        temp_disp[1]=T_tada/1000%10; //温度十位
  145. //                        temp_disp[2]=T_tada/100%10;//温度个位
  146. //                        temp_disp[3]=T_tada/10%10;//温度小数第一位
  147. //                        if(temp_disp[0]==0)temp_disp[0]=10;//百位消隐
  148.                 }
  149.                 /****数码管扫描显示****/               
  150.                 SMG_setxy(1,temp_disp[0],0);
  151.                 SMG_setxy(2,temp_disp[1],m);
  152.                 SMG_setxy(3,temp_disp[2],0);
  153.                 SMG_setxy(4,temp_disp[3],0);
  154.                
  155.                 /****按下设置键,设置参数****/
  156.                 if(Mode==0)
  157.                 {                         
  158.                         Mode_project=1;
  159.                         while(Mode==0);//按键松开                       
  160.                         while(1)
  161.                         {
  162.                                 //设置页面显示
  163.                                 switch(Mode_project)
  164.                                 {
  165.                                         case 1:
  166.                                                 SMG_setxy(1,12,0);
  167.                                                 SMG_setxy(2,11,0);
  168.                                                 SMG_setxy(3,T_H/10,0);
  169.                                                 SMG_setxy(4,T_H%10,0);
  170.                                         break;
  171.                                         case 2:
  172.                                                 SMG_setxy(1,13,0);
  173.                                                 SMG_setxy(2,11,0);
  174.                                                 SMG_setxy(3,T_L/10,0);
  175.                                                 SMG_setxy(4,T_L%10,0);
  176.                                         break;
  177.                                 }
  178.                                 //按下加键,对设定目标加
  179.                                 if(Add==0)
  180.                                 {
  181.                                         //利用显示延时
  182.                                         key_count=60;
  183.                                         while(key_count--)
  184.                                         {
  185.                                                 switch(Mode_project)
  186.                                                 {
  187.                                                         case 1:
  188.                                                                 SMG_setxy(1,12,0);
  189.                                                                 SMG_setxy(2,11,0);
  190.                                                                 SMG_setxy(3,T_H/10,0);
  191.                                                                 SMG_setxy(4,T_H%10,0);
  192.                                                         break;
  193.                                                         case 2:
  194.                                                                 SMG_setxy(1,13,0);
  195.                                                                 SMG_setxy(2,11,0);
  196.                                                                 SMG_setxy(3,T_L/10,0);
  197.                                                                 SMG_setxy(4,T_L%10,0);
  198.                                                         break;
  199.                                                 }
  200.                                         }
  201.                                         switch(Mode_project)
  202.                                         {
  203.                                                 case 1:T_H+=1;if(T_H>=100)T_H=100;break;
  204.                                                 case 2:T_L+=1;if(T_L>=100)T_L=100;break;
  205.                                         }               
  206.                                 }
  207.                                 //按下减键,对设定目标减
  208.                                 if(Reduc==0)
  209.                                 {
  210.                                         //利用显示延时
  211.                                         key_count=60;
  212.                                         while(key_count--)
  213.                                         {
  214.                                                 switch(Mode_project)
  215.                                                 {
  216.                                                         case 1:
  217.                                                                 SMG_setxy(1,12,0);
  218.                                                                 SMG_setxy(2,11,0);
  219.                                                                 SMG_setxy(3,T_H/10,0);
  220.                                                                 SMG_setxy(4,T_H%10,0);
  221.                                                         break;
  222.                                                         case 2:
  223.                                                                 SMG_setxy(1,13,0);
  224.                                                                 SMG_setxy(2,11,0);
  225. ……………………

  226. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
程序.rar (48.04 KB, 下载次数: 16)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表