【发布时间】:2018-12-19 11:58:26
【问题描述】:
我正在尝试使用 SIM808 模块,但即使只发送了“AT”,我也没有任何响应。我正在使用 5V/2.5A AC/DC 适配器。该板(ecb-v3.2)连接良好,因为两个二极管一直亮着,一个每三秒闪烁一次。我通过从 uC 向 PC 发送和接收数据来尝试我的 UART 代码,它工作正常。我还尝试在代码的不同部分显示一些数据,以找到存在错误的行。我认为问题出在我等待从 SIM808 模块接收单个字符的 while 循环中。我在代码中标记了这一行(在函数 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("\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);
}
}
我还检查了所有发送的可能性:AT\r", "AT\n", "AT\r\n", "AT\n\r。
提前感谢您的帮助。
【问题讨论】:
-
你确定调制解调器使用你认为的波特率吗?
-
根据应用说明,默认设置了自动绑定模式。这是来自应用笔记的引用。 “SIM800 系列默认设计为自动波特率模式。自动波特率允许 SIM800 系列自动检测主机设备的波特率。在应用中,主机设备必须与 SIM800 系列同步波特率。主机设备必须先发送字符“AT 或"at 同步波特率,建议发送"AT",直到主机收到"OK"响应,表示主机和SIM800系列正确同步。"
-
我注意到您在传输后设置了 DDRE=0。为什么?如果发送引脚受到影响,请注意:此时最后一个字符尚未完全移出。如果 uart 有一个很深的缓冲区,那就更糟了。此外,正如您在上面所说的:“建议发送 UNTIL ... OK 响应”。使用循环尝试 3 或 4 次。
标签: microcontroller avr gsm uart sim800