cancel
Showing results for 
Search instead for 
Did you mean: 

PWM Input measurement - TIM1

michal239955
Associate
Posted on May 29, 2015 at 15:14

Hello all,

I want to use TIM1 and its PWM input mode to obtain duty cycle and frequency of measured signal on STM8S003. But I have uncertainty about measured value.

Snippets of code

void initialization () {

//System Clocks:

CLK_CKDIVR_HSIDIV = 3; // fMASTER = fHSI / 8 = 16/8 = 2MHz

CLK_CKDIVR_CPUDIV = 0; // fCPU = fMASTER  = 2 MHz

CLK_PCKENR1_TIM1 = 1;

//Timer1

TIM1_PSCRH = 0x00;

TIM1_PSCRL = 0x01; // fTIM1 = fMASTER / (TIM1_PSCR + 1) = 1 MHz

TIM1_CR1_UDIS = 0; 

TIM1_CR1_ARPE = 1; 

TIM1_IER_CC1IE = 1; 

TIM1_IER_CC2IE = 1;

// Values are set according to RM0016 Reference manual, p. 169 PWM input signal measurement - Procedure

TIM1_CCMR1_CC1S = 1;

TIM1_CCER1_CC1P = 0;

TIM1_CCMR2_CC2S = 2;

TIM1_CCER1_CC2P = 1;

TIM1_SMCR_TS = 5;

TIM1_SMCR_SMS = 4;

TIM1_CCER1_CC1E = 1;

TIM1_CCER1_CC2E = 1;

TIM1_CR1_CEN = 1;  //start The Timer

}

@interrupt void PWM_vIsrHandleTimer1CaptureEvent(void) // irq12

{

if(TIM1_SR1_UIF) {

TIM1_SR1_UIF = 0;

// never occur

}

if(TIM1_SR1_CC1IF) {

TIM1_SR1_CC1IF = 0; //  read from TIM1_CCR1L should do this

dutyCycleInTicks = (TIM1_CCR1H << 😎 & 0xFF00;

dutyCycleInTicks |= (TIM1_CCR1L & 0xFF);

}

if(TIM1_SR1_CC2IF) {

TIM1_SR1_CC2IF = 0;

periodInTicks = (TIM1_CCR2H << 😎 & 0xFF00;

periodInTicks |= (TIM1_CCR2L & 0xFF);

}

Is it sufficient to use only this functions (+enable interrupt and etc) or something I am missing in work with Timers (reseting counter or?). Problem is why values are not related to input clock to TIM1. If I change TIM1_PSCRx prescaler or CLK_CKDIVR_HSIDIV (input clock to Tim1 peripheral), measured numbers are stilt the same. Generated PWM attached to TIM1_CH1 pin can vary in frequency, but value of TIM1_CCR1 is still same (4000), and TIM1_CCR2 is OK - change depends on duty cycle (for 50% it is 2000)... What am I missing?

Thanks, Regards

#pwm-stm8
1 REPLY 1