PDA

View Full Version : pic with countdown timer register?



evildrome
- 30th September 2014, 15:24
Hi All,

Newbie question...

Is there a PIC with a countdown timer register ?

I have an external event and on the event I want to calculate a delay (max 0.8 seconds), load it to a countdown timer and have an interrupt when the timer reaches zero.

Are there any PIC chips with that sort of capability?

Googling 'PIC Countdown timer' just gets you lots of results for how to build 0-99 sec timers.

Thanks,

Wilson.

pedja089
- 30th September 2014, 15:38
There is interrupt on overflow. You can easy calculate start value, depending on clock speed, prescaler settings and desired time/frequency for interrupt.

AvionicsMaster1
- 30th September 2014, 15:46
Being a non wonderful programmer myself I wonder why you'd need a countdown timer? At the maximum of .8 seconds you'd never be able to display the countdown sequence.

Why can't you use a chip that has an Interrupt on Change pin? Use the IOC event, calculate the delay and use a pause command?

towlerg
- 30th September 2014, 19:04
@AvionicsMaster1. Not wanting to be rude but that is a terrible idea, you will tie up the PIC doing nothing but pausing. The countdowntimer is so much better, little or no cpu time used for little or no programming effort.

George

AvionicsMaster1
- 1st October 2014, 05:53
I got the idea the event wouldn't happen all time and wouldn't require a pause every second of .8 second.

Even if there was only .2 seconds couldn't he do like 200 commands doing something else?

Just to me, a pause for an intermittent event would make more sense.

PS Maybe ifn I knew all of what he was trying to do I wouldn't make a poor suggestion like that. You'd also have to work harder than that to be rude. Teach on.

HenrikOlsson
- 1st October 2014, 07:22
Hi,
As far as I know there no count down timers in any PIC but I don't really see the need for one.
As have been said before, instead of counting from 100 to 0 you count from 156 to 0 (with an 8-bit timer). There's really no difference. What you need to do (which pedja089 also covered) is to set the timer up so you have enough "range" then simply calculate at what value you need to start in order to have it overflow after 0.8 seconds or whatever. Depending on the PIC and timer used you may not be able to get long enough time in "one" go. In that case you could, for example, set it up to give you "ticks" of 0.1s (or whatever) and count 8 of them.

/Henrik.

evildrome
- 1st October 2014, 10:52
Hi Guys,

Thanks, interrupt on overflow sounds ideal.

FYI my event happens regularly with a max freq of 600 events per second and a min freq of 30 events per second. Depending on the time between events I have to calculate a time delay to generate an output event to occur within the time window of the time measured between the last two input events.

So if the time measured between input events is 0.8 seconds, I need to calculate a time which will always be < or = 0.8 seconds.

I'm measuring other process control variables so I'm trying to unload counting/timing tasks to hardware.

Thanks,

Wilson.

evildrome
- 1st October 2014, 11:31
OK, so I am using the PIC16F877A.

Can I attach an external clock that only the timers will use (not the CPU) ?

If so I could attach a 32.768khz clock which at a prescaler of 1:32 should give me a max delay of 1 second inside which I could fit my required 0.8 second delay.

HenrikOlsson
- 1st October 2014, 11:32
Hi Wilson,
Even at the min frequency of 30 events per second the time between events is ~33ms while you're talking about 800ms?
Are you actually talking about 30-800 events per minute?

/Henrik.

HenrikOlsson
- 1st October 2014, 11:58
Hi Wilson.
Yes, you can connect an external clock source to either TMR0 or TMR1 on the 16F877A.
If, however, you're talking about connecting a 32.768kHz crystal directly then only TMR1 has that capability (it's got an internal oscillator circuit specifically for this purpose). TMR1 is 16bits wide so you'd get 2 overflows per second. A prescaler of 2:1 will allow you to fit your 800ms in one go.

/Henrik.

towlerg
- 1st October 2014, 18:55
@Wilson

as Henrik points out you have a problem. Even if you mean 600 times per minute thats still every 100ms. The countdown timer will barely have stated before your next event happens.

You need to state the problem in detail. Perhaps drawing a waveform diagram might help.

George

evildrome
- 2nd October 2014, 10:35
@George @/Henrik

Yes, you are quite correct. 30 - 600 events per second = 0.033 to 0.00167 seconds between events.

A 32.768kHz crystal with a prescaler of 2:1 looks ideal.

Thanks,

Wilson.