#include<reg52.h>
#include<string.h>
#include<intrins.h>
//**************************************************************************
#define uchar unsigned char
#define uint unsigned int
#define uchar unsigned char
#define uint unsigned int
uchar xdata getdata;
int xdata time;
sbit ST=P2^7;
sbit EOC=P2^3;
sbit OE=P2^2;
sbit P2_0=P2^0;
sbit E_CLK = P2^4; //clock input同步时钟输入端
sbit RW_SID = P2^5; // data input/output串行数据输入、输出端//
sbit RS_CS = P2^6 ; // chip select
//-------------------------------------
unsigned int tempL=0;
unsigned int tempH=0;
float temperature,tempdate;
sbit DQ = P1^0; //温度传感器
//-----------------------------------------
sbit EquipOne = P1^1; // 设备1
sbit EquipTwo = P1^2; // 设备2
sbit EquipThree = P1^3;// 设备3
sbit RedwornOne = P3^4;// 后门报警
sbit RedwornTwo = P3^4; //
sbit Bell = P1^6;//
sbit TC35iOpen = P1^7;// TC35i 点火
sbit P3_6 = P3^6;
sbit P3_7 = P3^7;
uchar xdata mgsOK[]="OK!";//
uchar xdata Homeworn[]="Home Gate Worning!";//
uchar xdata Meiqiworn[]="Coal Gas Worning!";//
char Tem[4];
char xdata Volt[4];
uchar xdata Telnum[12];
uchar xdata redata[40];
uchar xdata delchar,i;
uchar xdata zone,num2,datesbuf,k;// ,flagOK
bit havemess;
//bit flag;
//**************************************************************************
//延迟函数
void delayms(unsigned int time) //delay1
{
unsigned int n;
n=0;
while(n<time)
{n++;}
return;
}
void delays(uchar n) // void Delay11(uchar n)
{
uchar i=204;
while(n--)
{
while(i--);
}
}
void longdelay( unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<127;j++);
}
void delay(unsigned int n)
{
unsigned int i;
for(i=0; i<n; i++) {;}
}
/*********************************************************************/
//串行发送一字节数据
void SendByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
E_CLK=0;
if(dat&0x80)RW_SID=1;else RW_SID=0;
E_CLK=1;
dat=dat<<1;
}
}
//串行接收一字节数据
unsigned char ReceieveByte(void)
{
unsigned char i,d1,d2;
for(i=0;i<8;i++)
{
E_CLK=0;delay(100);
E_CLK=1;
if(RW_SID)d1++;
d1=d1<<1;
}
for(i=0;i<8;i++)
{
E_CLK=0;delay(100);
E_CLK=1;
if(RW_SID)d2++;
d2=d2<<1;
}
return (d1&0xF0+d2&0x0F);
}
//读取标志位BF
bit ReadBF(bit bf)
{
unsigned char dat;
SendByte(0xFA);//11111,01,0 RW=1,RS=0
dat=ReceieveByte();
if(dat>0x7F)bf=1;else bf=0;
return bf;
}
//写控制命令
void SendCMD(unsigned char dat)
{
SendByte(0xF8);//11111,00,0 RW=0,RS=0 同步标志
SendByte(dat&0xF0);//高四位
SendByte((dat&0x0F)<<4);//低四位
}
//写显示数据或单字节字符
void SendDat(unsigned char dat)
{
// while(ReadBF){;}
// RS_CS=1;
SendByte(0xFA);//11111,01,0 RW=0,RS=1
SendByte(dat&0xF0);//高四位
SendByte((dat&0x0F)<<4);//低四位
// RS_CS=0;
}
// 写汉字到LCD 指定的位置 x_add显示RAM的地址 dat1/dat2显示汉字编码
void display(unsigned char x_add,unsigned char dat1,unsigned char dat2){
SendCMD(x_add);//1xxx,xxxx 设定DDRAM 7位地址xxx,xxxx到地址计数器AC
SendDat(dat1);
SendDat(dat2);
}
//初始化 LCM
void initlcm(void)
{
SendCMD(0x30);//功能设置,一次送8位数据,基本指令集
SendCMD(0x0C);//0000,1100 整体显示,游标off,游标位置off
SendCMD(0x01);//0000,0001 清DDRAM
SendCMD(0x02);//0000,0010 DDRAM地址归位
SendCMD(0x80);//1000,0000 设定DDRAM 7位地址000,0000到地址计数器AC// SendCMD(0x04);//点设定,显示字符/光标从左到右移位,DDRAM地址加 一// SendCMD(0x0F);//显示设定,开显示,显示光标,当前显示位反白闪动
}
///---------------温度传感器驱动-------------------------////
Init_DS18B20(void)//ds1820 初始化子程序
{
float x=0;
DQ=1;
delayms(8);
DQ=0;
delayms(85);
DQ=1;
delayms(14);
x=DQ;
delayms(20);
}
//向DS18B20读一字节数据
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat=0;
for(i=8;i>0;i--)
{
DQ=1;
delayms(1);
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delayms(4);
}
return(dat);
}
//向DS18B20写一字节数据
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delayms(5);
DQ=1;
dat>>=1;
}
delayms(4);
}
//向DS18B20读温度值
float ReadTemperature(void)
{
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0x44);
delayms(125);
Init_DS18B20();
WriteOneChar(0xcc);
WriteOneChar(0xbe);
tempL=ReadOneChar();
tempH=ReadOneChar();
temperature=((tempH*256)+tempL)*0.0625;
delayms(200);
return(temperature);
}
//=======================================================================================
////////////////////////////////////////////////////////////////
void sendATC(void) // 发送AT+C,应答
{
SBUF=65; //将ASCII码A发送到串口去
longdelay(20);
SBUF=84; // 将ASCII码T发送到串口去
longdelay(20);
SBUF=43; //;发送"+"
longdelay(20);
SBUF=67; //;发送C
longdelay(20);
}
// 从SIM卡中读取一条短消息
// AT指令:AT+CMGR=1<CR>
void read_msg(unsigned char imsg)
{
for(k=0;k<40;k++)
{
redata[k]=0;
}
SBUF='A'; longdelay(20);
SBUF='T'; longdelay(20);
SBUF='+'; longdelay(20);
SBUF='C'; longdelay(20);
SBUF='M'; longdelay(20);
SBUF='G'; longdelay(20);
SBUF='R'; longdelay(20);
SBUF='='; longdelay(20);
SBUF=imsg; longdelay(20);
SBUF = 0x0d; longdelay(20);
SBUF = 0x0a; longdelay(20);
}
void sendCNMI(void) //发送(AT+CNMI=1,1,0,0,1),短信到达TE; //( 设置短消息到达自动提示) ///
{ //// New SMS message storage(显示新收到的短消息)
sendATC();
SBUF=78; //发送N
longdelay(20);
SBUF=77; //发送M
longdelay(20);
SBUF=73; //发送I
longdelay(20);
SBUF=61; //发送"="
longdelay(20);
SBUF=49; //发送1
longdelay(20);
SBUF=44; //发送","
longdelay(20);
SBUF=49; //发送1
longdelay(20);
SBUF=44; //发送","
longdelay(20);
SBUF=48; //发送0
longdelay(20);
SBUF=44; //发送","
longdelay(20);
SBUF=48; //发送0
longdelay(20);
SBUF=44; //发送","
longdelay(20);
SBUF=49; //发送1
longdelay(20);
SBUF=13; //;发送"回车"指令!
longdelay(20);
SBUF=10; //;发送"换行符"指令!
longdelay(20);
}
//发送一个ASCII
void SendASC(uchar ASC)
{
bit es;
es=ES;//
ES=0;//关闭串行中断
TI=0;
SBUF=ASC;
while(!TI);
TI=0;
ES=es;
}
/****************************************************************************/
//发送AT命令到TC35I
void SendCmdToTc35i(uchar *p)
{
uchar len;
len=strlen(p); /*strlen 的计算是以 '\0' 作为结束判断标志,strlen 从 str[0] 开始搜索,
当恰好遇到 '\0' 结束。 */
//strlen( )函数是测试字符串长度的函数.函数的值为字符串中的实际长度,不包括'\0'在内
while(len--)
{
SendASC(*p++);
}
}
//删除sim卡内全部短信息
void EraseMsgAll()
{
SendCmdToTc35i("AT+CMGD=1");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=2");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=3");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=4");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=5");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=6");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=7");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdToTc35i("AT+CMGD=8");
SendASC(0x0d); SendASC(0x0a); delays(50);
SendCmdT