Use timer interrupt (capture) to measure an accelerometer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Use timer interrupt (capture) to measure an accelerometer

Jump to solution
1,025 Views
Tompzone
Contributor II

Hi i am trying to measure a Memsic 2125 accelerometer, (measuring the pulse width) and i am using a Adapt9s12e128 board with a MC9S12E128, i am trying to use this code, but it seems that the isr is not working or that it's not doing anything (the accelerometer has 2 channels, but they are the same just differ on the axis, so if one works it's easy to make the other0

 

this is the code i am using, i hope you can help me find the error (the oscillator freq is 24 MHz via PLL)

 

            ORG RAMStart
 ; Insert here your data definition.
            org $3000
cnt         dc.w 0
trans       dc.w 0
per         dc.w 0
posx        dc.w $0
; code section
            ORG   ROMStart


Entry:
_Startup:
            ;bus 24/2=12Mhz T=1/12Mhz=.083333 us, overflow en 5461.33 us => 5.46133 ms = 183 Hz ov
            ;LDS   #RAMEnd+1       ; initialize the stack pointer            
            movb #$90, TIM1_TSCR1 ;timer,banderas rapidas
            movb #0, TIM1_TSCR2 ;no int ov, no presc
            movb #$e0, TIM1_TIOS ;1 capturas  c0
            movb #1, TIM1_TCTL3 ;1 capt tran pos #5
            movb #$10,TIM1_TIE ;int 1 capt #$30
            movb #$ff,TIM1_TFLG1 ;borra banderas            
            movw #$ffff,DDRAD
            movw #$ffff,ATDDIEN
            movb #1,DDRP ;salida led rojo
            cli         
 ;sensor 100 Hz PWM 50% dtcy = 0g 100Hz=10ms=10,000us
 ;form= |`TH`|_TL_| Tx=periodo F=TH/Tx
main:
            bset PTP,mPTIP_PTIP0
            movw posx,PTAD  
            bra main
medicionx:
            sei
            ldd TIM1_TCNT  ;borr band
            std trans
            movb #2,TIM1_TCTL3; config timer trans baja
            brclr TIM1_TFLG1,mTIM1_TFLG1_C4F,*
            ldd TIM1_TCNT
            std per
            subd trans ;long
            std posx ;tiempo
            movb #1,TIM1_TCTL3
            cli
            rti

;**************************************************************
;*                 Interrupt Vectors                          *
;**************************************************************
            ORG   $FFFE
            DC.W  Entry           ; Reset Vector
            org   Vtim0ch4
            dc.w  medicionx

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
809 Views
Tompzone
Contributor II

well i got it working, i had to change to the timer0, i dont know why timer1 never worked (i had set up the tim0 int vector first but changing it to tim1 didn't work either) but now it does display the value on the led's, but they are always changing even if the accelerometer us still, so am i making any mistakes on the operations? or is it something else

View solution in original post

0 Kudos
Reply
4 Replies
809 Views
kef
Specialist I

         ldd TIM1_TCNT  ;borr band 

 

This doesn't clear capture flag in TFFCA=1 mode. Please read TFFCA description. You need to clear capture flags twice in your ISR. Instead of reading TCNT you should read your capture TCx register.

You shouldn't sei and cli in ISR, since MCU does it automatically on entry and on exit from ISR.

0 Kudos
Reply
809 Views
Tompzone
Contributor II

well i modified the code as you suggested and it still doesnt work, nothing is being displayed on the led's connected to PTAD, i changed the code to this

 

medicionx:
            ldd TIM1_TC4  ;borr band
            bset TIM1_TFLG1,mTIM1_TFLG1_C4F
            std trans
            movb #2,TIM1_TCTL3; config timer trans baja
            brclr TIM1_TFLG1,mTIM1_TFLG1_C4F,*
            ldd TIM1_TC4
            bset TIM1_TFLG1,mTIM1_TFLG1_C4F
            std per
            subd trans ;long
            std posx ;tiempo
            neg posx
            movb #1,TIM1_TCTL3
            rti

 

i also cleared the falg by writing one to the corresponding bit

0 Kudos
Reply
809 Views
kef
Specialist I

I don't know why, maybe because you don't initialize stack pointer, maybe there are other reasons. But

 

            bset TIM1_TFLG1,mTIM1_TFLG1_C4F

 

1) All writes to TFLG1 are ignored in TFFCA=1 mode.

2) In TFFCA=0 mode above line will clear ALL TFLG1 flags. To clear just C4F you need to either

 

   movb #mTIM1_TFLG1_C4F, TIM_TFLG1

 

or

 

   bclr TIM1_TFLG1, #~mTIM1_TFLG1_C4F

0 Kudos
Reply
810 Views
Tompzone
Contributor II

well i got it working, i had to change to the timer0, i dont know why timer1 never worked (i had set up the tim0 int vector first but changing it to tim1 didn't work either) but now it does display the value on the led's, but they are always changing even if the accelerometer us still, so am i making any mistakes on the operations? or is it something else

0 Kudos
Reply