找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5239|回复: 0
收起左侧

单片机自习室监控系统程序(包括原理图)

[复制链接]
ID:273166 发表于 2019-8-18 07:31 | 显示全部楼层 |阅读模式
自习室监控系统用红外传感器来检查,通过12864液晶显示屏来显示

电路原理图如下:
0.png
Comment    Designator    LibRef
电解电容    C1    10uF
瓷片电容    C2, C3    30pF
USB5V    DC 1    Header 2
红外开关    P1, P2    MHDR1X3
发光二极管    POWER1    LED0
电阻    R1    10k
电阻    R2    10K
电阻    R3    2k
电位器    RV1    R
触点开关    S1    SW-PB
电源开关    SW1    SW-SPDT
蓝牙模块    LANYA    HC-05
单片机    U1    STC89C52
液晶屏    U2    LCD12864
温湿度传感器    U3    DTT11
晶振    Y1    12MHz
万能板        9x15cm

单片机源程序如下:
  1. #include <reg52.h>
  2. #define uchar unsigned char  //无符号字符型 宏定义        变量范围0~255
  3. #define uint  unsigned int         //无符号整型 宏定义        变量范围0~65535
  4. #include "LCD12864.h"
  5. #include<intrins.h>
  6. #include "dht11.h"

  7. sbit jin_1 = P2^0;          //进来传感器1
  8. sbit chu_1 = P2^1;          //出去传感器1

  9. uint zong_rs;    //总人数
  10. uint tongd1_jl;  //通道1进来的人数
  11. uint tongd1_cq;  //通道1出去的人数
  12. uchar t1_value; //通道1用做判断是进来还是出去的变量
  13. uchar t1_zk;    //通道1人走开
  14. uchar t1_j_xd;    //通道1进消抖
  15. uchar t1_c_xd;    //通道1出消抖
  16. uchar t1_qans_qx;  //通道1延时取消
  17. uint t1_qans_value;  //通道1延时取消
  18. uchar Rx_buf[4],Rxnum=0;
  19. uchar table_dht11[5]={1,2,3,4,5};

  20. void InitUART(void)                         //这是串口的基本配置,配置他的波特率是9600.这些参数都是标准的。
  21. {
  22.     TMOD = 0x20;
  23.     SCON = 0x50;
  24.     TH1 = 0xFD;
  25.     TL1 = TH1;
  26.     PCON = 0x00;
  27.     EA = 1;
  28.     ES = 1;
  29.     TR1 = 1;
  30. }

  31. /*
  32. * UART 发送一字节
  33. */
  34. void UART_send_byte(char dat)
  35. {
  36.         SBUF = dat;
  37.         while (TI == 0);
  38.         TI = 0;
  39. }

  40. /*
  41. * UART 发送字符串
  42. */
  43. void UART_send_string(unsigned char *buf)
  44. {
  45.         while (*buf != '\0')
  46.         {
  47.                 UART_send_byte(*buf++);
  48.         }
  49. }
  50. void Delay_ms(unsigned int n)
  51. {
  52.         unsigned int  i,j;
  53.         for(i=0;i<n;i++)
  54.                 for(j=0;j<123;j++);
  55. }
  56. void dst11()
  57. {
  58.         RH();//读出温湿度
  59.         table_dht11[2] = U8T_data_H;
  60.         table_dht11[0] = U8RH_data_H;
  61. }


  62. void tongd1_display()
  63. {
  64. /*******************通道1处理********************/
  65.         if(t1_value == 0)
  66.         {
  67.                 if(jin_1 == 0)
  68.                 {
  69.                         t1_j_xd ++;
  70.                         if(t1_j_xd >= 3)
  71.                         {
  72.                                 t1_j_xd = 0;
  73.                                 t1_value = 1;       
  74.                                 t1_zk = 0;
  75.                                 t1_qans_qx = 1;               
  76.                                 t1_qans_value = 0;
  77.                         }
  78.                 }
  79.                 else
  80.                         t1_j_xd = 0;       

  81.                 if(chu_1 == 0)
  82.                 {
  83.                         t1_c_xd ++;
  84.                         if(t1_c_xd >= 3)
  85.                         {
  86.                                 t1_c_xd = 0;
  87.                                 t1_value = 2;       
  88.                                 t1_zk = 0;               
  89.                                 t1_qans_qx = 1;
  90.                                 t1_qans_value = 0;
  91.                         }
  92.                 }
  93.                 else
  94.                         t1_c_xd = 0;       
  95.         }
  96.         else
  97.         {
  98.                 if(t1_qans_qx == 1)
  99.                 {
  100.                         t1_qans_value ++;             //5ms
  101.                         if(t1_qans_value > 400)         //2s
  102.                                 t1_value = 0;       
  103.                 }
  104.                 if(t1_value == 1)
  105.                 {
  106.                         if((chu_1 == 0) && (t1_zk == 0))
  107.                         {
  108.                                 tongd1_jl ++;              //通道1进来的人数加1       
  109.                                 zong_rs = tongd1_jl - tongd1_cq;
  110.                                 t1_zk = 1;   
  111.                                 t1_qans_value = 0;
  112.                                 t1_qans_qx = 0;
  113.                         }
  114.                         if(t1_zk == 1)                 //人走开
  115.                         {
  116.                                 if(chu_1 == 1)                 
  117.                                 {
  118.                                         t1_value = 0;
  119.                                         t1_zk = 0;                                
  120.                                 }                       
  121.                         }
  122.                 }
  123.                 if(t1_value == 2)
  124.                 {
  125.                         if((jin_1 == 0) && (t1_zk == 0))
  126.                         {
  127.                                 if(tongd1_jl  > tongd1_cq )
  128.                                         tongd1_cq ++;              //通道1出去的人数加1       
  129.                                 zong_rs = tongd1_jl - tongd1_cq;           //计算总人数
  130.                                 t1_zk = 1;  
  131.                                 t1_qans_value = 0;
  132.                                 t1_qans_qx = 0;               
  133.                         }
  134.                         if(t1_zk == 1)                 //人走开
  135.                         {
  136.                                 if(jin_1 == 1)
  137.                                 {
  138.                                         t1_value = 0;
  139.                                         t1_zk = 0;                                
  140.                                 }                       
  141.                         }
  142.                 }
  143.         }

  144. }

  145. void main(void)
  146. {
  147.     uchar Tx_Buf[15];
  148.         InitUART();                        //初始化串口     
  149.     LCD12864_Reset();                                                                //初始化液晶
  150.         LCD12864_HAIZI_SET();                                                        //设置为普通模式
  151.         LCD12864_NoWaitIdle_COM_Write(0x80);                                                //指针设置
  152.         LCD12864_write_word("座位:50 人员:   ");
  153.         LCD12864_NoWaitIdle_COM_Write(0x90);                                                //指针设置
  154.         LCD12864_write_word("余座:           ");                       
  155.         LCD12864_NoWaitIdle_COM_Write(0x88);                                                //指针设置
  156.         LCD12864_write_word("温度:   ℃      ");
  157.         LCD12864_NoWaitIdle_COM_Write(0x98);                                                //指针设置
  158.         LCD12864_write_word("湿度:   %      ");
  159.         dst11();                //先读出温湿度的值
  160.         while(1)
  161.         {       
  162.             tongd1_display();
  163.                        
  164.                    dst11();                //先读出温湿度的值       
  165.                 LCD12864_NoWaitIdle_COM_Write(0x8b);                                                //指针设置
  166.                 LCD12864_Data_Write(0x30+table_dht11[2]%100/10);
  167.                 LCD12864_Data_Write(0x30+table_dht11[2]%10);
  168.             Delay_ms(1);
  169.                 LCD12864_NoWaitIdle_COM_Write(0x9b);
  170.                 LCD12864_Data_Write(0x30+table_dht11[0]%100/10);
  171.                 LCD12864_Data_Write(0x30+table_dht11[0]%10);
  172.                 Delay_ms(1);                  
  173.                 LCD12864_NoWaitIdle_COM_Write(0x87);
  174.                 LCD12864_Data_Write(0x30+zong_rs/10);
  175.                 LCD12864_Data_Write(0x30+zong_rs%10);
  176.                 Delay_ms(1);
  177.                 LCD12864_NoWaitIdle_COM_Write(0x93);
  178.                 LCD12864_Data_Write(0x30+(50-zong_rs)/10);
  179.                 LCD12864_Data_Write(0x30+(50-zong_rs)%10);       
  180.             
  181.                 Tx_Buf[0]='T';        //帧头
  182.             Tx_Buf[1]=5+0x30;                 //将湿度数据送往发送数组,送给蓝牙模块让手机APP显示
  183.             Tx_Buf[2]=0+0x30;               
  184.             Tx_Buf[3]=zong_rs/10+0x30;
  185.             Tx_Buf[4]=zong_rs%10+0x30;

  186.                 Tx_Buf[5]=(50-zong_rs)/10+0x30;
  187.             Tx_Buf[6]=(50-zong_rs)%10+0x30;

  188.                 Tx_Buf[7]=table_dht11[2]/10+0x30;
  189.             Tx_Buf[8]=table_dht11[2]%10+0x30;

  190.                 Tx_Buf[9]=table_dht11[0]/10+0x30;
  191.             Tx_Buf[10]=table_dht11[0]%10+0x30;
  192.                 Tx_Buf[11]=0;//结束符
  193.             UART_send_string(Tx_Buf);          //串口发送温湿度数据出去

  194.         }
  195. }


  196. /*****************串口接收中断函数,接收蓝牙模块的数据*********************/
  197. void UARTInterrupt(void) interrupt 4
  198. {       
  199.         ES=0;        //关闭中断
  200.     if(RI)          //接收到数据
  201.     {

  202.         Rx_buf[Rxnum]=SBUF;  //接收数据进数组
  203.         if(Rx_buf[Rxnum]==0x08)
  204.                 Rxnum=0;
  205.         else
  206.                 Rxnum++;
  207. //                        if(Rx_buf[0]==0x01)//LED1灯
  208. //                {
  209. //                                 if(Rx_buf[1]==0x01)//开灯
  210. //                                        LED1=0;//开灯
  211. //                                else
  212. //                                        LED1=1;        //关灯       
  213. //                }
  214. //                if(Rx_buf[0]==0x02)//LED2灯
  215. //                {
  216. //                                 if(Rx_buf[1]==0x01)//开灯
  217. //                                        LED2=0;//开灯
  218. //                                else
  219. //                                        LED2=1;        //关灯       
  220. //                }
  221.         }
  222.     RI=0;

  223.    ES=1;          //关闭中断
  224. }
复制代码

所有资料51hei提供下载:
自习室蓝牙监控系统.7z (224.9 KB, 下载次数: 41)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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