PDA

View Full Version : Pulse Counter



srspinho
- 13th July 2004, 19:15
Hi Friends,

some time ago, Tim Box sent me an example of a Pulse Counter developped to PB Plus and was published by him on Pronton+ forum.

I did some changes in order to compile it on PBP.
It is working fine on a small on board computer I´ve built for my opel corsa.

I don't know if the code can be improved or changed to improve the performance. I did some tests and the code showed good results.

It's good to remember that the code was written by Tim and I just did some changes.

Comments are welcome.

my best regards for all.

==============================================


Define LCD_DREG PORTA
Define LCD_DBIT 0
Define LCD_RSREG PORTB
Define LCD_RSBIT 4
Define LCD_EREG PORTB
Define LCD_EBIT 5

REM Variables
TMR0_POSTCOUNT1 var WORD ' POSTSCALER COUNTERS
COUNT_RESULT var WORD ' THE RESULT OF THE COUNT
REFRESHED var BIT ' FLAG TO SAY COUNT REFRESHED

'-------------------------'
ON INTERRUPT GOTO PULSE_COUNT ' DEFINE WHERE TO JUMP TO ON AN INTERRUPT
'-------------------------'

GOTO INITIALIZE ' JUMP ALL INT CODE

'----- INTERRUPT CODE -------------------
Disable
PULSE_COUNT:
TMR0 = TMR0 + 7 ' RELOAD THE TIMER
TMR0_POSTCOUNT1 = TMR0_POSTCOUNT1 - 1
IF TMR0_POSTCOUNT1 = 0 THEN
TMR0_POSTCOUNT1 = 500 ' 1/4 sec (may need some adjust)
T1CON.0 = 0 ' TURN OFF THE TMR1
COUNT_RESULT.lowbyte = TMR1L ' GET THE COUNT
COUNT_RESULT.Highbyte = TMR1H ' GET THE COUNT
TMR1L = 0
TMR1H = 0
T1CON.0 = 1
REFRESHED = 1 ' SET FLAG TO SAY COUNT READ
ENDIF
INTCON.2 = 0 ' CLEAR INTTERUPT FLAG
Resume
enable ' RELOAD THE SYSTEM VARS
'------ END OF INTERRUPTS CODE ----------

'------ INTIALIzE ALL REGS --------------
INITIALIZE:

TMR0_POSTCOUNT1 = 500 ' INITIALZE THE POST SCALERS
OPTION_REG.0 = 0 ' SET THE PRESCALER TO 1:1
OPTION_REG.1 = 0
OPTION_REG.2 = 0
OPTION_REG.5 = 0 ' TMR0 CLOCK FROM MAIN OSC
OPTION_REG.3 = 0 ' IN THIS INSTANCE PRESCALER IS ASSINED TO TMR0
INTCON.2 = 0 ' CLEAR INTERRUPT FLAG
T1CON = %00000110
T1CON.0 = 1 ' TURN TIMER 1 ON
TMR0 = 7 ' LOAD THE TIMER
INTCON.5 = 1 ' ENABLE TMR0 INTERRUPTS
REFRESHED = 0 ' DONT MAKE FIRST READ UNTIL MAIN INT HAS OCCURED
INTCON.7 = 1 ' START THE WHOLE THING RUNNING
COUNT_RESULT = 0
pause 500
lcdout $FE,1



'------ MAIN CODE BELOW HERE -------------

LOOP:
IF REFRESHED = 1 THEN
lcdout $fe,1,$fe,2, "Freq = ", dec (COUNT_RESULT * 4) ' Show results in pulses / Sec
REFRESHED = 0 ' PREVENT A RE-READ UNTIL NEXT MAIN INTERRUPT
ENDIF
GOTO LOOP

END

surfies
- 9th March 2012, 11:23
Hi Srspinho, can I use this code for a revolution counter, revs per minute? I see your display is counts per second. Can I just multiply the COUNT_result with 240, that should give me pulses per min, yes?

c_moore
- 10th March 2012, 18:48
I must be missing something,but what pin is input?

surfies
- 10th March 2012, 20:43
Yeah, good question, C_Moore

c_moore
- 11th March 2012, 02:03
I don't see where any tris registers are initialized.

HenrikOlsson
- 11th March 2012, 10:10
Hi,
The code is "generic", ie you need to "fit" it to the device you're using.
First time I've seen it but as far as I can see it uses TMR1 as a counter and TMR0 as the timebase. So the pulses should go to the T1CKI-pin, whichever that is on the particular PIC you're using - RB6 on the 16F628, RC0 on the 16F877 etc.

/Henrik.

c_moore
- 11th March 2012, 18:34
Thanks Henrik.

srspinho
- 20th March 2012, 15:01
Hi c_moore,

sorry for my loooong delay :(

Henrik is right. BTW, thank you Henrik !

bye

daydream
- 1st May 2012, 15:40
how can i use this code for pic16f84a.
can anyone send me the hex or asm code thank you

Archangel
- 3rd May 2012, 15:50
how can i use this code for pic16f84a.
can anyone send me the hex or asm code thank you
16F84 has only 1 timer so it won't work, it should work in a 16f628 if you turn off the comparators

daydream
- 3rd May 2012, 16:21
hi archangel,i have pic16f628a.what are comparators?how do I turn them off?with this CMCON=7?
and if i connect my lcd and use a 4mhz crystal and pin RB6 for input,will lcd show the freq?
thank you so much.

Ramius
- 4th May 2012, 13:20
I am probably confused as to what the goal is here and PBP has a command called "Count" (Chapter 5.13, page 129). Best, Ed

c_moore
- 5th May 2012, 10:54
PBP as well as other compilers that have the COUNT command use a software loop to count pulses. which means that it's a blocking command and nothing else can happen while counting pulses. the code above uses hardware timers that can work in the background.

Ramius
- 5th May 2012, 22:56
Thanks Charlie, My knowledge is very limited and I guess when I read that you are specifying a lenght of time for "count" to run it was not any different than his "pause 500"? It just seems if you are pausing for half a second then you could be using this same amount of time for your time duration? Or did I miss something?

Best, Ed