View previous topic :: View next topic |
Author |
Message |
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
Periodic interrupt 30 uS |
Posted: Wed May 07, 2014 4:34 am |
|
|
Hi all, I've never used the CCP before, and I need to have a regular 30 uS interrupt.
I have created this code, but I don't get any interrupts.
Code: |
#define T3INTERVAL 960
void sample_init(void)
{
// timer
setup_timer_3(T3_INTERNAL | T3_DIV_BY_1);
// ccp
setup_ccp3(CCP_COMPARE_INT);
enable_interrupts(INT_CCP3);
CCP_3 += T3INTERVAL;
}
#int_ccp3
void timer3ccpinterrupt()
{
CCP_3 += T3INTERVAL;
}
|
compiler V 5.025
PIC 18F26K22
clock is 64 MHZ |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed May 07, 2014 5:17 am |
|
|
Your 'code' is just code..not a real program so no one here can 'cut/paste/test'.
Please post your entire program,it'll make life a lot easier.
hth
jay |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Wed May 07, 2014 6:39 am |
|
|
temtronic wrote: | Your 'code' is just code..not a real program so no one here can 'cut/paste/test'.
Please post your entire program,it'll make life a lot easier.
hth
jay |
Heres my test program.
Code: | // testccp.c
#include <18F26K22.h>
#device CONST=ROM
#device ADC=8
#FUSES BROWNOUT //No brownout reset
#FUSES PUT //Power Up Timer
#FUSES HSM //External oscillator
#FUSES PLLEN //enable the x4 PLL
#use delay(clock=64M, crystal = 16M)
#define CLOCK PIN_C0
#define T3INTERVAL 960
void sample_init(void)
{
// timer
setup_timer_3(T3_INTERNAL | T3_DIV_BY_1);
// ccp
setup_ccp3(CCP_COMPARE_INT);
enable_interrupts(INT_CCP3);
CCP_3 += T3INTERVAL;
}
#int_ccp3
void timer3ccpinterrupt()
{
CCP_3 += T3INTERVAL;
output_high(CLOCK);
output_low(CLOCK);
}
void Initialise()
{
delay_ms(100);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
sample_init();
enable_interrupts(GLOBAL);
}
void main()
{
Initialise(); //configure uC for operation
while(1);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed May 07, 2014 9:35 am |
|
|
Why do it with the CCP?.
Timer2. T2_DIV_BY_4. PR2=143.
Interrupt every 2304 clocks. 36uSec.
Keep it simple.
The reason it is not working, is the timer. CCP_USE_TIMER3_AND_TIMER4 is needed. Currently the CCP is running off timer1 (the default).
Best Wishes |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Thu May 08, 2014 1:28 am |
|
|
Thanks Tt. |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Thu May 08, 2014 3:10 am |
|
|
Supplementary question:-
What is the interrupt latency? is it 3 cycles + 2 to stack the PC? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu May 08, 2014 3:21 am |
|
|
No. About 30 instructions.
_Every_ register used inside the interrupt code, has to be saved.....
The compiler (unfortunately) defaults to saving everything.
Now, if your interrupt code is simple. One interrupt, and you only do basic operations like talking to an I/O pin, or incrementing a counter, then you can elect not to save the registers, by writing the handler as 'INT_GLOBAL'. You then save just the registers you need. Still be perhaps five instructions minimum. Remember your code then also needs to clear the interrupt flag, and restore the registers before exit. On the PIC18, certain basic registers (BSR, STATUS, and WREG), are saved for you by the hardware. These are restored by the RETFIE 1 instruction at the end of the handler. Unfortunately the saving this produces is 'lost' because of the number of other registers present.
The PIC is not the chip to choose for really fast interrupt handling. Many much older chips have the ability to save all the registers with just one instruction, or have alternative register sets for interrupt use. |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Thu May 08, 2014 10:17 am |
|
|
Yikes!
Is it 30 on entry plus 30 to restore on exit? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu May 08, 2014 11:30 am |
|
|
Actually about 28 each end. However if you reckon on 60 instruction times to get into and out of an interrupt you won't be far off.... |
|
|
|