PDA

View Full Version : Newbie - understanding pwm-command



Finchnixer
- 30th June 2008, 08:01
Hi!

I have only recently started playing around with picbasic and the software-command for pwm. I know people say it's "useless", cause the program hangs while the pwm-command is run, but it's a good place to start. Besides I'm using pic 12f629, without hpwm-module.

Anyways, I'm having problems understanding this bit of code:

loop:
for i = 1 to 256
value = 128 + sin i
pwm PortA.0,value,1
next i
goto loop

This creates a lovely sinus-fading action from off to nice and bright on. But how on earth does it work? Sinus is a value between -1 and +1, and I don't see how that can affect the duty-cycle to such a degree, i.e. from 0 to 255 for completely off and completely on. :-)
Can anyone please shed some light on this, as I can't find any further documentation on the pwm command explaining this.

Thanks!

HenrikOlsson
- 30th June 2008, 08:23
Hi,
Since PBP doesn't do floating point math the SIN operator returns a value between 0 and 255 representing -1 to +1 in two's complement.
From the manual:

SIN returns the 8-bit sine of a value. The result is in two's complement form (i.e. -127 to 127). It uses a quarter-wave lookup table to find the result. Sine starts with a value in binary radians, 0 to 255, as opposed to the usual 0 to 359 degrees
HTH
/Henrik.

Finchnixer
- 30th June 2008, 08:37
Thank you very much for such a fast response! It's all so much clearer to me now.
In due time, I'll reconnect the 16F690 which came with the PicKit 2 and learn hardware PWM setup. :-)