0
\$\begingroup\$

I am trying to use SIM808 module but I have no response even after sending only "AT". I am using 5V/2.5A AC/DC adapter. The board (ecb-v3.2) is rather connected well because two diodes are on all the time and one is blinking every three seconds. I tried my UART code by sending and receiving data from uC to PC and it worked. I also tried to display some data in different parts of my code to find a line with a bug. I think that problem is in the while loop in which I am waiting for receiving a single char from SIM808 module. I marked this line in the code (in function UART_RxChar()). I am using two UARTs in my code, one for sending data between uC and PC (channel 0 in the code) and second for sending data between uC and SIM808 (channel 1), but I checked both versions on my computer. This is my code:

#define F_OSC 7372800UL
#define BAUD 115200
#define ubrr ((F_OSC/16/BAUD)-1)
#include <avr/io.h>
#include <util/delay.h>
#include "lcd.h"

void UART_TxString(char *string_ptr, uint8_t channel);

void UART_Init( uint8_t channel )
{
    if ( channel == 0)
    {
        /*Set baud rate */
        UBRR0H = (unsigned char)(ubrr>>8);
        UBRR0L = (unsigned char)ubrr;
        /*Enable receiver and transmitter */
        UCSR0B = (1<<TXEN0) | (1<<RXEN0);
        /* Set frame format: 8data, 1stop bit */
        UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
    }
    else
    {
        /*Set baud rate */
        UBRR1H = (unsigned char)(ubrr>>8);
        UBRR1L = (unsigned char)ubrr;
        /*Enable receiver and transmitter */
        UCSR1B = (1<<TXEN1) | (1<<RXEN1);
        /* Set frame format: 8data, 1stop bit */
        UCSR1C = (1<<UCSZ10)|(1<<UCSZ11);
    }
}

char UART_RxChar( uint8_t channel )
{

    if ( channel == 0 )
    {
        while((UCSR0A & (1<<RXC0))==0);   // Wait till the data is received
        return(UDR0);                    // return the received char
    }
    else
    {
        //Here is the problem. The condition in the loop is always true.
        while((UCSR1A & (1<<RXC1))==0);   // Wait till the data is received
        return(UDR1);                    // return the received char
    }

}
void UART_TxChar(char ch, uint8_t channel)
{
    if ( channel == 0)
    {
        while((UCSR0A & (1<<UDRE0))==0); // Wait till Transmitter(UDR) register becomes Empty
        UDR0 =ch;             // Load the data to be transmitted
    }

    else
    {
        while((UCSR1A & (1<<UDRE1))==0); // Wait till Transmitter(UDR) register becomes Empty
        UDR1 =ch;             // Load the data to be transmitted
        UART_TxChar(ch,0);
    }

}
void UART_TxString(char *string_ptr, uint8_t channel)
{
    while(*string_ptr)
    UART_TxChar(*string_ptr++, channel);
}
void UART_RxString(char *string_ptr, uint8_t channel)
{
    char ch;
    while(1)
    {
        ch=UART_RxChar(channel);    //Reaceive a char
        UART_TxChar(ch, channel);     //Echo back the received char
        if((ch=='\r') || (ch=='\n')) //read till enter key is pressed
        {                          //once enter key is pressed
            *string_ptr=0;          //null terminate the string
            break;                //and break the loop
        }
        *string_ptr=ch;              //copy the char into string.
        string_ptr++;                //and increment the pointer
    }
}

int main(void)
{
    UART_Init(0);
    UART_Init(1);
    initLCD();
    UART_TxString("\r\nstart", 0);

    while(1)
    {
        char ans[15] = "";
        DDRE = 0xff;
        UART_TxString("AT\r\n",1);
        DDRE = 0x00;
        UART_RxString(ans,1);
        _delay_ms(2000);
    }
}

I also checked all possibilities sending: "AT\r", "AT\n", "AT\r\n", "AT\n\r".

Thank you in advance for any help.

\$\endgroup\$
  • \$\begingroup\$ 1. Are you certain you have the correct baud rate for the SIM808? 2. Are you certain the TX and RX pins are connected properly? \$\endgroup\$ – mike65535 Dec 19 '18 at 14:04
  • \$\begingroup\$ According to application notes there is an autobanding mode set by default. This is a quote from application note. "SIM800 Seriesis is designed in autobauding mode by default. Autobauding allows SIM800 series to automatically detect the baud rate of the host device. In application, host device must to synchronize the baud rate with SIM800 series. Host device must firstly send character "AT or "at to synchronize the baud rate. It is recommended to send "AT" until host device receives the "OK" response, which means host device and SIM800 series are correctly synchronized." \$\endgroup\$ – quark00 Dec 19 '18 at 14:28
  • \$\begingroup\$ I am connecting Rx uC pin to Tx pin in logic level converter, Tx pin in logic level converter to Tx SIM pin. And analogously I am connecting Tx uC pin to SIM module. \$\endgroup\$ – quark00 Dec 19 '18 at 14:30
  • \$\begingroup\$ Echoing it back is definitely a mistake, but perhaps not the first. \$\endgroup\$ – Chris Stratton Dec 19 '18 at 14:42
  • \$\begingroup\$ Is the level shifter necessary with the SIM808? What is the Vcc on the uC vs the SIM808? \$\endgroup\$ – mike65535 Dec 19 '18 at 14:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.