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
解決済! 解決策の投稿を見る。
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
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
OK
Thanks n Regards,
Robin
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