Dear sir,
The below is the program i wrote to measure input pulse width from 2hz to 100hz through STM32 TIM3_CH3. It is measuring the time. But it has wide variation. TIM3-CH3 capturing with wide range of variation in TIM3->CCR3. Also i'd configured PB0(TIM3-CH3) in input with pull up/pull down. Does it need to be changed to other configuration.
badly, i'm in need of your help.
Thanks in advance.
// Timer3 channel 3 initialization. RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // enable clock for TIM3 TIM3->CCR1 = 0x0000; //__TIM3_CCR1; // all CCRx default 0x0000 TIM3->CCR2 = 0x0000; //__TIM3_CCR2; // all CCRx default 0x0000 TIM3->CCR3 = 0x0000; //__TIM3_CCR3; // all CCRx default 0x0000 TIM3->CCR4 = 0x0000; //__TIM3_CCR4; // all CCRx default 0x0000 TIM3->CCMR1 = 0x0000; //__TIM3_CCMR1; // TIM3->SMCR = 0x0000; // Slave mode disabled TIM3->PSC = 0x02CF; //719 -- 10usec accuracy TIM3->ARR = 0xFFF0; // set auto-reload TIM3->CR1 = 0x0000; // URS =0 -- update request for counter ov/underflow,UGbit,through slave TIM3->CR2 = 0x0080; // TI1S =1-- TIMX_CH1,CH2,and CH3 pins are connected to TI1 input(XOR) TIM3->CCMR2 = 0x0001; //CC3 config as input,IC3 mapped to TI3 TIM3->CCER = 0x0100; //CC3E capture/compare 3 enable void RpmMonitor(void) { unsigned short int x = 0,y = 0,z = 0; TIM3->CR1 &= 0x1110; // disable timer TIM3->SR = 0x0000; TIM3->CNT = 0x0000; TIM3->CR1 |= TIMX_CR1_CEN; // enable timer do x = TIM3->SR; while (x == 0x0000); if ( x == 0x0001) // if only counter overflow skip { TIM3->CR1 &= 0x1110; // disable timer z = 0x0000; } else //for CC3OF or CC3IF or TIF { TIM3->EGR |= 0x0001; //setting UG bit to clear Counter Value TIM3->SR = 0x0000; do x = TIM3->SR; while (x == 0x0000); TIM3->CR1 &= 0x1110; // disable timer if ( x == 0x0001) // if only counter overflow skip { z = 0x0000; } else { z = TIM3->CCR3; } } }