AT91SAM7S串口通信 数据不正确 (十万火急)
最近在学习嵌入式,选用的芯片是AT91SAM7S256, 在做串口通讯部分的时候,程序用的是官方at91sam7s-ek里面的例程,稍作修改,但芯片向外设发送的数据总是不正确,芯片内设置的通讯模式是 普通模式,8位字符段(很疑惑,字符位是否等于数据位),1位停止位,MCK时钟,无校验,波特率设置为115200,将数据发送至PC端,用串口调试精灵调试。
下面是部分代码:
unsigned char USART_WriteBuffer(AT91S_USART *usart,
void *buffer,
unsigned int size)
{
// Check if the first PDC bank is free
if ((usart->US_TCR == 0) && (usart->US_TNCR == 0)) {
usart->US_TPR = (unsigned int) buffer;
usart->US_TCR = size;
usart->US_PTCR = AT91C_PDC_TXTEN;
return 1;
}
// Check if the second PDC bank is free
else if (usart->US_TNCR == 0) {
usart->US_TNPR = (unsigned int) buffer;
usart->US_TNCR = size;
return 1;
}
else {
return 0;
}
}
unsigned char USART_ReadBuffer(AT91S_USART *usart,
void *buffer,
unsigned int size)
{
// Check if the first PDC bank is free
if ((usart->US_RCR == 0) && (usart->US_RNCR == 0)) {
usart->US_RPR = (unsigned int) buffer;
usart->US_RCR = size;
usart->US_PTCR = AT91C_PDC_RXTEN;
return 1;
}
else if (usart->US_RNCR == 0) {
usart->US_RNPR = (unsigned int) buffer;
usart->US_RNCR = size;
return 1;
}
else {
return 0;
}
}
void ISR_Usart0(void)
{
unsigned int status;
unsigned int i = 0;
// Read USART status
status = AT91C_BASE_US0->US_CSR;
// Receive buffer is full
if ((status & AT91C_US_RXBUFF) == AT91C_US_RXBUFF) {
// Restart transfer if BPS is not high enough
bytesReceived += BUFFER_SIZE;
if (bytesReceived <= MAX_BYTES) {
for(;i!=BUFFER_SIZE;++i){
pString[currentRead++] = pBuffer[i];
}
USART_ReadBuffer(AT91C_BASE_US0, pBuffer, BUFFER_SIZE);
}
// Otherwise disable interrupt
else {
AT91C_BASE_US0->US_IDR = AT91C_US_RXBUFF;
AT91C_BASE_US0->US_IER = ~AT91C_US_RXBUFF;
USART_WriteBuffer(AT91C_BASE_US0, pString, MAX_BYTES);
currentRead = 0;
bytesReceived = 0;
AT91C_BASE_US0->US_IER = AT91C_US_RXBUFF;
AT91C_BASE_US0->US_IDR = ~AT91C_US_RXBUFF;
}
}
}
void ConfigureUsart0(void)
{
unsigned int mode = AT91C_US_USMODE_NORMAL
| AT91C_US_CLKS_CLOCK
| AT91C_US_CHRL_8_BITS
| AT91C_US_PAR_NONE
| AT91C_US_NBSTOP_1_BIT
| AT91C_US_CHMODE_NORMAL;
// Enable the peripheral clock in the PMC
PMC_EnablePeripheral(AT91C_ID_US0);
// Configure the USART in the desired mode @115200 bauds
USART_Configure(AT91C_BASE_US0, mode, 115200, BOARD_MCK);
// Configure the RXBUFF interrupt
AIC_ConfigureIT(AT91C_ID_US0, 0, ISR_Usart0);
AIC_EnableIT(AT91C_ID_US0);
// Enable receiver & transmitter
USART_SetTransmitterEnabled(AT91C_BASE_US0, 1);
USART_SetReceiverEnabled(AT91C_BASE_US0, 1);
}
int main(void)
{
// Configure pins
PIO_Configure(pins, PIO_LISTSIZE(pins));
// Configure USART0 and display startup trace
ConfigureUsart0();
// Configure TC0 to generate a 1s tick
USART_ReadBuffer(AT91C_BASE_US0, pBuffer, BUFFER_SIZE);
AT91C_BASE_US0->US_IER = AT91C_US_RXBUFF;
AT91C_BASE_US0->US_IDR = ~AT91C_US_RXBUFF;
// Infinite loop
char *pStr = "Hello World!";
while (1){
//USART_WriteBuffer(AT91C_BASE_US0, pStr, 12);
//Delay(5);
}
}
哪个好心人士帮忙看下,感激不尽!!
PS. 由于开发板很小,没有现成的串口,所以我是用三线制接法直接接到PC的。 电平不匹配啊,一个是RS232电平一个是TTL电平啊。大哥。
[解决办法]
单片机由于使用单一电源,其串口通信电平并非标准的rs232电平,必须将其转换为标准电平
我一般都是转换为485电平来通信,电脑端加一个232-485