View previous topic :: View next topic |
Author |
Message |
Andreas
Joined: 25 Oct 2004 Posts: 136
|
delay makes wrong values ? |
Posted: Tue Mar 25, 2014 9:24 am |
|
|
Hello Friends
After starting this project I had troubles to get the delay_ms(10) function to work I reduced my project to the minimum and now I got a very interesting behaviour.
The delay_ms(10) works perfect as long as the timer1 Interrupt is not active, but as soon I start the timer1 interrupt the delay_ms(10) makes a value of about 18ms !!!
Can somebody out there explain this ?
What I am doing wrong ??
Here is my code:
Code: |
#include "c:\Program Files (x86)\PICC\Devices\18F23K22.h"
#include "C:\Program Files (x86)\PICC\Drivers\stdio.h"
#FUSES HSH,PLLEN,PRIMARY,NOPUT,NOBROWNOUT,MCLR,NOLVP,DEBUG
#use delay(clock = 32000000)
int16 counter2;
int16 cc1 = 0;
#int_timer1
void timer1_isr()
{
}
#int_CCP5 // Triggerung auf positive Flanke zur Frequenzmessung
void ccp5_isr()
{
counter2 = CCP_5 - cc1;
cc1 = CCP_5;
}
void main()
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
enable_interrupts(int_TIMER1);
setup_CCP5(CCP_CAPTURE_FE);
enable_interrupts(INT_CCP5);
enable_interrupts(global);
while(True)
{
output_toggle(PIN_C4);
delay_ms(10);
output_toggle(PIN_C4);
delay_ms(10);
} // True
} //main
|
My Compiler is : CCS PCH C Compiler, Version 5.012, 5801
Thank You all for Your help !
best regards
Andreas out of Vienna |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Mar 25, 2014 9:51 am |
|
|
Its not "interesting" behaviour...
interrupts take time to handle...
if you need to generate a frequency that is not affected by interrupts you need to use the PWM hardware module...
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Mar 25, 2014 10:22 am |
|
|
When the interrupt is triggered the interrupt software handler routine (ISR) is called. The ISR has the task to figure out which hardware event triggered the interrupt, save all chip registers that might get changed by your interrupt routine. On interrupt exit the saved registers have to be restored.
Depending on chip model there will be more/less registers to store. On an average PIC18 it takes about 60 instruction times to enter the ISR and another 60 to exit again.
I'm even surprised your delay_ms is only slowed down by 55%. With your Timer1 interrupt rate I'd expected it to be (almost) completely blocked. |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Tue Mar 25, 2014 12:37 pm |
|
|
As the others have implied - the delay_ms() function is implemented by CCS by creating a delay with a calculated number of machine cycles (NOP or whatever) to get that delay. It will function correctly as long as nothing else adds instructions inside the delay loop. In this case, every time the timer interrupt goes off, if you are in the delay loop, the ISR adds probably a minimum of 120 cycles to the loop. If you are using interrupts, the only way to get accurate time is to have a timer that goes off every 10 ms or whatever works for you which you then use as your timebase (increment a timer counter inside the ISR).
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Tue Mar 25, 2014 1:46 pm |
|
|
The other key thing to mention, is just how often you are interrupting.
8bit counter, running off the instruction clock. The interrupt will be called every 256 instructions.... |
|
|
Andreas
Joined: 25 Oct 2004 Posts: 136
|
|
Posted: Thu Mar 27, 2014 11:02 am |
|
|
Hello Friends
Thanks very much to all who answerd, I was in the impression that the ccs compiler is compensating for interrupts.
Of course now is clear for me what happens.
Thanks and best regards
Andreas |
|
|
|