Wednesday 10 June 2009

PIC 18F4550, Interrupts and USB

One of the main challenges for this unit is to read data, store it and also have USB connectivity.
Obviously the data must be read with high precision timings so I decided to use an interrupt in order to accurately take data samples.
The PIC18F4550 is capable of doing high and low priority interrupt service routines.
I decided to use a high priority interrupt service routine (HISR) for the data acquisition samples and a low priority interrupt service routine (LISR) for doing other things, such as checking if any switches have been pressed and updating the LCD display.

I based all of the code on the Microchip USB framework 2.4 examples, using the 'USB Device - MCHPUSB - Generic Driver Demo' as the basis for my program.

After a lot of going around in circles here are some notes and pointers and things I have learnt:

You must use the polling method for USB if you are using the HISR for something else.
As log as the USB routine is polled every 100ns or so then its all OK. Do not try to use interrupts along with interrupt driven USB, as they dont get along, as much as I have found. There is probably a way to do this but still...

The clock frequency is not the same as the oscillator frequency. I am using an oscillator freq of 20MHz, but, in order to use the USB routines, there is an internal phase locked loop (PLL) which changes the frequency to 48MHz. This is the clock frequency being used. This messed up all my timings and caused much head scratching for quite a while.

Most other things are pretty simple - I'm using timer0 for the HISR and timer 2 for the LISR, with priority set in the interrupt set-up parameters.

I can now read data in the HISR, display data and look at the switches in the LISR, all while the USB connection is working in the background.

Now I need to sort out saving data onto SDcards....