UART缓冲区未在PIC24F上升起标志
我使用RN42-XV蓝牙模块从计算机向PIC24F发送字符。 模块连接/配对正确,发送的字符也正确(使用示波器) 以下是它的初始化方式:UART缓冲区未在PIC24F上升起标志,c,bluetooth,uart,microchip,pic24,C,Bluetooth,Uart,Microchip,Pic24,我使用RN42-XV蓝牙模块从计算机向PIC24F发送字符。 模块连接/配对正确,发送的字符也正确(使用示波器) 以下是它的初始化方式: void initUART(){ //Peripheral Pin Mapping RPINR19bits.U2RXR = 5; //pin 14 UART Receive RPOR5bits.RP11R = 3; //pin 17 UART Transmit //Configuring the UART U2BRG = BR
void initUART(){
//Peripheral Pin Mapping
RPINR19bits.U2RXR = 5; //pin 14 UART Receive
RPOR5bits.RP11R = 3; //pin 17 UART Transmit
//Configuring the UART
U2BRG = BRGVAL;
U2MODEbits.UARTEN = 1;
U2MODEbits.UEN = 0;
U2MODEbits.PDSEL = 0;// 8 bit no parity
U2MODEbits.STSEL = 0; // 1 stop bit
U2STAbits.UTXEN = 0;
U2STAbits.URXISEL = 0;
//Putting the UART interrupt flag down.
IFS1bits.U2RXIF = 0;
}
我还使用此函数获取缓冲区的内容:
int waitForChar(){
int receivedChar;
// Use the UART RX interrupt flag to wait until we recieve a character.
while(IFS1bits.U2RXIF == 1){
// Clear the UART RX interrupt flag to we can detect the reception
// of another character.
IFS1bits.U2RXIF = 0;
// U2RXREG stores the last character received by the UART. Read this
// value into a local variable before processing.
receivedChar = U2RXREG;
}
return receivedChar;
}
问题是,程序从未进入函数waitForChar()内的while循环,因为硬件从未引发UART中断标志。
我尝试了不同的PIC24Fs,但都遇到了相同的问题。我注意到了以下几点:
函数类型声明为
void
,因此它不返回任何内容。如果您试图分配其返回值,则应该会收到编译器警告。此外,它不等待字符。它返回的是“非阻塞”,但您需要一个返回值来告诉您它是否有字符。如果你想让它等待并返回一个字符,它可以是这样的
int waitForChar(){ // declare a return type
int receivedChar;
while(IFS1bits.U2RXIF == 0); // wait
receivedChar = U2RXREG;
IFS1bits.U2RXIF = 0; // clear status
return receivedChar;
}
UART初始化代码缺少此行:
AD1PCFG = 0xFFFF
ADC标志的优先级高于UART。该行禁用它们。是的,已更改为返回int。这无助于缓冲区读取任何内容。