explanation of code please

I am looking at making a binary clock so have just taken a look at the code example supplied with the binary clock project kit and i am at a bit of a loss trying to understand a section. Can someone please explain what is happening here so that i can understand it better or point me to the location in the manual as i donts seem to be able to find it. It is the sections AND %00001111 etc.
the code is:-
Code:
bcd_bin:
' convert the DS1307 BCD values to binary
temp = mins AND %00001111
mins = mins AND %11110000 * 10 / 16 + temp
temp = hours AND %00001111
hours = hours AND %11110000 * 10 / 16 + temp
temp = days AND %00001111
days = days AND %11110000 * 10 / 16 + temp
temp = months AND %00001111
months = months AND %11110000 * 10 / 16 + temp
' convert 24 hour clock values to 12 hour clock
if hours < 13 then do_return
hours = hours - 12
do_return:
return
 

rossko57

Senior Member
It is the sections AND %00001111 etc.
It is a technique called 'masking'

AND is a logical operator ('operator' is the name for actions like add, multiply)
In the Picaxe manuals you find the operators under LET

The % means to treat the value as a binary number

If you AND some byte value with %00001111, you get just the bottom four bits of the original value and 'mask out' or hide the top four bits.
See if you can work out what you get from some byte AND %11110000

To understand why they are doing this masking, you also need to understand how 'BCD' coding holds two-digit number values in a byte form.
 
Thanks for the reply rossko57 it starts to make a little more sense now. So for the following two lines of code the first line places the bottom four bits of mins into temp ?. And the second line re-calculates the mins variable top four bits only ?
temp = mins AND %00001111
mins = mins AND %11110000 * 10 / 16 + temp
I will play around in PE and see what the results are with different configurations.
 
Ok i think i have got how the AND is working in the masking but now i see a line with OR how is this explained please.
let temp_port = temp_port OR %00000001
 

hippy

Ex-Staff (retired)
The full code for that is ( "AXE114 Binary Clock.bas" sample code ) ...

Code:
display_hours_mins:
	' display hours and minutes
	let temp_port = mins * 4 AND %11111100
	let temp = hours AND %00000001
	if temp = 0 then hm1
	let temp_port = temp_port OR %00000001	
hm1:
	let pins = temp_port
What it is doing is moving the 'mins' bits and one of the 'hour' bits into 'temp_port' so they are in the correct positions to reflect how the hardware is connected to the output pins.

The code ensures the lsb of 'temp_port' reflects the lsb of 'hour'. the OR %0000001 sets the bit in 'temp_port' if required, otherwise it is left as cleared.

That code could be optimised and arguably improved upon but I imagine it is done this way to show the steps involved more clearly.
 
Top