Analog Input

Analog input is a little bit harder than digital output was - where digital output was handled strictly by writing a value to a ``memory'' location, analog input requires some setup. There are actually sixteen analog inputs (eight of them useful), of which we can read up to four at a time. Two control/status registers are used to set it up, and four data registers are used to read the data.

Analog System Registers

The two control/status registers are:
Option Register ($1039)
ADPU CSEL IRQE DLY CME   CR1 CR0
Only one of the bits in this register is important to us at this point: the ADPU bit. This must be set to 1 in order to use the A/D subsystem.

A/D Control/Status Register ($1030)
CCF   SCAN MULT CD CC CB CA
Some of these bits are control bits, while one is a status bit. They are pretty much all important to us; their meanings are:

CCF (status)
Conversion Complete Flag: set when there is valid data in the analog data registers
SCAN (control)
Continuous Scan Control: set this to a 1 if you want to keep scanning the analog ports over and over and over.
MULT (control)
Multiple Channel/Single Channel control: set this to a 1 if you want to read four channels; set it to a 0 if you only want to read one channel
CD-CA (control)
Channel Number. In single channel mode, this specifies the input channel that is read. In multiple channel mode, a block of four input channels specified by CD and CC is read.

The four data registers ADR1-ADR4 are located at addresses $1031 through $1034. When an analog value is read, it is placed in one of these four data registers; you can obtain it from there by reading it just like any other memory location.

Using the Analog System

First, you need to use the OPTION register to turn on the system. Then, you need to configure the analog port. Third, you need to wait until you're getting valid data, by waiting for the CCF flag to become true. Finally, you can read the data. If you're in SCAN mode, you only need to set it up once, and you can read it over and over again.