Hardware Reference
In-Depth Information
ATD0CTL3 5 0x22;
ATD0CTL4 5 0x05;
}
void wait20us (void)
{
TSCR1 5 0x90;
TSCR2 5 0;
TIOS
| 5 OC0;
TC0
5 TCNT 1 480;
while(!(TFLG1 & C0F));
}
Example 12.7
Write a program to perform A/D conversion on the analog signal connected to the AN7 pin.
Collect 20 A/D conversion results and store them at memory locations starting from $1000.
Use the same configuration as in Example 12.6.
Solution: To collect 20 conversion results, we need to write into the ATD0CTL5 register five
times. Each time we will wait for the SCF flag to be set and collect four results. The program is
as follows:
#include “c:\miniide\hc12.inc”
org
$1500
lds
#$1500
ldx
#$1000
; use index register X as a pointer to the buffer
jsr
openAD0
; initialize the ATD0 converter
ldy
#5
loop5
movb
#$87,ATD0CTL5
; start an A/D conversion sequence
brclr
ATD0STAT0,SCF,*
movw
ATD0DR0,2,x 1
; collect and save the conversion results
movw
ATD0DR1,2,x 1
; post-increment the pointer by 2
movw
ATD0DR2,2,x 1
;
movw
ATD0DR3,2,x 1
;
dbne
y,loop5
swi
end
The C language version of the program is as follows:
#include “c:\cwHS12\include\hcs12.h”
void openAD0 (void);
int buf[20];
void main (void)
{
int i;
openAD0();
for (i 5 0; i , 5; i 11 ) {
ATD0CTL5 5 0x87;
/* start an A/D conversion */
while (!(ATD0STAT0 & SCF));
/* wait for the A/D conversion to complete */
buf[4*i 1 0] 5 ATD0DR0;
/* save results right-justified */
buf[4*i 1 1] 5 ATD0DR1;
Search WWH ::




Custom Search