PDA

View Full Version : Pic controlers and counting ?



mark30504
- 17th December 2010, 01:16
Hello

New to PIC's but not to programming.

Currently I am counting input pluses with a PIC 16f886 at 20khz.

I am doing some thing like

IF pin high THEN
X=X+1

(not exact code but I am sure you get the gerneral idea)

The problem I am haveing is that the code is long and has quite a few RS232 outs
and by the time the logic loops back around to the IF statement I have missed counts.

Is there some special counting command I should be thinking of useing or can a make a pin a interupt that has precendence over what is going on ?

My only concern if there is a interupt fuction that I can not cut a RS232 string out in half.

Any thoughs ?

Mark

Jumper
- 17th December 2010, 01:33
Hi,

If you look at the datasheet you can find that the hardware TIMERS can be used in 2 different ways (timer or counter).

In the counter setup you have an external signal (TMR0 CLOCK INPUT for ie timer0) that will increment the counter on a selectable edge (rising or falling edge).

This way the hardware is counting everything in the background and after you have setup the counter in the SW you dont have to care at all about it until you want to read the counted number.

Just make sure you choose a timer that fits your biggest counted number. Some counters have 8 bit and some are 16 bit. As long as you make sure you do not overflow the counter it will not miss a single edge regardless what you do in your code.

mark30504
- 18th December 2010, 13:53
Hello

Thanks

I am hopeing to use RC0 and RC1 on my chip.
In the data sheet they are marked as Timers
There lables in the data sheet are T10S0/T1CK1 and T1OS1.
Assumeing these can be used. Do you happen to know how to tell the chip these are now counters ?
Also once this is done. Where/How to I read there value and get it into a varriable in my logic.

thanks for any thoughs

Mark

Jumper
- 18th December 2010, 15:04
Hi,

If we look the datasheet for the PIC18F886 you will notice that we have 3 timers:

Timer0: 8-bit Timer/Counter with 8-bit
Programmable Prescaler
• Enhanced Timer1:
- 16-bit timer/counter with prescaler
- External Gate Input mode
- Dedicated low-power 32 kHz oscillator
• Timer2: 8-bit Timer/Counter with 8-bit Period
Register, Prescaler and Postscaler

Timer0 can be clocked with the T0CKI (RA4)
Timer1 can be clocked with the T1CKI (RC0)
Timer2 can NOT be clocked with an external pin.

There is an other possibility and that is that you use interrupts for counting but that might upset the serial data unless you also do that part interrupt based.

But since you say that you are new to pics I would suggest that you use the timers as counters. Associated with the timers are the timer registers and that is where the current number of counts are stored.

8-bit timer/counter register (TMR0) for Timer0
16-bit timer/counter register pair (TMR1H:TMR1L) for Timer1

I suggest you start with Timer0 since that is the easiest one to set up.
5.0 TIMER0 MODULE in the datasheet is something you must read and understand before you will be successful with this.