Encoder Front Page
SRS Home | Front Page | Monthly Issue | Index
Google
Search WWW Search seattlerobotics.org

The 68HC12 Analog to Digital Module

By Kevin Ross

kevinro@nwlink.com


This month I will continue to present information about the 68HC12. Previous issues of the Encoder have touched on various aspects of the 68HC12. This article is going to describe the basics of how to use the Analog to Digital module of the 68HC812A4.

If you have programmed the A/D module on the 68HC11, then the 68HC12 is going to look very familiar, with the addition of a couple features. If you have never seen the 68HC11, then rest easy, I will go through the basics of the 68HC812A4 A/D module assuming you have no prior knowledge. It would be very helpful, however, if you had the 68HC812A4TS document handy. You can grab it from the web. This article is intended to help you understand what is in that document.

Overview

The purpose of the Analog to Digital (A/D) Module is to allow the HC12 to read analog voltages as 8 bit binary numbers. The 68HC812A4, and the 68HC912B32 both have 8 channels of 8-bit A/D. In other words, you have the ability to connect 8 independent analog signals to the HC12, and to be able to read the values into digital 8 bit numbers.

Whether you are doing robotics, or just about any other type of control application with a Microcontroller, the A/D module is a very powerful tool that you should consider investing the time to get to know.

The rest of this article will describe the various parts of the A/D module, and will demonstrate some software for performing specific tasks.

Big Picture

As always, engineers love to see a nice block diagram.

The A/D module is actually pretty straight forward to look at. Here is a very important note: The A/D system has its own power supply and reference voltages! What this means to you is that you need to connect your own +5 and GND to Vdda and Vssa resepectively. This is a power saving step on the 68HC812A4 in that the A/D system is only powered up if you actually wire it up. If you don't, then the A/D system is 'dead' on the chip.

I bring this up in particular because the SRS 68HC812A4 board does NOT connect the reference voltages for you. You will need to manually wire the pins to the appropriate voltages. Specifically, you need to connect pins 85 and 86 to +5v and GND respectively.

The reference voltages determine the scale of the A/D measurements. This allows you to use a precision voltage device to insure accurate readings. It is quite common to just tie Vrh to +5v, and Vrl to GND.

A Tale of Two Modes

The A/D system is capable of generating two types of results. The first is a series of eight sequential readings from a single channel. The results of these 8 samples are stored in the result registers ATD0 - ATD7. This is refered to as Single channel mode. There are some control bits that allow you to select which channel should be read.

The second mode is called Multichannel mode. In Multichannel mode, each input channel is selected, and one sample is stored in the result registers. The result from channel 0 is sent to ATD0, channel 1 to ATD1, and so on through channel 7 going to ATD7. This allows you to grab a fairly quick snapshot of all channels at once.

To use the A/D system, you need to consider which mode you would like to run in. The determination is made by considering what you need to do with the result. If your plan is to take an average reading of a single channel, then you will find that Single mode will allow you to aquire 8 conversion samples, for which you can quickly compute the average by summing the samples and shifting right by 3 bits. On the other hand, if you have 8 channels that don't require averaging, you can sample all 8 in one conversion sequence by using Multichannel mode.

The Control Registers

As with most subsystems on the 68HC12, the A/D system has a series of control registers that allow you to set certain control characteristics. You will need to set these control registers to appropriate values BEFORE you can expect the A/D system to function properly. I will now walk you through how to setup your conversions.

The first register you need to deal with is ATDCTL2 ($0062). This register has the ADPU bit, which is the A/D Power Up bit. This bit acts as the ON/OFF switch for the A/D system. Before you bother doing anything else, you need to set this bit to 1. Setting any other flags will be meaningless unless you turn on ADPU first.

Other common bits in the ATDCTL2 register include the AFFC and the AWAI bits. AFFC controls something called Fast Flag Clear which allows you to set the A/D system to automatically clear the conversion complete flag when a result register is read. This is useful if you are careful with your software and only read the result register once. The default is to disable the AFFC flag.

The AWAI controls how the A/D system behaves in WAIT mode. WAIT mode means the processor is in a low power consumption mode. Setting AWAI reduces power consumption in WAIT mode by discontinuing A/D operations. The default leaves the A/D system running during WAIT.

For now, just use the ADPU bit

	pokeb ATDCTL2,$80

ATDCTL3 allows you to set the response to entering Background Debug Mode. BDM mode basically stops the execution of the CPU. You can set up the A/D system to keep running in BDM mode, or to freeze after the current conversions, or to freeze immediately. This is useful if you are trying to debug a problem in your code, and want to see the exact values in the A/D system when your code stops. The default is to leave the A/D system running while BDM is active. The next line of code sets the A/D system to freeze while in BDM mode.

	pokeb	ATDCTL3,$03

The control register ATDCTL4 allows you to select the A/D sample time and clock prescalars. I haven't done much with this register, but will try to document it in a later article. For the most part, I just leave this one alone!

Making It Go

The real meat of the A/D system is found in ATDCTL5 and ATDSTAT. Once you have set the ADPU bit in ATDCTL2, you are ready to start reading values. ATDCTL5 is the control register with a lot of stuff in it.

A few terms to think about first. A conversion 'sequence' is what happens when you tell the A/D system to start a conversion. The sequence usually involves making more than one measurement. The measurements could be all on one channel, or on each of a set of channels.

S8CM is bit 6 of the ATDCTL5 register, and controls the number of conversions to be performed. The 68HC11 had the ability to run 4 conversions in a sequence. The 68HC12 has the ability to run 8. I imagine for compatibility reasons, this bit allows you to select the number of conversions you would like to have sequenced. I normally set this bit to 1 which enables 8 conversions per sequence.

SCAN is bit 5 of the ATDCTL5 register and controls the basic operational mode of the A/D system. If you set this bit to 0, then the A/D system will perform one conversion sequence then stop (setting some status bits to let you know when the sequence is complete). If you set this bit to 1, the A/D system will run continuously through the conversion sequence. This enables you to keep a running A/D conversion so you don't have to wait. However, you also need to take into account that some of the readings may be 'old' with respect to the other readings. My tests show that using the default prescalar and clock settings, the A/D system takes about 248 ticks of TCNT to complete a 8 channel sequence. Thats about 31 microseconds, which is pretty fast.

MULT is bit 4 of the ATDCTL5 register and enables the Multichannel conversions. When set to 0, the A/D sequence is done on a single channel. When set to 1, the A/D sequence is done across mulitple channels (4 or 8 depending on how S8CM is set). This is a very useful bit. If you want to take several successive readings of a channel to derive an average, then setting MULT to zero and S8CM to 1 will get you 8 sequential readings of the channel you want. Sum all of those values and divide by 8 to get yourself a quick basic average. On the other hand, if you need to sample all 8 channels at once, then set MULT to 1, and S8CM to 1, and you get one sample each for each channel.

The last 4 bits of ATDCTL5 define which channel is going to be scanned. You should look at table in the 68HC812/68HC912 manual for the A/D converter to see what some of the combinations mean. For the most part, you need to set the channel number in the lowest 3 bits. Channels are numbered 0 thru 7.

Here is a code fragment that does an average for 1 channel (defined by ChannelNumber) using an 8 sample sequence. Remember, the results are stored in addresses ADR0H thru ADR7H

	pokeb	atdctl5, %01000000 + ChannelNumber	' S8CM

	'
	' Wait for the SCF (Sequence Complete Flag) of ATDSTAT to be
	' set. When it is set, then the result registers are valid.
	'
	i = peek(tcnt)

	do
        loop while ( peek(atdstat) AND $8000) = 0

	j = peek(tcnt)
	printx "Single Channel scan took " ; j - i ; " clocks "


	j = 0
	' Calculate the average into j
	for i = 0 to 7
		j = j + peekb(adr0h + lshft(i))
	next
	j = j / 8

If you check out the manual, you will see that the A/D Result registers (ADR0H - ADR7H) are addressed at $70,$72,$74, etc. Obviously, Motorola is leaving room in the memory map to have higher resolution A/D converters in the future. The for loop above does a trick on 'i' by shifting it left 1 bit. That is a multiply by 2 operation, which calculates the correct offset from ADR0H.

Another sample bit of code is a 8 channel Multichannel sequence. This is quite straight forward, and here is the entire routine.

MultiChannelScan:

	'
	' Scan all 8 channels once then stop. The results are
	' stored in ADR0-ADR7
	'
	' Set atdctl5 for S8CM and MULT
	pokeb	atdctl5, %01010000

	j = peek(tcnt)
	' Wait for scan to be completed
	do loop while (peek(atdstat) AND $8000) = 0
	i = peek(tcnt)
	printx "MultiChannel took " ; i - j ; "clocks"

	return

A program written in SBASIC is available that implements some A/D functions. Check out adtest.bas for an SBASIC program that shows how to use this module. If you would like, you can also check out a 'C' version of this program (written using the Imagecraft ICC12 compiler) in the file adproj.c

Summary

The Analog to Digital conversion system on the 68HC12 is very similar to the 68HC11 series. The A/D system allows you to scan a single channel, or to scan multiple channels. The A/D system is a valuable resource on a Microcontroller, and one that you should consider using on your own projects.


 

List Of Tools and Documentation

Here are links and instructions for getting 68HC12 resources

Tools

as12 assembler is available from Karl Lunt, and is the freeware assembler for the HC12 family.

SBasic is Karl Lunts version of BASIC for the 68HC12. This is a good high level language.

Robotic and Microcontrollers Products is my page that allows you to order the SRS 68HC812A4 Development board, as well as my BDM12 interface board, and the new 68HC912B32 development board.

Imagecraft has a great C compiler for the 68HC12. The compiler runs around $150, but is a great bargin if you would like to write software in C for the HC12.

Documentation (online versions)

You can get online versions of the 68HC12 documentation from Motorolas website. Here are the files to grab. You can also get printed versions as described below.

CPU12RM-AD Index is an index to a series of .PDF files that create the Motorola CPU12 Reference Manual. This describes the instruction set and programming model for the chip. It is definitely a must have. There is also an Online Version at CPU12RM-AD or http://www.mcu.motsps.com/lit/manuals/cpu12/outline1.html

a4.pdf is the .PDF file of the 68HC812A4 Technical Summary that contains information specific to the 68HC812A4 version of the chip. This is the one with 4k of EEPROM, 1k RAM, and tons of I/O. This explains all of the systems on the chip, where the registers are, etc. You should definitely have one of these if you are using the 68HC812A4.

a4-ec.pdf
(250K bytes)
is a PDF file with the electrical characteristics of the 68HC812A4. Timing diagrams, electrical capacities and absolutes, accuracies, etc.

b32ts.pdf is the .PDF file of the 68HC912B32 Technical Summary that contains information specific to the 68HC912B32 version of the chip. This is the one with 32k of Flash, 768 bytes EEPROM, 1k RAM, and still lots of I/O. This explains all of the systems on the chip, where the registers are, etc. You should definitely have one of these if you are using the 68HC912B32.

b32-ec.pdf
(272K bytes)
is a PDF file with the electrical characteristics of the 68HC912B32. Timing diagrams, electrical capacities and absolutes, accuracies, etc.

Printed Versions

Motorola will also allow you to order printed documentation online at http://design-net.com/home2/lit_ord.html where you will find a form to fill out your name and address, then a spot on the bottom for document numbers. The following are the documents that you might be interested in

CPU12RM/AD The CPU12 Reference Manual (Must Have)
MC68HC812A4EC/D 68HC812A4 Electrical Characteristics
MC68HC812A4PP/D 68HC812A4 Product Preview (Not worth the effort!)
MC68HC812A4TS/D 68HC812A4 Technical Summary (Must Have)
MC68HC912B32ECD 68HC912B32 Electrical Characteristics
MC68HC912B32PPD 68HC912B32 Product Preview (Not worth the effort!)
MC68HC912B32TSD 68HC912B32 Technical Summary (Must Have)