Hardware Reference
In-Depth Information
buf[4*i 1 2] 5 ATD0DR2;
buf[4*i 1 3] 5 ATD0DR3;
}
asm ( “swi” );
}
Example 12.8
Write a C program to be run on the Dragon12 demo board to display the voltage (output of
a potentiometer) connected to the AN7 pin. Configure the AD0 properly, perform five conver-
sions per second, and display the voltage on the LCD.
Solution: The conversion result 1023 corresponds to 5 V. Therefore, we need to divide the con-
version result by 204.6 to convert the result back to voltage. Since the HCS12 does not support
floating-point operation directly, we multiply the conversion result by 10 and then divide the
product by 2046 to obtain the voltage value. The C program is as follows:
#include “c:\cwHCS12\include\hcs12.h”
#include “c:\cwHCS12\include\delay.h”
// include delay.c in the project
#include “c:\cwHCS12\include\lcd_util.h”
// include lcd_util.c in the project
void openAD0(void);
main(void)
{
char buffer[6];
/* used to hold the voltage value */
int temp;
char *msg1 5 “Voltage 5 ”;
openlcd();
buffer[1] 5 '.';
/* decimal point */
buffer[3] 5 0x20;
/* space character */
buffer[4] 5 'V';
/* volt character */
buffer[5] 5 0;
/* null character */
openAD0();
while(1) {
ATD0CTL5 5 0x87;
/* convert AN7, result right-justified */
while(!(ATD0STAT0 & SCF));
/* wait for conversion to complete */
buffer[0] 5 0x30 1 (ATD0DR0 * 10 )/2046;
temp 5 (ATD0DR0 * 10) % 2046;
/* find the remainder */
buffer[2] 5 0x30 1 (temp * 10)/2046;
/* compute the fractional digit */
cmd2lcd(0x80);
/* set LCD cursor to upper left corner*/
puts2lcd(msg1);
/* output the message “voltage 5 ” */
puts2lcd(&buffer[0]);
/* output voltage string */
delayby100ms(2);
/* wait for 200 ms */
}
return 0;
}
void openAD0 (void)
{
int i;
ATD0CTL2 5 0xE0;
/* enable AD0, fast ATD flag clear, disable AD0 in wait mode */
 
Search WWH ::




Custom Search