stc15f2k60s2与DHT11 将DHT11采集到的温湿度通过串口发送 为什么是乱码
2019-07-15 08:44发布
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
stc15f2k60s2与DHT11 将DHT11采集到的温湿度通过串口发送 为什么是乱码 程序如下
#include <STC15F2K60S2.H>
#include <intrins.h>
#include <stdio.h>
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned char u8;
#define uint unsigned int
#define uchar unsigned char
#define FOSC 11059200L //
#define BAUD 115200 //
sbit io=P2^0;
bit busy;
u8 datas=0;
uchar data_byte;
uchar RH,TH,RL,TL,T_H,T_L,R_H,R_L,check,num_check,flag,a,R,T;
void delay(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<100;i++);
}
void delay1()
{
uchar i;
for(i=0;i<1;i++);
}
void start()
{
io=1;
delay1();
io=0;
delay(25);
io=1;
delay1();
delay1();
delay1();
}
uchar receive_byte()
{
uchar i,temp;
for(i=0;i<8;i++)
{
while(!io);
delay1();
delay1();
delay1();
temp=0;
if(io==1)
temp=1;
while(io);
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
u8 receive()
{
uchar i;
start();
io=1;
if(!io)
{
while(!io);
while(io);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;
for(i=0;i<7;i++)
delay1();
io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)
{
RH=R_H;
RL=R_L;
R=RH+RL;
TH=T_H;
TL=T_L;
T=TH+TL;
check=num_check;
}
}
return RH;
}
u8 receive1()
{
uchar i;
start();
io=1;
if(!io)
{
while(!io);
while(io);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;
for(i=0;i<7;i++)
delay1();
io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)
{
RH=R_H;
RL=R_L;
R=RH+RL;
TH=T_H;
TL=T_L;
T=TH+TL;
check=num_check;
}
}
return TH;
}
void init()
{
AUXR = 0x40;
TMOD = 0x20;
TL1 = (65536- (FOSC/32/BAUD));
TH1 = (65536 - (FOSC/32/BAUD));
TR1 = 1;
ES = 1;
EA = 1;
}
void main()
{
init();//串口初始化
io = 1;
while(1)
{
datas=receive();
datas=T;
SendData(datas%100/10+'0');//十位
SendData(datas%10+'0'); //个位
datas=receive1();
datas=R;
SendData(datas%100/10+'0');//十位
SendData(datas%10+'0'); //个位
}
}
void Uart() interrupt 4 using 1
{
if (RI)
{
RI = 0; //清除RI位
P0 = SBUF; //P0显示串口数据
P22 = RB8; //P2.2显示校验位
}
if (ti)
{
TI = 0; //清除TI位
//清忙标志
}
}
一周热门 更多>