After more thought and a bit of direction to look at DIV32 by Dave.
Multiplication stores a 32 bit result internally but the normal divide only works on the lower 16 bits which causes the 'overflow' problem highlighted by Richard. DIV32 was created specifically to overcome the 16 bit division limitation and as Dave says is a solution here.
Scampy asked about */ and how that works. It works by removing the division in the calculation so there is no 16 bit divide, there is an 8 bit shift or a divide by 256 of the 32 bits whichever way one wishes to visualise the process. Knowing that */ will apply a division by 256 one has to take account of this in the program.
In this case
B_max = (4095/100)*maxbright
take 4095/100 and multiply by 256 giving 10483.2 which is rounded up to 10484. Resulting in this
B_max = maxbright*/10484 which results in an integer value of 2047 when maxbright is 50.





Bookmarks