//Displaying ATD channel 0 (POT) on LEDs of PORTT for HCS12G128 Tower Board //Modified and tested by Mazidi from Program 13-1C of HCS12 book by Mazidi & Causey //With Contribution from Shu-Jen Chen //Notice the on-chip ADC of HCS12G is slightly different from the HCS12DT256/512 covered in the textbook //See Chapter 12 (ADC10B12CV2) in the MC9S12GRMV1.pdf ref. Manual //In TWR-S12G128 the Bus Freq=6.25 MHz. //ATD channel 0 (AN0) is connected to RV1 brown Pot Trimer //Change the POT to see the changes on LEDs of PORTT //In Codewarrior, make sure you are in Open Source BDM //In Codewarrior F7(make), F5(Debug) and F5 again to start running #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void MSDelay(unsigned int); void main(void) { DDRT = 0xF0; // PORTT as output ATDCTL1 = 0; // 8-bit resolution ATDCTL3 = 0x88; // right justified, one sample per sequence ATDCTL4 = 0x02; // prescaler = 2; ATD clock = 6.25MHz / (2 * (2 + 1)) == 1.04MHz // should be between 0.25MHz and 8 MHz per specs. for(;;) { ATDCTL5 = 0; // start a single conversion on channel 0 while(!(ATDSTAT0 & 0x80)); PTT = ~ATDDR0L; // dump it on LEDs (invert the value because LEDs are low active) //MSDelay(250); //optional } } void MSDelay(unsigned int itime) { unsigned int i; unsigned int j; for(i=0;i