UART PIC24 - Receive continuous stream of ASCII characters

576 Views Asked by At

I am currently using the PIC24 with UART and am able to receive (Rx) and transmit (Tx) characters. The problem arises when trying to receive a continuous stream of characters, where there is no delay between start and stop bits.

The UART is setup with; 7 data bits, 1 parity bit and 1 stop bit at 1200baud

When displaying the received characters on a terminal it shows that only certain characters will print correctly. I do not believe this to be a baud rate error as I am able to receive and print characters correctly when small delay between start and stop bits i.e holding down a key on a keyboard.

To read two characters that were sent continuously I am doing as follows.

char buf[512];

while (U2STAbits.URXDA == 0); //wait while Rx buffer is full 
buf[0] = U2RXREG;

while (U2STAbits.URXDA == 0); //wait while Rx buffer is full 
buf[1] = U2RXREG;


while (U2STAbits.UTXBF); //wait while Tx Bit not free
U2TXREG = buf[0];

while (U2STAbits.UTXBF); //wait while Tx Bit not free
U2TXREG = buf[1];
1

There are 1 best solutions below

0
On

There might be other problems which we can't see since you just gave us only a small portion of your code.

Anyway, with the additional information in your comment, it's a mismatch in the parity:

Characters received fine:

char  code  binary    # of 1-bits
'A'   0x41  01000001  2 = even
'f'   0x66  01100110  4 = even
'?'   0x3f  00111111  6 = even

Characters not received:

char  code  binary    # of 1-bits
'a'   0x61  01100001  3 = odd
'b'   0x62  01100010  3 = odd
'1'   0x31  00110001  3 = odd

Please, the next time, instead of putting additional information in a comment, edit your question. That way all visitors have all the bits together without having to wade through all the comments.