01 — Bits and Bytes

This video looked at the claim that computers are mostly large collections of switches. While true, that claim doesn't get you very far. What really matters is what the switches are connected to, and how they are activated.

A switch that can be directly accessed by the programmer is called a bit, and eight bits are grouped together to make a byte.

One of the bytes in the ATmega328/P is called PORTB. Each bit in that byte can connect to one of the external pins of the chip. Two of the pins can't be used on the Arduino because they're dedicated to the 16 MHz crystal oscillator. The other 6 pins go to pins 8 to 13 on the Arduino, which connect to the LEDs on the ThinkerShield. So by playing with the bit values in PORTB, we can control whether the ThinkerShield LEDs are on or off.


0 MOV  #0b100001,PORTB
1 WAIT 5000
2 MOV  #0b000000,PORTB
3 WAIT 5000
4 JMP  0

Turns the top and bottom LEDs on for half a second, then off for half a second, and repeats.

This very simple program demonstrates how the processor deals with all programs. It just carries out the instruction at hand, then moves on to the next instruction. Only jump instructions can change where the next instruction comes from.

Further Experiments

Besides the three low LEDs alternating with the three high ones, you might like to try some other experiments.

  1. The human eye can't detect flicker if something is turned on and off quickly enough. On old-fashioned CRT displays, most people could see flicker with an on/off interval of 1/50th of a second, but not if it was less than 1/100th of a second.

    Experiment with the value in the WAIT instructions (using the same value in both) to work out how long this needs to be for you to notice the flicker. I find my threshold at a value around 130. That works out at about 1/38th of a second, so I'm not a high performer (I'll blame the ThinkerShield LEDs).

  2. Following on from that, work out which of the WAIT instructions controls the on time. Now keep the off time a little shorter than your flicker threshold, and try various values for the on time, between 5 and your off time. What do you notice about the brightness of the LEDs?

    This simple control of on and off intervals is given the very imposing name of Pulse Width Modulation (PWM). We'll do some more with it in later videos.

  3. I mentioned that instruction slots run from 0 to 255. If you enter
    255 WAIT 10000
    RUN 255
    you can see that it will delay for a full second, but then what? There's no slot 256, so where will it get the next instruction? Try it. This mightn't make sense right now, but there is an explanation in the next video.


Required Equipment