PDA

View Full Version : TMR0 as counter



savnik
- 20th June 2008, 11:40
I use TMR0 as counter to measure the frequency at RA4.
How to get the decimal numbers.
For 135,6 , i take only the 135 from TMR0.I want to take and the ,6 number.

sayzer
- 20th June 2008, 12:14
Hi Savnik,

Since TMR0 is 8-bit, there is no way you can get 135,6 as you like.

You need to use TMR1 which is 16-bit.
Then, you will read 135,6 at TMR1 register as 1356.


-----------------------------

Acetronics2
- 20th June 2008, 12:21
Hi, Sayzer

As our friend Savnik didn't tell us which pic he uses ... it's difficult to answer.

let's take the 18F252 i.e.

Timer 0 can be 8 or 16 bits ... as you decide.

Some more details from Savnik could be useful ... but we are used to !!!

Alain

sayzer
- 20th June 2008, 13:24
Hi, Sayzer

As our friend Savnik didn't tell us which pic he uses ... it's difficult to answer.

let's take the 18F252 i.e.

Timer 0 can be 8 or 16 bits ... as you decide.

Some more details from Savnik could be useful ... but we are used to !!!

Alain



You are right Alain.

That is my bad.

But, I take 16F628A as the default PIC in my head when there is no mention of the PIC type.

Soon enough I will change my default PIC to 18F2550.


:)

savnik
- 20th June 2008, 13:45
Thanks for yours answer.
My default PIC is pic16f88.
Maybe i change with pic18f1330 which have timer0 16bit.

Acetronics2
- 20th June 2008, 14:08
Hi, Savnik

Also could eventually be useful :

the PIC freq.

The Porta.4 max input freq. to be measured


Alain

sayzer
- 20th June 2008, 14:17
Thanks for yours answer.
My default PIC is pic16f88.
Maybe i change with pic18f1330 which have timer0 16bit.

Savnik,

16F88 already has 16-bit timer.

Why change the whole car while you can change one tire and get going?


------------------

savnik
- 20th June 2008, 18:30
Savnik,

16F88 already has 16-bit timer.

Why change the whole car while you can change one tire and get going?


------------------
From datasheet:
---------------------------
The Timer0 module timer/counter has the following
features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt-on-overflow from FFh to 00h
• Edge select for external clock

Acetronics2
- 20th June 2008, 19:42
Hi, Savnik

Sayzer was only telling there's a 16 Bits timer in 16F88 : Timer 1 ...

but, if you want to use RA.4 and keep the same pinout ( must we understand PC board is already made ??? ) ... you'll have to turn to 18F1320 !

Alain

savnik
- 20th June 2008, 20:08
Hi, Savnik

Sayzer was only telling there's a 16 Bits timer in 16F88 : Timer 1 ...

but, if you want to use RA.4 and keep the same pinout ( must we understand PC board is already made ??? ) ... you'll have to turn to 18F1320 !

Alain
Ok , i understand
Yes PC board is already made, so i turn to 18F1320.

Thanks

Darrel Taylor
- 20th June 2008, 21:30
Or you could just count the Overflows from timer0 and make it a 16-bit or larger counter.

DT

savnik
- 21st June 2008, 12:06
Or you could just count the Overflows from timer0 and make it a 16-bit or larger counter.

DT
Now count the TMR0 and also count the prescaler (256)

result var word ' 16 bit result of count, high from tmr0 low from prescaler
TMR0 = result.Byte1
prescaler = result.Byte0

But i cann't take the decimal numbers , only the integer

sayzer
- 21st June 2008, 13:44
Savnik,

I understood that you already have a PCB and can not change it.

Then, if you are not using RB6, then just short it to RA4 manually with a tiny cable and use Timer1.

-----------------

savnik
- 21st June 2008, 14:03
Then, if you are not using RB6, then just short it to RA4 manually with a tiny cable and use Timer1.-----------------
unfortunately i use RB4-RB7 for LCD

Acetronics2
- 21st June 2008, 14:20
Now count the TMR0 and also count the prescaler (256)

result var word ' 16 bit result of count, high from tmr0 low from prescaler
TMR0 = result.Byte1
prescaler = result.Byte0

But i cann't take the decimal numbers , only the integer



Hi, Savnik,

so, you want to use a ... 24 bits counter !!!

One more good reason to turn to 18F1320 AND use PBPL ... instead of PBPW !

But, once more ... will you tell us the PIC Osc frequency and the frequency range you intend to measure at PORTA.4

... as I smell something really interesting ...

Alain

savnik
- 21st June 2008, 14:31
Hi, Savnik,

so, you want to use a ... 24 bits counter !!!

One more good reason to turn to 18F1320 AND use PBPL ... instead of PBPW !

But, once more ... will you tell us the PIC Osc frequency and the frequency range you intend to measure at PORTA.4

... as I smell something really interesting ...

Alain
Now i use xtal 4mhz and the frequency range is from 300khz to 6mhz

skimask
- 21st June 2008, 21:17
But i cann't take the decimal numbers , only the integer

And yet AGAIN!!!
PBP doesn't do fractional numbers by default!!!!! NEVER has, NEVER will!!!
You have to create your own method to extract any fractions you need.
Multiply the number your dealing with by 100, the 2 rightmost digits are your fraction....simple as that!
Or maybe not...

savnik
- 23rd June 2008, 12:52
Another question.

I use TMR0 as counter to measure the frequency at RA4
How i know, how many times the TMR0 overflow.
The maximum times is 3

For first overflow INTCON.2 = 1 , After;

pic16f88
xtal =4mhz
option_reg = %10110111

skimask
- 23rd June 2008, 13:47
For first overflow INTCON.2 = 1 , After;

So clear it and at the same time, increment an 'overflow counter'.

savnik
- 23rd June 2008, 16:02
So clear it and at the same time, increment an 'overflow counter'.
I have make this but increment only for 1




overflow = 0
main:
TMR0 = 0

'start count
pauseUS 1000
'stop count

if INTCON.2 = 1 then overflow = overflow + 1
INTCON.2 = 0
Temp = TMR0
gosub LCD
goto main

savnik
- 31st March 2009, 18:42
Another question.

I use TMR0 as counter to measure the frequency at RA4
How i know, how many times the TMR0 overflow.
The maximum times is 3

For first overflow INTCON.2 = 1 , After;

pic16f88
xtal =4mhz
option_reg = %10110111
Until today i don't solve this problem.
To know how many times TMR0 has overflow;

Archangel
- 31st March 2009, 19:12
Until today i don't solve this problem.
To know how many times TMR0 has overflow;So did you solve the problem, or not solve the problem?

savnik
- 31st March 2009, 19:18
So did you solve the problem, or not solve the problem?
No.
I use TMR0 as counter to measure the input frequency at RA4 for 256ms.
The frequency is between 0.3mhz and 8mhz.
My problem is how many times the TMR0 overflow.
I use pic16f88 and 4 mhz xtal. (I don't to change these)

Archangel
- 31st March 2009, 19:34
No.
I use TMR0 as counter to measure the input frequency at RA4 for 256ms.
The frequency is between 0.3mhz and 8mhz.
My problem is how many times the TMR0 overflow.
I use pic16f88 and 4 mhz xtal. (I don't to change these)

Here is a link to an example Mister_e posted on ME Labs site.
http://www.melabs.com/resources/samples/submitted/counter.zip

Personally, I do not understand how a PIC can count faster than it's own oscillator is running 4mhz vs 8mhz, I think you will have to up the Frequency to 20, but I may be wrong too. I thought it took 4 cycles per instruction, likely that's wrong too.