EmbeddedRelated.com
Forums

Input Pulse Capture

Started by Ande_3 March 26, 2004
All,
I'd like to use my 430F149 to time the delay between two pulses (I'm 
reading a motor encoder) and need a push in the right direction.  The 
times I expect to encounter vary between 285us and 1.445ms.  I'm 
using TimerA which is being clocked at 1MHz.  Since TAR will roll 
over at 65.536ms, I should be good.

Here's how I'm thinking of implementing this:
Have the rising edge of the encoder pulse generate an interupt on pin 
x.  At that point, I reset TAR.  On the next rising edge I read TAR 
then reset it.  The read value is the period (in us) in between 
encoder ticks.

Does this sound correct?  Is there a beter way or some sample code 
out there?

Thanks,
Ande




Beginning Microcontrollers with the MSP430

Hi Ande, 

Use the capture function of the timer. There should be code examples 
on the TI website (you can get there with www.msp430.com)

Mike.

--- In msp430@msp4..., "Ande_3" <boyerJ@u...> wrote:
> All,
> I'd like to use my 430F149 to time the delay between two pulses 
(I'm 
> reading a motor encoder) and need a push in the
right direction.  
The 
> times I expect to encounter vary between 285us and
1.445ms.  I'm 
> using TimerA which is being clocked at 1MHz.  Since TAR will roll 
> over at 65.536ms, I should be good.
> 
> Here's how I'm thinking of implementing this:
> Have the rising edge of the encoder pulse generate an interupt on 
pin 
> x.  At that point, I reset TAR.  On the next
rising edge I read 
TAR 
> then reset it.  The read value is the period (in
us) in between 
> encoder ticks.
> 
> Does this sound correct?  Is there a beter way or some sample code 
> out there?
> 
> Thanks,
> Ande


Here's some simple code that measures pulse widths on TimerB_2. It is 
easy to comvert to Timer A, and pulse interval, simply don't XOR cmo/1, 
but SUBTRACT last time from this time to get the interval.

	BIC	#CCIFG,&CCTLB2		;CLEAR PENDING INTS
	BIS	#CCIE+CAP+CM0,&CCTLB2	;ENABLE
									;INT/CAP/RISING/SOURCE=PIN

/************************************************************************************************

	TIMER B.2 IS TOP DROP DETECTOR
	
*************************************************************************************************/



TB2INT:
	BIC	#CCIFG,&CCTLB2
	BIT	#CM0,&CCTLB2		;which edge?
	JZ	TTRAIL
	MOV	&TBR,&TOP_FRONT		;store this edge
	XOR	#CM1+CM0,&CCTLB2	;flip edge detect
	RETI
TTRAIL:
	MOV	&TBR,&TOP_TRAIL		;store trailing edge
	XOR	#CM1+CM0,&CCTLB2	
	RETI

cHEERS

Al




Ande_3 wrote:

> All,
> I'd like to use my 430F149 to time the delay between two pulses
(I'm 
> reading a motor encoder) and need a push in the right direction.  The 
> times I expect to encounter vary between 285us and 1.445ms.  I'm 
> using TimerA which is being clocked at 1MHz.  Since TAR will roll 
> over at 65.536ms, I should be good.
> 
> Here's how I'm thinking of implementing this:
> Have the rising edge of the encoder pulse generate an interupt on pin 
> x.  At that point, I reset TAR.  On the next rising edge I read TAR 
> then reset it.  The read value is the period (in us) in between 
> encoder ticks.
> 
> Does this sound correct?  Is there a beter way or some sample code 
> out there?
> 
> Thanks,
> Ande
> 
> 
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 


Hi Ande,

Are you using a quadrature encoder ? (they are most common)

If so, then you don't need to measure time intervals, it's better to
treat the quadrature as an 8 state process :
Say you have input A and B, these are the states :
    
   A                       B                    Example direction

----___               ------ (Hi)               UP

----___               ____ (Lo)               DOWN

___----               ------ (Hi)               DOWN

___----               ____ (Lo)               UP

Hi                       ----___                  DOWN

Lo                       ----___                 UP

Hi                       ___----                  UP

Lo                      ___----                  DOWN

So you need to pick up a change on either channel (A or B), whether it was a
rising
or falling edge, and at that moment whether the other channel was High or Low.
This way you get pulses and direction info.
The advantage of this scheme is that jitter between 2 states doesn't result
in false 
speed/position reading, as jittering between any 2 states will set the counter
back or forth
by one - this is very important.

-- Kris


> All,
> I'd like to use my 430F149 to time the delay between two pulses
(I'm 
> reading a motor encoder) and need a push in the right direction.  The 
> times I expect to encounter vary between 285us and 1.445ms.  I'm 
> using TimerA which is being clocked at 1MHz.  Since TAR will roll 
> over at 65.536ms, I should be good.
> 
> Here's how I'm thinking of implementing this:
> Have the rising edge of the encoder pulse generate an interupt on pin 
> x.  At that point, I reset TAR.  On the next rising edge I read TAR 
> then reset it.  The read value is the period (in us) in between 
> encoder ticks.
> 
> Does this sound correct?  Is there a beter way or some sample code 
> out there?
> 
> Thanks,
> Ande
> 
> 
> 
> 
> 
> .
> 
> 
> 
> 
>       
>            
>      
>      
> 
> 
> --------
> . 
> 
> 




I calculate the flowrate of a flowmeter by counting pulses all the time.  I
reset TAR and next time I read TAR and make the necessary calculations.
This seems to be the easiest way.  The uncertainty is 0.05%