Ok let's study a tiny code dedicated to contrast adjustement.
The code was made by JeePee, and available on ticalc here
So let's go !
#define bcall(label) rst 28h \ .dw label
Min .EQU 81h
Plus .EQU 80h
Enter .EQU 05h
Clear .EQU 09h
Ok as always we have the include, the equates then the .org 9D93h and finally the AsmCmp token.
Nothing interesting here, go on.
ld a,(8447h) ;load the current contrast from RAM
ld (curcontrast),a ;load it into a temporary variable
bcall(_getkey) ;checks for keys
cp Enter ;on Enter -> Restore the current contrast back in RAM
cp Min ;on Min -> Decrese the contrast with 1
cp Plus ;on Plus -> Increase the contrast with 1
cp Clear ;on Clear -> Don't restore current contrast back in RAM
jr nz,mainloop ;but reload old contrast back from RAM and apply that
ld a,(8447h) ;load the current contrast from RAM causes you pushed CLEAR ...
ld a,(curcontrast) ;load the current contrast ...
ld (8447h),a ;... load it back into RAM ...
call SetContrast ;... and apply it to the LCD
Here JeePee simply loads the contrast from the place where it's stored 8447h and keep it into a user variable (curContrast).
Then, as the program is reading keypress to change the contrast or exit, JeePee wait a key press (_getkey) and compare the result to the predefined consts.
So if the key is Enter, jump to the label Change, else if the key is Min, go to Minder, else if the key is Plus, go to Meer...
Finally if the key is not Clear, then restart to mainloop.
If it's Clear, restor the contrast (get it from 8447h) then jump to Do and setContrast the return (stop the program in this case).
Minder: ;Decrease the current contrast
Meer: ;increase the current contrast
Now we study the More or Less contrast part.
This is really easy to understand, into Minder, if the contrast is 0, we do nothing, else decrement a and setContrast.
Same thing for More (called Meer here). If the contrast is equal to 39, do nothing, else increment a and setContrast.
In any case, return to the main loop.
SetContrast: ;Apply the contrast to the LCD (port 10h)
lcdbusy: ;Smal Delay for the display driver
curcontrast .db 00h ;Temporary variable to store the contrast during the program
The set contrst add $18 to a (which contains a value from 0 to 39 in decimal).
Then it does an or with 11000000 and wait a moment (lcdbusy only make a pause).
Then the contrast is send to the port 10h and a text is displayed by drawcontrast (text start coords are 4,4), _disphl display th evalue of hl (itoa...) and hl contains a (a contains the contrast !).
You should notice that the curcontrast var is declared at the end of the file (adress 00).