HC11 Input and Output (I/O)
Input and Output with the HC11
HC11 does I/O and other "extracirricular activities" through various ports. Each port is an 8-bit, or 1 byte, port. Each bit in the port corresponds to a real pin on the CPU, and thus the value of that bit (1 or 0) corresponds to a real voltage level on the circuit board.
Port A is timers and counters. It is input/output.
Port B is a parallel output port. We use this for the motors.
Port C is a parallel input/output port. We use this for input on digital sensors (like switches).
Port D is a serial input/output port. We use this to download our program from the PC.
Port E is an analog signal input port. We use this for analog sensors like light sensors.
The HC11 does what is called "memory-mapped I/O". That is, the "registers" that contain input data are accessed by loading a value from a particular memory location, and the way to ouput a value is to store it at a particular memory location.
The HC11's "register block" is at $1000 to $103F. By the name "register block", the HC11 documentation means "the area of memory that is not actual memory, but I/O ports and configuration registers". Look at pages 38 and 39 of the pocket reference for the details on the register block.
Port B
Port B is at address $1004. Thus we can store a value to this address, and cause output values on Port B to change. Since port B is output only, what does it mean to load a value from this address?
On our circuit board, we use port B to control up to four motors, in forward and reverse. The upper four bits of port B are on/off bits for each motor, and the lower four bits are direction bits (0 forward, 1 reverse).
Port C
Port C is at address $1003. We use it only for input, and this is the default when the HC11 powers up, so we don't have to do anything special. Each bit corresponds to one digital sensor (like a switch). Since port C is input only (for us), what does it mean to store a value at this address?
An important feature is that the digital sensors are low-asserted. That means that if the switch is pressed, a 0 bit results. If the switch is not pressed or not even attached to that bit of the port, a 1 results.
Port D
Port D is at address $1008. Unlike ports B and C, which are parallel I/O ports, meaning 8 wires correspond to 8 bits of data, port D is for serial I/O, meaning that I/O is done one bit at a time. When you use a modem to communicate with another computer over a phone line, the communication is done one bit at a time, since you only have one wire. This is the way port D operates as well.
So, the byte at address $1008 is not all data. Each bit means something different, and we won't go into it now.
Port E
Port E is the Analog input channel. Although it can be used as an 8-bit digital input at address $100A, we only use it for analog input, using a variety of registers from addresses $1030 to $1034. Address $1030 is called ADCTL, and is a control register. You can write values into this register to control how the analog inputs work. Registers $1031 to $1034 are called ADR1, ADR2, ADR3, and ADR4. These are result, or data registers, and are the values that you get as input.
There is also another general configuration register, at $1039 and called OPTION, which has a couple of bits that control the A/D system. Bit 7, called ADPU, must be turned on (1) to make the A/D system work. Bit 6 is also for the A/D system, but we should leave this at zero. For clock speeds less than 750KHz, it should be one.
Although there are 8 A/D channels, only four digital values are computed during a single A/D conversion sequence (and are placed in ADR1--ADR4). The explanation below details where those four values are taken from.
The ADCTL register:
- Bit 7 -- CCF -- Conversion Complete Flag. This bit becomes 1 when the conversions are complete. it is a read-only bit. It is cleared (set to 0), when the ADCTL register is written to.
- Bit 6 -- unimplemented -- always 0.
- Bit 5 -- SCAN -- continuous scan -- if this bit is set, the A/D conversions are done continually (and CCF remains 1). If SCAN is 0, then only one conversion takes place (started when ADCTL is written to).
- Bit 4 -- MULT -- if this bit is 1, four A/D channels are converted, each resulting in a one-byte value. The next four bits in ADCTL determine which four channels are converted. If this bit is 0, then a single channel is converted four times in sequence. The lower four bits determine which channel is converted. This is useful to reduce the noise, because you can then average the four values and get a more reliable input value.
- Bits 3-0 -- CD, CC, CB, CA -- these select the channels that are converted. If MULT=1, then only CD and CC matter: a value of 00 selects the lower four channels, and a value of 01 selects the upper four channels. If MULT=0, then with CD=0, then the channel is selected by the 3-bit binary value of CC,CB, and CA. E.g., if CD-CA == 0101, the channel 5 is selected.