USING THE HC12 A/D CONVERTER
- Power up A/D Converter (ADPU = 1 in ATDCTL2)
- Set ATDCTL4 = 0x01 (Gives 2 MHz AD clock with 8 MHz E-clock, 8-bit
- Select 8-channel mode (S8CM = 1 in ATDCTL5)
- Set CD = 0 in ATDCTL5 (CD = 1 for factory test only)
- Select MULT in ATDCTL5:
- MULT = 0: Convert one channel eight times
- Choose channel to convert with CC, CB, CA of ATDCTL5.
- MULT = 1: Convert eight channels
- Select SCAN in ATDCTL5:
- SCAN = 0: Convert eight samples, then stop
- SCAN = 1: Convert continuously
- After writing to ATDCTL5, the A/D converter starts, and the SCF bit is cleared. After eight conversions are complete, the SCF
flag in ATDSTAT is set. You can read the results of the conversions
- If SCAN = 0, you need to write to ATDCTL5 to start a new
sequence. If SCAN = 1, the conversions continue automatically, and you
can read new values in ADR[0-7]H.
- To get interrupt after eight conversions completed, set ASCIE bit
of ATDCTL2. After eight conversions, ASCIF bit in ATDCTL2
will be set, and an interrupt will be generated.
- With 8 MHz E-clock and ATDCTL4 = 0x01, it takes 9 s to
make one conversion, 72 s to make eight conversions.
- On HC12 EVBU, AD channels 0 and 1 are used to determine start-up program
(D-Bug12, EEPROM or bootloader). Do not use AD channels 0 or 1 unless
absolutely necessary (you need 7 or 8 channels). If you do need AD channels 0
and/or 1, power up EVBU, then remove jumpers which select start-up program.
Normally, = 0 V, and = 5 V, so
Example: ADR0[15..6] = 448
=> = 2.19 V
- To use 10-bit result, set
ATDCTL4 = 0x81 (Gives 2 MHz AD clock with 8 MHz E-clock, 10-bit
mode),and add the following to hc12.h:
#define ADR0 (* (volatile unsigned int *)(_BASE+0x70))
#define ADR1 (* (volatile unsigned int *)(_BASE+0x72))
- You can get more accuracy by averaging multiple conversions. If you
need only one channel, set MULT = 0, then average all eight result
avg = ((ADR0>>6) + (ADR1>>6)
+ (ADR2>>6) + (ADR3>>6)
+ (ADR4>>6) + (ADR5>>6)
+ (ADR6>>6) + (ADR7>>6)) >> 3;