NB-IOT_BC95_B5底層代碼實現以及電路


  • 前言:時隔好幾個月一直在瞎忙,小白一個,抽空把之前做的還沒整完的補上,做事要有始有終,未雨綢繆。廢話不說上電路和代碼(代碼是基於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 }
  • 未完待續。。。。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM