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

📄 sms.c

📁 单片机控制GSM模块实现短信收发。系统采用西门子解析模块TC35I与单片机通信
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "c8051f020.h"								// SFR declarations
//#include "gb2unicode.h"
#include "gb2unicode_char.h"
#include "math.h"
#include "sms.h"
////////////////////////////////////////////////////////////////////////////


UCHAR code ini_display[]={0xc4,0xa3,0xbf,0xe9,0xd2,0xd1,0xd5,0xfd,0xb3,0xa3,0xc6,0xf4,0xb6,0xaf,
0xa3,0xac,0xbf,0xaa,0xca,0xbc,0xb9,0xa4,0xd7,0xf7};
//**************main函数开始************
void main (void)
{  
    UCHAR i,j;
    unsigned char xdata sms_rec[200]={0};
	unsigned char Byte_Len=0;
	unsigned char String_Len=0;
	unsigned char xdata Gb_Len=0;
	unsigned char ch[]="4F60597D000A003C676867AB003E";
	unsigned char xdata rec_buffer[200]; 
	unsigned char sms_index_high,sms_index_low;
	Mcu_ini();
	led1_close();
	led2_close();
	led3_close();
	Inter_Enable();	
	Baudrate_Init();			//	init the serial port
	Serial1_Inter_Close();
	//sound module test
	Serial1_Inter_Open();
    Serial0_Inter_Close();
    //test wireless module
	sms_receive_flag=0;
	sms_index_len=0;
	for(i=0;i<SMS_MAXLENGTH;i++)
	  for(j=0;j<2;j++)
	  sms_index[i][j]='0';
	sound_ini();
	start_GSM();
	//删除卡内所有短信
	for(i=1;i<10;i++)
    AT_cmgd('0','0'+i);
    for(i=0;i<10;i++)
    AT_cmgd('1','0'+i);
	//设置收到短信时发送CMTI
	AT_cnmi();
	Serial0_Inter_Open();
    led1_open();
    send(0xf4);
    disp_chn(ini_display,12);
    sound_send(ini_display,24);
	while(1)
	{
	if(sms_index_len>0)
	  {
	   led3_open();
	   Serial0_Inter_Close();
	   sms_index_high=sms_index[--sms_index_len][0];
       sms_index_low=sms_index[sms_index_len][1];
	   Serial0_Inter_Open();
	   Delay_1S();
	   Delay_1S();
       String_Len=AT_cmgr(sms_index_high,sms_index_low,SMS_Buffer);
	   Byte_Len=String2Bytes(SMS_Buffer, sms_rec,String_Len);
       Gb_Len=DecodeUcs2(sms_rec, rec_buffer,Byte_Len);
	   send(0xf4);
       disp_chn(rec_buffer,Gb_Len);
       sound_send(rec_buffer,Gb_Len*2);
	   AT_cmgd(sms_index_high,sms_index_low);
	   led3_close();
	  }
    }
} 

/******************************************
//MCU Initialization
Port Configuration and Oscillator set
==========================================*/
void Mcu_ini(void)
{
	unsigned char idata delay;

	OSCXCN = 0x67;					// Enable external crystal	
	WDTCN = 0xDE;					// disable watchdog timer
	WDTCN = 0xAD;
    XBR0 = 0x04;               		// Set UART0
   //  XBR0 = 0x00;   
	XBR1=0x00;                      //disable /INT0,/INT1
    XBR2 = 0x44;              		// Set UART1,Enable XBAR
    P0MDOUT = 0x05; 				// Output configuration for P0 P0.0->TX0,P0.2->TX1 Push-Pull, others Open-drain
//    P0MDOUT = 0x07; 	
    P1MDOUT = 0x00; 				// Output configuration for P1  all Open-Drain 
    P2MDOUT = 0x00; 				// Output configuration for P2  all Open-Drain
    P3MDOUT = 0x3b; 				// Output configuration for P3  P3.2(SOUND_READY),P3.6(LCD_BUSY),P3.7(LCD_REQ) Open-Drain,others Push-Pull
    P0 |= 0x0a;     				// Enable RX0,RX1
	P1 |= 0xff;
    P3 |= 0x44;     				
	delay=256;						// Delay >1 ms  before polling XTLVLD.
	while(delay--);
	while (!(OSCXCN & 0x80));		// Wait until external crystal has									// started.
	OSCICN = 0x0C;					// Switch to external oscillator
	OSCICN = 0x88;					// Disable internal oscillator; enable 
                        			// missing clock detector.
	while (!(OSCXCN & 0x80));		// Wait until external crystal has									// started.
	OSCICN = 0x08;					// Switch to external oscillator
}


/*-------------------------------------------------------------------------
Uart0 and Uart1 Initialization
Uart0 Baudrate:9600bps
Uart1 Baudrate:9600bps
=================================*/
void Baudrate_Init(void)
{
     //UART0 initialization   use timer2 as baudrate generator
     T2CON  = 0x30;      	//Set Timer2 as Baudrate Generator of UART0
     RCAP2H = 0xff;     	//Set Reload High Byte
	 RCAP2L = 0xc4;         //Set Reload Low Byte,baudrate=9600bps
     SCON0  = 0x50;      	//Set UART0 Mode 1 (1 Start Bit ,8 Data Bit ,1 Stop Bit) and Enable Receive 
     
	 //UART1 initialization   use timer4 as baudrate generator
     T4CON  = 0x30;       	//Set Timer4 Mode 2 (Auto Reload Byte)
     RCAP4H = 0xfe;			//Set Reload High Byte  
     RCAP4L = 0x20;			//Set Reload Low Byte=1200bps 
     SCON1  = 0x50;      	//Set UART1 Mode 1 (1 Start Bit ,8 Data Bit ,1 Stop Bit) and Enable Receive 
         
    //**********************  
    ET2 = 0;            	//Disable Timer2 Interrupt   
    EIE2 &= 0xfb;        	//Disable Timer4 Interrupt 
    TR2 = 1;            	//Start  Timer2
	T4CON |= 0x04;         //Start Timer4
   }  

//=================================================
//***********Interrupts Open and Close************* 
//=================================================

void Serial0_Inter_Close()
{ ES0=0; }
void Serial0_Inter_Open()
{ ES0=1; }
void Serial1_Inter_Close(void)
{ EIE2 &= 0xbf; }
void Serial1_Inter_Open(void)
{ EIE2 |= 0x4f; }
//========================
//open global interrupts
void Inter_Enable()
{
EA=1;
}

void Inter_Disable()
{
EA=0;
}
//*********************************


/*************************************************************************
GSM Program Module
***************************************************************************/
//*********************************sms函数****************************************
void	start_GSM(void)
{
	UINT idata i;
	led2_open();
//	for(i=0;i<5;i++) Delay_1S();
	power_down();
	start_tc35();
	for(i=0;i<11;i++) Delay_1S();
	AT_SEND();
	Delay_1S();
	Delay_1S();
//	Signal_test();
//	Delay_1S();
	AT_cmgf();
	Delay_1S();
	led2_close();//start complete
}

//cut off power 
void power_down(void)
{
PD=1;	//power off
Delay_1S();
//Delay_1S();
//Delay_1S();
//Delay_1S();
Delay_1S();
PD=0;
Delay_1S();//wait for module initialization
Delay_1S();
Delay_1S();
Delay_1S();
//Delay_1S();
//Delay_1S();
//Delay_1S();
}

//turn on GSM engine using Ignition line IGT
void start_tc35(void)
{
IGT=0;
Delay_1S();
Delay_1S();
IGT=1;
}

void uart0_send(unsigned char data_buffer[],unsigned char data_len)
{
  unsigned char i;
  SCON1 &= 0xFD;	//clear send flag
  for(i=0;i<data_len;i++)
  {
    SBUF0 = data_buffer[i];		
    while(TI0==0); 
    TI0=0;
  }
}
void AT_SEND(void)              
{
	UCHAR idata i,error_counter;
	error_counter=0;
	while(1)
	{
		TI0=0;
		RI0=0;
		for(i=0;i<5;i++)
		rec[i]=0x00;
		start_timer0();
       uart0_send(Command_At,3);
		for(i=0;i<9;i++)
		{
			while(!RI0);
			rec[i]=SBUF0;
			RI0=0;
		}
		close_timer0();

		if(rec[5]=='O'&&rec[6]=='K') break;
		else if(error_counter++<10)	Delay_1S();
				else deal_with_error();
	}
}


/***********Signal_test函数开始**********
Signal quality test
****************************/
void	Signal_test(void)
{
	UCHAR idata	i,error_counter;
	error_counter=0;
    while(1)                                           // AT+CSQ
    {
		TI0=0;
		RI0=0;
		start_timer0();
        uart0_send(Command_Csq,7);
    	for(i=0;i<28;i++)							//only receive part data (10)
    	{
      	  while(!RI0);
      	  rec[i] = SBUF0;
      	  RI0 = 0;
	    }
	    close_timer0();
       
        if(rec[15]!= '9'&& rec[16]!= '9') break;  
        else if(error_counter++<10)	Delay_1S();
				else deal_with_error();
    }
	Delay_1S();
    RI0=0;
}



/***********AT+cmgf*************
set preferred message format:text mode or PDU mode
In PDU mode, a complete SMS Message including all header information is passed
as a binary string (in hexadecimal format, so only this set of characters is allowed :
{‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’, ‘A’, ‘B’,’C’,’D’,’E’,’F’} ). Each pair or characters is converted to
a byte (ex : ‘41’ is converted to the ASCII character ‘A’, whoes ASCII code is 0x41 or 65).
In Text mode, every commands and responses are in ASCII characters.
********************************/
void	AT_cmgf(void)
{
	UCHAR idata	i;
    while(1)                                           // AT+CSQ
    {
		TI0=0;
		RI0=0;
		/*
    	for(i=0;i<10;i++)
    	{
      	  SBUF0 = Command_Cmgf[i];
      	  while(!TI0); TI0 = 0;
    	}
    	*/
		uart0_send(Command_Cmgf,10);
  		break;
		Delay_1S();
    }
	Delay_1S();
    RI0=0;
	TI0=0;
}
/***********AT+cmgf*************
set preferred message format:text mode or PDU mode
In PDU mode, a complete SMS Message including all header information is passed
as a binary string (in hexadecimal format, so only this set of characters is allowed :
{‘0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’, ‘A’, ‘B’,’C’,’D’,’E’,’F’} ). Each pair or characters is converted to
a byte (ex : ‘41’ is converted to the ASCII character ‘A’, whoes ASCII code is 0x41 or 65).
In Text mode, every commands and responses are in ASCII characters.
********************************/
/*
void	AT_cmgf(void)
{
	UCHAR idata	i,error_counter;
	error_counter=0;

    while(1)                                           // AT+CSQ
    {
		TI0=0;
		RI0=0;
		start_timer0();
    	for(i=0;i<10;i++)
    	{
      	  SBUF0 = Command_Cmgf[i];
      	  while(!TI0); TI0 = 0;
    	}    	
  			for(i=0;i<16;i++)							//only receive part data (10)
    	{
      	  while(!RI0);
      	  rec[i] = SBUF0;
      	  RI0 = 0;
	    }
	    close_timer0();
        if(rec[12]!= 'O'&& rec[13]!= 'K')break;  
        else if(error_counter++<10)	Delay_1S();
				else deal_with_error();
    }
	Delay_1S();
    RI0=0;
	TI0=0;
}
*/
/***********AT_cmgs****************************************
Send message function
After sending "CMGS" command,there will be 16 bytes received,
among which the 15th and 16th must be "0x3E"and "0x20"
***********AT+cmgs函数开始********************************/
void	AT_cmgs(void)
{
	UCHAR idata	i;
    while(1)                                           // AT+cmgs
    {
		TI0=0;
		RI0=0;
		/*
    	for(i=0;i<12;i++)
    	{
      	  SBUF0 = Command_Cmgs[i];
      	  while(!TI0); TI0 = 0;
    	}
		*/
		uart0_send(Command_Cmgs,12);
		start_timer0();
    	for(i=0;i<16;i++)							
    	{
      	  while(!RI0);
      	  rec[i] = SBUF0;
      	  RI0 = 0;
	    }
        close_timer0();
        if(rec[14]== 0x3E&& rec[15]== 0x20)
		break;  	
	//	Delay_1S();
    }

//			Delay_1S();
			for(i=0;i<50;i++);		//延时一段时间
        	RI0=0;
			TI0=0;
}

//***********AT+cmgs函数结束*************
//========sms有关的函数结束===================

//发送GSMhead[],Phone_number,GSM_set[],GSM_sms[],0x1A;
//***********SEND_sms函数开始*************
void	SEND_sms(void)
{
	 UCHAR idata	i;
 //   while(1)                                           
    {
		TI0=0;
		RI0=0;
		//send GSMhead(12 bytes)
    	for(i=0;i<12;i++)
    	{
      	  SBUF0 = GSMhead[i];
      	  while(!TI0); TI0 = 0;
      	}
        //send phone number(12 bytes)
		for(i=0;i<12;i++)
    	{
     	  SBUF0 = Center_number[i];
      	  while(!TI0); 
		  TI0 = 0;
      	}
        //send GSM_set(6 bytes)
		for(i=0;i<6;i++)
    	{
      	  SBUF0 = GSM_set[i];
      	  while(!TI0); TI0 = 0;
      	}
		//send sms contents(26 bytes)
		for(i=0;i<26;i++)
    	{
      	  SBUF0 = GSM_sms[i];
      	  while(!TI0); TI0 = 0;
      	}
        //send end bytes:0x1A
		SBUF0=0x1A;
		while(!TI0); TI0 = 0;	//发送信息结束
/*
    	for(i=0;i<75;i++)						
    	{
      	  while(!RI0);
      	  rec[i] = SBUF0;
      	  RI0 = 0;
	    }
   		 if(rec[72]== 'O'&& rec[73]== 'K')break;  
*/	
        Delay_1S();

    }


//			Delay_1S();
			for(i=0;i<255;i++);		//延时一段时间
        	RI0=0;
			TI0=0;
}


//***********SEND_sms函数结束*************

void    AT_cnmi(void)
{
	 UCHAR idata i,error_counter;
     error_counter=0;
	 Serial0_Inter_Close();
	 while(1)
	 {
    	TI0=0;
		RI0=0;
        uart0_send(Command_Cnmi,18);
		start_timer0();
    	for(i=0;i<24;i++)							//only receive part data (10)
    	{
      	  while(!RI0);
      	  rec[i] = SBUF0;
      	  RI0 = 0;
	    }
	    close_timer0();
       
        if(rec[20]== 'O'&& rec[21]== 'K') break;  
        else if(error_counter++<10)	Delay_1S();
				else deal_with_error();
       }
	Serial0_Inter_Open();
	Delay_1S();
    RI0=0;
}

//+CMGR: 3,,20
unsigned char AT_cmgr(unsigned char index_high,unsigned char index_low,unsigned char buffer[])
{
  	UCHAR idata	i;
	UCHAR byte_length=0;
	UCHAR length;
	UCHAR temp;
		TI0=0;
		RI0=0;
		  if(index_high=='0')
		    byte_length=82;
		  else
		    byte_length=83;
		Serial0_Inter_Close();
		uart0_send(Command_Cmgr,8);
		if(index_high!='0')
		  {
        SBUF0 =index_high;
      	while(!TI0); TI0 = 0;
          }
        SBUF0 =index_low;
      	while(!TI0); TI0 = 0;
		SBUF0 ='\r';
        while(!TI0); TI0 = 0;
        start_timer0();
        for(i=0;i<byte_length;i++)
		  {
		    while(!RI0);
      	  temp = SBUF0;
      	  RI0 = 0;
		  }
		  //接收两个字节的短信长度字符
		  //短信长度高位
	      while(!RI0);
      	  temp = SBUF0;
      	  RI0 = 0; 
          if((temp>='0')&&(temp<='9'))
		    length=(temp-'0')<<4;
		  else
		    length=(temp-'A'+10)<<4;
		  //短信长度低位
		  while(!RI0);
      	  temp = SBUF0;
      	  RI0 = 0; 
          if((temp>='0')&&(temp<='9'))
		    length|=(temp-'0');
		  else
		    length|=(temp-'A'+10);
			//length*2个短信内容字符
		  for(i=0;i<length*2;i++)
		    {
			  while(!RI0);
		    buffer[i]=SBUF0;
			  RI0=0;
			}
		  for(i=0;i<8;i++)
		    {
			  while(!RI0);
		       temp=SBUF0;
			  RI0=0;			  
			}
		close_timer0();
	Serial0_Inter_Open();
	Delay_1S();
    RI0=0;
	TI0=0;
    return length*2;
}



void    AT_cmgd(unsigned char index_high,unsigned char index_low)
{
  	UCHAR idata i,error_counter;

⌨️ 快捷键说明

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