Analog Capture
Function reads then convert analog to digital values and then returns that value as an integer
If used in a "for", a counter can be used to take readings from accumulative ADC channels (PAD0, PAD1, PAD3...)
void main(){
...
//wake up A?D converter... ATDCTL2
ATDCTL2 = ADPU;
delay(2);//~2ms
//set up desired behaviour... ATDCTL3
ATDCTL3 = SEQLEN | FIFO | FRZ;
// set up resolution and timing ... ATDCTL4
ATDCTL4 = SRES | SMP | PRS;
...
// Example usage
for(i=0; i<3; i++)
rdy[i]= readAnalog(i);
...
}
//readAnalog
//Function reads analog and converts to digital values and then returns that value
//input int i countes up channels
int readAnalog(int i){
int result=0;
//setup data parameter, conversion sequence and I/P channel... ATDCTL5
//start conversion
ATDCTL5 = DATA | SCAN | MULT | (CHAN+i);
while(!(ATDSTAT0 & SCF)){ //flashs LED2 for 100 ms after taking a successful reading
PTS = LED2;
delay(100);
PTS = 1;
delay(100);
}
//wait for conversion to be done
result = ATDDR0L+(ATDDR0H<<8); //low + high shifted 8 bits left (shift operator "<<" has a lower priority than "+")
return result;
}