How modify  LED brightness intensity using PWM?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How modify  LED brightness intensity using PWM?

跳至解决方案
2,165 次查看
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

标签 (1)
标记 (4)
1 解答
1,579 次查看
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

在原帖中查看解决方案

3 回复数
1,579 次查看
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

1,579 次查看
Robinwithu
Senior Contributor I

OK

Thanks n Regards,

Robin

0 项奖励
回复
1,580 次查看
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