How modify  LED brightness intensity using PWM?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Posting new questions will be temporarily unavailable from Dec 21 to Jan 5 due to a scheduled community upgrade.

How modify  LED brightness intensity using PWM?

Jump to solution
3,499 Views
alancalonico
Contributor II

Dear all

My homework for today is vary the LED brightness intensity using PWM. I understand as function PWM talking about duty, will be duty the time than can be in high status. but I don't understand which methods of codewarrior can I use to vary this brightness, could you help me?

 

i'm using a MCU MC9S8SH8 and CodeWarrior for MCU Version: 10.1

 

best regards

Labels (1)
1 Solution
2,913 Views
Robinwithu
Senior Contributor I

Hello Alan,

Here is code  for generating PWM on your PORTB all pins.

// Define Variables

unsigned char pulse = 0;                                            // Pulse starts at zero, and lengthens

unsigned char duty = 15;                                                 // Duty cycle

unsigned char freq = 255;                                                 // Frequency

       

void main()                                                              // Main

{

       PTBDD_PTBDD = 1;                                     // Define Port B as output port

           

    while( 1 )                                                                         // Loop forever...

    {

    PTBD_PTBD = 1;                                                                      // Start pulse

    for(pulse = 0; pulse < duty; pulse++);                                 // Lengthen pulse if < duty

    PTBD_PTBD = 0;                                                                      // End pulse

    for(pulse = 0; pulse < (freq - duty); pulse++);                   // Continue until frequency

    duty++;                                                                                         // Increment Duty cycle

    }       

}

Thanks and regards,

Robin

View solution in original post

3 Replies
2,913 Views
alancalonico
Contributor II

Thanks for your answer Robin, fortunately I can resolve with easy method explain following

First divide your amount of brightness step between 0xFF. i.e., if you want 8 steps divide 255/7 and you have next frame

byte frame[8]={0x00, 0x24, 0x48, 0x6c, 0x90, 0xb4, 0xd8, 0xff};

byte counter, error;

for(counter=0, counter<8; counter++)

{  

     error=PWM_SetRatio8(frame[counter]);

    }

well, just need use PWM_SetRatio8(frame[counter]); to change the brightness intensity

2,913 Views
Robinwithu
Senior Contributor I

OK

Thanks n Regards,

Robin

0 Kudos
Reply
2,913 Views
Robinwithu
Senior Contributor I

Hello Alan,

Here is code  for generating PWM on your PORTB all pins.

// Define Variables

unsigned char pulse = 0;                                            // Pulse starts at zero, and lengthens

unsigned char duty = 15;                                                 // Duty cycle

unsigned char freq = 255;                                                 // Frequency

       

void main()                                                              // Main

{

       PTBDD_PTBDD = 1;                                     // Define Port B as output port

           

    while( 1 )                                                                         // Loop forever...

    {

    PTBD_PTBD = 1;                                                                      // Start pulse

    for(pulse = 0; pulse < duty; pulse++);                                 // Lengthen pulse if < duty

    PTBD_PTBD = 0;                                                                      // End pulse

    for(pulse = 0; pulse < (freq - duty); pulse++);                   // Continue until frequency

    duty++;                                                                                         // Increment Duty cycle

    }       

}

Thanks and regards,

Robin