SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By ryan
#10641
hi all
code snippet give on SFE site to measure pulse width of Adxl202 is like
for(x = 0 ; x < 10 ; x++)
{

cx = 0;

while(XPWM == 1); //Wait for current Pulse to end
while(XPWM == 0);
while(XPWM == 1) cx++;

counter_x += cx;

}

My question is: are we measuring the whole pulse(T2) width coz we r increasing counter when XPWM is again 1. that means whole pulse not only the ON period (T1)of the pulse, which we want to calculate.As far as i know T2 is determined by Rset.
what if we want to measure just ON (T1) portion of the pulse
any help would be appriciated...
By Philba
#10648
I'm not sure I understand how this code tells you very much. It is only measuring the number of times the while loop has seen the pulse high (i.e. T1) but there is no calibration. i.e. no way to know T2 and thus no way to calculate acceleration via the formula: (T1/T2-.5)/12.5%

I thnk a better way is to use port change interrupt and a timer. here is pseudo code for how I measure a 202:
Code: Select all
portchange_ISR() {
    if(portb.i) {        // i is the appropiate pin on the port
        T2 <- timer1 // total period
        timer1 <- 0
    } else {
        T1_c <- timer1  // c is the channel number
    }
}

init_adxl () {
    init timer1
    set port change int
}
You could save a little time measuring T2 at the beginning but this is not a good idea since it will be temperature and supply dependent. Best to always measure it.

Now to handle 2 channels, you will need to treat the timer differently. I start it on the first positive edge and then save the second positive edge start time, subtracting it from the first neg edge time. second neg edge time is the actual time for the other channel. This works because the 2 channel's outputs are "centered". the datasheet has the info. here's a picture:
Image

by the way, the code snippet you showed does averaging which may or may not be what you want.
By Kuroi Kenjin
#10673
Does the chip you're working with have a CCP (Capture/Compare/PWM) module? The Capture function does what Philiba says, but automatically. It runs a timer and on change (fall or rise) the timer value is stored into a register. An interrupt is triggered so you can get the register before it either goes off and does it again or reconfigure...etc. At least this is what a PIC can do.
By Philba
#10688
you'll need 2 ccps and you also need to measure the T2 period which you might be able to get from the ccp module.