• 前言:时隔好几个月一直在瞎忙,小白一个,抽空把之前做的还没整完的补上,做事要有始有终,未雨绸缪。废话不说上电路和代码(代码是基于51写的,算是个简单的物联网小项目,具体的NB模组的配置只是对用到的东西进行配置),这是底层硬件及软件的实现,后边会将手机端APP(蓝牙传输)以及PC机端(电信物联网云平台)的开发写一下。
  • 项目名称:基于NBIOT无线光照数据采集
  • 项目完整底层代码和电路设计地址:http://github.com/bianjiji/NB-IOT_BC95-B5
  • 硬件设计

其中BC95模块用的是前一篇写的里边的那个模块,当时还用的液晶屏12864的功耗贼高,所以这个项目没有考虑一丢丢的功耗,很多地方可以优化的很好。这里就拿原来的贴上,那些传感器都是从实验室拿的,所谓就近取材没一点毛病。

  • 软件设计
  • 主函数部分
  1 #include "STC12C5A60S2.h"
  2 #include "illumination.h"
  3 #include "12864.h"
  4 #include "delay.h"
  5 #include "UART.h"
  6 #include "RhAndTemp.h"
  7 //#include "LMS.h"
  8 #include <string.h>
  9 #include <stdio.h>
 10 /****************宏定义**************************/
 11 #define uint unsigned int
 12 #define uchar unsigned char
 13 #define Buf2_Max 10  
 14 #define S2RI 0x01  
 15 /****************串口初始值*********************/
 16 #define  RELOAD      0xDC        //Fosc = 11.0592MHz, Baud0 = 9600  BRT=256-(11.0592M/32/9600/12^n)   
 17 #define  doubleBaud  0            //波特率是否加倍,0不倍,1加倍 
 18 #define  timeMod     1           //独立波特率发生器,0为12T模式,1为1T模式
 19 /************************变量定义****************/
 20 uchar code theme_one[]   = "xxxxxxxxxxxxxxxx";
 21 uchar code theme_two[]   = "  xxxxxxxxxxxx  ";
 22 uchar code theme_three[] = "  xxxxxxxxxxxx  ";
 23 uchar code theme_four[]  = "    xxxxxx      "; 
 24 
 25 uchar code dis_system[]  = "*****NB-IOT*****";
 26 uchar code dis_illum[]   = "光照:      Lux "; 
 27 uchar code dis_temp[]    = "温度:      ℃  "; 
 28 uchar code dis_humidty[] = "湿度:      %   "; 
 29 uint  Light_buf = 0;          //缓存光照数据
 30 uint count=0,count1=0;Second_Int=0;            //计数
 31 uchar UART_2Int_falg,temp2;//串口2中断标志及缓存变量
 32 uchar LightBuff_Send_flag=0,LightBuff_Send_To_phone=0,num=0;
 33 uchar sendDataBuf[30]={0};      //把要发送的数据转换成十六进制数存入待发送的数组中
 34 uchar RxCounter,RxBuffer[10];     //接收缓冲,最大USART_REC_LEN个字节.
 35 uchar *strx;
 36 extern uint temp_buf ;
 37 extern uint Hum_buf;
 38 uint Temperature, Humidty;
 39 /************************函数声明****************/
 40 void dis_init_screen(void);
 41 void dis_data_screen(void);
 42 void IntConfiguration(void);
 43 void SerialInti(void);
 44 void BC95Init(void);
 45 void Timer0_Init(void);
 46 void DATAToHEX(uint light);
 47 //void DATAToHEX(uint pm,uint temp,uint humid,uint light);
 48 /*****************************************************************************
 49 *函数名       :main
 50 *函数功能     :
 51 *输入         :
 52 *输出         :无
 53 ******************************************************************************/
 54 void main(void)
 55 {
 56     Init_guangzhao();                    //光照初始化
 57     LCD12864_Init();                  //LCD12864液晶屏初始化
 58     BC95Init();                      //NB_BC95-B5模块初始化
 59     delay_nMs(10);
 60     dis_init_screen();               //初始化显示菜单
 61     delay_1s(1);
 62     dis_data_screen();               //显示数据菜单
 63     Timer0_Init();                   //定时器0初始化
 64     SerialInti();                    //串口初始化
 65     UART1_Init( RELOAD,  doubleBaud,  timeMod);//UART1配置
 66     UART2_Init( RELOAD,  doubleBaud,  timeMod);//UART2配置
 67     while(1)
 68        {
 69            
 70            Light_buf = Get_guangzhao();                    //采集光照
 71            memset(sendDataBuf,0,sizeof(sendDataBuf));        //清空数组sendDataBuf[];
 72            //printf("光照强度:%dLux\r\n",Light_buf);
 73            RHAndTemp();//采集温湿度
 74            Temperature=temp_buf;
 75            Humidty=Hum_buf;
 76            Dispaly_tempAndHumidty(Temperature ,Humidty );//显示温湿度
 77            if(LightBuff_Send_To_phone==1)//到达1s,上传数据
 78            {
 79                LightBuff_Send_To_phone=0;
 80                DATAToHEX(Light_buf);
 81                //通过串口1 发送数据
 82                UART1_Send_data(sendDataBuf,5);
 83            
 84            }
 85             
 86            
 87            else if(LightBuff_Send_flag==1)//到达1分钟,上传数据
 88            {
 89                
 90                LightBuff_Send_flag=0;
 91                sprintf(&sendDataBuf[0], "AT+NMGS=5,00%08x\r\n",Light_buf);//将采集到的光照数据转换成十六进制上便于平台解析                
 92                printf("%s\r\n",sendDataBuf);                              //验证数据是否正确,可以注释掉不用。
 93                UART2_Send_data(sendDataBuf,30) ;                          //利用单片机串口2向NB模组发送数据上报到云平台
 94                TR0 = 1; 
 95                EA = 1;
 96             }
 97 
 98 //           DATAToHEX(0,temp_buf,Humidty_buf,Light_buf);
 99              
100 
101            
102         }
103       
104 }
105 /*****************************************************************************
106 *函数名       :void DATAToHEX(uint light)
107 *函数功能     :
108             前2位作为帧头    最后一位作为帧尾   中间为数据 
109             2个位的数据存入一个unsigned int的数据  
110             缓存数组的低位存入一个传感器数据的高八位,
111             缓存数组的高位存入一个传感器数据的低八位
112 ******************************************************************************/
113 
114 void DATAToHEX(uint light)
115 {
116 
117     sendDataBuf[0] =  0x88;       //帧头
118     sendDataBuf[1] =  0x66;
119     sendDataBuf[2] =  light/256;
120     sendDataBuf[3] =  light%256;
121     sendDataBuf[4] = 0xaa;               //帧尾                                     
122 }
123 
124 void SerialInti(void)//初始化程序(必须使用,否则无法收发)
125 {
126 
127     TH1=0xfd;  //装入初值,波特率9600
128     TL1=0xfd;
129     TR1=1;   //打开定时器
130     SM0=0;   //设置串行通讯工作模式,(10为一部发送,波特率可变,由定时器1的溢出率控制)
131     SM1=1;   //(同上)在此模式下,定时器溢出一次就发送一个位的数据
132     REN=1;   //串行接收允许位(要先设置sm0sm1再开串行允许)  
133     TI=0;
134     RI=0;     
135     EA=1; //    //开总中断    
136     ES=1;      //串行口中断开  
137 
138 }
139 /*****************************************************************************
140 *函数名       :dis_init_screen
141 *函数功能     :初始化菜单
142 *输入         :
143 *输出         :无
144 ******************************************************************************/
145 void dis_init_screen(void)
146 {
147     LCD12864_WriteString(1,0,theme_one);
148     LCD12864_WriteString(2,0,theme_two);
149     LCD12864_WriteString(3,0,theme_three);
150     LCD12864_WriteString(4,0,theme_four);
151 }
152 /*****************************************************************************
153 *函数名       :dis_data_screen
154 *函数功能     :初始化菜单数据
155 *输入         :
156 *输出         :无
157 ******************************************************************************/
158 void dis_data_screen(void)
159 {
160     LCD12864_WriteString(1,0,dis_system);
161     LCD12864_WriteString(2,0,dis_illum);
162     LCD12864_WriteString(3,0,dis_temp);
163     LCD12864_WriteString(4,0,dis_humidty);
164 }
165 
166 /*****************************************************************************
167 *函数名       :BC95Init
168 *函数功能     :初始化NB—BC95模组
169 *输入         :
170 *输出         :无
171 ******************************************************************************/
172 void BC95Init(void)
173 {
174     //delay_nMs(500);//等待BC95初始化
175     delay_nMs(100);
176     UART2_SendStr("AT+NCDP=139.159.140.34,5683\r\n");//设置CDP(电信云的IP地址)
177     delay_nMs(100);    
178     UART2_SendStr("AT+NRB\r\n");                     //软件复位NB模块
179     delay_nMs(100);                                       
180     UART2_SendStr("AT+NNMI=1\r\n");                  //通过NB模块自动收指令
181     delay_nMs(100);
182     UART2_SendStr("AT+CGATT=1\r\n");                 //设置NB模块的网络连接
183     delay_nMs(100);    
184 }
185 ///*****************************************************************************
186 //*函数名       : void CLR_Buf2(void)
187 //*函数功能     :清除缓存数据函数(串口2)
188 //*输入         :
189 //*输出         :无
190 //******************************************************************************/
191 //void CLR_Buf2(void)
192 //{
193 //    uint k;
194 //    for(k=0;k<Buf2_Max;k++)     
195 //    {
196 //        RxBuffer[k] = 0;
197 //    }
198 //    Second_Int = 0;              
199 //}
200 /*****************************************************************************
201 *函数名       :Timer0_Init
202 *函数功能     :定时器0初始化
203 *输入         :
204 *输出         :无
205 ******************************************************************************/
206 void Timer0_Init(void)
207 {
208 
209     AUXR = 0x80;                    //timer0 work in 1T mode==1T模式
210     TMOD |= 0x01;                    //set timer0 as mode1 (16-bit)
211     TL0 = 0xCD;        
212     TH0 = 0xD4;        //1ms
213     TF0 = 0;
214     TR0 = 1;                        //timer0 start running
215     ET0 = 1;                        //enable timer0 interrupt
216     EA  = 1;
217     //count = 0;
218 }
219 /*****************************************************************************
220 *函数名       :tm0_isr() interrupt 1 using 1
221 *函数功能     :定时器中断服务子函数
222 *输入         :
223 *输出         :无
224 ******************************************************************************/
225 void timer0_isr() interrupt 1 
226 {
227     TL0 = 0xCD;        
228     TH0 = 0xD4;        //1ms
229     count++;
230     if (count>1000)               //10ms * 100 -> 1s
231     {
232         count1++;
233         count=0; 
234         LightBuff_Send_To_phone = 1;
235         if(count1>60)
236         {    count1 = 0;               //reset counter
237             LightBuff_Send_flag = 1;   
238             EA=0;
239             TR0=0;
240         }
241     }
242 }
243 /*****************************************************************************
244 *函数名       :UART_2Interrupt(void) interrupt 8
245 *函数功能     :串口2中断服务子函数
246 *输入         :
247 *输出         :无
248 ******************************************************************************/
249 void UART_2Interrupt(void) interrupt 8
250 {
251     IE2 = 0x00;                       //关闭串口2中断  
252     if(S2CON&S2RI)
253         {
254             S2CON&=~S2RI;
255             UART_2Int_falg=1;
256             RxBuffer[Second_Int++]=S2BUF;
257             if(Second_Int>Buf2_Max) 
258                 { Second_Int=0;}
259         } 
260     IE2 = 0x01;                        //恢复串口2中断
261 }
262 #endif
  • 其他外设函数(串口、温湿度传感器、光照传感器、lcd12864驱动)
  1 #include <intrins.h>   
  2 #include "UART.H"   
  3 //缓存串口1和串口2标志符 
  4 //bit UART1_Recv_flag = 0;   
  5 //bit UART2_Recv_flag = 0;
  6 #define S2RI 0x01    
  7 #define S2TI 0x02    
  8 //bit PM_flag = 0;
  9 //uchar idata PM_ReData[9];
 10 uchar flag1,temp1; 
 11 void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod)   
 12 {   
 13     SCON |= 0x50;                     //串口1方式1,接收充许      
 14     BRT = RELOAD;                     //波特率9600   BRT=256-(11.0592M/32/9600/12^n) =0Xdc    
 15     if (timeMod == 1)                 //1T   
 16     {                                 //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS   
 17         AUXR |= 0x15;                 //串口1使用独立波特率发生器,独立波特率发生器1T   
 18     }   
 19     else                              //12T   
 20     {   
 21         AUXR |= 0x11;   
 22     }      
 23     if (doubleBaud == 1)   
 24     {   
 25         PCON |= 0x80;                 //波特率加倍   
 26     }   
 27     else   
 28     {   
 29         PCON &= 0x7F;                  //波特率不加倍   
 30     }   
 31     EA = 1;   
 32     ES = 1;                            //充许串口1中断  
 33     
 34 }    
 35 void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod)   
 36 {   
 37                                        //S2SM0  S2SM1   S2SM2   S2REN   S2TB8   S2RB8   S2TI     S2RI   
 38     S2CON |= 0x50;                     //串口2,方式1,接收充许      
 39     BRT = RELOAD;   
 40     if (timeMod == 1)                  //1T   
 41     {   
 42                                        //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS   
 43         AUXR |= 0x14;                  //串口1使用独立波特率发生器,独立波特率发生器1T   
 44     }   
 45     else                               //12T   
 46     {   
 47         AUXR = (AUXR | 0x10) & 0xFB;   
 48     }   
 49    
 50     if (doubleBaud == 1)   
 51     {   
 52         AUXR |= 0x08;                   //波特率加倍   
 53     }   
 54     else   
 55     {   
 56         AUXR &= 0xF7;                   //波特率不加倍   
 57     }   
 58        AUXR1 &= 0xef;                        //串口2在P1口通信
 59     EA = 1;                             //-   -   -    -    -    -     ESPI    ES2   
 60     IE2 |= 0x01;                        //充许串口2中断              
 61 }     
 62 void UART1_SendOneChar(uchar val)   
 63 {   
 64     //ES = 0;                           //关闭串口1中断   
 65    
 66     SBUF = val;   
 67     while(TI == 0);   
 68     TI = 0;   
 69    
 70     //ES = 1;                           //恢复串口1中断   
 71 }                             
 72 void UART1_Send_data(uchar str[],uchar len)   
 73 {   
 74     uchar i =0;
 75     for(;i<len;i++)   
 76     {   
 77         UART1_SendOneChar(str[i]);   
 78     }   
 79 }      
 80 //void UART1_SendStr(uchar *str)   
 81 //{   
 82 //    while( (*str)!=\'\0\' )   
 83 //    {   
 84 //        UART1_SendOneChar(*str);   
 85 //        str++;   
 86 //    }   
 87 //}  
 88 void UART1_SendOneByte(uchar val)   //发送1byte数据
 89 {   
 90     //ES = 0;                           //关闭串口1中断   
 91    
 92     SBUF = val;   
 93     while(TI == 0);   
 94     TI = 0;   
 95    
 96     //ES = 1;                           //恢复串口1中断   
 97 }  
 98 void UART2_SendOneChar(uchar val)   
 99 {   
100     //IE2 = 0x00;                       //关闭串口2中断      
101     S2BUF = val;       
102     while ((S2CON & 0x02) == 0);   
103     S2CON &= 0xFD;      
104     //IE2 = 0x01;                        //恢复串口2中断   
105 }        
106 void UART2_SendStr(uchar *str)   
107 {   
108     while( (*str)!=\'\0\' )   
109     {   
110         UART2_SendOneChar(*str);   
111         str++;   
112     }   
113 }   
114 void UART2_Send_data(uchar str[],uchar len)   
115 {   
116     uchar i =0;
117     for(;i<len;i++)   
118     {   
119         UART2_SendOneChar(str[i]);   
120     }   
121 }  
122 //重写putchar函数
123 uchar putchar(uchar c)
124 {
125     UART1_SendOneByte(c);
126     return c;
127 }
128 //void UART_2SendOneByte(uchar val)
129 //{
130 //    S2BUF = val;
131 //    while(!(S2CON&S2TI));  //若S2TI=0,在此等待
132 //    S2CON&=~S2TI;      //S2TI=0
133 //}
134   
135 //void UART2_Senddat(uchar str[],uchar len)   
136 //{   
137 //    uchar i =0;
138 //    for(;i<len;i++)   
139 //    {   
140 //        UART_2SendOneByte(str[i]);
141 //        //UART1_SendOneByte(str[i]);           
142 //    }   
143 //} 
144 //void UART2_Int(void) interrupt 8   
145 //{   
146 
147 //    if ((S2CON & 0x01) == 1)   
148 //    {   
149 //        S2CON &= 0xFE;   
150 //        //PM_ReData[count] = S2BUF;
151 //  
152 //    }      
153 //}  
154 /************串口1中断*************/
155 void UART_1Interrupt(void) interrupt 4
156 {
157     if(RI==1)
158         {
159             RI=0;
160             flag1=1;
161             temp1=SBUF;
162         }
163 }
  1 #include "illumination.h"
  2 #include "12864.h"
  3 #include "UART.H"  
  4 extern uint guangzhao=0;
  5 void Init_guangzhao(void)
  6 {
  7    //初始化BH1750,根据需要请参考pdf进行修改****
  8    Write_guangzhao(0x01);  
  9 }
 10 
 11 void  Write_guangzhao(uchar Address)               //单个写入数据
 12 {
 13     guangzhao_Start();                  //起始信号
 14     BH1750_SendByte(SlaveAdd);   //发送设备地址+写信号
 15     BH1750_SendByte(Address);    //内部寄存器地址,请参考中文pdf22页 
 16   //BH1750_SendByte(REG_data);       //内部寄存器数据,请参考中文pdf22页 
 17     guangzhao_Stop();    
 18 }
 19 /*********************************************************
 20 *函数名         :  read_BH1750_data
 21 *函数功能       :连续读出BH1750内部数据
 22 *输入           :
 23 *输出           :
 24 *********************************************************/
 25 void read_BH1750_data(uchar Data[])
 26 {   uchar i;    
 27     guangzhao_Start();                          //起始信号
 28     BH1750_SendByte(SlaveAdd+1);         //发送设备地址+读信号
 29     
 30      for (i=0; i<3; i++)                     //连续读取6个地址数据,存储中Data
 31     {
 32         Data[i] = RecvByte();          //Data[0]存储0x32地址中的数据
 33         if (i == 3)
 34         {
 35 
 36            guangzhao_SendACK(1);                //最后一个数据需要回NOACK
 37         }
 38         else
 39         {        
 40            guangzhao_SendACK(0);                 //回应ACK
 41         }
 42      }
 43      guangzhao_Stop();                           //停止信号
 44      delay_nMs(5);
 45 }
 46 /****************************************************************************
 47 *函数名      :guangzhao_Start
 48 *函数功能    :起始信号
 49 *输入        :无
 50 *输出        :无
 51 *****************************************************************************/
 52 void guangzhao_Start()
 53 {
 54     SDA = 1;                    //拉高数据线
 55     SCL = 1;                    //拉高时钟线
 56     delay_5us(1);                //延时
 57     SDA = 0;                    //产生下降沿
 58     delay_5us(1);                //延时
 59     SCL = 0;                    //拉低时钟线
 60 }
 61 /*****************************************************************************
 62 *函数名      :guangzhao_Stop
 63 *函数功能    :停止信号
 64 *输入        :无
 65 *输出        :无
 66 *****************************************************************************/
 67 void guangzhao_Stop()
 68 {
 69     SDA = 0;                    //拉低数据线
 70     SCL = 1;                    //拉高时钟线
 71     delay_5us(1);                //延时
 72     SDA = 1;                    //产生上升沿
 73     delay_5us(1);                //延时
 74 }
 75 
 76 /*****************************************************************************
 77 *函数名       :guangzhao_SendACK
 78 *函数功能     :发送应答信号
 79 *输入         :入口参数:ack (0:ACK 1:NAK)
 80 *输出         :无
 81 ******************************************************************************/
 82 void guangzhao_SendACK(bit ack)
 83 {
 84     SDA = ack;                  //写应答信号
 85     SCL = 1;                    //拉高时钟线
 86     delay_5us(1);                //延时
 87     SCL = 0;                    //拉低时钟线
 88     delay_5us(1);                 //延时
 89 }
 90 /****************************************************************************
 91 *函数名:      :BH1750_RecvACK
 92 *函数功能     :接收应答信号
 93 *输入         :无
 94 *输出         :bit
 95 *****************************************************************************/
 96 bit BH1750_RecvACK()
 97 {
 98     SCL = 1;                    //拉高时钟线
 99     delay_5us(1);                //延时
100     CY = SDA;                   //读应答信号
101     SCL = 0;                    //拉低时钟线
102     delay_5us(1);                  //延时
103 
104     return CY;
105 }
106 /*****************************************************************************
107 *函数名        :BH1750_SendByte
108 *函数功能      :向IIC总线发送一个字节数据
109 *输入          :BYTE dat
110 *输出          :无
111 ******************************************************************************/
112 void BH1750_SendByte(BYTE dat)
113 {
114     BYTE i;
115     for (i=0; i<8; i++)         //8位计数器
116     {
117         dat <<= 1;              //移出数据的最高位
118         SDA = CY;               //送数据口
119         SCL = 1;                //拉高时钟线
120         delay_5us(1);            //延时
121         SCL = 0;                //拉低时钟线
122         delay_5us(1);            //延时
123     }
124     BH1750_RecvACK();
125 }
126 /*******************************************************************************
127 *函数名         :RecvByte
128 *函数功能       :从IIC总线接收一个字节数据
129 *输入           :无
130 *输出            :BYTE
131 ********************************************************************************/
132 BYTE RecvByte()
133 {
134     BYTE i;
135     BYTE dat = 0;
136 
137     SDA = 1;                    //使能内部上拉,准备读取数据,
138     for (i=0; i<8; i++)         //8位计数器
139     {
140         dat <<= 1;
141         SCL = 1;                //拉高时钟线
142         delay_5us(1);            //延时
143         dat |= SDA;             //读数据               
144         SCL = 0;                //拉低时钟线
145         delay_5us(1);            //延时
146     }
147     return dat;
148 }
149 /********************************************************************************
150 *函数名        :Get_guangzhao
151 *函数功能      :获取光照数据
152 *输入          :无
153 *输出          :uint
154 *********************************************************************************/
155 uint Get_guangzhao(void)
156 {
157   uchar guangzhao_buf[8];
158   Write_guangzhao(0x01);      // power on
159   Write_guangzhao(0x10);      // H- resolution mode
160     delay_nMs(180); 
161     read_BH1750_data(guangzhao_buf); //采集传感器的数据
162     guangzhao = guangzhao_buf[0];
163     guangzhao = (guangzhao<<8) + guangzhao_buf[1];
164     guangzhao = (uint)(guangzhao/1.2);
165 
166     LCD_WriteCmd(0X93);
167     LCD_WriteData(guangzhao/10000+0x30);
168     LCD_WriteData(guangzhao%10000/1000+0x30);
169     LCD_WriteData(guangzhao%10000%1000/100+0x30);
170     LCD_WriteData(guangzhao%10000%1000%100/10+0x30);
171     LCD_WriteData(guangzhao%10+0x30);
172     return guangzhao;
173 }
174 
175 #endif
  1 #include "RhAndTemp.h"
  2 #include "12864.h"
  3 #include "delay.h"
  4 ////引脚定义
  5 sbit  Port = P2^0;
  6 //定义全局变量
  7 uint temp_buf ;
  8 uint Hum_buf;
  9 //温湿度采集标志位
 10 uchar  FLAG;
 11 uchar  U8count,temp;
 12 uchar  TX_data_H,TX_data_L,RH_TX_data_H,RH_TX_data_L,checkdata;
 13 uchar  TX_data_H_temp,TX_data_L_temp,RH_TX_data_H_temp,RH_TX_data_L_temp,checkdata_temp;
 14 uchar  recivedatadata;
 15 /*************************************************************************************************
 16 *函数名     :dispaly_tempAndHumidty
 17 *函数功能   :在LCD显示温湿度
 18 *输出       :uint humidity,uint temp
 19 *输入       :无
 20 *************************************************************************************************/
 21 void Dispaly_tempAndHumidty(uint humidity,uint temp)
 22 {
 23      //显示湿度
 24       LCD_WriteCmd(0x8b);
 25      LCD_WriteData(humidity/100+0x30);
 26      LCD_WriteData(humidity%100/10+0x30);
 27      LCD_WriteData(\'.\');
 28      LCD_WriteData(humidity%10+0x30);
 29 
 30      //显示温度
 31     
 32      LCD_WriteCmd(0x9b);
 33      LCD_WriteData(temp/100+0x30);
 34      LCD_WriteData(temp%100/10+0x30);
 35      LCD_WriteData(\'.\');
 36      LCD_WriteData(temp%10+0x30);   
 37 } 
 38 void  recivedata(void)
 39       {
 40        uchar i;       
 41        for(i=0;i<8;i++)       
 42         {        
 43                FLAG=2;    
 44            while((!Port)&&FLAG++);
 45          Delay10us();Delay10us();Delay10us();Delay10us(); Delay10us();
 46               temp=0;
 47          if(Port)temp=1;
 48             FLAG=2;
 49          while((Port)&&FLAG++);
 50            //超时则跳出for循环          
 51             if(FLAG==1)break;
 52            //判断数据位是0还是1                   
 53         // 如果高电平高过预定0高电平值则数据位为 1             
 54            recivedatadata<<=1;
 55               recivedatadata|=temp;                    //0
 56          }
 57        
 58     }
 59 
 60     //--------------------------------
 61     //-----湿度读取子程序 ------------
 62     //--------------------------------
 63     //----以下变量均为全局变量--------
 64     //----温度高8位== TX_data_H------
 65     //----温度低8位== TX_data_L------
 66     //----湿度高8位== RH_TX_data_H-----
 67     //----湿度低8位== RH_TX_data_L-----
 68     //----校验 8位 == checkdata-----
 69     //----调用相关子程序如下----------
 70     //---- Delay();, Delay_10us();,recivedata(); 
 71     //--------------------------------
 72 
 73 void RHAndTemp(void)
 74     {
 75       //主机拉低18ms 
 76        Port=0;
 77        Delay25ms();
 78        Port=1;
 79      //总线由上拉电阻拉高 主机延时20us    
 80        Delay50us();
 81      //主机设为输入 判断从机响应信号 
 82        Port=1;
 83      //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行      
 84        if(!Port)         //T !      
 85        {
 86        FLAG=2;
 87      //判断从机是否发出 80us 的低电平响应信号是否结束     
 88        while((!Port)&&FLAG++);
 89        FLAG=2;
 90      //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
 91        while((Port)&&FLAG++);
 92      //数据接收状态         
 93        recivedata();
 94        RH_TX_data_H_temp=recivedatadata;
 95        recivedata();
 96        RH_TX_data_L_temp=recivedatadata;
 97        recivedata();
 98        TX_data_H_temp=recivedatadata;
 99        recivedata();
100        TX_data_L_temp=recivedatadata;
101        recivedata();
102        checkdata_temp=recivedatadata;
103        Port=1;
104        //数据校验 
105      
106        temp=(TX_data_H_temp+TX_data_L_temp+RH_TX_data_H_temp+RH_TX_data_L_temp);
107        if(temp==checkdata_temp)
108            {
109                  RH_TX_data_H=RH_TX_data_H_temp;
110                  RH_TX_data_L=RH_TX_data_L_temp;
111               TX_data_H=TX_data_H_temp;
112                  TX_data_L=TX_data_L_temp;
113                  checkdata=checkdata_temp;
114 
115               Hum_buf =   (RH_TX_data_H<<8) + RH_TX_data_L;
116               temp_buf    =   (TX_data_H<<8)  + TX_data_L;
117            }
118          
119       }    
120        delay_1s(1);       
121 }
  • 未完待续。。。。。。。

版权声明:本文为Arvin824原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/arvin824/p/12192669.html