How modify  LED brightness intensity using PWM?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How modify  LED brightness intensity using PWM?

ソリューションへジャンプ
1,255件の閲覧回数
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 解決策
669件の閲覧回数
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 返答(返信)
669件の閲覧回数
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

669件の閲覧回数
Robinwithu
Senior Contributor I

OK

Thanks n Regards,

Robin

0 件の賞賛
670件の閲覧回数
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