SIM900:如何确定串行(UART)传输和接收之间等待的时间

SIM900: How to be sure how much time to wait between serial(UART) transmit and receive


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

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

现在我想编写 SIM900_command 函数,它将使用 SIM900_transmit( char* ) 和SIM900_reveive( 字符** );

所以我的详细问题是:

如何知道从AT命令到收到SIM900的应答之间需要等待多长时间。我不想只放 _delay_ms(1000).

提前致谢...

通常,您会等到角色可用,然后再接收它。看看here。你有一个带有单个 UART 的 AVR 设备:

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

因此,使用 while(!(UCSRA) & (1<<RXC)); 您基本上会阻止执行,直到 uart 上有可用字符。

我发现了我的主要问题。现在我将修复旧的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 );

感谢您的帮助。