//串口部分 #include "uart.h"
uchar uart_num;
/**********************************
函数定义
**********************************/
/****
*******串口初始化函数
*****/
void Uart_Init(void)
{
EA=1; //打开总中断
ES=0;
TMOD|=0x20; //设置定时器1工作方式为方式2
TR1=1; //启动定时器1
TH1=0xfd; //波特率9600
TL1=0xfd;
SCON=0X50; //允许接收
}
/****
*******串口发送一个字节函数
*****/
void Uart_Sent_Char(uchar date)
{
SBUF=date;
while(!ti);
TI=0;
}
/****
*******串口发送字符串函数
*****/
void Uart_Sent_Str(uchar *date)
{
while(*date != '\0')
Uart_Sent_Char(*date++);
}
/****
******* 串口中断服务程序函数
*****/
void ser() interrupt 4
{
static uchar string[1]; //接收一位数据,如果接收两位数据,则string[2]
if(RI)
{
RI=0;
string[0]=SBUF;
switch(string[0])
{
case('1'): uart_num = 1 ;break;
case('2'): uart_num = 2 ;break;
default: break;
}
}
}
//main void main() { while(DHT11_Init()); //DHT11初始化 Delay_function(10); //延时10ms Lcd1602_Init(); //LCD1602初始化 Delay_function(10); //延时10ms lcd1602_clean(); //清屏 Delay_function(10); //延时10ms Ds1302_Init(); //时间芯片初始化 Delay_function(10); //延时10ms Init_timer(); //串口初始化 Uart_Init(); //定时器初始化 IR_per = 1; // ds1302_write_time(); //向DS302写入时钟数据,只在第一次烧录时开启 while(1) { Key_function(); //按键函数 Monitor_function(); //监测函数 display_function(); //显示函数 Manage_function(); //处理函数
Delay_function(1); //延时10ms time_num++; //计时变量+1 if(time_num > 100) { sprintf(send_buf,"温度:%.1f%℃",temp/10.0); //发送温度数据采用浮点型 Uart_Sent_Str(send_buf); sprintf(send_buf,"湿度:%.1f%%",humi/10.0); //发送湿度数据采用浮点型 Uart_Sent_Str(send_buf); sprintf(send_buf,"蓝牙标记:%d",uart_num); //发送湿度数据采用浮点型 Uart_Sent_Str(send_buf); time_num = 0; } } }
|