PDA

View Full Version : Integer Arithmetic Assistance Required



Aussie Barry
- 26th February 2015, 05:11
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

richard
- 26th February 2015, 05:45
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)

HenrikOlsson
- 26th February 2015, 08:23
Hi,
One possible solution

Dummy VAR WORD

Dummy = Red * Pot4
ScaledRed = DIV32 1024

Dummy = Green * Pot4
ScaledGreen = DIV32 1024

Dummy = Blue * Pot4
ScaledBlue = DIV32 1024

/Henrik.

Aussie Barry
- 26th February 2015, 09:45
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

HenrikOlsson
- 26th February 2015, 11:17
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.

Aussie Barry
- 26th February 2015, 12:18
Hi Henrik,

The DIV32 approach worked perfectly - thank you.

Cheers
Barry
VK2XBP