AnsweredAssumed Answered

Code not working

Question asked by blue.tran on Oct 1, 2014
Latest reply on Oct 28, 2014 by karpavicius.linas

hello Mr Clive, would you please have a look at my code, im trying to switch on an Led via gsm with my stm32F4. 
so in this code i wanted to see if i can transmit and receive but nothing is really hapening.
please. thanks




#include "stm32f4xx.h"                  // STM Device header
#include <stm32f4xx_rcc.h>                                   // STM Device header
#include <stm32f4xx_gpio.h>                                   // STM Device header
#include <stm32f4xx_usart.h>
#include <misc.h>     
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define osObjectsPublic                          // Define objects in main module
#define MAX_STRLEN 50                  //defining my max string length
#define CTRL_Z 26 






char gprs_characters; //declare a variable for storing the GPRS information






volatile char received_string[MAX_STRLEN+1]; // this will hold the recieved string


void Delay(__IO uint32_t nCount) {
  while(nCount--) {
  }
}


// this initialises USART1
void init_USART1(uint32_t baudrate){
     
  GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as TX and RX
     USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
     NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)


     // enable APB2 peripheral clock for USART1
          
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
          
     // enable the peripheral clock for the pins used by USART1, PB6 for TX and PB7 for RX
          
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);


     //Setting up the RX AND TX pins.
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; // Pins 7 (RX) and pin 6(tx) are used
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;                // the pins are configured as alternate function.
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;          
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;               
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;               
     GPIO_Init(GPIOB, &GPIO_InitStruct);                         




     // configuring  pin 6(TX) and pin 7(RX) as alternate functions so they can be used by the usart.
      
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);








     USART_InitStruct.USART_BaudRate = 9600;                    // the baudrate is set to the value we passed into this init function
     USART_InitStruct.USART_WordLength = USART_WordLength_8b;// 8 bits 
     USART_InitStruct.USART_StopBits = USART_StopBits_1;          // 1 stop bit 
     USART_InitStruct.USART_Parity = USART_Parity_No;          
     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
     USART_InitStruct.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; // enable the receiver
     USART_Init(USART1, &USART_InitStruct);                    






     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt


     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;           
     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;           
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;          
     NVIC_Init(&NVIC_InitStructure);                    


     // finally this enables the complete USART1 peripheral
     USART_Cmd(USART1, ENABLE);


}




void SendData(USART_TypeDef* USARTx, volatile char *s){


     while(*s){
          // wait until data register is empty
          while( !(USARTx->SR & 0x00000040) );
          USART_SendData(USARTx, *s);
          *s++;
     }
}


void USART1_IRQHandler(void){


     // check if the USART1 receive interrupt flag was set
     if( USART_GetITStatus(USART1, USART_IT_RXNE) ){


          static uint8_t cnt = 0; // this counter is used to determine the string length
          char t = USART1->DR; // the character from the USART1 data register is saved in t


          /* check the if the maximum string length has been been reached*/
          if( (t != 'n') && (cnt < MAX_STRLEN) ){
               t = USART_ReceiveData(USART1); // Collect Char;
               cnt++;
          }
          else{ // otherwise reset the character counter and print the received string
               cnt = 0;
               
               USART_SendData(USART1,t); 
               
 
          }
     }
}


void send_sms(char msg[])
{
     SendData(USART1,"ATE0\r\n"); //turn off echo
  Delay(15000);


  SendData(USART1,"AT+CMGF=1\r\n"); // select text mode
  Delay(15000);


     SendData(USART1,("AT + CMGS = \"+27734438641\"\r\n"));
     Delay(500000);


     SendData(USART1,(strcat(msg , "\x1A")));
     Delay(5000);
     
}
uint8_t rx_incoming()
{
     uint8_t x = USART_ReceiveData(USART1);
     return x;
}
volatile unsigned char i=0,j=0,yesgot=0,show[20],message[100],backbench,dustbin;
volatile unsigned int storedloc=0,gotit;
// main: initialize and start the system
int main (void) {                                                                                               
     
     init_USART1(9600); // initialize USART1 at 9600 baud rate.


     SendData(USART1, "ate0\r\n");
 
  SendData(USART1, "at+cmgf=1\r\n");
     Delay(500000);
     
   while(storedloc==0)
      {
           gotit= rx_incoming();
           if(gotit=='+')
           {
           gotit= rx_incoming();
           if(gotit=='C')
           { gotit=rx_incoming();
                if(gotit=='M')
                {
                     gotit=rx_incoming();
                     if(gotit=='T')
                     {
                          gotit=rx_incoming();
                          if(gotit=='I')
                          {
                               gotit=rx_incoming();
                               if(gotit==':')
                               {
                                    gotit=rx_incoming();
                                    while(gotit!=',')gotit=rx_incoming();
                                    storedloc=rx_incoming();
                               }
                          }
                     }
                }
           }
      }
     }
   //delay_ms(100);
      SendData(USART1,("AT + CMGR =\r\n"));
     
          while(yesgot==0)
          {
               gotit=rx_incoming();
               if(gotit=='+')
               {
                gotit=rx_incoming();
                if(gotit=='C')
                {
                     gotit=rx_incoming();
                     if(gotit=='M')
                     {
                          gotit=rx_incoming(); if(gotit=='G')
                          {
                               gotit=rx_incoming();
                               if(gotit=='R')
                               {
                                    gotit=rx_incoming();
                                    if(gotit==':')
                                    {
                                         gotit=rx_incoming();
                                         while(gotit!=0xa){gotit=rx_incoming();}
                                         for(i=0;gotit!=0xd;i++){gotit=message[i]=rx_incoming();}
                                         //SendData(USART1, "at+cmgd=\r\n");
                                         
                                         yesgot=1;
                                    }
                               }
                          }
                     }
                    }
                }
           }
               
           if(message[0]=='S')
           {
                if(message[1]=='E')
                {
                     if(message[2]=='N')
                     {
                          if(message[3]=='D')
                          {
                               //I send a message back
                               send_sms("I successfully received the command");
                          }
                     }
                }
           }




}
          
     
     









Outcomes