标题:
基于STC89C52的NRF24L01无线温度传输系统程序设计
[打印本页]
作者:
ysc199908
时间:
2019-12-3 21:58
标题:
基于STC89C52的NRF24L01无线温度传输系统程序设计
基于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 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
red_R
时间:
2019-12-9 15:16
感谢楼主的分享
作者:
lhqsbz
时间:
2021-8-16 15:57
不错不错,顶一个,正常准备做一个这样的东西来用用。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1