📄 hpjk05.c
字号:
#include "reg51f023.h" //the head file of c8051f023
#include <string.h> //the file that contains functions to deal with the character string
#include "user.h" //the head file that the user oneself defines
//the subprogram that clear XRAM of MCU
//the parameter n: the length of XRAM, the subprogram clears XRAM from 0x0000 to 0xn
void clear_xram(n)
unsigned int n;
{
unsigned int i;
pointer=0x000;
for (i=0;i<n;i++)
{
*pointer=0;
pointer++;
}
}
//the subprogram that initializes the pins concerned
void PORT_Init(void)
{
XBR0=0x04; // Enable UART0 (and SMB0 )
XBR2=0x44; // Enable UART1、crossbar and weak pull-ups
}
//the subprogram that initializes system clock
void SYSCLK_Init(void)
{
unsigned char i; // delay counter
OSCXCN=0x67; // start external oscillator with
// 11.0592MHz crystal
for (i=0;i<255;i++); // wait for XTLVLD to stabilize
while (!(OSCXCN&0x80)); // Wait for crystal osc. to settle
OSCICN=0x08; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//the delay subprogram
//the parameter n: it is used to setup the length of delay time
void Delay(n)
unsigned char n;
{
unsigned long i=0;
for(;i<100000*n;i++){}
}
//the subprogram that initializes the UART0 and UART1 of c8051f023
//NOTE: the interrupt isn't opened in the subprogram
//NOTE: both UART0 and UART1 interrupt set to high priority level
void UART_Init(void)
{
//*************there are some random values that enter into UART 1 after power up*************
Delay(1);
SCON1=SCON1&0xFE; //clear RI1
RI0=0; //clear RI0、RI1
TI0=0;
//********************************************************************************************
CKCON=0x40; // Timer1 and Timer4 use SYSCLK as time base
//UART1
SCON1=0x50; // SCON1: mode 1, 8-bit UART, enable RX
T4CON=0x34; // set T4 as baudrate maker for UART1
RCAP4=65536-(SYSCLK/BAUDRATE1/32);
//UART0
SCON=0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD=0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1=-(SYSCLK/BAUDRATE0/16); // set Timer1 reload value for baudrate
TR1=1; // start Timer1
CKCON=0x50; // Timer1 and Timer4 use SYSCLK as time base
PCON=0x80;
PS0=1;
EIP2=0x40;
}
//the subprogram initializes Timer2
//NOTE: the Timer2 interrupt set to low priority level
void T2_ini(void)
{
RCAP2=65536-11059200/(12*16);
TH2=RCAP2H;
TL2=RCAP2L;
TR2=1;
PT2=0;
}
//the timer subprogram
//the subprogram contains a alarm timer
void T2_ISR (void) interrupt 5 using 3
{
TF2=0;
time_subsecond++;
if(time_subsecond==16)
{
time_subsecond=0;
if (alarm_timer0==1)
{
alarm_timer0_unit--;
if (alarm_timer0_unit==0)
{
alarm_timer0=0;
alarm_timer0_ring=1;
}
}
}
}
//the subprogram that opens or closes GSM module
void GSM_sw(void)
{
unsigned long i=0;
ON_CON=0;
for(;i<100000;i++){}
ON_CON=1;
}
//the UART1 sending data subprogram
//the parameter n: the start address pointer of desired data
void gsm_message_send(n)
unsigned char n;
{
unsigned char j;
for (j=0;j<=n;j++)
{
SBUF1=gsm_comd_send[j];
while ((SCON1&0x02)==0) {}
SCON1=SCON1&0xFD;
}
}
//the subprogram that judges the status (open or close) of GSM module and contains the sequent processing
void GSM_on(void)
{
unsigned long i=0;
SCON1=SCON1&0xFE;
SBUF1='A';
while((SCON1&0x02)==0){}
SCON1=SCON1&0xFD;
SBUF1='T';
while((SCON1&0x02)==0){}
SCON1=SCON1&0xFD;
SBUF1='\x0D';
while((SCON1&0x02)==0){}
SCON1=SCON1&0xFD;
Delay(3);
if ((SCON1&0x01)==0)
GSM_sw();
SCON1=SCON1&0xFE;
}
//the subprogram that sends a short message to PCs, when MCU receives a short message from PCs (the control center)
//the short message to send to PCs contains the command code of the control center
//the globe variable *pointer contains the command code
void gsm_message_answer(void)
{
unsigned char i;
unsigned char temp1;
SCON1=SCON1&0xFC;
temp1=SBUF1;
temp1=SBUF1;
strcpy (gsm_comd_send,"at+cmgs=16\x0d");
for (i=0;gsm_comd_send[i]!='\x0d';i++)
{
SBUF1=gsm_comd_send[i];
while ((SCON1&0x02)==0) {}
SCON1=SCON1&0xFD;
}
SBUF1=gsm_comd_send[i];
while ((SCON1&0x02)==0) {}
SCON1=SCON1&0xFD;
for (i=0;i<100;i++)
{
while ((SCON1&0x01)==0) {}
gsm_comd_response[i]=SBUF1;
SCON1=SCON1&0xFE;
i--;
if (gsm_comd_response[i]=='\x3E')
{
i++;
if (gsm_comd_response[i]=='\x20')
break;
i--;
}
i++;
}
i=i+3;
temp1=i;
strcpy (gsm_comd_send,"0891683108200805F011F20D91683188603774F33108FF01");
gsm_comd_send[48]=*pointer;
gsm_comd_send[49]=*(pointer+1);
for (i=0;i<50;i++)
{
SBUF1=gsm_comd_send[i];
while ((SCON1&0x02)==0) {}
SCON1=SCON1&0xFD;
}
SBUF1='\x1A';
while ((SCON1&0x02)==0) {}
SCON1=SCON1&0xFD;
i=temp1;
for (;i<100;i++)
{
while ((SCON1&0x01)==0) {}
gsm_comd_response[i]=SBUF1;
SCON1=SCON1&0xFE;
i-=3;
if (gsm_comd_response[i]=='\x4F')
{
i++;
if (gsm_comd_response[i]=='\x4B')
break;
i--;
}
i+=3;
}
}
//the subprogram that enable GPS module to enter idle mode during initialization period or the end of observation
void stop_gps()
{
unsigned char i=0,temp;
strcpy(vp_comd_send,"@@Eg"); //@=40h E=45h g=67h
vp_comd_send[4]=0;
vp_comd_send[5]=0x22;
vp_comd_send[6]=0x0d;
vp_comd_send[7]=0x0a;
SBUF0=vp_comd_send[i];
i++;
for (;i<8;i++)
{
while(!TI0){}
TI0=0;
SBUF0=vp_comd_send[i];
}
while(!TI0){}
TI0=0;
Delay(2);
temp=SBUF0;
RI0=0;
temp=SBUF0;
RI0=0;
}
//the subprogram uses VP COMMAND (Aa) to check the start time of receiving gps data
void time_adjust(void)
{
unsigned char i=0,temp;
temp=SBUF0;
temp=SBUF0;
RI0=0;
TI0=0;
strcpy(vp_comd_send,"@@Aa\xFF\xFF\xFF\xDF\x0D\x0A");
SBUF0=vp_comd_send[i];
i++;
for (;i<10;i++)
{
while(!TI0){}
TI0=0;
SBUF0=vp_comd_send[i];
}
while(!TI0){}
TI0=0;
for(i=0;i<10;i++)
{
while(!RI0){}
RI0=0;
vp_comd_response[i]=SBUF0;
}
}
//the subprogram that clear the short message received
void delete_message(n)
unsigned char n;
{
unsigned char k,l;
strcpy (gsm_comd_send,"at+cmgd=");
gsm_comd_send[10]=0x0d;
if (n==50) {k=0;l=5;}
else if (n>=40) {k=n-40;l=4;}
else if (n>=30) {k=n-30;l=3;}
else if (n>=20) {k=n-20;l=2;}
else if (n>=10) {k=n-10;l=1;}
else {k=n;l=0;}
gsm_comd_send[8]=0x30+l;
gsm_comd_send[9]=0x30+k;
gsm_message_send(11);
Delay(1);
SCON1=SCON1&0xFE;
}
void pc_number_gain(pointer)
unsigned char *pointer;
{
unsigned char i,temp;
for (i=0;i<=10;i=i+2)
{
temp=*(pointer+i);
*(pointer+i)=*(pointer+i+1);
*(pointer+i+1)=temp;
}
}
unsigned char ascii_to_hex(void)
{
unsigned char temp,temp1,temp2;
temp1=(ascii[0]-0x30)*10;
temp2=(ascii[1]-0x30);
temp=temp1+temp2;
return (temp);
}
void read_message(void)
{
unsigned char i,j,temp;
unsigned char *temp_point;
unsigned char message_number[15];
temp=SBUF1;
temp=SBUF1;
SCON1=SCON1&0xFC;
for (i=0;i<200;i++)
gsm_comd_response[i]='\0';
strcpy (gsm_comd_send,"at+cmgl=\x30\x0d");
gsm_message_send(10);
pointer=gsm_comd_response;
alarm_timer0=1;
alarm_timer0_ring=0;
alarm_timer0_unit=10;
ET2=1;
EIE2=0x40;
EA=1;
for (;;)
{
if (alarm_timer0_ring==1)
{
pointer=gsm_comd_response;
break;
}
if (strstr(gsm_comd_response,"ERROR"))
{
pointer=gsm_comd_response;
break;
}
if (strstr(gsm_comd_response,"OK"))
break;
}
Delay(1);
EA=0;
ET2=0;
EIE2=0;
i=pointer-gsm_comd_response;
if (i>50)
{
temp_point=strchr(gsm_comd_response,',');
temp_point-=2;
if (*temp_point<0x30)
{
temp=*(temp_point+1)-0x30;
delete_message(temp);
}
else
{
ascii[0]=*temp_point;
ascii[1]=*(temp_point+1);
temp=ascii_to_hex();
delete_message(temp);
}
temp_point=strchr(gsm_comd_response,',');
pc_number_gain(temp_point+34);
for (j=0;j<11;j++)
message_number[j]=*(temp_point+34+j);
message_number[j]='\0';
if (strcmp(message_number,"13880673473")==0)
{
task=2;
ascii[0]=*(temp_point+68);
ascii[1]=*(temp_point+69);
pc_code=ascii_to_hex();
pointer_code=temp_point+70;
pointer=temp_point+68;
gsm_message_answer();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -