Analog Input Port

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, to get actual analog input requires some setup. You can use port E as only a digital input port by reading memory address $100A, but this will only work if digital input devices are connected to the port.

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. This works like our lds #STACK instruction in order to use the stack for subroutine calls.

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 (to 1) by the system when there is valid data in the analog data registers; it is a read-only bit. It is cleared (set to 0), when the ADCTL register is written to set the control bits. A busy-wait loop (see below) may be used to discover when this bit is set by the system.
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; therefore, CD=0, and 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. In multiple channel mode, a block of four input channels specified by CD and CC only is read; a value of 00 selects the lower four channels (0-3), and a value of 01 selects the upper four channels (4-7).

The four data registers are located at addresses $1031 through $1034 and are called ADR1, ADR2, ADR3, and ADR4. These are result, or data registers, and are the values that you get as input. 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 using the ADCTL register. Third, you need to wait until you're getting valid data (it takes 128 cycles to input and convert the four channels, ADR1 - ADR4, whether it be a single bit four times or four bits one time), by waiting for the CCF flag to become true. This can be done with a busy-wait loop which would look like the following code:

ADCTL   equ   $1030
CCF     equ   $80
    .
    .
    .
        ldx   #ADCTL
wait    brclr 0,x CCF wait

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.