Joke Collection Website - Public benefit messages - How to write a program for reading short messages in sim900 with 5 1 single chip microcomputer?

How to write a program for reading short messages in sim900 with 5 1 single chip microcomputer?

Purchase module business to send data, and the serial port configuration program is omitted. The main program is as follows:

/**********************************************************************************

* Project name: SMS control LED

* Description: Control the LED of the development board through SMS.

* experimental platform: C5 1

* Library version:

* Author: clay figurine communication module development platform team

* Blog:

* Taobao:

* Hardware connection description

Communication with GPRS module by serial port of single chip microcomputer

C5 1 GPRS module

p30(RXD)-& gt; receive data

p 3 1(TXD)->; TXD

GND->; GND

* Software function description

After the board is powered on, the RUNING _ LED will flash at a frequency of one second.

The mobile phone sends "onled" to light the LED;;

The mobile phone sends "offled" to turn off the LED.

**********************************************************************************/

# contains "config.h"

# contains "string.h"

# contains "delay.h"

# including "uart.h"

# define automatic _ startup1/defines that there is a self-startup function from the start of V 1.2.

# define buf 1 _ max 200// serial port 1 cache length

/* * * * * * * * * * Local constant statement * * * * * * * * *

sbit runing _ led = p 1^2; //Run indicator light

sbit led = p 1^3; //Control indicator light

/* * * * * * * * * * Local variable declaration * * * * * * * *

xdata u8 UART 1 _ Buf[Buf 1 _ Max];

U8 times =0, First_Int = 0, Shi Jian = 0;

Bdata u8 logo; //Timer flag bit

Sbit timer 0 _ start = flag 0; //Timer 0 delays the start of the counter.

/* * * * * * * * * * Local function declaration * * * * * * * *

void GPIO _ config(void); //pin initialization

void timer 0 init(void); //Timer 0 initialization

void CLR _ buf 1(void); //Clear the serial receive cache

u8 Find(u8 * a); //Find a string

void Second_AT_Command(u8 *b,u8 *a,u8 wait _ time); //Send instruction

void Wait _ CREG(void); //Query waiting module registered successfully.

void Set _ Text _ Mode(void); //Set SMS to text mode.

void Check _ New _ Message(void); //Query new SMS

/* * * * * * * * * External function and variable declaration * * * * * * * * * * * * * * *

/*******************************************************************************

* function name: main

* Description: Main functions

* input:

* Output:

* return:

* Note: The baud rate of serial port is 9600, and the default baud rate of GPRS module is 1 15200, which needs to be modified by serial assistant.

Only 9600 can be used.

The program will delete the short messages on the mobile phone card. Please pay attention.

*******************************************************************************/

Invalid master (invalid)

{

timer 0 init(); //Initialize Timer 0

GPIO _ config();

EA = 1; //Open host interrupt

UART 1 init(); //Initialize serial port 9600

wait _ CREG(); //Query waiting module registered successfully.

set _ Text _ Mode(); //Set SMS to text mode.

//Second _ AT _ Command(" AT+CMGD = 1,4 "," OK ",3); //Delete all messages on the card.

while( 1)

{

Check_New_Message()。

}

}

/*******************************************************************************

* function name: Uart 1

* Description: serial port 1 interrupt service entrance function.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

Invalid Uart 1 () interrupt 4

{

Intermediate frequency (RI)

{

RI = 0; //Clear the RI bit

UART 1 _ Buf[First _ Int]= SBUF; //Save the received string in the cache.

first _ int++; //The cache pointer moves backward.

if(First _ Int & gt; Buff1_ max)//If the cache is full, point the cache pointer to the first address of the cache.

{

first _ Int = 0;

}

}

Intermediate frequency (TI)

{

TI = 0; //clear the TI bit

}

}

/*******************************************************************************

* Function name: Timer0 _ ISR

* Description: Timer 0 interrupts the service input function every 20 milliseconds.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

Invalid timer 0_ISR () interrupt 1

{

Static u8time _ count = 0;

TR0 = 0; //Turn off the timer

TL0 = 0x00// Resets the initial value of the timer.

TH0 = 0xB8// Resets the initial value of the timer.

time _ count++;

if(Time _ count & gt; =50)

{

time _ count = 0;

running _ LED = ~ running _ LED;

}

If(count_20ms) //20ms delay counter

count _ 20 ms-;

If (timer 0_start)

times++;

if(Times & gt; (50 * Shi Jian))

{

Timer _ start = 0

Times = 0;

}

TR0 = 1; //Start the timer

}

/*******************************************************************************

* function name: GPIO_config

* Description: IO port configuration function

* input:

* Output:

* return:

* Note:

*******************************************************************************/

void gpio _ config(void)

{

LED = 1;

running _ LED = 1;

}

/*******************************************************************************

* Function name: Timer0init

* Description: Timer 0 is initialized and timed for 20 ms.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

void time r0 init(void)//20 ms @ 1 1.0592 MHz。

{

AUXR & amp= 0x7F///timer clock 12T mode

TMOD & amp; = 0xF0//

TMOD | = 0x 0 1; //Set the timer mode. 16-bit timer

TL0 = 0x00// Sets the initial value of the timer.

TH0 = 0xB8// Sets the initial value of the timer.

TF0 = 0; //Clear the TF0 flag

TR0 = 1; //Timer 0 starts counting.

ET0 = 1; //Enable Timer0 interrupt

}

/*******************************************************************************

* function name: CLR_Buf 1

* Description: Clear the cached data of serial port 2.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

void CLR_Buf 1(void)

{

u 16k;

for(k = 0; k & ltbuf 1 _ Max; K++) // Clears the cache content to zero.

{

UART 1 _ Buf[k]= 0x 00;

}

first _ Int = 0; //The starting storage location of the received string.

}

/*******************************************************************************

* function name: Find

* Description: Determines whether the cache contains the specified string.

* input:

* Output:

* Return: unsigned char:65438+ 0 specified characters found, but 0 specified characters not found.

* Note:

*******************************************************************************/

U8 lookup (u8 *a)

{

if(strstr(Uart 1_Buf,a)! = empty)

Returns1;

other

Returns 0;

}

/*******************************************************************************

* function name: the second AT command

* Description: Function of sending AT command.

* Input: pointer for sending data, expected reply and waiting time for sending (unit: s).

* Output:

* return:

* Note:

*******************************************************************************/

void Second_AT_Command(u8 *b,u8 *a,u8 wait_time)

{

u8i;

u8 * c;

c = b; //save the string address to c.

CLR _ buf 1();

I = 0;

while(i == 0)

{

If (! Find(a)// Find the character to answer.

{

If (Timer0 _ start = = 0)

{

b = c; //give the string address to B.

For (b; *b! ='\0'; b++)

{

UART 1 _ send data(* b);

}

UART 1 _ SendLR();

Times = 0;

Shi Jian = wait _ time

Timer0 _ start =1;

}

}

other

{

I = 1;

Timer _ start = 0

}

}

CLR _ buf 1();

}

/*******************************************************************************

* function name: Set_Text_Mode

* Remarks: Set SMS to text mode.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

Void Set_Text_Mode (invalid)

{

Second_AT_Command("ATE0 "," OK ",3); //Cancel echo

Second_AT_Command("AT+CNMI=3,2,0,0,0 "," OK ",3); //Directly output new short messages.

second _ AT _ Command(" AT+CMGF = 1 "," OK ",3); //Text mode

second _ AT _ Command(" AT+CPMS = \"SM\ ",\"SM\ ",\ " SM \ ",\ " OK ",3); //All operations are performed in the SIM card.

}

/*******************************************************************************

* Function name: Check message record

* Description: Check whether there is any new information, and execute the information content instruction.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

Void Check_New_Message (invalid)

{

U8 temperature = 0;

if(strstr(Uart 1_Buf,"+CMT))! = null)// If the cache string contains "+CMT", it means there is a new short message.

{

delay _ ms(3); //Wait to receive all data.

if(strstr(Uart 1_Buf," onled ")! = empty)

{

LED = 0;

}

if(strstr(Uart 1_Buf," offled ")! = empty)

{

LED = 1;

}

CLR _ buf 1();

//Second _ AT _ Command(" AT+CMGD = 1,4 "," OK ",3); //Delete SMS

}

}

/*******************************************************************************

* function name: Wait_CREG

* Note: Wait for the module to register successfully.

* input:

* Output:

* return:

* Note:

*******************************************************************************/

Void Wait_CREG (invalid)

{

u8i;

u8k;

I = 0;

CLR _ buf 1();

while(i == 0)

{

CLR _ buf 1();

UART 1 _ SendString(" AT+CREG?" ); //Query module network registration status

UART 1 _ SendLR();

delay _ ms(250);

for(k = 0; k & ltbuf 1 _ Max; k++)

{

if(Uart 1_Buf[k] == ':')

{

If ((UART1_ buf [k+4] ='1') | (UART1_ buf [k+4] =' 5')//indicates that the network registration is successful.

{

I = 1;

Break;

}

}

}

}

}