PDA

View Full Version : Count pulses between VARIABLE TIME



RodSTAR
- 15th October 2007, 12:44
Hi to all of you electro-guys,
I have 3 questions, I wish someone could help:

-The first question: Is there any posibility by using Interrupts to capture (count) PULSES and TIME between two time-points? let me try to be clearer, for example this is my idea:

1) START sampling/capturing pulses in background...
2) ... do other things, processes, LCDOUT, PAUSE 100, whatever, etc... (while still sampling in background)
3) STOP sampling/capturing pulses in background !!!
4) and get 2 variables something like:
4.a) "PULSES_CAPTURED": stores the number of pulses captured between START and STOP.
4.b) "CAPTURED_TIME_MSEC" stores the TIME (in ms or us) elapsed between START and STOP.
so i can get speed by dividing: PULSES_CAPTURED/CAPTURED_TIME_MSEC.

I know PULSES_CAPTURED are TMR1H and TMR1L, by using Timer1 using INTERRUPTS

The main idea is NOT to "freeze" or halt the program by using PAUSE for Timer1 to capture like i've seen our great friend mister_e doing for example:


Start:
TMR1L=0 ' Clear
TMR1H=0 ' Counter
'
TMR1ON=1 ' Start counter
PAUSE 1000 ' Sampling time
TMR1ON=0 ' Stop counter
'
CounterA.HIGHBYTE=TMR1H ' Get results
CounterA.LOWBYTE=TMR1L '
'
' Blah blah blah
'
goto start


I think it would be powerfull/usefull to get something like this

Start:
TMR1L=0 ' Clear
TMR1H=0 ' Counter
'
TMR1ON=1 ' Start counter
WHILE something
Do_Critical_Things 'Things that cant wait and cannot be interrupted by a PAUSE statement
HSEROUT CYCLE_SPEED 'Other things.
PAUSEUS 10
HSERIN BLA BLA 'Other things.
I2CREAD BLA BLE BLI BLO BLU ....
I2CWRITE BLA BLA
....
PAUSEUS 10
LCDOUT CYCLE_SPEED, BLA, BLA
PAUSE 100 (not so much)
WEND
TMR1ON=0 ' Stop counter
'
CounterA.HIGHBYTE=TMR1H ' Get results (PULSE COUNT)
CounterA.LOWBYTE=TMR1L '

ElapsedTime.HIGHBYTE = ELAPSED_CAPTURED_TIME_MSEC_H
ElapsedTime.LOWBYTE = ELAPSED_CAPTURED_TIME_MSEC_H
'
' Blah blah blah
'
CYCLE_SPEED = CounterA / ElapsedTime
goto start

so i can get speed by dividing CounterA by ElapsedTime

An example of this is to get precise count (distance), speed and direction from a quadrature encoder.

Because as a unresolved problem to me, nowadays i plan to use 3 PIC16F628A's to control a DC motor: PIC_01 (does PID and out to motor driver), and receives from PIC_02 (pulse_count and direction) and from PIC_03 (speed [by capturing pulses in fixed lapses])
And PIC_01 connects to a PIC18F4550 (main chip) which also will work as a USB connection to PC. (also thanks to mister_e) (before i did it RS-232 [serial])

I want to attach a GIF to show my schema but can't find how to upload an image.

-The second question is about Interrupt detecting pulses from a quadrature encoder.
-The third question: is it possible to communicate a PIC which is connected directly to two other PIC's [the ones I described just before]?
example: i have PIC1, PIC2 AND PIC3.
PIC2 and PIC3 are connected directly to PIC1. In other words: One pin of PIC1 to PIC2 and other pin from PIC1 goes to PIC3.
PIC2 (encoder_count and direction) and PIC3 (speed) feed data to PIC1

Thank for your time, and your possible help or orientation.

oops !!, it's 8:38am, i didn't sleep doing this, and in a few minutes I have to be in a meeting... :'(

regards

Rodrigo.