I'm using a Freescale MC9S08DZ60 microcontroller, and Ive written the code for applying PWM on a specific pin.'This is an attempt for Breathing Effect of LED, a question i had already asked - LED Breathing Effect. Even Though the advice was sound, it just made the LED blink at random intervals. I changed my approach.

Default Setting: SET OUT TO COMPARE(Fade ON). Increment the Channel Value Register by the value of i. Checks for the value of Channel Value Register,then switch to CLEAR OUTPUT ON COMPARE(Fade OUT), update the value of i as before onto the Channel value Register.

BUT, when the code is flashed, It SWITCHES between the options(FADE ON & FADE OUT) only ONCE. That is after the first switch between options, it remains to FADE OUT option.

Default Settings: Modulo Counter = 65535.

Period = 4.096ms.

Edge Aligned.

On Channel 1.

PWM out action: Set OUtput on Compare.

Channel Duty = 4.095ms.

How is it possible to switch between these two PWM options?

Is the number of Timer 'TICKS' an attribute that i am overlooking?

Thanks in advance.


 TPM1C2SC &= 0b11110011; //TO SET OUTPUT ON COMPARE.
 TPM1C2SC |= 0b00000100;  

 TPM1C2VL =~ (i<<1);

 if(TPM1C2V == 255 ) 
   TPM1C2SC &= 0b11110011; // CLEAR OUTPUT ON COMPARE. 
   TPM1C2SC |= 0b00001000;

   TPM1C2VL =~ (i>>1);
   TPM1C2VH += TPM1C2VL;


1 Answer 1


You're setting TPM1C2SC to one value on every iteration of the outer loop, and setting it to a different value for only part of one iteration out of every 65536.

You need a state variable to keep track of whether you're currently fading up or down, and only update TPM1C2SC to the next value on every 65536th iteration. Don't forget to initialize it before entering the loop, too.

  • \$\begingroup\$ Thank you david. I had misunderstood the timing concept! i overlooked TICKS! \$\endgroup\$
    – sheetansh
    Apr 27, 2013 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.