找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32f103zet6智能楼宇系统源程序

[复制链接]
跳转到指定楼层
楼主
ID:353926 发表于 2019-4-9 09:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于STM32f103zet6的智能楼宇程序
  1. #include "stm32f10x.h"
  2. #include "delay.h"
  3. #include "led.h"
  4. #include "key.h"
  5. #include "bsp_lcd.h"
  6. #include "dht11.h"
  7. #include "GY30.h"
  8. #include "qc.h"

  9. void display(void);

  10. extern const unsigned char gImage_tu1[153608];
  11. extern const unsigned char gImage_two[153608];

  12. u8 yema=0;
  13. u8 s=0;
  14. u8 key_num;

  15. int num=30;
  16. char str[50];
  17. int ws = 40,wx = 6,ss = 70,sx = 30,gs = 1000,gx = 9;
  18. u8 wd,sd;
  19. u32 gz;

  20. void bj(void)
  21. {
  22.         if(wd>ws || wd<wx)
  23.         {
  24.                 BEEP_ON;
  25.         }
  26.         else
  27.         {
  28.                 BEEP_OFF;
  29.         }
  30.         if(sd>ss || sd<sx)
  31.         {
  32.                 LED4_ON;
  33.         }
  34.         else
  35.         {
  36.                 LED4_OFF;
  37.         }
  38.         if(gz>gs)
  39.         {
  40.                 LED2_ON;
  41.         }
  42.         else
  43.         {
  44.                 LED2_OFF;
  45.         }
  46.         if(gz<gx)
  47.         {
  48.                 LED1_ON;
  49.         }
  50.         else
  51.         {
  52.                 LED1_OFF;
  53.         }
  54. }

  55. void jiemian1(void)
  56. {
  57.         yema = 1;//记录当前页码
  58.         display();
  59. //       
  60. //        BACK_COLOR = GRAY;                //背景灰色
  61. //        POINT_COLOR = BLACK;        //字体黑色
  62. //        LCD_ShowButton(70,20,168,65,"LED_ON");//16号字体

  63. //        LCD_ShowButton(70,100,168,145,"LED_OFF");//16号字体
  64. //       
  65. //        LCD_ShowButton(70,180,168,225,"光照:");//16号字体
  66. //       
  67. //        LCD_ShowButton(70,260,168,305,"PM2.5:");//16号字体

  68. //        LCD_ShowButton(162,284,239,319,"下一页");//16号字体
  69. }
  70. void jiemian2(void)
  71. {
  72.         yema = 2;//记录当前页码
  73.        
  74.         Paint_Bmp(0,0,240,320,gImage_two);        //画图

  75.         BACK_COLOR = GRAY;                //背景灰色
  76.         POINT_COLOR = BLACK;        //字体黑色
  77.        
  78.         sprintf(str,"%3d ",ws);//将湿度值转换成字符串
  79.         LCD_ShowString(120,10,500,500,24,str,0);//将字符串的内容显示出来
  80.         LCD_ShowString(155,15,500,500,16,"℃",0);//将字符串的内容显示出来
  81.        
  82.         sprintf(str,"%3d ",wx);//将湿度值转换成字符串
  83.         LCD_ShowString(120,40,500,500,24,str,0);//将字符串的内容显示出来
  84.         LCD_ShowString(155,45,500,500,16,"℃",0);//将字符串的内容显示出来
  85.        
  86.         sprintf(str,"%3d ",ss);//将湿度值转换成字符串
  87.         LCD_ShowString(120,110,500,500,24,str,0);//将字符串的内容显示出来
  88.         LCD_ShowString(155,110,500,500,24,"%",0);//将字符串的内容显示出来
  89.        
  90.         sprintf(str,"%3d ",sx);//将湿度值转换成字符串
  91.         LCD_ShowString(120,140,500,500,24,str,0);//将字符串的内容显示出来
  92.         LCD_ShowString(155,140,500,500,24,"%",0);//将字符串的内容显示出来

  93.         sprintf(str,"%3d ",gs);//将湿度值转换成字符串
  94.         LCD_ShowString(140,180,500,500,24,str,0);//将字符串的内容显示出来
  95. //        LCD_ShowString(170,180,500,500,24,"lx",0);//将字符串的内容显示出来
  96.        
  97.         sprintf(str,"%3d ",gx);//将湿度值转换成字符串
  98.         LCD_ShowString(140,200,500,500,24,str,0);//将字符串的内容显示出来
  99. //        LCD_ShowString(170,200,500,500,24,"lx",0);//将字符串的内容显示出来
  100.        
  101.         LCD_ShowButton(70,6,120,36,"温度上阈");//16号字体
  102.         LCD_ShowButton(70,36,120,66,"温度下阈");//16号字体
  103.        
  104.         LCD_ShowButton(70,106,120,136,"湿度上阈");//16号字体
  105.         LCD_ShowButton(70,136,120,166,"湿度下阈");//16号字体
  106.        
  107.         LCD_ShowButton(70,176,120,206,"光照上阈");//16号字体
  108.         LCD_ShowButton(70,206,120,236,"光照下阈");//16号字体
  109.        
  110.         LCD_ShowButton(70,266,120,296,"空气质量");//16号字体
  111.        

  112. }

  113. u8 status=0;

  114. void gongneng(void)
  115. {
  116.         if(tp_dev.x[0]>162 && tp_dev.y[0]>284 && tp_dev.x[0]<239 && tp_dev.y[0]<319)
  117.         {
  118.                 jiemian1();//切换界面
  119.         }
  120.         else if(tp_dev.x[0]>70 && tp_dev.y[0]>6 && tp_dev.x[0]<120 && tp_dev.y[0]<36)
  121.         {       
  122.                 status=1;
  123.         }
  124.                 else if(tp_dev.x[0]>70 && tp_dev.y[0]>36 && tp_dev.x[0]<120 && tp_dev.y[0]<66)
  125.         {       
  126.                 status=2;
  127.         }
  128.         else if(tp_dev.x[0]>70 && tp_dev.y[0]>106 && tp_dev.x[0]<120 && tp_dev.y[0]<136)
  129.         {
  130.                 status=3;
  131.         }
  132.         else if(tp_dev.x[0]>70 && tp_dev.y[0]>136 && tp_dev.x[0]<120 && tp_dev.y[0]<166)
  133.         {
  134.                 status=4;
  135.         }
  136.                 else if(tp_dev.x[0]>70 && tp_dev.y[0]>176 && tp_dev.x[0]<120 && tp_dev.y[0]<206)
  137.         {
  138.                 status=5;
  139.         }
  140.                 else if(tp_dev.x[0]>70 && tp_dev.y[0]>206 && tp_dev.x[0]<120 && tp_dev.y[0]<236)
  141.         {
  142.                 status=6;
  143.         }
  144. }

  145. void LED_Ctrl(void)
  146. {
  147.         if(tp_dev.x[0]>10 && tp_dev.y[0]>10 && tp_dev.x[0]<55 && tp_dev.y[0]<65)
  148.         {
  149.                 LED1_ON;//开灯
  150.         }
  151.         else if(tp_dev.x[0]>70 && tp_dev.y[0]>100 && tp_dev.x[0]<168 && tp_dev.y[0]<145)
  152.         {
  153.                 LED1_OFF;//关灯
  154.         }
  155.         else if(tp_dev.x[0]>162 && tp_dev.y[0]>284 && tp_dev.x[0]<239 && tp_dev.y[0]<319)
  156.         {
  157.                 jiemian2();//切换界面
  158.         }
  159. }

  160. void BEEP_Ctrl(void)
  161. {
  162.         if(tp_dev.x[0]>70 && tp_dev.y[0]>20 && tp_dev.x[0]<168 && tp_dev.y[0]<65)
  163.         {
  164.                 BEEP_ON;//开蜂鸣器
  165.         }
  166.         else if(tp_dev.x[0]>70 && tp_dev.y[0]>100 && tp_dev.x[0]<168 && tp_dev.y[0]<145)
  167.         {
  168.                 BEEP_OFF;//关蜂鸣器
  169.         }
  170.         else if(tp_dev.x[0]>162 && tp_dev.y[0]>284 && tp_dev.x[0]<239 && tp_dev.y[0]<319)
  171.         {
  172.                 jiemian1();//切换界面
  173.         }
  174. }

  175. void display(void)
  176. {
  177.         Paint_Bmp(0,0,240,320,gImage_tu1);        //画图
  178.         DHT11_Read_Data();                //读取温湿度数据
  179.         wd = Temperature;
  180.         sd = Humidity;
  181.         sprintf(str,"%3d℃",wd);//将温度值转换成字符串
  182.         POINT_COLOR = BLACK;
  183.         BACK_COLOR = WHITE;
  184.         LCD_ShowString(145,48,500,500,16,str,0);//将字符串的内容显示出来
  185.        
  186.         sprintf(str,"%3d %%",sd);//将湿度值转换成字符串
  187.         LCD_ShowString(145,115,500,500,16,str,0);//将字符串的内容显示出来
  188.        
  189.         BH1750_ReadDate();        //读取光照强度
  190.         gz = (u32)BH1750_Value;
  191.         sprintf(str,"%5d lx",gz);//将湿度值转换成字符串
  192.         LCD_ShowString(145,180,500,500,16,str,0);//将字符串的内容显示出来
  193.        
  194.         if(KQ == 1)
  195.         {
  196.                 LCD_ShowString(145,240,500,500,16,"fine",0);
  197.         }
  198.         else
  199.         {
  200.                 LCD_ShowString(145,240,500,500,16,"DANGER",0);
  201.         }
  202. }

  203. //主函数
  204. int main(void)
  205. {
  206.         delay_init();        //延时初始化,必须初始化
  207.         LED_Init();                //LED初始化
  208.         KEY_Init();                //按键初始化
  209.         BEEP_Init();
  210.         TFTLCD_Init();        //屏幕初始化
  211.         TP_Init();                        //触摸初始化
  212. //        TP_Adjust();                //触摸校准

  213.         DHT11_Init();
  214.         Init_BH1750();
  215.        
  216.         tp_dev.xfac = 0.075815;
  217.         tp_dev.xoff = -32;
  218.         tp_dev.yfac = 0.086930;
  219.         tp_dev.yoff = -17;
  220.        
  221.         jiemian1();
  222.        
  223.        
  224.         while(1)
  225.         {
  226.                 if(yema == 1)                                //判断是否在第一页
  227.                 {
  228.                         jiemian1();
  229.                 }
  230.        
  231.                 TP_Scan(0);//触摸扫描
  232.                
  233.                 bj();
  234.                
  235.                 key_num = key_scan();
  236.                 if(status == 1)
  237.                 {
  238.                         if(key_num == 1)
  239.                         {
  240.                                 ws++;
  241.                                 sprintf(str,"%3d ",ws);//将湿度值转换成字符串
  242.                                 LCD_ShowString(120,10,500,500,24,str,0);//将字符串的内容显示出来
  243.                                 LCD_ShowString(155,15,500,500,16,"℃",0);//将字符串的内容显示出来
  244.                         }
  245.                         if(key_num == 2)
  246.                         {
  247.                                 ws--;
  248.                                 sprintf(str,"%3d ",ws);//将湿度值转换成字符串
  249.                                 LCD_ShowString(120,10,500,500,24,str,0);//将字符串的内容显示出来
  250.                                 LCD_ShowString(155,15,500,500,16,"℃",0);//将字符串的内容显示出来
  251.                         }
  252.                 }
  253.                
  254.                 if(status == 2)
  255.                 {
  256.                         if(key_num == 1)
  257.                         {
  258.                                 wx++;
  259.                                 sprintf(str,"%3d ",wx);//将湿度值转换成字符串
  260.                                 LCD_ShowString(120,40,500,500,24,str,0);//将字符串的内容显示出来
  261.                                 LCD_ShowString(155,45,500,500,16,"℃",0);//将字符串的内容显示出来
  262.                         }
  263.                         if(key_num == 2)
  264.                         {
  265.                                 wx--;
  266.                                 sprintf(str,"%3d ",wx);//将湿度值转换成字符串
  267.                                 LCD_ShowString(120,40,500,500,24,str,0);//将字符串的内容显示出来
  268.                                 LCD_ShowString(155,45,500,500,16,"℃",0);//将字符串的内容显示出来
  269.                         }
  270.                 }
  271.                
  272.                 if(status == 3)
  273.                 {
  274.                         if(key_num == 1)
  275.                         {
  276.                                 ss++;
  277.                                 sprintf(str,"%3d ",ss);//将湿度值转换成字符串
  278.                                 LCD_ShowString(120,110,500,500,24,str,0);//将字符串的内容显示出来
  279.                                 LCD_ShowString(155,110,500,500,24,"%",0);//将字符串的内容显示出来
  280.                         }
  281.                         if(key_num == 2)
  282.                         {
  283.                                 ss--;
  284.                                 sprintf(str,"%3d ",ss);//将湿度值转换成字符串
  285.                                 LCD_ShowString(120,110,500,500,24,str,0);//将字符串的内容显示出来
  286.                                 LCD_ShowString(155,110,500,500,24,"%",0);//将字符串的内容显示出来
  287.                         }
  288.                 }
  289.                
  290.                 if(status == 4)
  291.                 {
  292.                         if(key_num == 1)
  293.                         {
  294.                                 sx++;
  295.                                 sprintf(str,"%3d ",sx);//将湿度值转换成字符串
  296.                                 LCD_ShowString(120,140,500,500,24,str,0);//将字符串的内容显示出来
  297.                                 LCD_ShowString(155,140,500,500,24,"%",0);//将字符串的内容显示出来
  298.                         }
  299.                         if(key_num == 2)
  300.                         {
  301.                                 sx--;
  302.                                 sprintf(str,"%3d ",sx);//将湿度值转换成字符串
  303.                                 LCD_ShowString(120,140,500,500,24,str,0);//将字符串的内容显示出来
  304.                                 LCD_ShowString(155,140,500,500,24,"%",0);//将字符串的内容显示出来
  305.                         }
  306.                 }
  307.                
  308.                 if(status == 5)
  309.                 {
  310.                         if(key_num == 1)
  311.                         {
  312.                                 gs++;
  313.                                 sprintf(str,"%3d ",gs);//将湿度值转换成字符串
  314.                                 LCD_ShowString(140,180,500,500,24,str,0);//将字符串的内容显示出来
  315.                         }
  316.                         if(key_num == 2)
  317.                         {
  318.                                 gs--;
  319.                                 sprintf(str,"%3d ",gs);//将湿度值转换成字符串
  320.                                 LCD_ShowString(140,180,500,500,24,str,0);//将字符串的内容显示出来
  321.                         }
  322.                 }
  323.                                
  324.                 if(status == 6)
  325.                 {
  326.                         if(key_num == 1)
  327.                         {
  328.                                 gx++;
  329.                                 sprintf(str,"%3d ",gx);//将湿度值转换成字符串
  330.                                 LCD_ShowString(140,200,500,500,24,str,0);//将字符串的内容显示出来
  331.                         }
  332.                         if(key_num == 2)
  333.                         {
  334.                                 gx--;
  335.                                 sprintf(str,"%3d ",gx);//将湿度值转换成字符串
  336.                                 LCD_ShowString(140,200,500,500,24,str,0);//将字符串的内容显示出来
  337.                         }
  338.                 }
  339.                
  340.                 if(tp_dev.sta & 0x40)        //判断触摸是否按下
  341.                 {
  342.                         if(yema == 1)                                //判断是否在第一页
  343.                         {
  344.                                 jiemian1();
  345.                                 LED_Ctrl();
  346.                         }
  347.                         else if(yema == 2)        //判断是否在第二页
  348.                         {
  349.                                 gongneng();
  350.                         }
  351.                         tp_dev.sta &= 0xbf;//清零第6位
  352.                 }       
  353.         }
  354. }
复制代码
全部资料51hei下载地址:
最终程序.7z (285.15 KB, 下载次数: 17)

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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