nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

UART communication between Raspberry Pi 3 and PIC18F4550

Tue May 29, 2018 2:49 am

Hi everyone.
I need to send some data from PIC18F4550 to the Raspberry Pi 3, using UART communicattion. I just need to send some values (numbers, the instantaneous velocity of a robot), and then in Raspberry I will print this number on the screen.
This is the code for PIC18F4550 (the pins are connected correctly):

Code: Select all

void main(void) 
{
    ADCON1 = 0x0F;  
    TRISB = 0;
    TRISC = 0b10000010;
    TRISD = 0;
    unsigned char UART1Config = USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & 
    USART_BRGH_HIGH
    unsigned char baud = 311;
    OpenUSART(UART1Config,baud); //Open USART communication
    unsigned char teste = '3';
    while(1)
    {
        WriteUSART(teste);
    } 
}
I'm using PLIB libraries and MPLAB IDE for the PIC project. The baud rate is 311 following the formula from this link (I'm using FOSC = 48MHz): https://singularengineer.com/programmin ... 8f-serial/

This is the code for Raspberry Pi 3:

Code: Select all

import RPi.GPIO as GPIO
import time
import sys
import serial

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

ser = serial.Serial("/dev/ttyS0", 9600)
GPIO.setup(10, GPIO.IN) #RX
GPIO.setup(8, GPIO.OUT) #TX
while True:
    velocity= ser.read(1) #Read one byte
    time.sleep(1)
    if velocity== 3:
        print('yes')
        break
    else:
        print('no')
GPIO.cleanup()
I made the initial configurations, like going to "sudo leafpad /boot/config.txt" and disabling serial... etc.
But I'm getting just "no" answers, when both running. What I'm doing wrong?

Thanks for any help!

klricks
Posts: 5594
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu May 31, 2018 1:11 am

nicolasludwig wrote:
Tue May 29, 2018 2:49 am
......
This is the code for Raspberry Pi 3:

Code: Select all

import RPi.GPIO as GPIO
import time
import sys
import serial

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

ser = serial.Serial("/dev/ttyS0", 9600)
GPIO.setup(10, GPIO.IN) #RX
GPIO.setup(8, GPIO.OUT) #TX
while True:
    velocity= ser.read(1) #Read one byte
    time.sleep(1)
    if velocity== 3:
        print('yes')
        break
    else:
        print('no')
GPIO.cleanup()
I made the initial configurations, like going to "sudo leafpad /boot/config.txt" and disabling serial... etc.
But I'm getting just "no" answers, when both running. What I'm doing wrong?

Thanks for any help!
The port should be /dev/serial0

Serial should be enabled in config.txt not disabled.
Serial console should be disabled in cmdline.txt
It's easiest to use Interfaces menu in sudo raspi-config to do the settings for you. Answer NO to serial console and YES to serial hardware... reboot....

Do a loopback test to verify serial is working....... install minicom or cutecom (GUI)... connect TX to RX.... Typed characters should be echoed back.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

theonecalledtom
Posts: 9
Joined: Fri May 18, 2018 7:06 pm

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu May 31, 2018 2:01 am

I've read that you may need to edit your boot commandline to remove the use of the serial port. sudo nano /boot/cmdline.txt

klricks
Posts: 5594
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu May 31, 2018 2:24 am

theonecalledtom wrote:
Thu May 31, 2018 2:01 am
I've read that you may need to edit your boot commandline to remove the use of the serial port. sudo nano /boot/cmdline.txt
Yes but using sudo raspi-config will do the edits to cmdline.txt and config.txt for you.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu May 31, 2018 7:24 pm

klricks wrote:
Thu May 31, 2018 1:11 am
The port should be /dev/serial0

Serial should be enabled in config.txt not disabled.
Serial console should be disabled in cmdline.txt
It's easiest to use Interfaces menu in sudo raspi-config to do the settings for you. Answer NO to serial console and YES to serial hardware... reboot....

Do a loopback test to verify serial is working....... install minicom or cutecom (GUI)... connect TX to RX.... Typed characters should be echoed back.
Alright, I did it. Made the configurations with sudo raspi-config, connected TX and RX of Raspberry and installed Minicom, but I can't write anything on terminal. I get stuck at the terminal, as you can see on the image link below.
https://imgur.com/a/f6f05pz

klricks
Posts: 5594
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu May 31, 2018 11:24 pm

nicolasludwig wrote:
Thu May 31, 2018 7:24 pm
........
Alright, I did it. Made the configurations with sudo raspi-config, connected TX and RX of Raspberry and installed Minicom, but I can't write anything on terminal. I get stuck at the terminal, as you can see on the image link below.
https://imgur.com/a/f6f05pz
Still using the wrong port.... As I mentioned above It's /dev/serial0
Start minicom like this:

Code: Select all

minicom -D /dev/serial0
To quit CTRL a then q
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Fri Jun 01, 2018 12:13 am

klricks wrote:
Thu May 31, 2018 11:24 pm
nicolasludwig wrote:
Thu May 31, 2018 7:24 pm
........
Alright, I did it. Made the configurations with sudo raspi-config, connected TX and RX of Raspberry and installed Minicom, but I can't write anything on terminal. I get stuck at the terminal, as you can see on the image link below.
https://imgur.com/a/f6f05pz
Still using the wrong port.... As I mentioned above It's /dev/serial0
Start minicom like this:

Code: Select all

minicom -D /dev/serial0
To quit CTRL a then q
Of course, my bad. Yes, it's working with Minicom, but still doesn't work with PIC18F4550.
The variable "velocity" is printing just " b'\x00' ", even when the connections with PIC are on.
I think the command for read the byte from the PIC is incorrect.
Any idea?

klricks
Posts: 5594
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Fri Jun 01, 2018 12:44 am

nicolasludwig wrote:
Fri Jun 01, 2018 12:13 am
.......
Of course, my bad. Yes, it's working with Minicom, but still doesn't work with PIC18F4550.
The variable "velocity" is printing just " b'\x00' ", even when the connections with PIC are on.
I think the command for read the byte from the PIC is incorrect.
Any idea?
I don't think you are supposed to have these lines. The pins are already configured as serial port at boot (config.txt).
I think these will turn the pins back into normal GPIO's. Not what you want.

Code: Select all

GPIO.setup(10, GPIO.IN) #RX
GPIO.setup(8, GPIO.OUT) #TX
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Fri Jun 01, 2018 1:12 am

klricks wrote:
Fri Jun 01, 2018 12:44 am
I don't think you are supposed to have these lines. The pins are already configured as serial port at boot (config.txt).
I think these will turn the pins back into normal GPIO's. Not what you want.

Code: Select all

GPIO.setup(10, GPIO.IN) #RX
GPIO.setup(8, GPIO.OUT) #TX
I commented these lines, and nothing changed.

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Sat Jun 02, 2018 7:00 pm

Anyone have an idea? It's a simple thing and I'm stuck in it. :(
Thanks!

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu Jun 21, 2018 12:02 am

Hi guys.
I came back to this job and I made it work. The PIC is sending the correct char to Raspberry, I had forgotten to interconnect the GND.
But, when I'm sending the char 'a' from the PIC, printing the variable "velocity" on Raspberry it returns -> b'a' <- (without the arrows, of course).

How can I get the "b" out of print?

User avatar
DougieLawson
Posts: 33053
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu Jun 21, 2018 12:24 am

Code: Select all

#!/usr/bin/python3

bytes_1 = b'some bytes from serial here'
string_1 = bytes_1.decode()

print (bytes_1)
print (string_1)
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu Jun 21, 2018 12:42 am

DougieLawson wrote:
Thu Jun 21, 2018 12:24 am

Code: Select all

#!/usr/bin/python3

bytes_1 = b'some bytes from serial here'
string_1 = bytes_1.decode()

print (bytes_1)
print (string_1)
Thank you very much, it worked!

nicolasludwig
Posts: 11
Joined: Tue May 29, 2018 2:27 am

Re: UART communication between Raspberry Pi 3 and PIC18F4550

Thu Jun 21, 2018 1:18 am

Another problem... I can't receive a diferent char on my loop.

Code: Select all

while True:
	v = ser.read(1) #Read one byte
	valor = v.decode()
	print(valor)
	time.sleep(1)
I'm getting just the first char I write on PIC, the others are not printing on Raspberry. If I reset the code and start again, then the last char sended is printed.
What's wrong?

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 5 guests