PIC18F45K50 UART doesn't receive data (solved) - Page 1
Forum section: electronics / beginners

author: FrankBuss
author_rank: Unknown
timestamp: December 09, 2017, 04:04:58 pm
content: I tried to use the UART of a PIC18F45K50. This is the source code:https://pastebin.com/Rz6kEhwVWhen I send data (#if 0 in line 134) I can see this, measured at pin 25, RC6/TX (I'm using the 40 pin PDIP version) :So the baud rate setting and UART initialization seems to be right, and the internal oscillator is surprisingly accurate.But when I try to receive data and echo it (#if 1 in line 134), then I don't get an echo. The received data looks like this, again testing with 0x55, ASCII letter 'U', sent from Minicom, which works with even some cheap USB serial adapters with 500 kHz baudrate or higher (with -b 500000), measured at pin 26, RC7/RX:Doesn't look very clean, because the ground connections are not very good, but shouldn't matter much. I'm sure it is something simple, but I can't find the problem. I used this presentation to implement the functions:http://www.dmi.unict.it/~santoro/teaching/lap1/slides_pic/UART_MCU.pdfI only added the additional test for frame errors when receiving, because the text says there could be a frame error, but the code doesn't test it. But it doesn't work without the additional test either. I can receive the bytes in Minicom when sending it from the PIC, but the read_char function doesn't receive anything and doesn't return.

author: Twoflower
author_rank: Frequent Contributor
timestamp: December 09, 2017, 04:35:43 pm
content: Not sure, but it looks like you're sending with 5V and receiving 3.3V signals. Could that be the problem?

author: FrankBuss
author_rank: Unknown
timestamp: December 09, 2017, 04:57:33 pm
content: No, that's not the problem. The electrical characteristics says 2.0 V min for 4.5 V to 5.5 V supply voltage. And I tried it with 3.3 V supply voltage as well and doesn't work.

author: FrankBuss
author_rank: Unknown
timestamp: December 09, 2017, 06:31:46 pm
content: I found the problem: I forgot the line "ANSELC = 0", because for this PIC, not only the PORTA pins are set to analog input after reset, but the PORTC pins as well, and it doesn't even change if you enable the UART receiver. Works even at 1 MHz now.Sometimes I feel like I'm solving a puzzle when trying to initialize the PIC registers by hand.

author: AndyC_772
author_rank: Super Contributor
timestamp: December 09, 2017, 07:20:36 pm
content: You ARE solving a puzzle, but the solution is a really useful one to know. The fact that PIC pins initialise to analogue mode catches a lot of people out; you're not the first and certainly won't be the last.

author: FrankBuss
author_rank: Unknown
timestamp: December 09, 2017, 09:06:25 pm
content: I knew this already from other PICs, and the datasheet says it, too, was just confused because with other PICs there is no ANSELC. BTW, I think the datasheet is wrong: It says I need to disable the ANSEL bit for the TX pin as well, but it worked without changing anything, which of course can confuse someone even more that RX doesn't work then.PS: earlier I wrote that the high level threshold is 2 V for supply voltage 4.5 V to 5.5 V, which is only true for pins in TTL mode. When using it with the EUSART module, the pin uses the Schmitt Trigger levels (ST), which requires min 0.8 Vdd to detect high level, so 4 V for 5 V supply voltage. I guess in reality it is lower, because it worked nevertheless.