How to change a period at runtime

cancel
Showing results for 
Search instead for 
Did you mean: 

How to change a period at runtime

1,030 Views
Contributor I

Hi,

I am new to MCU's and PE.

I wanna change a period of a output square signal on my FRDM kl25z based on the value from the a/d converter, but i dont know how to change it at runtime. I tried TimerUnit component but there i can only set a fixed period or if i tried TimerInt compoment and set in property: period  in timing dialog box "from interval" PE said that this component desnt support it. So can anyone help me and tell me which component and method use.

I am using CW 10.5

Thank you

and sorry for my English :smileyhappy:

0 Kudos
4 Replies

12 Views
Contributor II

I was facing the same problem, on reading the datasheet, changing the period is as simple as writing the new period value to the timer's MOD register and then setting up a new pulse width. Just make sure that happens when the Count register is reset so the timer starts with the new period. I am using KDS in Linux and PWM component. I have the following code for a siren implementation using a buzzer and a red LED.

#define PERIOD_1KHz 0x3E80UL
#define PERIOD_500Hz 0x1F40UL
uint16_t period=0x3E80UL;
extern uint8_t change_period;‍‍‍‍// set in main method to signal a change 
                             //to occur in the next timer cycle

//In the Timer's OnEnd callback, toggle between 2 frequencies
void PWM1_OnEnd(void){
     if(change_period){
          if(period==PERIOD_1KHz){
               TPM1_MOD = PERIOD_500Hz;
               period=PERIOD_500Hz;
               PWM1_SetDutyMS(1);
          }else{
               TPM1_MOD = PERIOD_1KHz;
               period=PERIOD_1KHz;
               PWM1_SetDutyUS(500);
          }
          change_period=0;//reset flag
     }
}

//in main method
uint8_t i=0, change_period=0;
void main(void){
 for(;;) {
       i++;
      WAIT1_Waitms(100);
      RED_SetVal();
      WAIT1_Waitms(100);
      RED_ClrVal();
      if(i>2){
           change_period=1;//set flag
           i=0;
      }
  }
}
0 Kudos

12 Views
NXP Employee
NXP Employee

Hi,

in TimerUnit_LDD, you can change the period of the whole timer using SetPeriodTicks method and also compare value for individual channels using SetOffsetTicks.

Please switch the inspector to 'Advanced' view mode using the 'Advanced' button. Then you'll see all methods offered by the TimerUnit component.

If you don't need multiple channels of each timer, you can also used PPG_LDD (Programmable Pulse Generator) which is simpler to setup and provides directly the methods for settings period or frequency (SetFrequencykHz etc...) and duty (SetRatio16 etc).

Best regards

Petr Hradsky

Processor Expert Support Team

0 Kudos

12 Views
Contributor III

I tried a bunch of different components and got a lot of red flags and methods that didn't work or weren't available. Verr-rry Frustrating! Finally I tried the PPG, Programmable Pulse Generator and it works very nicely. I don't get the SetPeriodTicks16 or SetPeriodTicks32 methods but I do get the SetPeriodUS and SetFreqHz methods. Just make sure that the Runtime Settings Type is set to Interval and the Low Limit and High Limits are set very wide. Then it works like a charm.

0 Kudos

12 Views
NXP Employee
NXP Employee

Hello,

have a look at this tutorial: http://mcuoneclipse.com/2013/03/16/pwm-for-processor-expert-explained/

You can use the SetRatio8() or SetRatio16() functions.

Erich

0 Kudos