|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define KEY0_OK 1
#define KEY1_OK 2
#define KEY2_OK 3
#define KEY3_OK 4
sbit SENSER=P3^7;
sbit LCDE=P2^7; //enable bit,LCDE--P2.7
sbit RS=P2^6; //register select bit,RS--P2.6
sbit RW=P2^5; //read or write select bit,RW--P2.5
sbit BF=P0^7; //busy mark bit,BF--P0.7
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit KEY0=P1^4;
sbit KEY1=P1^5;
sbit KEY2=P1^6;
sbit KEY3=P1^7;
uchar TEM_ARR[10]=0;
uchar HELLO[7]="HELLO";
uchar WAIT[9]="WAITING";
uchar REC_BUF[20]=0;
uchar key_mark=0;
uchar key0_mark=0;
uchar record=1;
void Delay_1us_(uint temp);
void Delay_ms(uint i);
void Display_son(uchar *p,uchar temp);
void Display(uchar temp);
void manage_data(uint temp);
unsigned char BusyTest(void);
void Init_1602();
void Write_Com(uchar dictate);
void Write_Data(uchar Data);
void Key_detect();
void Reset_18b20();
void Write_byte_18b20_(uchar temp);
uchar Read_byte_18b20_();
uint Get_tem_18b20();
void Send_data(uchar dat);
void Send_arr(uchar arr_capacity,uchar *p);
void Send_com(uchar *p);
void Arring_initialize();
void Init_Timer();
void GSM_receive();
void main()
{
Init_Timer();
Init_1602();
Reset_18b20();
Arring_initialize();
while(1)
{
Key_detect();
GSM_receive();
Display(key_mark);
}
}
/************KEY DETECT FUNCTION*************************************************************************************/
void Key_detect()
{
if(KEY0==0)
{
Delay_1us_(10);
if(KEY0==0) //chose the one to be sended
{
if(key0_mark<2)
key0_mark++;
else
key0_mark=0;
LED0=0;
key_mark=KEY0_OK;
}
while(!KEY0);
LED0=1;
}
if(KEY1==0) //affirm send samething
{
Delay_1us_(10);
if(KEY1==0)
{
key_mark=KEY1_OK;
LED1=0;
}
while(!KEY1);
LED1=1;
}
if(KEY2==0) //display temperature
{
Delay_1us_(10);
if(KEY2==0)
{
key_mark=KEY2_OK;
LED2=0;
}
while(!KEY2);
LED2=1;
}
if(KEY3==0) //transform the model of send
{
Delay_1us_(10);
if(KEY3==0)
{
key_mark=KEY3_OK;
LED3=0;
}
while(!KEY3);
LED3=1;
}
}
/***********************temperature**************************************************************************************/
/**************************************************
*function: 18b20 reset
*parameter: none
*return: none
*modify: none
************************************************/
void Reset_18b20()
{
SENSER=1;
SENSER=0;
Delay_1us_(55);
SENSER=1;
Delay_1us_(10);
Delay_1us_(1);
}
/**************************************************
*function: write one byte to 18b20
*parameter: uchar
*return: none
*modify: none
************************************************/
void Write_byte_18b20_(uchar temp)
{
uchar i;
for(i=0;i<8;i++)
{
SENSER=0;
_nop_();
_nop_();
SENSER=temp&0x01;
Delay_1us_(5);
SENSER=1;
temp>>=1;
}
}
/**************************************************
*function: read one byte from 18b20
*parameter: none
*return: byte
*modify: none
************************************************/
uchar Read_byte_18b20_()
{
uchar i,flag=0;
for(i=0;i<8;i++)
{
SENSER=0;
flag>>=1;
SENSER=1;
if(SENSER)
flag|=0x80;
Delay_1us_(6);
}
return flag;
}
/**************************************************
*function: get temperture from 18b20
*parameter: none
*return: uint temperture
*modify: flag_h/l
************************************************/
uint Get_tem_18b20()
{
uchar flag_l=0,flag_h=0; //record temperture high and low byte
uint flag;
ES=0;
TR0=0;
Reset_18b20();
Write_byte_18b20_(0xcc); //skip rom
Write_byte_18b20_(0x7f); //set 11 bit (0.125)
Reset_18b20();
Delay_1us_(1);
Write_byte_18b20_(0xcc); //skip serial number check
Write_byte_18b20_(0x44); //transport temperture
Delay_1us_(50000);
Delay_1us_(50000);
Reset_18b20();
Delay_1us_(1);
Write_byte_18b20_(0xcc); //skip serial number check
Write_byte_18b20_(0xbe); //read command
Delay_1us_(1);
flag_l=Read_byte_18b20_(); // read low byte
flag_h=Read_byte_18b20_(); //read high byte
flag=flag_h*256+flag_l;
ES=1;
TR0=1;
return flag;
}
/**********************display*****************************************************************************************/
/**************************************************
*function: Delay 1 um
*parameter: uint
*return: none
*modify: none
************************************************/
void Delay_1us_(uint temp)
{
while(temp--);
}
/*毫秒延时*/
void Delay_ms(uint i)
{
unsigned int j;
for(;i>0;i--)
{
for(j=0;j<125;j++)
{;}
}
}
/*****************************
****serve for display()
******************************/
void Display_son(uchar *p,uchar temp)
{
uchar i;
ES=0;
Write_Com(0x80+1);
for(i=0;i<temp;i++)
{ if(p==0) break; Write_Data(p); }
Write_Com(0x80+0x40+1);
for(i=0;i<16;i++)
{
//if(REC_BUF==0) break;
Write_Data(REC_BUF);
}
ES=1;
record=1;
key_mark=0;
}
/*****************************
***head display function
********************************/
void Display(uchar temp)
{
uint temperature;
temperature=Get_tem_18b20();
switch(temp)
{
case KEY0_OK:
switch(key0_mark)
{
case 1: manage_data(temperature);
Display_son(TEM_ARR,6);
break;
case 2: Display_son(HELLO,6);
break;
}
break;
case KEY1_OK: //sending
switch(key0_mark)
{
case 1: Display_son(TEM_ARR,6);
Send_arr(sizeof(TEM_ARR),TEM_ARR);
//Send_data(0x0D);
//Send_data(0x0A);
//EA=1;
Delay_ms(1500);
//EA=0;
break;
case 2: Display_son(HELLO,6);
Send_arr(sizeof(HELLO),HELLO);
//Send_data(0x0D);
//Send_data(0x0A);
//EA=1;
Delay_ms(1500);
//EA=0;
break;
}
break;
case KEY2_OK:
manage_data(Get_tem_18b20());
Display_son(TEM_ARR,6);
break;
case KEY3_OK:break;
}
}
/******manage temperature data*********/
void manage_data(uint temp)
{
uchar container[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uchar hundred_R,ten_R,ge_R;
uint record;
record=(temp*0.125)*5;
hundred_R=record/1000;
ten_R=(record%1000)/100;
ge_R=(record%100)/10;
TEM_ARR[0]=container[hundred_R];
TEM_ARR[1]=container[ten_R];
TEM_ARR[2]=container[ge_R];
}
/*****************************************************
*function:check if busy
*parameter: none
*return:result result=1,busy;result=0,free
*modify: none
***************************************************/
unsigned char BusyTest(void)
{
bit result;
RS=0; //根据规定,RS为低电平,RW为高电平时,可以读状态
RW=1;
LCDE=1; //LCDE=1,才允许读写
Delay_1us_(5); //给硬件反应时间
result=BF; //将忙碌标志电平赋给result
LCDE=0;
return result;
}
/*****************************************************
*function:initialize 1602
*parameter: none
*return: none
*modify: none
***************************************************/
void Init_1602()
{
LCDE=0X00;
Write_Com(0x38);
Write_Com(0x0E);
Write_Com(0x06);
Write_Com(0x80);
}
/*****************************************************
函数功能:将模式设置指令或显示地址写入液晶模块
入口参数:dictate
***************************************************/
void Write_Com(uchar dictate)
{
while(BusyTest()==1); //如果忙就等待
RS=0; //根据规定,RS和RW同时为低电平时,可以写入指令
RW=0;
LCDE=0; //LCDE置低电平(根据表8-6,写指令时,LCDE为高脉冲,
// 就是让LCDE从0到1发生正跳变,所以应先置"0"
Delay_1us_(1); //给硬件反应时间
P0=dictate; //将数据送入P0口,即写入指令或地址
Delay_1us_(1); //给硬件反应时间
LCDE=1; //LCDE置高电平
Delay_1us_(1); //给硬件反应时间
LCDE=0; //当LCDE由高电平跳变成低电平时,液晶模块开始执行命令
}
/*****************************************************
函数功能:将数据(字符的标准ASCII码)写入液晶模块
入口参数:Data(为字符常量)
***************************************************/
void Write_Data(uchar Data)
{
while(BusyTest()==1);
RS=1; //RS为高电平,RW为低电平时,可以写入数据
RW=0;
LCDE=0; //LCDE置低电平(根据表8-6,写指令时,E为高脉冲,
// 就是让LCDE从0到1发生正跳变,所以应先置"0"
P0=Data; //将数据送入P0口,即将数据写入液晶模块
Delay_1us_(1); //给硬件反应时间
LCDE=1; //LCDE置高电平
Delay_1us_(1); //给硬件反应时间
LCDE=0; //当LCDE由高电平跳变成低电平时,液晶模块开始执行命令
}
/*******************send message**********************************************************************************************/
/***************
****mcu send data to sim300
***************************/
void Send_data(uchar dat) /*发送数据函数*/
{
SBUF =dat;
while(!TI);
TI = 0;
}
/**************************
****mcu send arr to sim300
***************************/
void Send_arr(uchar arr_capacity,uchar *p) /*发送数组函数*/
{
uchar m;
for(m=0;m<arr_capacity;m++)
Send_data(p[m]);
}
/****************************
***mcu send comend to sim300
********************
void Send_com(uchar *p) /*通过串口发送字符串
{
while(*p)
{
Send_data(*p);
p++;
}
Send_data(0x0D);
Send_data(0x0A);
}
/*********ARRING initialize****************/
void Arring_initialize()
{
uchar i=1;
for( i=0;i<20;i++)
REC_BUF=0;
for( i=0;i<3;i++)
TEM_ARR=0;
//HELLO[]="HELLO";
//WAIT[]="WAITING";
for(i=3;i<9;i++)
TEM_ARR=0x20;
REC_BUF[1]='A';
}
/******************************
中断的初始化
****************************/
void Init_Timer()
{
TMOD=0X21; //定时器0定时模式工作方式1
TH0=(65536-912)/256;
TL0=(65536-912)%256;
PCON=0x00; ///波特率不加倍
SCON=0x50; //串行工作方式1允许串行接收
TH1=0xFD; //实验板上晶振位11.0592MHZ,选择波特率为9600
TL1=0xFD; //在相应计数器上赋值
ES=1; //串行中断开启
TR1=1; //开启定时器1
ET0=1; //定时器0打开
TR0=1; //启动定时器0
EA=1; //总中断开启
}
/***********receive data********************************************************************************************************/
/*******************************
***mcu receive data from sim300
******************************/
void receive(void) interrupt 4/*通过中断,串口接收数据*/
{
if(RI)
{
REC_BUF[record]=SBUF;
record++;
RI=0;
}
}
/******************************/
void GSM_receive()
{
//EA=1;
//Delay_ms(6000);
//EA=0;
uint temperature=0;
uchar i=1;
if(REC_BUF[1]=='b')
{
temperature=Get_tem_18b20();
manage_data(temperature);
Send_arr(sizeof(TEM_ARR),TEM_ARR);
Display_son(TEM_ARR,8);
REC_BUF[1]=0;
// Send_data(0x0D);
// Send_data(0x0A);
}
else if(REC_BUF[1]!=0)
{
Display_son(HELLO,6);
REC_BUF[1]=0;
}
if(key_mark==4)
{
for( i=1;i<20;i++)
REC_BUF=0X20;
key_mark=0;
Display_son(WAIT,8);
}
} |
|