0

I am having trouble with my university project for embedded systems. The goal is to establish an interface between a SM5100B GSM module and ATMEGA16A microcontroller, using UART (which I did, using the correct ports from the datasheets), and to be able to send/receive simple SMS messages by sending AT commands trough the Tx and Rx ports from atmega to gsm and vice versa, via C code in Atmel.(not using hyperterminal)

When I tested the GSM module using TeraTerm, i was able to connect properly, and send AT commands easily, also managed to send and recieve an SMS with the SIM card inserted, so everything works fine.

Now I'm trying to do that using the microcontroller.

Here is the code I have so far:

#define F_CPU 7372800UL 
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/io.h>
#include <string.h>

#define BAUD 9600
#define MYUBRR ((F_CPU/16/BAUD)-1) //BAUD PRESCALAR (for Asynch. mode)

void GSM_init(unsigned int ubrr ) {
    /* Set baud rate */
    UBRRH = (unsigned char)(ubrr>>8);
    UBRRL = (unsigned char)ubrr;
    /* Enable receiver and transmitter */
    UCSRB = (1<<RXEN)|(1<<TXEN);
    /* Set frame format: 8data, 2stop bit */
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);    
}

void USART_Transmit(char data ) {
    /* Wait for empty transmit buffer */
    while ( !( UCSRA & (1<<UDRE)) );
    /* Put data into buffer, sends the data */
    UDR = data;
}

void USART_Transmits(char data[] ) {
    int i;

    for(i=0; i<strlen(data); i++) {
        USART_Transmit(data[i]);
        _delay_ms(300);
    }
}

int main(void)
{
    GSM_init(MYUBRR);

    char text_mode[] = "AT+CMGF=1";
    char send_sms[] = "AT+CMGS=";
    char phone_number[] = "00385*********";
    char sms[] = "gsm sadness";

    USART_Transmits(text_mode);
    _delay_ms(1000);
    USART_Transmits(send_sms);
    _delay_ms(1000);
    USART_Transmit(34);//quotation mark "
    //_delay_ms(300);
    USART_Transmits(phone_number);
    //_delay_ms(300);
    USART_Transmit(34);//quotation mark "
    //_delay_ms(300);
    USART_Transmit(13);//enter
    //_delay_ms(300);
    USART_Transmits(sms);
    _delay_ms(1000);
    USART_Transmit(26);//ctrl+z
    _delay_ms(300);
    USART_Transmit(13);//enter
    _delay_ms(3000);

    while (1) 
    {
    }
}

However, my code isn't working, it's not sending the message.

The functions for transmitting are taken from the datasheet and everywhere on the internet I search I find the same ones over and over again. Is the problem in AT responses that I'm not reading correctly? Or in parsing AT commands to the serial port? Can anybody help me understand where I'm going wrong with this, or where I can look for to understand how to make this work?

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
bad_wolf
  • 1
  • 3
  • What is "The problem"? You're not stating anywhere what is not working... – tofro Mar 15 '17 at 17:48
  • sorry, the problem is that this code that i posted here is not working, its not sending the message, and i dont understand why. – bad_wolf Mar 15 '17 at 17:51
  • i've tried googling so many times, but most of the answers are for arduino, or using rs232 connections + hyperterm.. and my teacher is unwilling to help. so i'm really looking for anything that might kickstart the sending of the message, any clues, advice, code corrections, source codes, anything might help really.. – bad_wolf Mar 15 '17 at 17:58
  • 2
    Commands are supposed to end with a carriage return character, '\0xD'. – UncleO Mar 15 '17 at 19:02
  • i am trying to send carriage return as ascii when i do "USART_Transmit(13);" . that isn't sufficient?? – bad_wolf Mar 15 '17 at 19:08
  • 1
    I only see that at one point in your posted code. For example, it doesn't appear between `AT+CMGF=1` and `AT+CMGS=`. – UncleO Mar 15 '17 at 22:06
  • "gsm sadness" made me smile ;) What UncleO said, you definitely need no end the command with a carriage return character '\r' which corresponds to 0x0d or 13 decimal. Change "AT+CMGF=1" to "AT+CMGF=1\r" for example. – Rev Mar 16 '17 at 07:40
  • @bad_wolf You need to read **all of chapter 5** in the V.250 specification. That will take your knowledge about AT command handling many orders of magnitude higher than your current level. Link and other relevant information in [this](http://stackoverflow.com/a/31126602/23118) and [this](http://stackoverflow.com/a/15591673/23118) answer. – hlovdal Mar 16 '17 at 17:20
  • Your CPU frequency is really 7372800 Hz? – Bence Kaulics Mar 21 '17 at 12:19
  • yes it really is haha – bad_wolf Mar 21 '17 at 18:15

0 Answers0