routine. The source file S12_ATD.C also contains some additional
functions for the integrated ATD module.
//=============================================================================
// File: S12_ATD.C - V1.00
//=============================================================================
//-- Includes -----------------------------------------------------------------
#include "datatypes.h"
#include <mc9s12dp512.h>
#include "s12_atd.h"
//-- Code ---------------------------------------------------------------------
// Func: Initialize ATD module
// Args: -
// Retn: -
//
void initATD0(void) {
// enable ATD module
ATD0CTL2 = BM_ADPU;
// 10 bit resolution, clock divider=12 (allows ECLK=6..24MHz)
// 2nd sample time = 2 ATD clocks
ATD0CTL4 = BM_PRS2 | BM_PRS0;
}
//-----------------------------------------------------------------------------
// Func: Perform single channel ATD conversion
// Args: channel = 0..7
// Retn: unsigned, left justified 10 bit result
//
UINT16 getATD0(UINT8 channel) {
// select one conversion per sequence
ATD0CTL3 = BM_S1C;
// right justified unsigned data mode
// perform single sequence, one out of 8 channels
ATD0CTL5 = BM_DJM | (channel & 0x07);
// wait until Sequence Complete Flag set
// CAUTION: no loop time limit implemented!
while((ATD0STAT0 & BM_SCF) == 0) ;
// read result register
return ATD0DR0;
}
//-----------------------------------------------------------------------------
User Manual
17