Integer Arithmetic Assistance Required


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166

    Default Integer Arithmetic Assistance Required

    Hi All,

    I need some assistance with PBP3 integer arithmetic.
    I am working on an RGB LED application where the intensity of each of the RGB LED's is PWM controlled.
    The PWM will be controlled by slide pots with the values read by ADC channels.
    All ADC and PWM channels are 10-bit, slider pots range from 0 to 1023.
    This is all very straight forward - no problems thus far.
    Where things get complicated is that I want to be able to adjust the overall light intensity of a specific colour mix via a fourth slider pot.
    For example, suppose I have a colour mix of Red = 980, Green = 650 and Blue = 870 but I want to reduce the overall light output because it is too bright for a specific need.
    To dim the overall light output but maintain the exact same colour mix would be quite difficult.
    What I would like is to be able to use the fourth slider to scale the intensity of the three individual colours:

    Scaled Red = 980 * Pot4 Setting/1024
    Scaled Green = 650 * Pot4 Setting/1024
    Scaled Blue = 870 * Pot4 Setting/1024

    Let's assume that Pot4 is set at half scale (512). I am confused as to what the values for the three scaled colours will be using PBP integer maths?
    For the red colour, 980 * 512 is too large and 512/1024 is too small (ie less than one) for 16-bit integer maths yet the actual equation solution (490) is well within word variable limitations.

    As Pot4 Setting can vary anywhere between 0 and 1023, I am having difficulty working out a simple way to implement the scaled colour equations (Scaled Colour = Colour * Po4 Setting/1024) using PBP 16-bit integer math.
    I am using a 16F1509 so using LONGS are not an option.

    All comments and suggestions are welcomed and greatly appreciated.

    Cheers
    Barry
    VK2XBP

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: Integer Arithmetic Assistance Required

    you might get a better result using pot 4 to control a pwm signal that is fed to a common control element driving all the leds (10 bit pwm is no too difficult either)

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: Integer Arithmetic Assistance Required

    Hi,
    One possible solution
    Code:
    Dummy VAR WORD
    
    Dummy = Red * Pot4
    ScaledRed = DIV32 1024
    
    Dummy = Green * Pot4
    ScaledGreen = DIV32 1024
    
    Dummy = Blue * Pot4
    ScaledBlue = DIV32 1024
    /Henrik.

  4. #4
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: Integer Arithmetic Assistance Required

    Hi Henrik,

    I knew there had to be a simple way of doing this, I just couldn't figure it out on my own - thank you!
    Will the DIV32 command be affected if I am using DT-Interrupts? What happens is an interrupt occurs between the two arithmetic operations?
    Dummy = Red * Pot4
    Interrupt occurs here
    ScaledRed = DIV32 1024

    Cheers
    Barry
    VK2XBP

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,519


    Did you find this post helpful? Yes | No

    Default Re: Integer Arithmetic Assistance Required

    It shouldn't have any effect. DT-INTS saves the system variables when entering the ISR so even if an interrupt happens in between the multiplication and the division AND the ISR happens to use the same system variables (R0 and R2 IIRC) holding the 32 bit result from the multiplication no harm is done since the content of those variables are restored by DT-INTS when exiting the ISR.

    /Henrik.

  6. #6
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    166


    Did you find this post helpful? Yes | No

    Default Re: Integer Arithmetic Assistance Required

    Hi Henrik,

    The DIV32 approach worked perfectly - thank you.

    Cheers
    Barry
    VK2XBP

Similar Threads

  1. Assistance Required with VB.net
    By malc-c in forum Off Topic
    Replies: 32
    Last Post: - 11th September 2010, 18:26
  2. Resolution integer math assistance?
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th January 2010, 03:01
  3. VB.net and bit arithmetic
    By Ron Marcus in forum Off Topic
    Replies: 1
    Last Post: - 7th May 2008, 04:38
  4. keypad lcd and arithmetic help
    By csubroncs in forum General
    Replies: 0
    Last Post: - 27th January 2004, 17:18

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