// LAB A // PAX, comments updated 3/30/17 for ATDCTL4 // INPUT: POT 0-5Vdc (PORT
ID: 3814114 • Letter: #
Question
// LAB A
// PAX, comments updated 3/30/17 for ATDCTL4
// INPUT: POT 0-5Vdc (PORTAD channel 0, OUTPUT: LEDs 1-4 (PORT T UPPER NIBBLE)
//This program demonstrates the use of the ADC.
// AN0 is connected to the potentiometer on the TWR-S12G128.
// Observe the result on the LEDs
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
// Global variable
int value;
// Function Prototypes
void init_ATD(void);
void main(void)
{
DDRT = 0xF0; // PORT T7 – T4 output (LEDs)
init_ATD(); // Call ATD initialization function
// the infinite loop reads the ADC conversion result saving it to value
for(;;)
{
while(!(ATDSTAT0_SCF)); // wait for conversion to complete
value = ATDDR0; // Read ATD0 data and assign result to variable value
ATDSTAT0_SCF = 1; // clear conv seq complete flag
ATDCTL5 = 0; // start a new conversion
PTT = (~value) & 0xF0; // logic Low turns on LED
} // end of infinite for loop
} // end of main
// initialize A to D converter
void init_ATD(void)
{
ATDCTL1_SRES = 0; // 8 bit results
ATDCTL3_DJM = 1; // right justified data
ATDCTL3_S8C = 0; // one conversion per sequence
ATDCTL3_S4C = 0; // one conversion per sequence
ATDCTL3_S2C = 0; // one conversion per sequence
ATDCTL3_S1C = 1; // one conversion per sequence
ATDCTL4_PRS = 01; // ATDCTL4_PRS=11; note original lab uses bus clk div 24, = ATD clock freq, 6.25MHz/2(PRS+1)=260kHz TOO LOW!!! CHANGE so that fATDCLK = 500kHz-2MHz range, ATDCTL4_PRS=2; 6.25MHz/2(1+1)=1.56MHz VALID!!! ATDCTL4_PRS=5; 6.25MHz/2(5+1)=520kHz VALID!!! ref page 440-1”A/D Conversion Frequency” in textbook.
ATDCTL4_SMP = 4; // 12 ATD clock cycle to sample PHASE 1:2 CYCLES, PHASE 2: 12 CYCLES, PHASE 3: 8 CYCLES = 22 CYCLES(1/1.56MHz) = 14.7us & 22 CYCLES(1/520kHz) = 42.3us ref page 442”Calculating Conversion Time” in textbook.
ATDSTAT0_SCF = 1; // clear flag
ATDCTL5 = 0; // select channel 0 – starts first conversion
}
1. What SFRs commands are utilized for the configuration of the ADC? How does each affect the operation of the ADC?
Explanation / Answer
The command used for configuration of the ADC.
Command :
init_ATD(); // Call ATD initialization function
The above command is used for ATD initialization.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.