nitin.joshi52@gmail.com
Newbie level 5
- Joined
- Mar 30, 2013
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,396
void init_ADC(int port) {
// AD1CON1 = 0x00E0
_ADON = 0; // Turn the device off while we set it up
_ADSIDL = 0; // Allow the device to continue operating in idle mode
_ADDMABM = 0; // DMA buffers are written in scatter/gather mode.
_AD12B = 0; // Use the 10 bit operation mode.
_FORM = 0b00; // we're using a signed integer 10-bit operation
_SSRC = 0b111; // Allow the internal counter to end sampling and start conversion (auto-convert)
_SIMSAM = 0; // Samples on multiple channels are done individually.
_ASAM = 0; // Sampling begins when SAMP bit is set.
_SAMP = 0; // Do not start sampling yet
_DONE = 0; // Clear the Done status bit
// AD1CON2 = 0x0000
_VCFG = 0b000; // Set V_REFH to A_VDD and V_REFL to A_VSS
_CSCNA = 0; // Do not scan inputs CH0+.
_CHPS = 0b00; // Convert CH0.
_BUFS = 0; // Not using _BUFM.
_SMPI = 0b0000; // Increments the DMA address or generates interrupt after completion of every sample/conversion operation
_BUFM = 0; // Start filling the buffer from the start address
_ALTS = 0; // always use channel input selects for Sample A
// AD1CON3 = 0x1F02
_ADRC = 0; // Use the clock derived from the system clock.
_SAMC = 0b11111;// Take samples for 31 clock cycles
_ADCS = 0x02;
AD1CSSL = 0;
switch(port) {
case 2: // Set AN0 as an input.
_PCFG0 = 0; // Set the analog/digital port to be analog.
break;
case 3: // Set AN1 as an input.
_PCFG1 = 0; // Set the analog/digital port to be analog.
break;
case 4: // Set AN2 as an input.
_PCFG2 = 0; // Set the analog/digital port to be analog.
break;
case 5: // Set AN3 as an input.
_PCFG3 = 0; // Set the analog/digital port to be analog.
break;
case 6: // Set AN4 as an input.
_PCFG4 = 0; // Set the analog/digital port to be analog.
break;
case 7: // Set AN5 as an input.
_PCFG5 = 0; // Set the analog/digital port to be analog.
break;
default:
error(20); // Invalid port specified
}
_ADON = 1; // Turn the ADC on.
}
int ADC_read(int port)
{
int ADCReading = 0; // variable to return.
ADC1BUF0 = 0; // Clear the buffer
_DONE = 0; // Clear the done bit.
switch(port) {
case 2: // Read ADC info from AN0.
if(0 == _PCFG0) AD1CHS0 = 0x0000;
else error(81);
break;
case 3: // Read ADC info from AN1.
if(0 == _PCFG0) AD1CHS0 = 0x0001;
else error(81);
break;
case 4: // Read ADC info from AN2.
if(0 == _PCFG0) AD1CHS0 = 0x0002;
else error(81);
break;
case 5: // Read ADC info from AN3.
if(0 == _PCFG0) AD1CHS0 = 0x0003;
else error(81);
break;
case 6: // Read ADC info from AN4.
if(0 == _PCFG0) AD1CHS0 = 0x0004;
else error(81);
break;
case 7: // Read ADC info from AN5.
if(0 == _PCFG0) AD1CHS0 = 0x0005;
else error(81);
break;
default:
error(80);
break;
}
// If there is no reading then take one.
if(!ADCReading) {
AD1CON1bits.SAMP = 1; // start sampling ...
while (!_DONE); // wait for it to finish sampling
ADCReading = ADC1BUF0; // put the bits in the return variable
}
return ADCReading; // return the reading
}
22.3ADC and DMA
If more than one conversion result needs to be buffered
before triggering an interrupt, DMA data transfers can
be used. ADC1 can trigger a DMA data transfer. If
ADC1 is selected as the DMA IRQ source, a DMA
transfer occurs when the AD1IF bit gets set as a result
of an ADC1 sample conversion sequence.
The SMPI<3:0> bits (AD1CON2<5:2>) are used to
select how often the DMA RAM buffer pointer is
incremented.
The ADDMABM bit (AD1CON1<12>) determines how
the conversion results are filled in the DMA RAM buffer
area being used for ADC. If this bit is set, DMA buffers
are written in the order of conversion. The module
provides an address to the DMA channel that is the
same as the address used for the non-DMA
stand-alone buffer. If the ADDMABM bit is cleared, then
DMA buffers are written in Scatter/Gather mode. The
module provides a scatter/gather address to the DMA
channel, based on the index of the analog input and the
size of the DMA buffer.
Which Compiler are you using? mikroC PRO dsPIC?
my compiler is xc16 .......................device is microstick(dspIC33fj64mc802)
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?