That explains it, thanks very much. When I do the math by hand the 255's cancel each other out and then it's just 412+100.
If I put brackets around the dividing part will that fix it?
That explains it, thanks very much. When I do the math by hand the 255's cancel each other out and then it's just 412+100.
If I put brackets around the dividing part will that fix it?
That'll truncate the division, leaving you with 412/255 = 1If I put brackets around the dividing part will that fix it?
You can use PBPL mode so that it uses LONGs, which should work but it'll be larger and slower if that matters. Otherwise your best bet might be to turn the equation into a lookup table.
To be honest, I don't use PBP much, so maybe others can suggest something else.
Thanks again tumbleweed. I can't use PBPL without moving up to a 18F chip, and the minimum # of pins is then 18 (I only need two outputs so an 8-pin chip is ideal. Also, the PCB is pretty cramped now as it is).
But a lookup table could be tricky given the 0-255 range of the 8-bit ADC input but a PWM duty cycle range of 512. Maybe I should switch to 10-bit ADC and just divide by 2?
Why not look at using the DIV32 command?
Dave Purola,
N8NTA
EN82fn
That's an interesting operation. Splitting the equation up into two parts for DIV32 seems to work...Is it safe to do something like that or would you suggest making the DIV32 statement simpler so that you know it immediately follows the multiplication (I don't know what the OP's intent for the " - 0" part of the above is for)?Code:LEDBrVal = ((compVal - 0) * (MaxDuty - MinDuty)) LEDBrVal = DIV32 (MaxADCVal - 0) + MinDuty
Yes, The way you have it is OK. Just remenber that the result of the first multiplication before the DIV32 is to a dumby variable and it won't be usefull after the DIV32 function. It should be sized as a word.
Dave Purola,
N8NTA
EN82fn
I grabbed this scaling function from an Arduino library and was emulating that:
In general, the minimum brightness level is 0. But in this case, I only want to adjust the LED brightness between 50-100% duty cycle, so maybe I need to tweak that part as well.Code:' Arduino Map function to emulate: ' =============================== ' map(value, fromLow, fromHigh, toLow, toHigh) ' long map(long x, long in_min, long in_max, long out_min, long out_max) ' { ' return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; ' }
Bookmarks