⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carremotecontrolbygsm.c

📁 单片机PIC控制GSM发送SMS,然后控制汽车和提供地图
💻 C
字号:

//#include<pic.h>
#include <p18f44k20.h>
#include "interrupt.h"
#include "spi.h"
//#fuses HS,NOWDT,NOPROTECT,NOLVP
//#use delay(clock=20000000)
//PIC16F877A
// __CONFIG(0x1832);
//// __CONFIG(WDTDIS&PWRTEN&BORDIS&LVPDIS&PROTECT&HS)
#define bank2 
#define bank1
#define uchar unsigned char 
#define uint unsigned int

typedef struct BytePosition_struct
{
     uchar position;
     uchar value;

}BytePositionStruct;
  BytePositionStruct   ValuePosition;

/*v1.00
change back to loop ram 
*/
#define SIM_MEMORY_LEN 20
#define OVERFLOW 10




#define  INIT_AMOUNT   3
bank1 uint f_ack_gsm_init=0x3f;

bank1 uint f_ack_gsm_info=0;

bank1 uchar f_waiting_ack=0;
//bit0,1  flag of send "AT" to GSM 
//f_ack_gsm_init.0=0 mean sending data to GSM
//f_ack_gsm_init.1=0 mean get a ACK signal from GSM
bank1 uchar ATResponseTimerSignel;

bank1 char PhoneNumber[11];
bank1 char SMSContent[32];
bank1 uchar f_SMS_send;
bank1 uchar f_new_SMS;
bank1 uchar DelSMSLocation;
bank1 uchar f_DelSMS;

void delay()//500 7.16MS
{
    int i;
    for(i=0;i<400;i++)
       {;}
}

void ClearBufReceiving(void)
{
     uchar i;
	 for(i=0;i<BUF_RECEIVING_LENGTH;i++)
	 	BufReceiving[i]=0;
}
/*COMMENT: JudgeReceiveData()
  Data of GSM respond AT command Process
  return value is position|0x80 if the data required was in BufReceiving[]
  return 0 if ther data required wasn't in BufReceiving[]
*/
uchar JudgeReceiveData(const uchar *p_string)
{
  uchar tmp_t;
  tmp_t=BufReceivedTailCounter;
  while(BufReceivedHeadCounter!=tmp_t)//looking for first data in BufReceiving
  	{
		if(BufReceiving[tmp_t]==*p_string)
			                                            break;
		tmp_t++;
		tmp_t&=BUF_RECEIVING_MASK;		
  	}
  if(BufReceivedHeadCounter==tmp_t)return(0);//the first data didn't receive.

  while((BufReceivedHeadCounter!=tmp_t)&&(*p_string!='\0'))
		{
		if(BufReceiving[tmp_t]!=*p_string)
			{
			return(0);
			}
		p_string++;
		tmp_t++;
		tmp_t&=BUF_RECEIVING_MASK;	
  		}
  if(*p_string=='\0')return(tmp_t|0x80);//return the current tail pointer and equal flag set to bit 7
  else return(0);
}
uchar JudgeReceiveDataExtension(const uchar *p_string,uchar max_distance)
{
  uchar tmp_t,max;
  tmp_t=BufReceivedTailCounter;
  while(BufReceivedHeadCounter!=tmp_t)//looking for first data in BufReceiving
  	{
		if(BufReceiving[tmp_t]==*p_string)
			                                            break;
		tmp_t++;
		tmp_t&=BUF_RECEIVING_MASK;		
  	}
  if(BufReceivedHeadCounter==tmp_t)return(0);//the first data didn't receive.

  while((BufReceivedHeadCounter!=tmp_t)&&(*p_string!='\0'))
		{
		if(BufReceiving[tmp_t]!=*p_string)
			{
			return(0);
			}
		p_string++;
		tmp_t++;
		tmp_t&=BUF_RECEIVING_MASK;	
		if(*p_string=='*')//if get a '*' from p_string. meaning that  available data after '*'
			{
			  p_string++;
			  max=0;
			  while(BufReceivedHeadCounter!=tmp_t)//looking for first data in BufReceiving
			  	{
					if(BufReceiving[tmp_t]==*p_string)
						                                            break;
					tmp_t++;
					tmp_t&=BUF_RECEIVING_MASK;		
					max++;
					if(max>max_distance)return(0);
			  	}			
			}
  		}
  if(*p_string=='\0')return(tmp_t|0x80);//return the current tail pointer and equal flag set to bit 7
  else return(0);
}
/*
uchar JudgeData(const char *p_string,uchar postion)
{
  uchar i;
  i=postion;
  while(*p_string!='\0')
  	{
  	if(BufReceiving[i]!=*p_string)return(0);
	if(i<BUF_RECEIVING_LENGTH)i++;
	p_string++;
  	}
  return(1);
}*/
/*COMMENT: AmountOfReceivedData()
  How many data received in BufReceiving[] and return the result
*/
uchar AmountOfReceivedData(void)
{
  uchar tmp_t;
  uchar amount=0;
  tmp_t=BufReceivedTailCounter;
  while(BufReceivedHeadCounter!=tmp_t)
		{
		tmp_t++;
		tmp_t&=BUF_RECEIVING_MASK;
		amount++;
  		}
  return(amount);
}





/*
Function   Name: GetByte
Return   Value:struct inside is the position befor the data and the data required
Parameters   :the position after the data
PurPose        :
       switch the ASCII to HEX data from receiving buffer
Reason of change: 
Contect of change: 
Date of change: 
Date of establish: 2008-9-14 
*/
void GetByte(uchar position_current)
{
   uchar i,tmp,tmp1;
   //BytePositionStruct  result;
   tmp=position_current;

   for(i=0;i<BUF_RECEIVING_LENGTH;i++)
   	{
   	if((BufReceiving[tmp]>='0')&&(BufReceiving[tmp]<='9'))break;
	tmp--;
	tmp&=BUF_RECEIVING_MASK;
   	}
   if(i=BUF_RECEIVING_LENGTH)
   	{
   	
   	}
   tmp1=1;
   ValuePosition.value=0;  //NewSMSPosition 
   for(i=0;i<BUF_RECEIVING_LENGTH;i++)
   	{
   	if((BufReceiving[tmp]<'0')||(BufReceiving[tmp]>'9'))break;
	ValuePosition.value+=(BufReceiving[tmp]-'0')*tmp1;
	tmp1=tmp1*10;
	tmp--;
	tmp&=BUF_RECEIVING_MASK;
   	}   
   
   ValuePosition.position=tmp;
}


/*COMMENT: ProcessDataReceivedByUart()
  Data of GSM respond AT command Process
  GSM will return a status if sending a command to GSM
  if the command was disposed by GSM ,GSM will return "OK"
  if the command wasn't disposed by GSM ,the flag of f_ack_gsm_init will be set to 1 that express Sending the command once again

*/
void ProcessDataReceivedByUart(void)
{
  uchar tmp,tmp1,i,a;
  if(BufReceivedHeadCounter==BufReceivedTailCounter)return;
  //amount=AmountOfReceivedData();
  tmp1=JudgeReceiveData("\r\n");
  if(tmp1==0)return;
  //tmp=JudgeReceiveData("OK\r\n");
  //tmp1=JudgeReceiveData("ERROR\r\n");
  //tmp2=JudgeReceiveDataExtension("+CMTI:*\r\n",10);//don't care the datas from '*' to '\r' 
  //tmp3=JudgeReceiveDataExtension("+CREG:*,1,*\r\n",10);//don't care the datas from '*' to '\r' 
  //tmp4=JudgeReceiveDataExtension("+CREG:*,5,*\r\n",10);//don't care the datas from '*' to '\r' 
  //a=tmp+tmp1+tmp2+tmp3+tmp4;
  //if(a==0)return;
  
  if(tmp=JudgeReceiveData("OK\r\n"))//response is "ok"
  	{

		   BufReceivedTailCounter=tmp&0x7f;
		   f_waiting_ack=0;
		   if(f_DelSMS)f_DelSMS=0;
		   if(f_new_SMS==3)// SMS data  finished to receive
		   	{
		   	//stat->-----tmp1&0x7f;// SMS contect
		   	f_DelSMS=1;
			DelSMSLocation=ValuePosition.value;
		   	}
		   for(a=0,i=0;     a<INIT_AMOUNT;   a++,i=a*2)
		                         if(((f_ack_gsm_init>>i)&0x03)==0x02)
		   	                                                            f_ack_gsm_init&=~(0x03<<i);//clear flag of send "AT" 
	
  	}  	
  else if(tmp=JudgeReceiveData("ERROR\r\n"))//response is "error"
  	{

		   BufReceivedTailCounter=tmp&0x7f;
		   f_waiting_ack=0;
		   for(a=0,i=0;     a<INIT_AMOUNT;   a++,i=a*2)
		                         if(((f_ack_gsm_init>>i)&0x03)==0x02)
		   	                                                            f_ack_gsm_init|=0x01<<i; //send "AT" to GSM over again	       
		   //DisableGSM( );
		   //delay();
		   //EnableGSM( ); 
  	}    	 
  else if(tmp=JudgeReceiveDataExtension("+CMTI:*\r\n",10))
  	{
		   BufReceivedTailCounter=tmp&0x7f;
		   GetByte(BufReceivedTailCounter);
		   if(ValuePosition.value<SIM_MEMORY_LEN)
		                                                     f_new_SMS=1;
  	}
  else if(tmp=JudgeReceiveDataExtension("+CREG:*,1,*\r\n",10))
  	{
		   BufReceivedTailCounter=tmp&0x7f; //Registered, home network
		   f_ack_gsm_init&=~0x03;
		   
  	}
  else if(tmp=JudgeReceiveDataExtension("+CREG:*,5,*\r\n",10))
  	{
		   BufReceivedTailCounter=tmp&0x7f;    //Registered, roaming
		   f_ack_gsm_init&=~0x03;
		   
  	}  
  else if(tmp=JudgeReceiveDataExtension("+CMGR:*\r\n",60))
  	{
		   BufReceivedTailCounter=tmp&0x7f;    //Registered, roaming
		   if(f_new_SMS==2)f_new_SMS=3;
		   
  	}   
  //+CMGR:
  //else if(amount>=5)BufReceivedTailCounter=BufReceivedHeadCounter;
  //not available data in BufReceiving. then make two pointers equal.
    
}


void ProcessGSMDataReceivedByUart(void)
{
  if(f_SMS_send==2)
  	{
  	if((JudgeReceiveData(">"))&&(BufReceivedHeadCounter==1))
		f_SMS_send=3;
  	
       else if(BufReceivedHeadCounter>=1)
	   	f_SMS_send=1;//send AT command to GSM over again
  	}
  if(f_SMS_send==4)//step 4:send the sms content
  	{
  	//+CMGS: 21\r\n
  	//OK\r\n
  	if((JudgeReceiveData("OK\r\n"))&&(BufReceivedHeadCounter>=13))
		f_SMS_send=0;
  	}
  }

void ShiftByteToBufferSendByUart(uchar data)
{
  BufSending[BufSendHeadCounter]=data;
  BufSendHeadCounter++;
  BufSendHeadCounter&=BUF_SENDING_MASK;
}

void ATSend(const uchar *p_string)
{
  while(*p_string!='\0')
  	{
         BufSending[BufSendHeadCounter]=*p_string++;
         BufSendHeadCounter++;
         BufSendHeadCounter&=BUF_SENDING_MASK;
  	}

}
void ATSendFromRam( uchar *p_string)
{
  while(*p_string!='\0')
  	{
         BufSending[BufSendHeadCounter]=*p_string++;
         BufSendHeadCounter++;
         BufSendHeadCounter&=BUF_SENDING_MASK;
  	}

}
void ATSendData(uchar *p_string,uchar num)
{
  while(num--)
  	{
         BufSending[BufSendHeadCounter]=*p_string++;
         BufSendHeadCounter++;
         BufSendHeadCounter&=BUF_SENDING_MASK;
  	}

}
void ATSendByte(uchar byte1)
{

         BufSending[BufSendHeadCounter]=byte1;
         BufSendHeadCounter++;
         BufSendHeadCounter&=BUF_SENDING_MASK;

}
void GSMModuleInitialize(void)
{
          if(f_ack_gsm_init==0)return;
          if((ATResponseTimerSignel>OVERFLOW)&&(f_ack_gsm_init&0x02))
		  	{
		  	//AT response time overflow
		  	f_ack_gsm_init|=0x01;//send AT command to GSM over again
		  	ATResponseTimerSignel=0;
          	}
          if((ATResponseTimerSignel>OVERFLOW)&&(f_ack_gsm_init&0x08))
		  	{
		  	//AT response time overflow
		  	f_ack_gsm_init|=0x04;//send AT command to GSM over again
		  	ATResponseTimerSignel=0;
          	}		  
	   if(f_ack_gsm_init&0x03)
	   	{
	   	if((f_ack_gsm_init&0x03)==0x03)
	   		{
		     ATSend("AT+CREG?\r");//check the network registration status of the modem.
		     f_ack_gsm_init&=~0x01;
			 ATResponseTimerSignel=0;
	   		}
	   	}
	   else if(f_ack_gsm_init&0x0c)
	   	{
	   	if((f_ack_gsm_init&0x0c)==0x0c)
	   		{
		     ATSend( "AT+CMGF=1\r" ); //Text
		     f_ack_gsm_init&=~(0x01<<2);
			 ATResponseTimerSignel=0;
	   		}
	   	}
	   else if(f_ack_gsm_init&0x30)
	   	{
	   	if((f_ack_gsm_init&0x30)==0x30)
	   		{
		     ATSend( "AT+CNMI=2,1\r" ); //Buffer handling
		     f_ack_gsm_init&=~(0x01<<4);
			 ATResponseTimerSignel=0;
	   		}
	   	}	   
   
   }
/*
Function   Name: SMSSend
Return   Value:void
Parameters   :void
PurPose        :
       Send the SMS to GSM via uart
       AT+CMGS="phone NO."
       >"SMS contect"
globe variable:
       f_SMS_send:step of current sending,f_SMS_send=1->start SMS sending
       ATResponseTimerSignel:senting time. if ATResponseTimerSignel>OVERFLOW. meaning nonresponse

Reason of change: 
Contect of change: 
Date of change: 
Date of establish: 2008-9-14 
*/
void SMSSend(void) 
{

  if(f_SMS_send==1)
  	{
	  ATSend("AT+CMGS=");
	  ATSendData(PhoneNumber,11);
	  ATSend("\r");
	  f_SMS_send=2;
	  ClearBufReceiving();
	  BufReceivedHeadCounter=0; 
	  ATResponseTimerSignel=0;    	
  	}
  if(f_SMS_send==3)
  	{
	  ATSendFromRam(SMSContent);
         ATSendByte(0x1a);//ctrl-Z
	  ClearBufReceiving();
	  BufReceivedHeadCounter=0; 
	  ATResponseTimerSignel=0;   
	  f_SMS_send=4;
  	}
   if((ATResponseTimerSignel>OVERFLOW)&&((f_SMS_send==2)||(f_SMS_send==4)))
  	{
  	//AT response time overflow
  	f_SMS_send=1;//send sms to GSM over again
        }
}
void ByteIntoAScll(uchar byte){	
  if(byte/100){
    BufSending[BufSendHeadCounter++]=byte/100+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
    BufSending[BufSendHeadCounter++]=(byte%100)/10+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
    BufSending[BufSendHeadCounter++]=byte%10+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
    }
  else if(byte/10){
    BufSending[BufSendHeadCounter++]=byte/10+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
    BufSending[BufSendHeadCounter++]=byte%10+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
    }
  else {
  	BufSending[BufSendHeadCounter++]=byte+0x30;
	BufSendHeadCounter&=BUF_SENDING_MASK;
  	}
}
void GetSMS(void)
{
  if(f_waiting_ack)return;
  if(f_new_SMS==1)
  	{
  f_new_SMS=2;//
  ATSend("AT+CMGR=");
  ByteIntoAScll(ValuePosition.value);
  ATSend("\r");
  f_waiting_ack=1;  
  	}
  //AT+CMGR=3

}

void DelSMS(void)
{
  if(f_waiting_ack)return;
  if(f_DelSMS)
  	{
	  ATSend("AT+CMGD=");
	  ByteIntoAScll(DelSMSLocation);
	  ATSend("\r");
	  f_waiting_ack=1;
  	}
  //AT+CMGD=?
}
void  CollectorTransistorControl(uchar  OnOrOff)//OnOrOff=1 open =0 close
{
      if(OnOrOff)DDRDbits.RD0=1;//PORTD|=0x01;
	  else DDRDbits.RD0=0;//PORTD&=0xfe;
}
////////////test function///////
void ShiftStringToBufferReceivedByUart(const uchar *p_string)
{
  while(*p_string!='\0')
  	{
         BufReceiving[BufReceivedHeadCounter]=*p_string++;
         BufReceivedHeadCounter++;
         BufReceivedHeadCounter&=BUF_RECEIVING_MASK;
  	}

}
void test(void){
	uint a;
	//a=(WDTDIS&PWRTEN&BORDIS&LVPDIS&PROTECT&HS);
	//a=(WDTDIS&PWRTEN&BORDIS&LVPDIS&UNPROTECT&HS);

	ShiftStringToBufferReceivedByUart("OK");
	ShiftStringToBufferReceivedByUart("eiahfldK");
	ShiftStringToBufferReceivedByUart("OK");
	ShiftStringToBufferReceivedByUart("ehgiia ");
	ShiftStringToBufferReceivedByUart("ERROR");
	while(1){
		//ProcessDataReceivedByUart();
		
		}
}
////////////////////////////
/*
Function   Name: initialize_port
PurPose        :
       initialize PORT that ready for outputing TTL to control peripheral equipment
*/
void initialize_port(void)
//I/O port intiialize
{

 TRISD=0X00; //initialize PORTD to output
 PORTD=0X00;//PORTD=0Xff; //output  PORTD all to 0
}

/**/
void SleepProg(void)
{  
   //asm("clrwdt");
   //WDTE=0;
   //asm("sleep");
   //asm("nop");
   //asm("clrwdt");
}
void main(void)
{
   initialize_port();
   initialize_uart();
   InitTimer1();
   //initialize_spi();
   ////////test/////////////////
   test();
   //////////////////////////
   while(1)
   {
   GSMModuleInitialize();
   ProcessDataReceivedByUart();
   UartSendData();
   //asm("clrwdt");
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -