******主程序
#include <string.h>
#include "Lcd12864.h"
#include "Lcd1602.h"
#include "PWM.h"
#include "config.h"
#include <ds18b20.h>
#include "ds1302.h"
#include "eeprom.h"
#include "uart.h"
#include "key.h"
#include "ad.h"
#include "Time.h"
#include "BH1750.h"
#include "PulseSensor.h"
#include "hcsr501.h"
#include "dht11.h"
extern bit Alarmflag;
bit flagstar = 0;
extern bit buzzerflag;
extern unsigned char set_temper;
extern unsigned char step;
extern struct sTime bufTime; //日期时间缓冲区
extern bit irflag; //红外接收标志,收到一帧正确数据后置1
extern unsigned char RH,RL,TH,TL;
extern unsigned char PulsePin; // Pulse Sensor purple wire connected to analog pin 0(P1.0为传感器输入口)
void InitTimer0(void);
unsigned int cnt_i=0;
bit staMute = 0; //静音标志位
extern unsigned char AlarmHour; //闹钟时间的小时数
extern unsigned char AlarmMin; //闹钟时间的分钟数
unsigned int temper;
signed int rout; // PID Response (Output)
signed int rin; // PID Feedback (Input)
signed char counter=0;
unsigned char number=0;
unsigned char str[30];
extern unsigned char falgstep;
void InitTimer1(void)
{
TMOD&= 0x0F;
TMOD|= 0x10;
TH1 = 0xD4;
TL1 = 0xCD;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void Sys_Init()
{
InitTimer1(); //AD初始化
Uart2Init(0,1,220);//初始化串口,设置波特率9600
Uart1Init(0,1,220);//初始化串口,设置波特率9600
}
void main(void)
{
EA=1;
Sys_Init(); //系统初始化
while(1)
{
if(step==0)
{
UartAction();
}
}
}
extern unsigned char i;
void InterruptTimer1() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x66;
}
********uart.c
#include "config.h"
#include "uart.h"
#include "string.h"
#define Buf_Max 100 //缓存长度,这个长度的最大值要根据单片机型号和程序的实际情况来,我的这个100几乎是实际情况的最大限度了
unsigned char Rec_Buf[Buf_Max]; //定义缓存数组
unsigned char Rec_BufU2[Buf_Max]; //定义缓存数组
unsigned char i=0;
unsigned char a=0; //定义缓存指针
unsigned char bufRxd[100];
unsigned char cntRxd = 0;
bit Uart1_flagFrame = 0; //设置帧接收完成标志
bit Uart2_flagFrame = 0; //设置帧接收完成标志
char Uart1Init(char smod,char brtx12,unsigned char reload)
{
SCON = 0X50;//8位可变波特率,无奇偶位(SM0=0,SM1=1),使能串口接收模块(REN=1)
BRT = reload;//设置独立波特率发生器波特率
if(smod == 1)
{
PCON |= SMOD; //SMOD = 1;//波特率倍速位
}
else if(smod == 0)
{
PCON &= (~SMOD); //SMOD = 0;//取消波特率倍速位
}
else
{
return -1;
}
if(brtx12 == 1)
{
AUXR |= BRTx12;//BRTx12 = 1;1T模式
}
else if(brtx12 == 0)
{
AUXR &= (~BRTx12);//BRTx12 = 0;12T模式
}
else
{
return -2;
}
AUXR |= S1BRS;//串口1设置为使用独立波特率发生器
AUXR |= BRTR;//开启波特率发生器
ES = 1; //开串口中断
EA = 1; //开总中断
return 0;
}
char Uart2Init(char s2smod,char brtx12,unsigned char reload)
{
S2CON = 0X50;//8位可变波特率,无奇偶位
BRT = reload;//设置独立波特率发生器波特率
if(s2smod == 1)
{
AUXR |= S2SMOD; //S2SMOD = 1;//波特率倍速位
}
else if(s2smod == 0)
{
AUXR &= (~S2SMOD); //S2SMOD = 0;//取消波特率倍速位
}
else
{
return -1;
}
if(brtx12 == 1)
{
AUXR |= BRTx12;//BRTx12 = 1;1T模式
}
else if(brtx12 == 0)
{
AUXR &= (~BRTx12);//BRTx12 = 0;12T模式
}
else
{
return -2;
}
AUXR |= BRTR;//开启波特率发生器
IE2 |= ES2;// ES2 = 1; //允许串口2中断
EA = 1; //开总中断
return 0;
}
//void Uart1Send(char i)
//{
// TI = 0; //清空发送完中断请求标志位
// SBUF = i; //将数据放入寄存器发送
// while(TI == 0);//等待发送完毕,发送完毕 TI == 1
// TI = 0; //清空发送完中断请求标志位
//}
//void Uart1Sends(char* at)
//{
//// while(*at != '\0') //判断一串数据是否结束
// {
// SBUF=*(at++); //发送数据
// while(TI==0); //查询发送是否结束
// TI=0; //清除发送一标志位
// }
//}
void CLR_Buf(void)
{
unsigned char k;
for(k=0;k<Buf_Max;k++) //将缓存内容清零
{
Rec_Buf[k] = 0;
}
i = 0; //接收字符串的起始存储位置
}
//void CLR_BufU2(void)
//{
// unsigned char k;
// for(k=0;k<Buf_Max;k++) //将缓存内容清零
// {
// Rec_BufU2[k] = 0;
// }
// a = 0; //接收字符串的起始存储位置
//}
//bit Hand(unsigned char *b)
//{
// if(strstr(Rec_Buf,b)!=NULL)
// return 1;
// else
// return 0;
//}
//bit HandU2(unsigned char *b)
//{
// if(strstr(Rec_BufU2,b)!=NULL)
// return 1;
// else
// return 0;
//}
extern bit flagrxt;
void SerialUart1() interrupt 4
{
ES=0;
if(RI) //如果是接送中断,则进行处理
{
RI = 0; //清除接收中断标志位
Rec_Buf[i] = SBUF; //将接收到的字符串存到缓存中
i++;
}
if (TI) //字节发送完毕
{
TI = 0; //清零发送中断标志位
}
ES=1;
}
void Uart2Send(char i)
{
unsigned char temp = 0;
IE2 &= (~ES2);//关闭串口2中断//ES2 = 0;
S2CON &= (~S2TI);//S2TI = 0;
S2BUF = i;//装入数据
do
{
temp = S2CON;
temp = temp & 0x02;//temp = S2TI;
}while(temp == 0);//判断是否发送完毕
S2CON &= (~S2TI);//S2TI = 0;
IE2 |= ES2;//ES2 = 1;
}
//void Uart2Sends(char* data_at)
//{
//
// unsigned char cnt=0;
//
// IE2 &= (~ES2);//关闭串口2中断//ES2 = 0;
// S2CON &= (~S2TI);//S2TI = 0;
//
//
// while(*(data_at+cnt)) //判断一串数据是否结束
// {
// S2BUF = *(data_at+cnt);//装入数据
// while((S2CON & S2TI) == 0);
// S2CON &= (~S2TI);//S2TI = 0;
// cnt++;
//
// }
//
// S2CON &= (~S2TI);//S2TI = 0;
// IE2 |= ES2;//ES2 = 1;
//}
void InterruptUart2() interrupt 8
{
IE2 &= (~ES2);//关闭串口2中断//ES2 = 0;
if(S2CON & S2RI)
{
S2CON &= (~S2RI); //S2RI = 0;
Rec_BufU2[a]=S2BUF;
a++; //缓存指针向后移动
if(Rec_BufU2[a-2] == 0x0d && Rec_BufU2[a-1] == 0x0a && a >= 2)
{
Uart2_flagFrame=1;
}
if(a>100) //如果缓存满,将缓存指针指向缓存的首地址
{
a = 0;
}
}
IE2 |= ES2;
if(S2CON & S2TI)
{
S2CON &= (~S2TI); //S2TI = 0;
}
}
***********time.c
#include "time.h"
#include <string.h>
#include<string.h> //后面的一些函数要用到的头文件,如strstr等
#include <intrins.h>
#include "Lcd12864.h"
#include "Lcd1602.h"
//#include "PWM.h"
#include "config.h"
#include <ds18b20.h>
#include<stdio.h>
#include "eeprom.h"
#include "ds1302.h"
#include "uart.h"
#include "key.h"
#include "dht11.h"
#include "time.h"
#include "Infrared.h"
#include "Moto.h"
#include "BH1750.h"
#include "ad.h"
#include "hcsr501.h"
extern unsigned char str[];
extern bit flagstar;
unsigned char flagstep=0;
bit Alarmflag=0;
bit buzzerflag=0;
extern unsigned char i; //定义缓存指针
unsigned int AlarmHour = 26; //闹钟时间的小时数
unsigned char AlarmMin = 50; //闹钟时间的分钟数
unsigned char AlarmHour1 = 0x00; //闹钟时间的小时数
unsigned char AlarmMin1 = 0x00; //闹钟时间的分钟数
struct sTime bufTime; //日期时间缓冲区
extern signed char KeyCodeMap[4];
extern signed char KeySta[4];
extern bit Uart1_flagFrame ;
extern bit Uart2_flagFrame;
unsigned char step = 0;
extern unsigned char Rec_Buf[]; //定义缓存数组
extern unsigned char psec=0xAA; //秒备份,初值AA确保首次读取时间后会刷新显示
unsigned char str_eeprom[12];
unsigned char toch = 0;
double ADNum=0;
unsigned int Value,b; //定义两个变量
bit res = 0;
int intT,intT1,intT2,intT3,decT; //温度值的整数和小数部分
int temp = 0;
extern unsigned char RH,RL,TH,TL;
unsigned char str_TX[20];
unsigned char falgstep = 1;
unsigned int j;
unsigned int tempvlue = 0;
unsigned int dis_data=0;//BH1750定义
extern unsigned char BUF[8];
unsigned char lightflag=5;
extern signed char counter;
unsigned char str_TX[20];
extern unsigned int cnt_i;
void delay_Nms(unsigned int Ncount) //误差 -0.018084490741us
{
unsigned char a,b;
while(Ncount--)
{
for(b=18;b>0;b--)
for(a=149;a>0;a--);
}
}
char strtx1[8]={0x23,0x01,0x04,0x00,0x00,0x00,0x01,0x23};
char strtx2[8]={0x23,0x01,0x04,0x00,0x00,0x00,0x02,0x23};
char strtx3[8]={0x23,0x01,0x04,0x00,0x00,0x00,0x03,0x23};
char strtx4[8]={0x23,0x01,0x04,0x00,0x00,0x00,0x04,0x23};
extern unsigned char i;
unsigned char cnttx=0;
bit flagrxt=0;
void UartAction()
{
if(i!=0)
{
delay_Nms(5);
}
if(i==4)
{
if(Rec_Buf[0]==0x7E&&Rec_Buf[1]==0x41&&Rec_Buf[2]==0x03&&Rec_Buf[3]==0x0D)
{
for(cnttx=0;cnttx<8;cnttx++)
{
Uart2Send(strtx1[cnttx]);
}
led6=0;
led7=1;
}
else if(Rec_Buf[0]==0x7E&&Rec_Buf[1]==0x41&&Rec_Buf[2]==0x06&&Rec_Buf[3]==0x0D)
{
for(cnttx=0;cnttx<8;cnttx++)
{
Uart2Send(strtx2[cnttx]);
}
led6=0;
led7=1;
}
else if(Rec_Buf[0]==0x7E&&Rec_Buf[1]==0x41&&Rec_Buf[2]==0x09&&Rec_Buf[3]==0x0D)
{
for(cnttx=0;cnttx<8;cnttx++)
{
Uart2Send(strtx3[cnttx]);
}
led6=0;
led7=1;
}
else if(Rec_Buf[0]==0x7E&&Rec_Buf[1]==0x41&&Rec_Buf[2]==0x19&&Rec_Buf[3]==0x0D)
{
for(cnttx=0;cnttx<8;cnttx++)
{
Uart2Send(strtx4[cnttx]);
}
led6=0;
led7=1;
}else
{
led6=1;
led7=0;
i=0;
}
CLR_Buf();
i=0;
}
else if((i>=1&&i<=3)||i>=5)
{
led6=1;
led7=0;
i=0;
}
}
*********time.h
#ifndef _TIME_H
#define _TIME_H
void Showstep();
void InputSetStep();
void NumberUpAcktion();
void NumberDownAcktion();
void KeyAction(unsigned char keycode);
void AlarmMonitor();
void ShowBcdByte(unsigned char x, unsigned char y, unsigned char bcd);
void RefreshTimeShow();
void view();
void HC05Action();
void refreshTempHun();
void Alarm();
void refreshTemp();
void refreshTemp1();
void refreshTemp2();
void refreshTemp3();
void refleshhum();
void refreshDs1302();
void eepromreadbyte();
void refreshalarm();
void UartAction();
void AlarmMonitor();
void led2sta(unsigned char sta);
void led2sta(unsigned char sta);
void refreshvoice();
void refreshtouch();
void reflrshuartshow();
#endif
电脑通过串口助手发送指令收不到指定的指令, 还请有经验的大牛们不吝赐教,谢谢!
|