CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Timer1 - Time calculating

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hakam_saffour



Joined: 14 May 2008
Posts: 13
Location: Germany

View user's profile Send private message

Timer1 - Time calculating
PostPosted: Mon Jul 14, 2008 3:56 am     Reply with quote

Hello:
I am testing Timer1 as the follwing:
1- get the value of timer 1
2- compare it with a specific number
3- output 1's & 0's (in order to be able to measure the time on the OSC)

CLK freq. = 10MHz
PIC No: PIC18F8520
Compiler: PCWHD Compiler 4.057

Code:

setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(GLOBAL);
 
while(1){
if (2000==get_timer1()) //0.4*2000=800uS
   {
   cnt=~cnt;
   output_e(cnt);
   set_timer1(0);
   }
}


The problem is:
when I compare with value of 2000, the output signal is 800uS ON and 800uS OFF, which is correct.
BUT......if I changed the value to 1000, the output signal became 26.2mS ON and 26.2mS OFF (which is 0.4*65536 uS), this supposed to be 400uS.!!
Why this is happeniing????? Sad it is supposed to be simple!!
Is it hardware or software problem??

Thank you for you advise.
_________________
Hakam B Saffour
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 6:24 am     Reply with quote

It is only by very good luck that you got the results you expected when comparing to 2000. You have configured Timer 1 to count internal instruction clocks with no pre-scaler. That means it counts by 1 on most instructions. Since your loop for checking Timer 1 takes several instructions for each check, Timer 1 can be at 998 on one check and it can be at 1013 on the next check. So it will seem to skip right over 1000.

You could change your check to
Code:

if (2000<=get_timer1())

to allow for the fact that you cannot sample Timer 1 often enough to catch it at any particular value. Of course will will inject a small error in the rate at which cnt is toggled. But that error is unavoidable in any case.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
hakam_saffour



Joined: 14 May 2008
Posts: 13
Location: Germany

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 4:11 am     Reply with quote

Hi RLScott,
Thank you for your advise.

Would you please help me to find out the mistake here. It is about the same issue, I set Timer 2 to interrupt every 5uS, and in the interrupt routine I just toggled an output.

CLK freq. = 40MHz
PIC No: PIC18F8520
Compiler: PCWHD Compiler 4.057

Code:
#fuses HS,H4,NOWDT,NOPROTECT,NOLVP
#use delay(clock=40M,oscillator=10M)

int8 counter=0;

#INT_TIMER2
void timer_test(void) {
counter=~counter;
output_e(counter);
}
void main(){
setup_timer_2(T2_DIV_BY_1, 24, 2);// overflow every 2.5uS and interrupt every 5uS
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
set_timer2(0);
while(1);
}


What I suppose to got is 5uS ON and 5uS OFF signal, BUT what I got is 34uS Toggled signal.

I checked the ASM file to count the number of instructions in the interrupt routine, and it was 24 instructions-is it too much?- (24*0.1uS=2.4uS), so adding 2.4 to the 5, I will get 7.2uS (while in the reality I got 34uS)...WHY?

What I want is to have an exact 5uS interrupt routine, in order to execute some instructions in the interrupt routine.

I am not experienced with timers, and this confused me alot.

Thank you for your patience, appreciate your help.
_________________
Hakam B Saffour
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 6:34 am     Reply with quote

I don't know why you are getting a toggle every 34 usec. But the interrupt routine time does not add to the toggle time (unless it is more than 5 usec). Timer 2 is set to load and begin counting again all on its own. It does not wait for your interrupt routine to start counting again. So it should interrupt as often as your setup_timer_2 function says.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
guest1
Guest







PostPosted: Tue Jul 15, 2008 7:10 am     Reply with quote

The interrupt service routine code generated by the compiler contains a lot of instructions to save the scratch variables, status register etc and then restore them. In addition it has to determine the source of the interrupt. It is likely that all this code takes considerably longer to execute than 5 usec.

There is a fast interrupt which saves and restores nothing (keyword FAST) but you must save anything the ISR changes yourself.
Guest








PostPosted: Tue Jul 15, 2008 8:39 am     Reply with quote

Code:
#fuses HS,H4,NOWDT,NOPROTECT,NOLVP
You can not combine the HS and H4 fuse! Most likely the H4 fuse is neglected and your system is running at 10MHz instead of 40MHz.

This combined with the CCS interrupt overhead of about 60 instructions, not 24, would explain the longer period timing.
hakam_saffour



Joined: 14 May 2008
Posts: 13
Location: Germany

View user's profile Send private message

PostPosted: Wed Jul 16, 2008 3:02 am     Reply with quote

Quote:
Code:
#fuses HS,H4,NOWDT,NOPROTECT,NOLVP
You can not combine the HS and H4 fuse! Most likely the H4 fuse is neglected and your system is running at 10MHz instead of 40MHz.



It is mentioned in the data sheet of PIC18F8520 that in order to use the PLL (in order to use 40MHz clock freq), fisrt HS has to be enabled then H4.

I tried to use H4 only without HS, and the controller did not work at all.
_________________
Hakam B Saffour
hakam_saffour



Joined: 14 May 2008
Posts: 13
Location: Germany

View user's profile Send private message

PostPosted: Wed Jul 16, 2008 3:07 am     Reply with quote

Dear guest1:

would you please clarify this...
Quote:
There is a fast interrupt which saves and restores nothing (keyword FAST) but you must save anything the ISR changes yourself.


I can not believe that it is not possible to have exact 5uS interrupt service routine!! Confused
_________________
Hakam B Saffour
indy31
Guest







PostPosted: Wed Jul 16, 2008 3:49 am     Reply with quote

hakam_saffour wrote:
Quote:
Code:
#fuses HS,H4,NOWDT,NOPROTECT,NOLVP
You can not combine the HS and H4 fuse! Most likely the H4 fuse is neglected and your system is running at 10MHz instead of 40MHz.



It is mentioned in the data sheet of PIC18F8520 that in order to use the PLL (in order to use 40MHz clock freq), fisrt HS has to be enabled then H4.
HS+PLL is one single fuse setting not two. CCS calls this H4. You can't set both the HS fuse and H4. Check the end of your list file (*.lst) for the fuses that are actually programmed. I'm sure you will only see the HS fuse and that causes your CPU to run at 10MHz instead of 40MHz.

Quote:
I tried to use H4 only without HS, and the controller did not work at all.
Did you all power from the chip after setting the H4 fuse?
Quote:
PLL operation cannot be changed “onthe-
fly”. To enable or disable it, the controller must
either cycle through a Power-on Reset, or switch the
clock source from the main oscillator to the Timer1
oscillator and back again.
Ttelmah
Guest







PostPosted: Wed Jul 16, 2008 6:56 am     Reply with quote

Once you have your clock sorted, have a look at EX_GLINT.c, which shows how to program an interrupt which only saves the minimum values, does one small job, and returns.
Currently, your 'handler' uses 24 instructions. The 'global' handler, will probably add about another 60. Total of perhaps 84 instructions. At 10MHz, about 34uSec. Exactly what you are seeing.

Best Wishes
hakam_saffour



Joined: 14 May 2008
Posts: 13
Location: Germany

View user's profile Send private message

PostPosted: Fri Jul 18, 2008 1:31 pm     Reply with quote

Thank you all, for your advices.

I will check them next Monday.

Regards'
_________________
Hakam B Saffour
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group