. It is really slow, taking ages to check whether the button is pressed and then taking time to switch each segment on and off individually.
Hi,
Yes the program structure is certainly "clunky" but it shouldn't be particularly slow, except in the simulator. However, after
varA = 9 it will take values from 10 to 255 (with none of the
IF statements being true) until VarA overflows back to 0 again. Is that the problem? Probably the simplest "fix" for this is to add a line
IF varA > 9 THEN : varA = 0 : ENDIF immediately after incrementing varA (at this point you might later want to introduce a "carry" into a second digit, e.g.
VarC = VarC + 1 ).
Also note that if you wish to count individual button-presses you should check that the button has first been released with an instruction such as DO : LOOP WHILE PinC.5 = 1 , somewhere in the active loop.
It looks as if all those HIGH and LOW commands might have been constructed by "trial and error" rather than to a logical pattern. For example the code for "1" has four HIGHs and four LOWs although only two segments should be lit? Also some "digits" are controlled by 7 HIGH/LOW lines and others by 8 , and do they all work correctly with a
descending count? A methodical creation of a Lookup Table should help to reduce these issues.
Certainly a Lookup Table method can make the program much "tidier" but not necessarily much faster in itself. Basically you need to create a block of data where the lowest bit of each byte represents (say) the top-right segment, the next bit represents the lower-right segment, around to the sixth bit for the top segment and seventh for the middle bar. Thus for example the digit 1 would be represented by %00000011 (or 3) and zero by %00111111 (or 63 or $3F) and would be written near the top of the program with an instruction such as:
DATA 0 , (%0111111 , %0000011 . %1101101 , etc..
) . Then the appropriate data for the digits 0 - 9 can be read back by the program with an instruction:
READ varA , W1 .
Ideally, the seven segments (or 8 if using the decimal point) should be driven on the same Port (in practice, B.0 to B.6 or B.1 to B.7), but that raises issues when you want to drive two (or more) digits, particularly if using the Darlington driver chip for a Common Cathode display. But currently the program appears to be driving only one digit, and I think we should stay with that for now and try to walk before we start to run. Then, the (up to) 7 or 8 segments that need to be lit can be represented by the 8 bits (i.e. either 0 or 1) within a variable Byte and written to the whole port (B) with a
PINSB = W1 type of command in a single operation. The 8 bits of W1 (specifically) can be accessed as "bit variables" named
bit8 up to
bit15 , so it's possible to set individual output pins with commands of the form
PinB.1 = bit9 . It's also possible to write to a selected group of pins within a single port in a single instruction, but again I think it's better to Keep It SimpleS for now.
Cheers, Alan.