1

i have written a library in C for using the SIM900 GSM with my uC but it has many many bugs. Sometimes it works sometimes not. My hardware works fine I think.

I rewrote it and made sure that the basic functions are bug free.

  • SIM900_transmit( char* );
  • SIM900_reveive( char** );
  • SIM900_on();
  • SIM900_off();

Now I want to write the SIM900_command function that will use SIM900_transmit( char* ) and SIM900_reveive( char** );

So my detailed question is:

How to know how much time to wait between AT command and receiving the answer from SIM900. I do not want to just put _delay_ms(1000).

Thanks in advance...

Tedi
  • 203
  • 1
  • 13
  • This is on-topic at [electronics.SE] as it is based around the timing specifications of a piece of hardware. – nanofarad Aug 23 '15 at 13:58
  • If it's communicating over UART you simply wait until a character is available at the interface. – PhilMasteG Aug 23 '15 at 14:02
  • I do this inside SIM900_receive( char**); Am I good? It is something like:
        if(uart_available>0)
        {
            //Code to receive here....
        }
    
    
    – Tedi Aug 23 '15 at 14:16

2 Answers2

1

Usually, you wait until a character is available and then receive it. Have a look here. There you have for AVR devices with a single UART:

unsigned char uart_recieve (void)
{
    while(!(UCSRA) & (1<<RXC));
    return UDR;
}

So, with while(!(UCSRA) & (1<<RXC)); you basically block execution until there is a character available at the uart.

PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
  • What you said is inside uart_available in some other form. I used it and it works. Thank you. It was helpful and made me look in the right direction. – Tedi Aug 23 '15 at 16:58
  • It said I can mark my own answer as correct in two days... I will try to remember and do this. Your answer also is good. Thanks again. – Tedi Aug 23 '15 at 18:58
1

I found my main problem. Now I will fix the old SIM900 library.

OLD CODE:

/*
 * Sent the AT command.
 */
softuart_puts_P( "Transmit" );
SIM900_transmit( "AT\r" );
softuart_puts_P( " completed\r\n" );

/*
 * Wait until SIM900 answers back.
 */
while ( uart_available() == 0 )//BUG: Loop in a loop(receive has one) caused chaos.
{   
    /*
     * Sent the AT command.
     */
    softuart_puts_P( "Received: [" );
    cString answer = newEmptyString();
    SIM900_receive( &answer );
    softuart_puts( answer );
    softuart_puts_P( "]\r\n" );
    deleteString( &answer );
}

NEW CODE

/*
 * Sent the AT command.
 */
softuart_puts_P( "Transmit" );
SIM900_transmit( "AT\r" );
softuart_puts_P( " completed\r\n" );

/*
 * Wait until SIM900 answers back.
 */
while ( uart_available() == 0 );// TODO: Put timeout in case SIM900 is switched off.

/*
 * Sent the AT command.
 */
softuart_puts_P( "Received: [" );
cString answer = newEmptyString();
SIM900_receive( &answer );
softuart_puts( answer );
softuart_puts_P( "]\r\n" );
deleteString( &answer );    

This does the work: while ( uart_available() == 0 );

Thanks for your help.

Tedi
  • 203
  • 1
  • 13