Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home3/validus/public_html/wp-includes/class-wp-hook.php on line 298

Tutorial 04 – Counting in Binary

The counting in binary tutorial demonstrates the use of the dirs and pins commands on the PICAXE-08M microcontroller to control the ports. It uses the PICAXE School Experimenter Board circuit diagram layout and configuration.

Tutorial Task
Use the three LED’s as outputs and count in binary from 0 to 7 and display the value on the three LED’s. You can then count backward from 7 to 0 and display the values on the LED’s and start all over again. 


 

Tutorial Solution 
Open a new program window in the PICAXE Programming Editor for your Running LED’s program. Instead of using high and low commands to switch on the LED’s use a variable to switch all three LED’s on together. 

The pins input/output port register can be used to write or read all the input or output ports into or out of a byte variable. The three LED’s are on port 0 (red), port 1 (yellow) and port 2 (green) and we can switch them on by setting the pins register equal to a byte variable. For 0 the outputs green, yellow, red will be 000, 1 will be 001 up to 7 which will be 111.

init:                                                         ;Initiation label

            symbol counter = b0               ;Rename the byte variable b0 to counter
            let dirs = %000010110           ;port1,2 an4 defined as outputs

main:                                                      ;This is a label called main

            For counter=0 to 7                    ;Start at 0 loop to 7
                        let pins = counter         ;Output count on port 0,1 and 2
                        pause 500                     ;Pause 0.5 seconds
            Next counter                             ;Increment Counter and loop back
            For counter=7 to 0 step -1       ;Start at 7 loop to 0
                        let pins = counter         ;Output count on port 0,1 and 2
                        pause 500                     ;Pause 0.5 seconds
            Next counter                             ;Decrement Counter by 1 and loop back
            goto main                                  ;Loopback to the beginning
            End                                            ;End of the program

You can download the program file, Tutorial 4 – Counting in binary.zip by right clicking on it and selecting ‘save link as’ from the popup menu.