Tool/software:
Hello,
I am trying to measure the PWM duty cycle using the General Purpose Timer Module's Capture Compare Mode in Edge Timer Configuration. The frequency of the signal measured is 482 Hz. Below is the code I used for initialization of the pins.
```SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
TimerConfigure(TIMER2_BASE,
(TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_CAP_TIME_UP | TIMER_CFG_B_CAP_TIME_UP));
TimerControlEvent(TIMER2_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
TimerControlEvent(TIMER2_BASE, TIMER_B, TIMER_EVENT_NEG_EDGE);
TimerEnable(TIMER2_BASE, TIMER_A);
TimerEnable(TIMER2_BASE, TIMER_B);```
The init functions above are all part of tivaware library. I think the initialization worked fine as I am able to get some timer values on providing a PWM signal on the pin.
Below is the code I am using for recording the total PWM pulse.
```void SpinMotorImpl::MeasureSpinSpeed() {
static uint16_t counter = 0;
static bool previous_measured = false;
static uint32_t spin_mot_hlfb_rising_prev=0;
uint32_t status_pos = TimerIntStatus(TIMER2_BASE, false);
if ((status_pos & TIMER_CAPA_EVENT ) != 0) {
const uint32_t spin_mot_hlfb_rising = (TimerValueGet(TIMER2_BASE, TIMER_A)&0x00FFFFFF);
const uint32_t spin_mot_hlfb_falling = (TimerValueGet(TIMER2_BASE, TIMER_B)&0x00FFFFFF);
if(!previous_measured)
{
spin_mot_hlfb_rising_prev = spin_mot_hlfb_rising;
previous_measured=true;
}
else
{
counter++;
if(counter==1000)
{
syslog_->Printf(SYSLOG_LEVEL_INFO, "m_evt=MeasureSpinSpeed timings=%ld, %ld, %ld", spin_mot_hlfb_rising, spin_mot_hlfb_rising_prev, spin_mot_hlfb_falling);
counter=0;
}
previous_measured = false;
TimerReset(TIMER2_BASE, TIMER_BOTH);
}
TimerIntClear(TIMER2_BASE, TIMER_CAPA_EVENT);
TimerIntClear(TIMER2_BASE, TIMER_CAPB_EVENT);
}
}```
The MeasureSpinSpeed is called every 1 ms. I cannot print at every cycle because that crashes the micro, I am guessing because it overloads it, which is why I have a counter that counts upto 1000 and I print every sec that way.
Based on this code, I expected to see the spin_mot_hlfb_rising to be higher than the spin_mot_hlfb_rising_prev and the spin_mot_hlfb_falling to be somewhere in between ( there should be a falling edge between two rising edges.)
However, this is what I am seeing:
m_evt=MeasureSpinSpeed timings=27640, 40830, 43379 ca
Based on this log message: spin_mot_hlfb_rising_prev is 40830, spin_mot_hlfb_falling = 43379 and spin_mot_hlfb_rising = 27640. The values differ every log message but the difference between these values is consistent.
I have two questions:
1. The timer seems to be wrapping around 65536. I cannot figure out how to prescale this. The datasheet says that in Edge Timer mode, the prescale byte just acts like an extra 8 bits to the 16 bit signal. I am guessing this makes it a 24 bit signal, which is why I am using a 0x00FFFFFF to extract out 24 bits but it still seems to wrap around 65536.
2. Why is the current timer value for the rising edge always lower than the prev rising edge and the falling edge is always higher than both of them. Tapping the signal at 1 kHz for a 482 Hz signal should be fine I think. The falling edge should be in between them, unless it is the wrapping around somewhere in between in which case figuring out how to prescale would really help here.
FYI: TimerReset resets the GPTMTAV and GPTMTBV registers of the timer to 0,TimerIntClear sets the GPTMICR flag for clearing the GPTMRIS flag of the timer, TimerValueGet gets the values in GPTMTnR registers and TimerIntStatus gets the values of the GPTMRIS registers to check if there has been an edge.
Not sure if I clarified my question above but would love to answer any questions you would have about my question.
Thank you in advance for your help.