Hairy Animal
Member
While playing with the highly recommended DS3231 RTC board (a sample of which costing all of 82p inc P&P has just arrived from China), I was finding it messy to work out which bits in the various control registers were set (or not set) when reading the registers and outputting via SERTXD. Ideally there would be the opposite of e.g. regval = %10100101 but so far as I can see, it doesn't exist in PicAxe basic.
So, I quickly wrote this and found it really useful for seeing instantly which bits in the DS3231's control registers were set, and which weren't.
I daresay that it can be improved on/shortened, etc, and would welcome such improvements from the gurus (my programming skills are very limited).
It does have one particular limitation in that a value such as %10111001 will display as "1011 1001" okay, but because decimal numbers are displayed with leading zeroes blanked, %00110001 displays as "11 1".
I expect this could easily be corrected by converting to text rather than word values, but I haven't bothered so far as it was sufficiently useful for me to work out what the registers were set to.
So, I quickly wrote this and found it really useful for seeing instantly which bits in the DS3231's control registers were set, and which weren't.
Code:
SYMBOL temp_byte = b0 ; b0 used as a temporary counter etc.
SYMBOL temp_wrd1 = w1 ; Symbol for holding word 1
SYMBOL temp_wrd2 = w2 ; Symbol for holding word 2
main:
LET temp_byte = control
GOSUB hex2bin ; Convert the control value to binary.
SERTXD ("Reg.14 control ",#temp_wrd2," ",#temp_wrd1,LF,CR)
GOTO main
END
hex2bin:
; Byte value to be converted should be in temp_byte.
; Output as two words in temp_wrd2 & temp_wrd1.
; Convert low nibble
LET temp_wrd1 = temp_byte & %00000001
LET temp_wrd1 = temp_byte & %00000010 / 2 * 10 + temp_wrd1
LET temp_wrd1 = temp_byte & %00000100 / 4 * 100 + temp_wrd1
LET temp_wrd1 = temp_byte & %00001000 / 8 * 1000 + temp_wrd1
; Convert high nibble
LET temp_wrd2 = temp_byte & %00010000 /16
LET temp_wrd2 = temp_byte & %00100000 /32 * 10 + temp_wrd2
LET temp_wrd2 = temp_byte & %01000000 /64 * 100 + temp_wrd2
LET temp_wrd2 = temp_byte & %10000000 /128 * 1000 + temp_wrd2
;
RETURN
It does have one particular limitation in that a value such as %10111001 will display as "1011 1001" okay, but because decimal numbers are displayed with leading zeroes blanked, %00110001 displays as "11 1".
I expect this could easily be corrected by converting to text rather than word values, but I haven't bothered so far as it was sufficiently useful for me to work out what the registers were set to.