Newbie - understanding pwm-command


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    3

    Red face Newbie - understanding pwm-command

    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!

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default

    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Wonderful!

    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. :-)
    Last edited by Finchnixer; - 30th June 2008 at 09:56.

Similar Threads

  1. Active low input?
    By CosMecc in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 8th August 2010, 21:31
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 07:03
  3. turning gpio.0 high after pwm command
    By sirvo in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 21st February 2007, 19:22
  4. Strangw results when using PWM command
    By malc-c in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th July 2006, 13:14
  5. Can I do this???
    By noobie in forum mel PIC BASIC
    Replies: 2
    Last Post: - 10th June 2006, 19:57

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts