找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4394|回复: 6
收起左侧

基于单片机的智能家居系统设计存在诸多小问题求助

[复制链接]
ID:314928 发表于 2018-5-26 03:31 | 显示全部楼层 |阅读模式
我是单片机小白,自己摸索了毕业设计,感觉好困难,论文出了一些问题,劳烦大神指教一二            我们的毕业设计要求使用protues进行电路仿真,相关系统原理图中的各个器件按照参考资料都已画好,程序也有(存在问题,编译下来有很多的错误),鄙人目前使用的软件版本是protues7.8和keil4,来进行联调仿真。
老师制定的任务书如下
论文题目:基于单片机的智能家居系统设计

设计目的及任务
以单片机为控制核心,设计实现智能家居控制,该系统具有室内防盗报警功能室内环境参数实时检测功能。主要设计任务:
1. 实现红外人体感应报警
2. 实现智能检测室内温度湿度;
3. 实现智能时钟显示

设计思路
参考所查阅的资料,基本设计思路是:该系统AT89C52(原计划用STC89C52,可是protues元件库中没有,只好用它来代替)为控制中心,加以外围电路包括采集电路、传输电路、LCD1602( LM016)显示电路、数据处理电路、报警装置电路、时钟电路及电路元器件,报警系统装置采用发光二极管和蜂鸣器,数据采集装置使用温湿度传感器(SHT10),时钟芯片选用DS1302。单片机开发软件采用C语言编程,实现室内防盗报警功能和室内环境参数实时检测功能。

技术要求、预期成果
完成单片机的智能家居系统设计,在设计过程中能熟练的使用C语言和proteus仿真软件,掌握单片机及传感器的技术原理和应用,熟悉系统各部分电路的功能,使整个系统能够完成对室内环境的实时监测和及时应对。在答辩中能够尽可能的演示设计中的环境数据采集和实时监测的过程。

存在的主要问题:
1.我按照参考资料画好了protue仿真的大部分器件,连接好了温湿度传感器(SHT10)的相关电路,结果是用掉了p0、p2p3的大部分接口,只剩下p1口了,时钟芯DS1302和热释电红外传感如何设计才能接到AT89C52的单片机上,还能实现红外人体感应报警和智能时钟显示吗?

2.室内防盗报警功能是如果监测到有人闯入室内,会发出声光报警。红外人体感应通过热释电红外传感器实现,报警系统装置(声光报警电路采用发光二极管和蜂鸣器实现。参考资料里采用三极管驱动蜂鸣器报警来提醒温湿度超过限定值,我只需要实现室内环境参数实时检测功能,温湿度的报警电路可以去掉吗,用室内防盗报警电路替换,也就是用发光二极管和蜂鸣器,配合热释电红外传感器实现防盗报警,这个怎么更改呢?

3.智能时钟显示这一块,显示器已经用来显示温湿度了,怎么再切换显示时间呢,而且时钟芯片仅起显示时间的功能,不对电路做其他控制,具体如何设置呢?

4.原资料的程序编译下来有很多错误,无法生成.hex文件,求大神结合我要实现的功能更改一下我的源程序。
原参考资料仿真图

原参考资料仿真图

原参考资料仿真图


我参考画的
360截图-48351593.jpg
热释电红外传感器内部电路图

热

声光报警模块电路

声光报警模块电路

声光报警模块电路

参考资料源程序(本身好像有错还是不完整,编译之后无法生成.hex文件)
  1. //*********主函数*****************
  2. void main(void)
  3. {
  4.               value humi_val,temp_val;
  5.         unsigned char error,checksum;
  6.         unsigned int wendu,shidu;
  7.         LCD_init();      
  8.         s_connectionreset();
  9.         LCD_disp_str(0,1,"TE ");
  10.               LCD_disp_str(0,2,"RH ");
  11. //*********初始化温度显示区*********
  12. LCD_disp_str(2,1,"TTT.T C");
  13. //*********初始化湿度显示区*********
  14. LCD_disp_str(2,2,"RRR.R%");
  15. delay_n10us(20000);     //延时0.2s
  16. while(1)
  17. { error=0;
  18.    error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI);   
  19.    error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP);  
  20.    if(error!=0) s_connectionreset();                 
  21.      else
  22.      { humi_val.f=(float)humi_val.i;                 
  23.        temp_val.f=(float)temp_val.i;                  
  24.       calc_SHT10(&humi_val.f,&temp_val.f);         
  25.           wendu=10*temp_val.f;
  26.          LCD_disp_char(2,1,wendu/1000+'0');       //显示温度百位
  27.        LCD_disp_char(3,1,(wendu%1000)/100+'0');  //显示温度十位
  28.       LCD_disp_char(4,1,(wendu%100)/10+'0');   //显示温度个位
  29.          LCD_disp_char(6,1,(wendu%10)+'0');   //显示温度小数点后第一位
  30.           shidu=10*humi_val.f;
  31.          LCD_disp_char(2,2,shidu/1000+'0');       //显示湿度百位
  32.       LCD_disp_char(3,2,(shidu%1000)/100+'0'); //显示湿度十位
  33.       LCD_disp_char(4,2,(shidu%100)/10+'0');   //显示湿度个位
  34.          LCD_disp_char(6,2,(shidu%10)+'0');   //显示湿度小数点后第一位
  35.           }
  36.          
  37.                delay_n10us(80000);                   //延时约0.8s
  38.         }
  39. }
  40. 系统部分程序:  
  41. /*************定义接口********************
  42.          P0------DB0~DB7  (LCD1602)     
  43.          P2.4------RS      (LCD1602)
  44.          P2.5------RW      (LCD1602)
  45.          P2.6------E       (LCD1602)
  46.                P3.5------SOUNDER (蜂鸣器)
  47.             P3.6------SCK     (SHT10) //时钟
  48.             P3.7------DATA    (SHT10) //数据
  49. *****************************************/
  50. #include <AT89x51.h>
  51. #include <intrins.h>
  52. #include <math.h>    //Keil library   
  53. #include <stdio.h>   //Keil library
  54. #define TEMPUP 24   //温度上限, 达到该温度蜂鸣器报警
  55. typedef unsigned char BYTE;
  56. typedef unsigned int WORD;
  57. typedef bit BOOL ;
  58. /*******************************************/
  59. sbit buzzer = P3^5; //蜂鸣器
  60. BOOL start;
  61. /******************************************/
  62. //************第一部分LCD1602设置******
  63. START*************************************/
  64. #define LCD_DB        P0
  65.         sbit         LCD_RS=P2^5;   //P2^5是p2.5
  66.         sbit         LCD_RW=P2^6;   //P2^6是p2.6
  67.         sbit         LCD_E=P2^7;    //P2^7是p2.7
  68. /******定义函数****************/
  69. #define uchar unsigned char
  70. #define uint unsigned int
  71. void LCD_init(void);                          //初始化函数
  72. void LCD_write_command(ucharcommand);        //写指令函数
  73. void LCD_write_data(uchardat);               //写数据函数
  74. void LCD_disp_char(ucharx,uchar y,uchar dat);//在某个屏幕位置上显示一个字符,X(0-15),y(1-2)
  75. void LCD_disp_str(ucharx,uchar y,uchar *str); //LCD1602显示字符串函数
  76. void delay_n10us(uintn);                     //延时函数
  77. /*--------------------------------------
  78. ;初始化LCD1602
  79. ;-------------------------------------*/
  80. void LCD_init(void)
  81. {
  82. delay_n10us(10);
  83. LCD_write_command(0x38);//设置8位格式,2行,5x7
  84. delay_n10us(10);
  85. LCD_write_command(0x0c);//整体显示,关光标,不闪烁
  86. delay_n10us(10);
  87. LCD_write_command(0x06);//设定输入方式,增量不移位
  88. delay_n10us(10);
  89. LCD_write_command(0x01);//清除屏幕显示
  90. delay_n10us(100);       //延时清屏,延时函数,延时约n个10us
  91. }
  92. /*--------------------------------------
  93. ;LCD1602写指令函数
  94. ;-------------------------------------*/
  95. void LCD_write_command(uchardat)
  96. {
  97. delay_n10us(10);
  98. LCD_RS=0;         //指令
  99. LCD_RW=0;         //写入
  100. LCD_E=1;          //允许
  101. LCD_DB=dat;
  102. delay_n10us(10);  
  103. LCD_E=0;
  104. delay_n10us(10);  
  105. }
  106. /*--------------------------------------
  107. ;LCD1602写数据函数
  108. ;-------------------------------------*/
  109. void LCD_write_data(uchardat)
  110. {
  111. delay_n10us(10);
  112. LCD_RS=1;          //数据
  113. LCD_RW=0;          //写入
  114. LCD_E=1;           //允许
  115. LCD_DB=dat;
  116. delay_n10us(10);
  117. LCD_E=0;
  118. delay_n10us(10);
  119. }
  120. /*--------------------------------------
  121. ;LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
  122. ;-------------------------------------*/
  123. void LCD_disp_char(ucharx,uchar y,uchar dat)
  124. {
  125.   uchar address;
  126.   if(y==1)
  127.          address=0x80+x;
  128.   else
  129.          address=0xc0+x;
  130.   LCD_write_command(address);
  131.   LCD_write_data(dat);
  132. }
  133. /*--------------------------------------
  134. ;LCD1602显示字符串函数,在某个屏幕起始位置{X(0-15),y(1-2)}上显示一个字符串。
  135. ;-------------------------------------*/
  136. void LCD_disp_str(ucharx,uchar y,uchar *str)
  137. {
  138.   uchar address;
  139.   if(y==1)
  140.          address=0x80+x;
  141.   else
  142.          address=0xc0+x;
  143.   LCD_write_command(address);
  144.   while(*str!='\0')
  145.   {
  146.     LCD_write_data(*str);   
  147.     str++;
  148.   }
  149. }
  150. /*--------------------------------------
  151. ;延时函数,延时约n个10us
  152. ;-------------------------------------*/
  153. void delay_n10us(uintn)  //延时n个10us@12M晶振
  154. {      
  155.         uint i;           
  156.         for(i=n;i>0;i--)   
  157.         {
  158.        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
  159.               }
  160. }                                    
  161. //***************第一部分LCD1602设置*******
  162. END****************************************
  163. //*************第二部分SHT10设置*************
  164. START****************************************
  165. sbit SCK  = P3^6;     //定义通讯时钟端口
  166. sbit DATA = P3^7;      //定义通讯数据端口
  167. typedef union  
  168. { unsigned int i;      //定义了两个共用体
  169.   float f;
  170. } value;
  171. enum {TEMP,HUMI};      //TEMP=0,HUMI=1
  172. #define noACK 0             //用于判断是否结束通讯
  173. #define ACK   1            //结束数据传输
  174.                             //adr  command r/w
  175. #define STATUS_REG_W0x06   //000   0011   0
  176. #define STATUS_REG_R0x07   //000   0011   1
  177. #define MEASURE_TEMP0x03   //000   0001   1
  178. #define MEASURE_HUMI0x05   //000   0010   1
  179. #define RESET        0x1e  //000   1111    0
  180. /****************定义函数****************/
  181. voids_transstart(void);               //启动传输函数
  182. voids_connectionreset(void);          //连接复位函数
  183. char s_write_byte(unsignedchar value);//SHT10写函数
  184. char s_read_byte(unsignedchar ack);   //SHT10读函数
  185. char s_measure(unsigned char*p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数
  186. void calc_SHT10(float*p_humidity ,float *p_temperature);//温湿度补偿
  187. /*--------------------------------------
  188. ;启动传输函数
  189. ;-------------------------------------*/  
  190. void s_transstart(void)
  191. // generates a transmissionstart  
  192. //       _____         ________
  193. // DATA:      |_______|
  194. //           ___     ___
  195. // SCK : ___|   |___|  |______
  196. {   
  197.    DATA=1; SCK=0;                   //Initial state
  198.    _nop_();
  199.    SCK=1;
  200.    _nop_();
  201.    DATA=0;
  202.    _nop_();
  203.    SCK=0;  
  204.    _nop_();_nop_();_nop_();
  205.    SCK=1;
  206.    _nop_();
  207.    DATA=1;        
  208.    _nop_();
  209.    SCK=0;      
  210. }
  211. /*--------------------------------------  
  212. ;连接复位函数
  213. ;-------------------------------------*/
  214. void s_connectionreset(void)
  215. // communication reset:DATA-line=1 and at least 9 SCK cycles followed by transstart
  216. //      _____________________________________________________         ________
  217. // DATA:                                                     |_______|
  218. //          _   _    _    _   _    _    _   _    _        ___    ___
  219. // SCK : __| |__| |__| |__| |__||__| |__| |__| |__| |______|   |___|   |______
  220. {   
  221.   unsigned char i;  
  222.   DATA=1; SCK=0;                    //Initial state
  223.   for(i=0;i<9;i++)                  //9 SCK cycles
  224.   {
  225.     SCK=1;
  226.     SCK=0;
  227.   }
  228.   s_transstart();                   //transmission start
  229. }
  230. /*--------------------------------------
  231. ;SHT10写函数
  232. ;-------------------------------------*/
  233. char s_write_byte(unsignedchar value)
  234. //----------------------------------------------------------------------------------
  235. // writes a byte on theSensibus and checks the acknowledge  
  236. {  
  237.   unsigned char i,error=0;   
  238.   for (i=0x80;i>0;i/=2)             //shift bit for masking
  239.   {  
  240.     if (i & value) DATA=1;          //masking value with i , write toSENSI-BUS
  241.     elseDATA=0;                        
  242.     SCK=1;                          //clk for SENSI-BUS
  243.     _nop_();_nop_();_nop_();        //pulswith approx. 3 us     
  244.     SCK=0;
  245.   }
  246.   DATA=1;                           //release DATA-line
  247.   SCK=1;                           //clk #9for ack  
  248.   error=DATA;                       //check ack (DATA willbe pulled down by SHT10),DATA在第9个上升沿将被SHT10自动下拉为低电
  249.   _nop_();_nop_();_nop_();
  250.   SCK=0;
  251.   DATA=1;                           //release DATA-line
  252.   return error;                     //error=1 in case of noacknowledge //返回:0成功,1失败
  253. }
  254. /*--------------------------------------
  255. ;SHT10读函数
  256. ;-------------------------------------*/
  257. char s_read_byte(unsignedchar ack)  
  258. // reads a byte form theSensibus and gives an acknowledge in case of "ack=1"  
  259. {  
  260.   unsigned char i,val=0;
  261.   DATA=1;                           //release DATA-line
  262.   for (i=0x80;i>0;i/=2)             //shift bit for masking
  263.   { SCK=1;                          //clk for SENSI-BUS
  264.     if (DATA) val=(val | i);        //read bit   
  265.        _nop_();_nop_();_nop_();       //pulswith approx. 3 us
  266.     SCK=0;              
  267.   }
  268.   if(ack==1)DATA=0;                 //in case of"ack==1" pull down DATA-Line
  269.   else DATA=1;                      //如果是校验(ack==0),读取完后结束通讯
  270.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us
  271.   SCK=1;                            //clk #9 for ack
  272.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us  
  273.   SCK=0;                 
  274.   _nop_();_nop_();_nop_();          //pulswith approx. 3 us
  275.   DATA=1;                           //release DATA-line
  276.   return val;
  277. }
  278. /*--------------------------------------
  279. ;测量温湿度函数
  280. ;-------------------------------------*/
  281. char s_measure(unsigned char*p_value, unsigned char *p_checksum, unsigned char mode)
  282. // makes a measurement(humidity/temperature) with checksum
  283. {  
  284.   unsigned error=0;
  285.   unsigned int i;
  286.   s_transstart();                   //transmission start
  287.   switch(mode){                     //send command to sensor
  288.     caseTEMP  :error+=s_write_byte(MEASURE_TEMP); break;
  289.     case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
  290.     default    : break;   
  291.   }
  292.   for (i=0;i<65535;i++) if(DATA==0) break;//wait until sensor has finished the measurement
  293.   if(DATA) error+=1;                // or timeout (~2 sec.) isreached
  294.   *(p_value) =s_read_byte(ACK);    //read thefirst byte (MSB)
  295.   *(p_value+1)=s_read_byte(ACK);    //read the second byte (LSB)
  296.   *p_checksum =s_read_byte(noACK);  //read checksum
  297.   return error;
  298. }
  299. /*--------------------------------------
  300. ;温湿度补偿函数
  301. ;-------------------------------------*/
  302. void calc_SHT10(float*p_humidity ,float *p_temperature)
  303. // calculates temperature[C] and humidity [%RH]
  304. // input :  humi [Ticks] (12 bit)
  305. //          temp [Ticks] (14 bit)
  306. // output:  humi [%RH]
  307. //          temp [C]
  308. { const float C1=-4.0;              // for 12 Bit
  309.   const float C2=+0.0405;           // for 12 Bit
  310.   const float C3=-0.0000028;        // for 12 Bit
  311.   const float T1=+0.01;             // for 14 Bit @ 5V
  312.   const float T2=+0.00008;           // for 14 Bit @ 5V
  313.   float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit
  314.   float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  315.   float rh_lin;                     // rh_lin:  Humidity linear
  316.   float rh_true;                    // rh_true: Temperaturecompensated humidity
  317.   float t_C;                        // t_C   : Temperature [C]
  318.   t_C=t*0.01 - 40;                  //calc. temperature fromticks to [C]
  319.   rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  320.   rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity[%RH]
  321.   if(rh_true>100)rh_true=100;       //cut if the value is outside of
  322.   if(rh_true<0.1)rh_true=0.1;       //the physical possible range
  323.   *p_temperature=t_C;               //return temperature [C]
  324.   *p_humidity=rh_true;              //return humidity[%RH]
  325. }
  326. //**********第二部分SHT10设置***************
  327. //END****************************************
复制代码

回复

使用道具 举报

ID:34643 发表于 2018-5-26 07:08 | 显示全部楼层
1、DS1302和热释电红外传感可以连接到P1口上。
2、温湿度的报警电路不需要去掉,只需要修改程序就行。修改程序后也可以两者的报警共用。
3、智能时钟显示这一块,显示器已经用来显示温湿度了,如果要切换显示时间可以用按键来进行切换,也可以分时进行轮流显示。

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:303383 发表于 2018-5-26 05:51 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

ID:303383 发表于 2018-5-26 10:47 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

ID:314928 发表于 2018-5-26 21:55 | 显示全部楼层
51mcu 发表于 2018-5-26 07:08
1、DS1302和热释电红外传感可以连接到P1口上。
2、温湿度的报警电路不需要去掉,只需要修改程序就行。修改 ...

谢谢你的建议,小白一枚,画图连线还可以,修改程序对我有点难了,原程序编译后的错误都不知道怎么修改,再加入新程序更不会写了
回复

使用道具 举报

ID:314928 发表于 2018-5-26 22:12 | 显示全部楼层
原程序编译之后,出现了几个错误,如下图所示,怎么修改呢?
回复

使用道具 举报

ID:336678 发表于 2018-5-28 13:51 | 显示全部楼层
上面编译错误含义是 value没有定义,可以换成常用的类型定义即可。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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