2
我可以用下面的代碼接收,但不幸的是,沒有任何迴應。我究竟做錯了什麼?UART沒有響應
#include <pic18f25k80.h>
#include "config.h"
#include <usart.h>
int i = 0;
unsigned char MessageBuffer[200];
void main() {
OSCCONbits.IRCF = 0b110; // 8MHz
TRISB6 = 0; // TX set as output
TRISB7 = 0; // RX set as output
// Clear TX interrupt
// Set RX interrupt
// 8-bit Asynch. mode
// BRGH = 1 = high baud mode
// 51 = ((8MHz/baud)/16)-1 with baud = 9600
Open2USART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE
& USART_EIGHT_BIT & USART_BRGH_HIGH, 51);
RC2IF = 0; // reset RX2 flag
RC2IP = 0; // not high priority
RC2IE = 1; // Eneble RX2 interrupt
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts
RCSTA2bits.SPEN = 1; // enable USART
while(1){
}
}
void interrupt ISR() {
if(PIR3bits.RC2IF == 1) {
if(i<200) { // buffer size
MessageBuffer[i] = Read2USART(); // read byte from RX reg
if (MessageBuffer[i] == 0x0D) { // check for return key
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i=0;
return;
}
i++;
RC2IF = 0; // clear RX flag
} else {
puts2USART(MessageBuffer);
for(;i>0;i--)
MessageBuffer[i] = 0x00; // clear array
i = 0;
return;
}
}
}
我發送0x41十六進制代碼,我檢查了範圍,看看是否正在接收。根據我的代碼,接收到的數據應該被回送。當我檢查TX引腳時,什麼都沒有發生。
嘗試了'printf()'來查看代碼的哪些部分運行? – 2014-11-03 13:19:15
您可能想要將RX引腳設置爲輸入 – rm5248 2014-11-03 13:31:45
將RX引腳設置爲輸入或不是由它決定,它不是由端口外設聲明,而是由UART本身聲明。 它看起來像'puts2USART'是不正確的數據傳輸功能。我試過'TXREG2 = RCREG2;'。所以接收到的所有內容都發送到Transmit寄存器,並且工作。但仍然在尋找一個功能,可以做'發送':'文本''' – 2014-11-03 13:58:13