2013-12-16 06:12 AM
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 10void 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);}2013-12-16 06:37 AM
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--)
{
}
}
2013-12-16 06:49 AM
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''
2013-12-16 06:51 AM
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.2013-12-16 07:55 AM
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);2013-12-16 08:00 AM
After
USART_Init(USART1, &USART_InitStructure); Add USART_Cmd(USART1, ENABLE);2013-12-16 08:37 AM
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?
2013-12-16 09:05 AM
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 SMS2013-12-18 04:52 AM
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?
2013-12-18 05:24 AM
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.