How modify  LED brightness intensity using PWM?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How modify  LED brightness intensity using PWM?

Jump to solution
1,239 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
653 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
653 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

653 Views
Robinwithu
Senior Contributor I

OK

Thanks n Regards,

Robin

0 Kudos
654 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