我正在尝试使用SIM808模块,但是即使仅发送" AT"也没有响应。我使用的是5V/2.5A AC/DC适配器。董事会(ECB-V3.2)的连接良好,因为两个二极管一直在上面,一个二极管每三秒钟闪烁一次。我通过将UART代码从UC到PC发送并接收数据来尝试使用我的UART代码。我还尝试在代码的不同部分显示一些数据,以找到带有错误的行。我认为这个问题是在我正在等待接收SIM808模块的单个字符的同时循环中。我在代码中标记了这一行(在函数 uart_rxchar())中。我使用的是我的代码中的两个UART,一个用于在UC和PC之间发送数据(代码中的频道0),第二个用于在UC和SIM808(频道1)之间发送数据,但我在计算机上检查了两个版本。这是我的代码:
#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("rnstart", 0);
while(1)
{
char ans[15] = "";
DDRE = 0xff;
UART_TxString("ATrn",1);
DDRE = 0x00;
UART_RxString(ans,1);
_delay_ms(2000);
}
}
我还检查了发送的所有可能性:ATr", "ATn", "ATrn", "ATnr
。
预先感谢您的任何帮助。
问题是您正在将接收到的字符回到LINE上的SIM808模块
上UART_TXCHAR(CH,频道);//回声回到收到的char
这可能会使SIM808混淆并将其放在不一致的状态下。
尝试将SIM808模块连接到PC,并使用Coolterm在命令触发后分析响应。