Hi,
I have to measure the frequency (~100kHz) of a PWM as precise as possible (want to capture positive edges with Timer0(B) in 16-bit mode, up-counting edge time mode).
I'm using a TM4C123GCL launchpad with 80MHz CPU clock settings.
The following code basically works but fails my precision expectations:
// Interrupt handler
void Timer0BISR(void) { TIMER0_ICR_R |= TIMER_ICR_CBECINT; // clear capture interrupt ticksBetweenEdges = TIMER0_TBR_R & 0x0000FFFF; // volatile uint32_t
ticksDelay = TIMER0_TBV_R - TIMER0_TBR_R; TIMER0_TBV_R = 0; // reset counter (only the lower 16-bits are reset when in 16-bit mode) }
// Configuration of Timer0B
void configureTimer0B_risingEdgeTime(void) { // Target: detect positive edges (edge time mode) // Configure TIMER0 (TimerB) capture to measure received PWM signal --> PB7 (TIMER0 T0CCP1) TIMER0_CTL_R = 0; // disable timer TIMER0_CFG_R = TIMER_CFG_16_BIT; // 16 Bit mode TIMER0_TBMR_R = TIMER_TBMR_TBCMR | TIMER_TBMR_TBCDIR | TIMER_TBMR_TBMR_CAP; // set edge time capture mode, count up TIMER0_CTL_R |= TIMER_CTL_TBEVENT_POS; // timer event on positive edges TIMER0_TBPR_R = 0; // disable prescaler extension TIMER0_TBILR_R = 0x0000FFFF; // upper bound for timeout event (for up-counting only) TIMER0_IMR_R |= TIMER_IMR_CBEIM; TIMER0_TBV_R = 0; // reset counter TIMER0_CTL_R |= TIMER_CTL_TBEN; // start timer IntEnable(INT_TIMER0B); // Enable timer interrupt in NVIC }
Can anybody give me some hints how to increase the precision. Actually I have an error of approx. 1,5%.
I also have no idea why I have to mask the TBR register in the interrupt handler with 0x0000FFFF to get the correct ticksBetweenEdges.... I always set the counting value (TBV register) to 0 and the frequency of 100kHz will never overflow at 16-bits.
Many thanks in advance!
BR
Andy
