[sdiy] That code for the pic CCP Timer
Fredrik Carlqvist
ifrc at iar.se
Wed Mar 16 14:39:05 CET 2005
>Someone sent me a code snippet to use the CCP on the pic to generate
Interrupts at a specified frequency.. any chance they can resend?
That would be me... Resending:
Don't forget to use the fast return feature on PIC18.
If you are using a PIC with a CCP unit, you can use that easier/better than
just a timer. The CCP method keeps the overhead to a minimum.
I would use the interrupt from CCP1 in compare mode with the side effect
enabled: to clear the timer. The only thing you will have to do is to write
the interrupt period time into the CCP registers (CCPR1L and CCPR1H). Every
time the timer reaches the value in the period registers, it will be
automatically reset and a CCP1IF interrupt will be generated. Inside the
interrupt service routine, you just read out the next wave value and output
it to the R-2R ladder and return.
In the main loop, you keep the period registers CCPR1L and CCPR1H updated.
Make sure that you never accidentally write zero into them (for example when
changing from 0xFF to 0x100 and writing the low byte first)! Polling the
UART for MIDI data, you will probably need a table to look up appropriate
interrupt intervals for different MIDI note numbers.
This works the same way on both PIC18 and PIC16 architectures. Using PIC16,
I would enable the CCP1IF interrupt only, and poll all other things, like
MIDI data in for example. Probably, you will have no choise but to write the
application in assembly, because the interrupt overhead of a C compiler
would probably limit the top frequency severely.
On PIC18, I would keep the CCP interrupt at high priority and any other
interrupts on low.
The following example sets up the CCP and TIMER1 units to form a DCO on a
PIC16:
// set up CCP oscillator (PIC16F818)
T1CON = 0x01; // enabled, no prescaler
CCP1CON = 0x0B; // special event trigger
CCP1IE = 1;
If you need more details, don't hesitate to ask!
Good luck!
Fredrik C
More information about the Synth-diy
mailing list