andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Connection between Pi and PIC

Sun Mar 17, 2013 9:02 pm

Hello! Before I start I would like to say that I'm a complete novice with microcontrollers and would like to be treated as one and receive answers acordingly... more like "Q: How many legs does a human have? A: A human has two legs, one in the left, one in the right.", if you know what I mean. I know these questions may seem basic to some, but please, if you can, help me.

I recently started a project in which I have to make a connection between a RPi and a PIC32mx695f512h. Problem is... I don't even know how to start. All I know is that I need to write some protocols on both and that's it. So my questions are:

1. What protocols should I use?
2. How do I program the Pi?
3. How do I program the PIC?
4. How do I connect the two?
5. What software do I have to use for both?

I would like to use python for the Pi and C for the PIC. And I have a PICKit3 also.
Any help would be useful. Thank you!

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Connection between Pi and PIC

Mon Mar 18, 2013 12:16 am

Hi, I would suggest some reading. I bought this book in December and it covers the basics including using the GPIO pins. From there you need to read how to program micro controlors and pics (not in the book). I would suggest starting with an Arduino and learn that as well. Then you can start to learn how to get the both to communicate. Take it slow and read read read. Depending on how mcuh time you put into reading both subjects it should not take that long to pick up. Both platforms are easy to learn.

Good luck.


http://www.amazon.com/Raspberry-User-Gu ... 111846446X

DBryant
Posts: 281
Joined: Sat Feb 02, 2013 12:41 pm
Location: Berkshire, UK

Re: Connection between Pi and PIC

Mon Mar 18, 2013 9:13 am

As suggested do some reading. Google for PIC and you will find a wealth of material, for example http://www.voti.nl/swp/ has some useful introductory comments (maybe). I do the same but use Arduino in place of PIC, and hopefully you will soon be up and running.

Probably a key issue for you, certainly in refining your search will be how the two devices are to be connected. If you have free choice then a wired connection using simple serial messages may be a good place to start; you could then Google "pic serial libraries" and find for example, http://microcontrollerprojects00.blogsp ... -uart.html and https://projects.drogon.net/raspberry-p ... l-library/. And don't forget to search the forums, certainly the last URL I've seen via posts here.

Probably all appears quite daunting so the previous comment to take things slowly is most sound advice. And your reading will re-define the sort of questions you are asking, which are worthy of pages of response.

Exciting times ahead!

andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Re: Connection between Pi and PIC

Mon Mar 18, 2013 4:00 pm

Exciting times indeed. Thank you for your responses. Unfortunately, for this project I got myself in, I don't have that much time to start from down to above. If you can, I would like you to give me more straightforward answers and for the terms I don't understand, I could research there. :D

DBryant
Posts: 281
Joined: Sat Feb 02, 2013 12:41 pm
Location: Berkshire, UK

Re: Connection between Pi and PIC

Mon Mar 18, 2013 5:33 pm

To program the Pi
For Python you will need to install the Python interpreter.
To come up to speed in Python you might consider installing the interpreter on a Windows system e.g. http://www.activestate.com/activepython. Any scripts you write can be transferred to the Pi and might be expected to execute without change.
To access the GPIO then you might consider wiringPI which has a Python interface; another post was bemoaning the lack of documentation in this area. Perhaps others can shed light in this area.

To program PIC.
I don't do PIC but looking at the URL I posted earlier its sounds very much like the Arduino work flow.
For Arduino there is an IDE that can be downloaded in which you can load, create, edit, compile and upload sketches (in Arduino speak, but pieces of C++ when it comes down to it). The editor is a bit on the basic side but it does a job. Compilation is part of the IDE but will be cross-compiling from Windows and/or Linux (Intel-based) to that required for the microcontroller. The upload simply squirts the compiled binary down a serial/USB line to the controller. As I say, the elements are probably similar on the PIC-side.

To connect the two.
A couple of wires for Tx/Rx to produce a serial comms link.
For an 'Arduino end' I'd used something like SoftSerial, which allows any two pins to be constructed as a serial connection. The 'Arduino style' includes a loop in which you can scan for input characters on the line, process that, carry out actions and maybe send response messages. All bog standard stuff, done by many before and hence my earlier suggesting to search out serial libraries for PIC.
For the Python end you need to look for exactly the same; for historical reasons I use Tcl, but basically you can select a USB port, configure its speed, handshake etc, and write code to read and write through it. The Python docs are pretty good as far as my experience goes, and you will find code fragments for all of this stuff. To read data you'd have an event handler to respond to data on the lines, handle dropped connections and the like and also process the input to carry out actions. These actions might be to respond with write of data back down the same line.

For a protocol.
Well the serial bit is underpinning the whole thing, so to keep it simple you might just squirt Ascii messages down the line, possible comma-separated and terminated by LF. Depends very much on the exact type of messag going back and forth. At both PIC and Python ends you read lines of text and simply chop it up on the basic of the commas. It would work and later you could improve the robustness of the messaging (say a type-length-value (TLV) protocol or some more advanced) and indeed there will be libraries out there to help you. But of course you need the libraries that have the same protocol on the PIC and Python platforms, so you might do something very simply just to get up and running,

What software?
Well its all mentioned above - for the Pi Python interpreter and they do an IDE too, or just use your favourite editor. Plus any libraries that you decide to use. And a PIC IDE if it exists and for what platforms you'd have to investigate.

Hope this kicks things off for you

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Connection between Pi and PIC

Mon Mar 18, 2013 10:59 pm

andreitudora wrote:Exciting times indeed. Thank you for your responses. Unfortunately, for this project I got myself in, I don't have that much time to start from down to above. If you can, I would like you to give me more straightforward answers and for the terms I don't understand, I could research there. :D
On the Pi, you can edit and test Python programs by running IDLE from the desktop. You may also need to look into something called PYSERIAL which is an add-on module for Python. A good general book about Python is 'Python programming for the absolute beginner' by Mike Dawson. It starts off assuming you've never written a computer program before. By the end of the book you are doing some pretty complicated stuff. There are also some good online resources for learning Python as well: Try googling 'Invent With Python' and 'learnpythonthehardway'

For the PIC, Microchip (the makers of all of the PIC series microcontrollers) supply a free 'Integrated Development Environment', (commonly abbreviated to 'IDE'), called 'MPLAB'. It is definitely available for Windows. Not sure about other platforms.

If you have a PICKIT3, you almost certainly got a disc with it with MPLAB on it.

In MPLAB, you get the basic tools you need to write programs for more or less every PIC microcontroller which Microchip make. The main language supported is Assembly Language, but over the years MPLAB has also included bundled cut-down or trial versions or limited device-range versions of 'C' compilers such as those by Hi-Tech. If you already installed MPLAB, did it offer to install any 'C' compilers as it installed?

You are quite specific about the PIC chip you intend to use: May I take it that is the chip which is already fitted to the demo board which came with the PICKIT 3?

If you've never programmed any microprocessor before, you are going to have some pretty steep stairs to climb if someone is pushing you to do something within a deadline.

Don't even try to connect the Pi and the PIC together at first - your first aims are:
-Research how to write some Python code on the Pi to flash a LED connected to one of the GPIO pins. (Search these forums).
-Modify that code so it reads the state of a switch connected to one of the other GIO pins and sets the LED on or off accordingly.

-On the PIC, same thing, research how to flash a LED connected to one of the port pins. There's a good chance that the example code which came with your PICKIT 3 includes some routines to flash LEDs.
-Then modify the code so that the LED shows the state of a switch connected to one of the other port pins.

At this point, you have the makings of the most basic kind of communication between the Pi and the PIC. If the PI's data output is connected to the sensing input on the PIC and the PIC's data output is connected to the sensing input on the PI, then each is able to send a signal to the other. Note that if you directly connect the Pi and a PIC together like this, the PIC MUST be running on 3V, not 5V. The 0V or 'ground' lines on both devices must also be connected together.

The most basic possibility is that the sending device just turns its output either on or off and the receiving device detects that and does something in response.

A more sophisticated version has the sending device sending a train of eight pulses with equal spaces in between, where a long pulse equals a binary 'one' and a short pulse equals a binary 'zero'. The receiving device measures the length of each pulse as it arrives, decides whether it is a long pulse (representing '1') or a short pulse (representing '0'), and assembles the received '1's and '0's into an 8-bit number, which can have one of 256 possible values. Each one of these 256 possible values can be a different command to your receiving device. This is only one of many possible forms of 'serial data', and is similar to the signalling method used by some remote controls. Generating and handling serial data of any sort by directly manipulating or reading the data inputs and outputs is known as 'bit banging'. It's a good thing for a beginner to attempt because it leads to a good understanding of how serial data works. Serial data formats commonly used for inter-device communications are a little bit different from the format I've just described, but are also slightly more difficult to bit-bang, especially on the receiving end of things.

However, most modern microcontrollers (and the Pi) have built in hardware for handling the inputting and outputting of serial data for you - most commonly called a UART, but always something which sounds quite similar to that - in the PIC chips that I am familiar with, the onboard UART is called the 'USART'. For using the Pi's built in serial UART, search these forums for instances of 'Serial' and 'UART', especially in the PYTHON programming forum. For information about the UART built into PIC chips you will need to refer to the Microchip datasheet for your specific chip, and again, if the chip is the one on the board included with the PICKIT 3, you should find the datasheet for that chip somewhere on the CD which came with it.

In and of themselves, UARTS only send and receive raw numbers. It's up to you what those raw numbers are used to represent. The word 'protocol' is a very broad term indeed. Broadly, it is the name given to whatever system of message passing both devices will use to communicate with each other. A really simple 'protocol' could be as simple as sending a number between 0 and 255. The receiving device receives the number and performs some action based on the number it has been given. You could call this the 'Chinese restaurant menu protocol', or anything else you like. The point is that if you are writing the code which these devices will use to communicate with each other and no other devices will be involved, you are entirely free to invent a protocol yourself.
Last edited by SiriusHardware on Wed Mar 20, 2013 6:36 pm, edited 4 times in total.

PhillyNJ
Posts: 112
Joined: Sat Dec 29, 2012 4:07 am

Re: Connection between Pi and PIC

Tue Mar 19, 2013 12:59 pm

@DBryant and @SiriusHardware - Well done. It's users like you that help the community grow. I hope that @andreitudora is able to pick this up. For me when I started it was very daunting, but once I started to read, It became clear that it is not very hard at all. For me, it comes down to reading on electronics, datasheets and watching a bunch of youtube videos.

Exciting times indeed!

andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Re: Connection between Pi and PIC

Thu Mar 21, 2013 9:38 pm

I would like to thank you very much for your responses.
I'm passed writing python code on the Pi or C code on the PIC. I've managed to use the GPIO pins on the Pi and test them with a led, also, with MPLAB I've done the same for the PIC. My research led me to choose on connecting the two between SPI and yes, there aren't any other devices between this connection regarding the use of protocols. What I'm struggling right now is activating the SPI on my Pi. It's new and never had an update of firmware which I found out that it's necessary in order for it to activate the SPI. I have to update the service packages, the firmware and kernel, but... I'm on a proxy server and I've just managed to make the internet work, meaning I set the proxy ip in the terminal and in Midori. And te updates don't work, it won't connect to the necessary sites. I think I made the internet work only on my browser, not everywhere and so... I need your help again.

1. What can I do to make the updates work?
2. Regarding the protocols, can I receive more info on that?
3. For the PIC, I've found that I can use the plib.h library to set the pins, but... I don't know if I have to download it or if it's included in MPLAB. Any suggestions on this?

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: Connection between Pi and PIC

Thu Mar 21, 2013 10:01 pm

I am aware that you wrote you want to use PIC32, and my experiece is with PIC12, PIC16 and PIC18 but to try to give you some tips which may or may not be useful:
andreitudora wrote: 2. Regarding the protocols, can I receive more info on that?
Regarding protocols for the PIC there is good documentation from Microchip about various compilers and the libraries, C18 or XC8 for example, easiest to find through google but beware the documents are unfortunately very big and can for hard to explain reasons differ from chip to chip. Also if you go into the directories of your MPLAB you find somewhere like
"C:\Program Files (x86)\Microchip\mplabc18\v3.44\doc "
the rather useful Peripheral Library Help Resource (this is at least for the C18 compiler the case. The Microchip.com forums are also not bad, though in some theme areas too full of complicated and less than useful posts with tips like "Read the manual", which always bothers me cos they're mostly pdfs with 400+ pages.
andreitudora wrote: 3. For the PIC, I've found that I can use the plib.h library to set the pins, but... I don't know if I have to download it or if it's included in MPLAB. Any suggestions on this?
plib.h should be included in your compiler somewhere, for example on a windows machine under something like
"C:\Program Files (x86)\Microchip\xc8\v1.12\include".
If MPLAB is not doing a red underscore or not complaining about this when you build then that should be fine.

One small point: SiriusHardware correctly wrote

Code: Select all

If you have a PICKIT3, you almost certainly got a disc with it with MPLAB on it. 
however this may be the older version, not the curent MPLABX. There are some confusing differences. I personally use the MPLABX I have more success with that.

andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Re: Connection between Pi and PIC

Thu Mar 21, 2013 10:15 pm

I am also using MPLABX. Thanks for the response. I'll probably have more questions later. :D

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Connection between Pi and PIC

Thu Mar 21, 2013 10:41 pm

If you have a choice then don't use I2C for the connection as the Pi has a bug in its I2C interface.

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: Connection between Pi and PIC

Fri Mar 22, 2013 5:34 am

Gert van Loo wrote:If you have a choice then don't use I2C for the connection as the Pi has a bug in its I2C interface.
Wow I wasn't expecting that! Is this fixable / going to get fixed?

andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Re: Connection between Pi and PIC

Fri Mar 22, 2013 7:32 am

Gert van Loo wrote:If you have a choice then don't use I2C for the connection as the Pi has a bug in its I2C interface.
I'm going to use SPI, but I can't get the Pi to update! :(

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Connection between Pi and PIC

Fri Mar 22, 2013 6:06 pm

andreitudora wrote:
I'm going to use SPI, but I can't get the Pi to update! :(
Where did you hear that SPI is broken and needs updating? I'm not saying that is incorrect, I just don't remember hearing or reading about this anywhere myself. If it was broken, I would be interested to know when / on what date / in which version of Raspbian it was fixed.

May we ask which image you originally flashed the SD card with? For example, the most up to date Raspbian is "2013-02-09-wheezy-raspbian.zip". Are you running an older version?

Although it might seem drastic, re-flashing your SD card with the very latest version of your OS might fix some or all of your problems - of course you would need to copy any Python code files you've made (and any other work you have done) off the SD card onto safe storage first. Or - if you'd rather keep your current SD card the way it is and you have a spare card handy, try flashing the latest image to the spare card and see if what you are trying to do works on that one.

If none of these options appeal to you, try to update one more time (it will fail again) and this time post the exact failure messages you are seeing, here in a 'code' window. Someone with better Linux knowledge than myself should be able to suggest a solution, if you can supply enough information about the problem.

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: Connection between Pi and PIC

Fri Mar 22, 2013 6:11 pm

I thought SPI was just not incuded as standard or something, and had to be set up. I would just suggest starting a new topic when posting your error though, because I suspect many of the real Linux experts are not watching this topic.

andreitudora
Posts: 16
Joined: Sat Mar 09, 2013 1:34 pm

Re: Connection between Pi and PIC

Fri Mar 22, 2013 7:40 pm

I have the latest version of Raspbian, but I also followed the instructions on this link http://neophob.com/2012/08/raspberry-pi ... pi-device/

In the end, I went with my Pi to someone who doesn't have internet with proxy and done those updates.
The next step in my project is connecting through SPI my Pi and the PIC32. Can you give me any leads on this?

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Connection between Pi and PIC

Mon Mar 25, 2013 1:03 am

andreitudora wrote:I have the latest version of Raspbian, but I also followed the instructions on this link http://neophob.com/2012/08/raspberry-pi ... pi-device/

In the end, I went with my Pi to someone who doesn't have internet with proxy and done those updates.
The next step in my project is connecting through SPI my Pi and the PIC32. Can you give me any leads on this?
For the PIC side of things, have a look at this page on Microchip's website, which offers a lot of datasheets, application notes and so on for the chip you mentioned earlier.

http://www.microchip.com/wwwproducts/De ... e=en545654

Scroll down until you see the heading 'Code examples', and then down a little further until you see 'PIC32 SPI code example'. Download that and have a look through it. I only glanced at it briefly but the example code appears to be written in 'C' which is your language of choice.

For a detailed document about the use of the SPI interface on PIC32 family devices, scroll a little further down the same page to the heading 'Reference manual' and then a little further down to

'PIC32 Family Reference Manual, Sect. 23 Serial Peripheral Interface'. Download and read that as well.

For the Pi side of things, there is a lot of general discussion about SPI in this forum under the 'Hardware And Peripherals' heading under 'Interfacing (DSI, CSI, I2C, etc.)' .Go there and search for 'SPI'.

You might also find this page a useful starting point, but read the comments below the article as they may contain relevant observations or corrections.

http://www.cmdrkeen.net/2012/11/06/play ... on-the-pi/

Hope some of this helps. Let us know how you get on.
Last edited by SiriusHardware on Mon Mar 25, 2013 1:10 am, edited 1 time in total.

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Connection between Pi and PIC

Mon Mar 25, 2013 1:08 am

PS1981 wrote:I thought SPI was just not incuded as standard or something, and had to be set up.
I think you are quite close to the mark there - it appears that the SPI drivers are 'blacklisted' by default so they don't load, so you have to un-blacklist them in order to make the SPI port available for use. The article I linked to in the previous post seems to explain how to do that.

The article the OP linked to also presents that information, but in addition, it seems to suggest that the Kernel at the time of writing of that article did not support SPI. I would be surprised if the current (Early 2013) image of Raspbian does not contain a newer version of the Kernel which already supports SPI, but it is probably still blacklisted by default to leave the relevant pins usable as general purpose I/O.

kinsa
Posts: 457
Joined: Sat Dec 01, 2012 10:16 pm

Re: Connection between Pi and PIC

Sat Mar 30, 2013 9:01 am

Hi,

Take a look here: http://www.raspberrypi.org/phpBB3/viewt ... 37&t=33809

I've successfully used SPI with PIC32.

Cheers!
42

paulwatford
Posts: 8
Joined: Mon Dec 22, 2014 8:37 pm

Re: Connection between Pi and PIC

Mon Dec 22, 2014 8:39 pm

Hi Try this link:

http://tronixstuff.com/2011/08/26/tutor ... -io-ports/

Works perfectly for PI as well,

Moses_q8
Posts: 1
Joined: Tue Oct 25, 2016 5:06 pm

Re: Connection between Pi and PIC

Tue Oct 25, 2016 5:19 pm

hi
peace upon you
I have a senior project : implementing a system park car
using raspberry pi, pic, controller, ultrasonic sensor, speaker, and lcd
I do not know which pic to use and why I choose it????
how to program the pi?
how to program the pic ??
how to connect them??
how to connect the controller and the Lcd to the pic??
may you reply me as soon as possible ??
thanks

faithful: moses

Return to “Beginners”