cancel
Showing results for 
Search instead for 
Did you mean: 

send sms with sim900 on stm32f107

mariolep
Associate II
Posted on December 16, 2013 at 15:12

I have to send a sms with sim900 form my stm32f107 board. I wrote this code, but it does not work. I can't understand why.

/* Includes */

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''misc.h''

#define CR 13

#define LF 10

void RCC_Configuration(void);

void GPIO_Configuration(void);

void NVIC_Configuration(void);

void sendMessage(char header[23], char message[150]);

void Delay(__IO uint32_t nCount);

int main(void){

char SendMessageTO[23] = ''AT+CMGS=�?+3473802890�?'';

char SendMessageMESSAGE[150] = '' THIS IS A SAMPLE MESSAGE            '';

USART_InitTypeDef USART_InitStructure;

/* System Clocks Configuration */

RCC_Configuration();

/* NVIC configuration */

//NVIC_Configuration();

/* Configure the GPIO ports */

GPIO_Configuration();

/* USARTx configuration ------------------------------------------------------*/

/* USARTx configured as follow:

        - BaudRate = 9600 baud

        - Word Length = 8 Bits

        - Two Stop Bit

        - Odd parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit enabled

*/

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init( USART1,&USART_InitStructure);

sendMessage(SendMessageTO, SendMessageMESSAGE);

while(1)

{

}

}

void RCC_Configuration(void)

{

/* Enable GPIO clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

/* Enable USART2 Clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

}

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure USART1 Rx as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

/* Configure USART1 Tx as alternate function push-pull   */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

void sendMessage(char header[23], char message[150])

{

int ctr;

for (ctr=0;ctr<23;ctr++)

{

USART_SendData(USART1,(int)header[ctr] );

Delay(10);

}

USART_SendData(USART1,CR);

Delay(10);

USART_SendData(USART1,LF);

Delay(10);

Delay(1000);

for (ctr=0;ctr<150;ctr++)

{

USART_SendData(USART1,(int) message[ctr] );

}

USART_SendData(USART1,CR);

USART_SendData(USART1,LF);

}

void Delay(__IO uint32_t nCount) //in millisecond

{

nCount = nCount * 5940 *2;//381;

while(nCount--)

{

}

}

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef  VECT_TAB_RAM

/* Set the Vector Table base location at 0x20000000 */

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

#else  /* VECT_TAB_FLASH  */

/* Set the Vector Table base location at 0x08000000 */

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

#endif

/* Enable the TIM2 global Interrupt */

//NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

10 REPLIES 10
Posted on December 16, 2013 at 15:37

A slightly more clueful approach, but still ignoring the responses from the modem might look like this.

/* Includes */
#include ''stm32f10x.h''
#include ''stm32f10x_gpio.h''
#include ''misc.h''
#define CR 13
#define LF 10
void RCC_Configuration(void);
void GPIO_Configuration(void);
void sendMessage(*header, char *message);
void Delay(__IO uint32_t nCount);
int main(void)
{
USART_InitTypeDef USART_InitStructure;
char SendMessageTO[] = ''AT+CMGS=\''+3473802890\''''; // Not goofy angle quotes
char SendMessageMESSAGE[] = ''THIS IS A SAMPLE MESSAGE'';
/* System Clocks Configuration */
RCC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- Two Stop Bit
- Odd parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init( USART1,&USART_InitStructure);
sendMessage(SendMessageTO, SendMessageMESSAGE);
while(1)
{
}
}
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable USART2 Clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
} 
void USART1_OutChar(char ch)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for the TX buffer to be EMPTY FFS
USART_SendData(USART1, ch);
}
void USART1_OutString(char *s)
{
while(*s)
USART1_OutChar(*s++);
}
void sendMessage(char *header, char *message)
{
USART1_OutString(header);
USART1_OutString(''

'');
Delay(1000); // Should really wait for '>' or ERROR response from modem
USART1_OutString(message);
USART1_OutString(''

'');
USART1_OutChar(0x1A); // ^Z Termination of SMS
}
void Delay(__IO uint32_t nCount) //in millisecond
{
nCount = nCount * 5940 *2;//381;
while(nCount--)
{
}
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mariolep
Associate II
Posted on December 16, 2013 at 15:49

thank you for your support but there is something that is going wrong. When i run the program it stuscks somewhere and in the console I get this message looping forever: ''No trace recived from target''

chen
Associate II
Posted on December 16, 2013 at 15:51

Are you expecting everyone to know what a sim900 is?

I certainly do not.

From your code - it looks like you are trying to communicate with it via RS232.

Is that right?

Have you tried to communicate with the sim900 via a PC with a terminal program?

Have you looked at the stm32f107 output on a PC with a terminal program?

Is the sim900 port a proper RS232 (ie +-12V)

Most RS232 ports on embedded micros are only logic level (ie 3.3v or 5V)

You MUST use a RS232 level converter - such as a MAX232 chip.

mariolep
Associate II
Posted on December 16, 2013 at 16:55

The program sends the first A from the header ''AT+CMGS=\''+3473802890\'''' , than it stucks on this instruction when it has to send T

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

Posted on December 16, 2013 at 17:00

After

  USART_Init(USART1, &USART_InitStructure);

Add

  USART_Cmd(USART1, ENABLE);

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mariolep
Associate II
Posted on December 16, 2013 at 17:37

Thanks, the program executes all the instructions but the sms is still not send. How can I investigate on what is happening? When I use rfid I write a command and then I read the interface i2c to see if there is a problem. Here  can I do something similar?

Posted on December 16, 2013 at 18:05

You'd perhaps want to talk to the modem, check other aspects are working. It will respond to commands with OK/ERROR, etc. You'd want to read the AT command spec for the modem and understand it.

You might also want to wait for the modem to register on the wireless network before attempting to send the SMS
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mariolep
Associate II
Posted on December 18, 2013 at 13:52

I think I am talking with the modem, but his response is not what I expect. Not OK or ERROR but some rubbish char. What's going wrong?

Posted on December 18, 2013 at 14:24

No idea, I don't have your board(s), schematics, or data sheets. Provide or cite if you expect to receive useful advice.

You'll want to check how you have connected the modem and board together, if CMOS or RS232 interfacing is required, that clocks are set appropriately, etc. Use a scope check supplies, and signals are appropriate.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..