example 11 analog-to-digital converter lecture l5.1
TRANSCRIPT
miniDragon+
Serial cable
Reset button
Power plug
2 pushbutton switches
A/D Pot
Run/Load switch
7-segment display
I/O headersKeypad header
LCD Header
ATD0 PinsPADD0 – PADD7(ch# 0 – 7)
Pins 67,69,71,73, 75,77,79,81
ATD1 PinsPAD8 – PAD15(ch# 0 – 7)
Pins 68,70,72,74, 76,78,80,82
ATD0 PinsPADD0 – PADD7(ch# 0 – 7)
Pins 67,69,71,73, 75,77,79,81
ATD1 PinsPAD8 – PAD15(ch# 0 – 7)
Pins 68,70,72,74, 76,78,80,82
Pot
Method of Successive Approximation
1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 00000V
5V
2.5V
3.75V
3.125V3.4375V Vin = 3.5V
step 1 step 2 step 3 step 4
voltage
Method of Successive Approximation
Control
D/A Converter
V
V
in
DA
Binary OutputC+
-
1111 1110 1101 1100 1011 1010 1001 1000 0111 0110 0101 0100 0011 0010 0001 00000V
5V
2.5V
3.75V
3.125V3.4375V Vin = 3.5V
step 1 step 2 step 3 step 4
voltage
Table 11.1 C Function calls for the A/D converters
Function Description void ad0_enable(void); Enable ATD0 for 10 bits int ad0conv(char ch#); Return the average of 4 successive readings of channel ch# void ad1_enable(void); Enable ATD1 for 10 bits int ad1conv(char ch#); Return the average of 4 successive readings of channel ch#
// Example 11: A/D Converter -- Pot#include <hidef.h> /* common defines and macros */#include <mc9s12dp256.h> /* derivative information */#include "main_asm.h" /* interface to the assembly module */#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
int val;
void main(void) { PLL_init(); // set system clock frequency to 24 MHz ad0_enable(); // enable a/d converter 0 lcd_init(); // enable lcd while(1) { val = ad0conv(7); // read pot on channel 7 val = val >> 1; // shift 1 bit right (divide by 2) set_lcd_addr(0x40); // display on 2nd row of LCD write_int_lcd(val); // write value in field of 5 ms_delay(100); // delay 0.1 seconds }}
Example 11