PIC18 SPI slave ADC (for RPi)

Thread Starter

nsaspook

Joined Aug 27, 2009
13,311
This is a clip of the Raspberry PI using a PIC18 (Powered at 3.3 Volts from the PI) as a 12 channel external ADC via SPI. The RPi program just reads all 12 channels and displays the data. The hex byte 4C is a configuration byte sent from the PIC that tells the host it's ADC configuration.

Rich (BB code):
/*
 * bit 7 high for command
 * bit 6 0 send lower or 1 send upper byte ADC result first
 * bits 3..0 port address
 *
 * bit 7 low  for config data sent in CMD_DUMMY per uC type
 * bits 6 config bit code always 1
 * bit    5 0=ADC ref VDD, 1=ADC rec FVR=2.048
 * bit  4 0=10bit adc, 1=12bit adc
 * bits 3..0 number of ADC channels
 * 
 */
Channel 0 is connected to a pot for input voltage adjustment, channel 1 is connected to VDD , and the rest are just floating. It's a mess of wiring with breakouts so the signals can be easily monitored. :D

http://flic.kr/p/dmagUX

PIC firmware C program:
https://github.com/nsaspook/daq_gert/blob/master/RPi_PIC/SlaveO.c
RPi testing C program:
https://github.com/nsaspook/daq_gert/blob/master/pictest.c
 
Last edited:
This is good stuff, thanks for your work and bringing it to our attention. I finally got a Pi to play with this winter, so will be diving in sometime in the near future. I am a real newbie on the microprocessor and linux end of things.

I took a brief look at your code and had a few general questions that I'm sure is obvious to others.

1) Is the SPI library for the Pi using a hardware SPI module on the BRCM chip, or is it software?

2) Can the Pi use SPI or other hardware interrupts like the UART?

3) Do you have links to how a beginner can compile project code and the toolchain involved?

4) Is there a link to the repository of libraries and library commands associated with low level type of microcontroller functions (UART, I2C, PWM etc.)?
 

Thread Starter

nsaspook

Joined Aug 27, 2009
13,311
1. The SPI is using the hardware on the chip, the BCM2835 has a SPI master.
2. The Linux kernel separates direct SPI hardware manipulation from the Userland via a set of message and process queues so only at the direct kernel driver level (like my module) do you have full access to them. It's not something a beginner should mess without knowing the danger because there is little to stop you from causing massive disk corruption from overwriting kernel memory.

This is the basic information needed to understand the structure: http://lwn.net/Kernel/LDD3/
A HOW-TO to install my module in a RPi system: https://github.com/nsaspook/daq_gert/blob/master/RPi_Comedi_HOWTO.txt

3. User programs that use SPI (or any IO) device open and operate them with the IOCTL interface (files) or a device library (Comedi, etc) that abstracts that device into a standard C program interface. So programming something on the RPi is like any normal system that has a C (gcc) compiler. I use Netbeans on a x86 Linux system to write the software and then compile it on the RPi using a NFS mounted drive they share.

4. This should be a good starting place for hardware interface software. https://projects.drogon.net/raspberry-pi/
 
Last edited:
Thanks for the links and info, looks like I have plenty of reading to do now too :). Time to load some software and dig in. I had a go at OpenWRT with a PIC, but only got as far as ECHO xx to the ...../USB0 (or something like that)....lol.
 
Top