EmbeddedRelated.com
Forums

HC12 ATD (Analog To Digital) Converter

Started by Ashley August 12, 2005
I have just started learning microcontrollers.

Can someone help me to provide the program (C) of the following
steps:

1. Configure and enable the ATD (i.e. Analog to Digital) subsystem of
the Motorola HCS12 (ATDCTL2, 3, 5). 10 Bit accuracy should be used.

2. Trigger conversions on 4 channels of the ATD.

3. Wait for the 4 conversions to be completed.

4. Print the 4 results on the screen on a single line as numbers in
the range 0 to 5 Volts.

5. Wait a short while.

6. Return to step 2.

The C program will be run on IAR Embedded Workbench for 68HC12. Below is my C program (with comments) to the above steps. I think I'm
wrong. Thank you for correcting me. Also, I can't do step 5. Any tips
for beginners like me will always be welcomed. Thank you again.
#include <stdio.h>

typedef unsigned char U8;
typedef unsigned int U16;

#define ATDCL2 (*(volatile U8*)0x0082)
#define ATDCL3 (*(volatile U8*)0x0083)
#define ATDCL4 (*(volatile U8*)0x0084)
#define ATDCL5 (*(volatile U8*)0x0085)
#define ATDSTAT0 (*(volatile U8*)0x0086)
#define ATDDR (*(volatile U16*)0x0090) void delay (int time)
{
while (time-->0);
}

void ADC_convert (void); void main (void)
{ /* Step 1 */
ATDCL2 = 0x80;
ATDCL3 = 0x00;
ATDCL4 = 0x01;

ADC_convert ();

}

void ADC_convert (void)

{
while(1) /* Step 6 */

{
ATDCL5 = 0x03; /* Step 2 */

while ((ATDSTAT0 & 0x8000) != 0x8000)
{;} /* Step 3 */

printf ("%x %x %x %x\n", ATDDR[0], ATDDR[1], ATDDR[2], ATDDR
[3]); /* Step 4 */
}
}