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 support@ccsinfo.com

Precision timer1 in milliseconds

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



Joined: 11 Mar 2015
Posts: 3

View user's profile Send private message

Precision timer1 in milliseconds
PostPosted: Mon Sep 07, 2015 7:01 pm     Reply with quote

I am looking to have the accuracy of timer1 in milliseconds for PIC16F877A.

Tks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 07, 2015 7:53 pm     Reply with quote

If you use a 4 MHz crystal with the 16F877A, the slowest clock that you
can use for Timer1 is 125 KHz. That gives a clock period of 8 usec.
You can't clock it at 1 ms intervals using the oscillator derived clock.
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 9:34 am     Reply with quote

PCM programmer wrote:
If you use a 4 MHz crystal with the 16F877A, the slowest clock that you
can use for Timer1 is 125 KHz. That gives a clock period of 8 usec.
You can't clock it at 1 ms intervals using the oscillator derived clock.


You have 2 options that come in mind:

Either use a external oscillator (slow) for timer1 clock source (if supported)

Or have a free running timer that increment a int8 then use the desired value to trigger your 1 ms timing code. (at 8 usec timer 1 firing rate, you know that int8 will overflow at about 2.04 ms)

ex (8 usec trigger rate):

Code:


int8 count;

#int_timer1

void timer1_ISR(){
if (count > 125)
  {
   // code to execute at 1 ms
  count =0;
  }
  else count++;
}


_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 08, 2015 11:39 am     Reply with quote

Quote:

external oscillator (slow) for timer1


actually that is not correct.....

to get a 1 msec rollover in timer one
the MINIMUM frequency with a DIVDE by one prescalar is
65.536 Mhz ....... 32.768 mhz will yield 2msec rollover and so on

and increasing the prescalar divisor only make the problem worse.

so going lower in clock frequency is not helpful.
Higher is the only direction and the 877 won't clock that fast.

AND using INTS to do it with a timer1 register preload is sure to increase jitter ..over unfettered"rollover"
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 11:43 am     Reply with quote

asmboy wrote:
Quote:

external oscillator (slow) for timer1


actually that is not correct.....

to get a 1 msec rollover in timer one
the MINIMUM frequency with a DIVDE by one pescalar is
65.536 Mhz

and increasing the prescalar divisor only make the problem worse.

so going lower in clock frequency is not helpful.

AND using INTS to do it with a timer1 register preload is sure to increase jitter ..




over unfettered"rollover"


I don't know this chip (that's why I wrote if supported)...


What's your take on that problem asmboy?
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 08, 2015 11:44 am     Reply with quote

Given a good choice of master clock - he should consider using timer2 ;-))

master clock 4.096 mhz crystal
timer 2 prescale 4 - postscale 1 -compare register 255
for precise 1 msec TMR2IF rollover


Last edited by asmboy on Tue Sep 08, 2015 4:53 pm; edited 3 times in total
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 11:51 am     Reply with quote

asmboy wrote:
Given a good choice of master clock - he should consider using timer2 ;-))


Yeah but he mentioned timer 1 Wink

Let's wait for his reason to stick with timer1 at the first place Smile
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 08, 2015 12:00 pm     Reply with quote

because it says '877 - this has the arbitrary glowing aura of school /Proteus
shining back at me.....
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Tue Sep 08, 2015 12:03 pm     Reply with quote

asmboy wrote:
because it says '877 - this has the arbitrary glowing aura of school /Proteus
shining back at me.....


Weird, I've never touch a pic16 in my whole life (even at school, we worked on 2550)...

I feel they are outdated compared to the features pic18/dspic/pic24 have...

I started with a PIC18F2550 (launched around 2001-2002) powerful enough for many projects!

Money aside I prefer to stick with few chips but fully featured so I know them well.

Nope I don't work in manufacturing so cost per chip ain't an issue.


EDIT: i guess I'm just a fresh meat and skipped the 877 bandwagon Very Happy
_________________
Regards,
Laurent

-----------
Here's my first visual theme for the CCS C Compiler. Enjoy!
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Sep 09, 2015 10:31 pm     Reply with quote

asmboy wrote:
because it says '877 - this has the arbitrary glowing aura of school /Proteus
shining back at me.....


My thoughts exactly. He may as well use a hammer and chisel as these are tools of the same period.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
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