beautypg.com

3 – adc code examples, Ds4830a user’s guide – Maxim Integrated DS4830A Optical Microcontroller User Manual

Page 63

background image

DS4830A User’s Guide

63

7.3 – ADC Code Examples

7.3.1 – One Sequence of 4 Voltage Conversions for Ch0 (Diff), Ch1 (Diff), Ch14 (Single), and Ch15 (Single)

PINSEL = 0xC00F; //Configure Pin as ADC Ch0 (Diff), Ch1 (Diff), Ch14 (Single) and Ch15(Single)

REFAVG_bit.INTREF = 1;

//Enable ADC internal reference


for(iCounter = 0; iCounter < 1000; iCounter++); //Wait ~1ms to settle ADC internal reference

ADCN_bit.ADCONT = 0;

//run a single conversion sequence


ADST_bit.ADCFG = 1;

//set ADDATA for configuration (ADCFG)

ADST_bit.ADIDX = 0;

//ADIDX = 0, set to ADCFG [0]


ADDATA = 0x0020;

//ADCFG [0]: Differential voltage, CH0, 1.2V FS, Right Aligned

ADDATA = 0x2021;

//ADCFG [1]: Differential voltage, CH1, 0.6V FS, Right Aligned

ADDATA = 0x400E;

//ADCFG [2]: Single voltage, CH14, 2.4V FS, Right Aligned

ADDATA = 0x600F;

//ADCFG [3]: Single voltage, CH15, 6.55V FS, Right Aligned


ADST_bit.ADCFG = 0;

//set ADDATA to data buffer


ADADDR_bit.ADSTART = 0; //start sequence with ADCFG [0]
ADADDR_bit.ADEND = 3; //end sequence with ADCFG [3]

ADST_bit.ADCONV = 1;

//start the conversions


while (!ADST_bit.ADDAI);

//wait for conversions to complete


ADST_bit.ADDAI = 0; //Clear ADDAI flag

ADST_bit.ADIDX = 0;

//set ADDATA to data buffer [0]


ch0_volt = ADDATA;

//read and store ch0 voltage to variable

ch1_volt = ADDATA;

//read and store ch1 voltage to variable


ADST_bit.ADIDX = 14;

//set ADDATA to data buffer [14] (according to channel number)


ch14_volt = ADDATA;

//read and store ch14 voltage to variable

ch15_volt = ADDATA;

//read and store ch15 voltage to variable


7.3.2 – Continuous Conversion of 16 Samples of Ch0 with Location Override


PINSEL = 0x0003; //Configure Pins as ADC Ch0 (Diff)

REFAVG_bit.INTREF = 1;

//Enable ADC internal reference


for(iCounter = 0; iCounter < 1000; iCounter++); //Wait ~1ms to settle ADC internal reference

ADCN_bit.ADCONT = 1;

//run a continuous conversion sequence

ADCN_bit.LOC_OVR = 1;

//location override enable


ADST_bit.ADCFG = 1;

//set ADDATA as configuration (ADCFG)

ADST_bit.ADIDX = 0;

//ADIDX = 0, set to ADCFG [0]


ADDATA = 0x0020;

//ADCFG [0]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 0

ADDATA = 0x0120;

//ADCFG [1]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 1

ADDATA = 0x0220;

//ADCFG [2]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 2

ADDATA = 0x0320;

//ADCFG [3]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 3


ADDATA = 0x0420;

//ADCFG [4]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 4

ADDATA = 0x0520;

//ADCFG [5]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 5

ADDATA = 0x0620;

//ADCFG [6]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 6

ADDATA = 0x0720;

//ADCFG [7]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 7


ADDATA = 0x0820;

//ADCFG [8]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 8

ADDATA = 0x0920;

//ADCFG [9]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 9

ADDATA = 0x0A20;

//ADCFG [10]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 10

ADDATA = 0x0B20;

//ADCFG [11]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 11


ADDATA = 0x0C20;

//ADCFG [12]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 12

ADDATA = 0x0D20;

//ADCFG [13]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 13

ADDATA = 0x0E20;

//ADCFG [14]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 14

ADDATA = 0x0F20;

//ADCFG [15]: Differential voltage, CH0, 1.2 V FS, Right Aligned and Location override 15