SIM900:如何确保在串行(UART)发送和接收之间等待多长时间

时间:2015-08-23 13:55:53

标签: gcc avr uart sim900

我已经在C中编写了一个库,用于将SIM900 GSM与我的uC一起使用,但它有很多错误。有时它有时不起作用。我认为我的硬件工作正常。

我重写了它并确保基本功能没有错误。

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

现在我要编写 SIM900_command 功能,该功能将使用 SIM900_transmit (char *)和 SIM900_reveive (char **);

所以我的详细问题是:

如何知道在AT命令和从SIM900接收答案之间等待的时间。我不想只放_delay_ms(1000)。

提前致谢...

2 个答案:

答案 0 :(得分:1)

通常,你等到角色可用然后接收它。看看here。您可以使用单个UART进行AVR设备:

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

所以,使用while(!(UCSRA) & (1<<RXC));,你基本上会阻止执行,直到uart上有一个角色为止。

答案 1 :(得分:1)

我发现了我的主要问题。现在我将修复旧的SIM900库。

旧代码:

/*
 * 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 );
}

新代码

/*
 * 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 );    

这可行: while(uart_available()== 0);

感谢您的帮助。