基于STC89C52單片機,使用DS18B20數字溫度傳感器,通過NRF24L01無線模塊傳輸溫度數據的系統設計。
單片機源程序如下:
- /*
- STA
- */
- #include "reg52.h"
- #include "lcd1602.h"
- #include <stdio.h>
- #include"temp.h"
- #include "intrins.h"
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- #define FOSC 11059200L //System frequency
- #define BAUD 9600 //UART baudrate
- /*Define UART parity mode*/
- #define NONE_PARITY 0 //None parity
- #define ODD_PARITY 1 //Odd parity
- #define EVEN_PARITY 2 //Even parity
- #define MARK_PARITY 3 //Mark parity
- #define SPACE_PARITY 4 //Space parity
- #define PARITYBIT NONE_PARITY //Testing even parity
- bit busy;
- char cStr[50] ;
- char Rec,xx=0;
- char Re[10];
- void SendData(BYTE dat);
- void SendString(char *s);
- void Delay2000ms(void);
- void Delay200ms(void);
- void USART_Init(void)
- {
- #if (PARITYBIT == NONE_PARITY)
- SCON = 0x50; //8-bit variable UART
- #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
- SCON = 0xda; //9-bit variable UART, parity bit initial to 1
- #elif (PARITYBIT == SPACE_PARITY)
- SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
- #endif
-
- TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
- TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
- TR1 = 1; //Timer1 start run
- ES = 1; //Enable UART interrupt
- EA = 1; //Open master interrupt switch
- }
- /*
- SendString("AT+CWMODE=3\r\n");
- Delay2000ms();
- SendString("AT+RST\r\n");
- Delay2000ms();
- SendString("AAT+CIPMUX=1\r\n");
- Delay2000ms();
- SendString("AT+CIPSERVER=1,8089\r\n");
- Delay2000ms();
- */
-
- void main()
- {
-
- unsigned char i = 1;
- int temp = 0;
- float tp;
- unsigned char aa[10];
- USART_Init();
- LCD1602_Init();
-
- Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();
- sprintf(cStr,"AT+CIPSTART=\"%s\",\"%s\",%s\r\n","TCP","192.168.4.1","8089"); //端口號字符串
- SendString(cStr);
- Delay2000ms();
- SendString("AT+CIPMODE=1\r\n");
- Delay2000ms();
- SendString("AT+CIPSEND\r\n");
- Delay2000ms();
- while(1)
- {
- temp = DS18B20_1_GetTemp();
- tp=temp; //如果溫度是正的那么,那么正數的原碼就是補碼它本身
-
- temp=tp*0.0625*100+0.5;
- // sprintf(aa,"%d\r\n",i);
- sprintf(aa,"%d\r\n",temp);
- SendString(aa);
- // SendString(aa);
- Delay200ms();
- i++;
- if(i>50) i=1;
- }
- }
- /*----------------------------
- UART interrupt service routine
- ----------------------------*/
- void Uart_Isr() interrupt 4
- {
- if (RI)
- {
- RI = 0; //Clear receive interrupt flag
- Re[xx] = SBUF; //P0 show UART data
- xx++;
- if((Re[xx-1]=='\n'&&Re[xx-2]=='\r')|xx == 10)
- {
- LCD1602_DispString(1,0," ");
- xx=0;
- LCD1602_DispString(1,0,Re);
- }
- }
- if (TI)
- {
- TI = 0; //Clear transmit interrupt flag
- busy = 0; //Clear transmit busy flag
- }
- }
- /*----------------------------
- Send a byte data to UART
- Input: dat (data to be sent)
- Output:None
- ----------------------------*/
- void SendData(BYTE dat)
- {
- while (busy); //Wait for the completion of the previous data is sent
- ACC = dat; //Calculate the even parity bit P (PSW.0)
- if (P) //Set the parity bit according to P
- {
- #if (PARITYBIT == ODD_PARITY)
- TB8 = 0; //Set parity bit to 0
- #elif (PARITYBIT == EVEN_PARITY)
- TB8 = 1; //Set parity bit to 1
- #endif
- }
- else
- {
- #if (PARITYBIT == ODD_PARITY)
- TB8 = 1; //Set parity bit to 1
- #elif (PARITYBIT == EVEN_PARITY)
- TB8 = 0; //Set parity bit to 0
- #endif
- }
- busy = 1;
- SBUF = ACC; //Send data to UART buffer
- }
- /*----------------------------
- Send a string to UART
- Input: s (address of string)
- Output:None
- ----------------------------*/
- void SendString(char *s)
- {
- while (*s) //Check the end of the string
- {
- SendData(*s++); //Send current char and increment string ptr
- }
- }
- void Delay2000ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- i = 15;
- j = 2;
- k = 235;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void Delay200ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- i = 2;
- j = 103;
- k = 147;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
復制代碼
所有資料51hei提供下載:
基于STC89C52的無線溫度傳輸系統.rar
(100.79 KB, 下載次數: 100)
2019-12-3 21:58 上傳
點擊文件名下載附件
|