bubble_d
Junior Member level 3
Hi Everyone,
So I need help Implementing a 4 channel ADC where I read 4 voltages.
So far I have one ADC working, but I dont really understand how I would do multiple adc sampling.
When I initialize my ADC, do I just need to change my AD1PCFGH (Analog inputs) to select my four input pins?
Also I am unsure how to read the voltages of three channels, do i need four ADC read functions where I return 4 separate buffers which will give me my voltage?
Here is what I have for now: (ADC.h)
Obviously I am getting errors that are telling me ADC1BUF1 is undeclared.
Can anyone help me out? Or even lead me in the right direction with an example?
Thank you.
[EDIT] I just realized there is no ADC1BUF0 . I guess Im looking into this all wrong... I set it to ADC1BUF0 and the program ran but I was getting the max voltages (I.e the voltages added together but it clips at 3.3) How do I do this!~!!
So I need help Implementing a 4 channel ADC where I read 4 voltages.
So far I have one ADC working, but I dont really understand how I would do multiple adc sampling.
When I initialize my ADC, do I just need to change my AD1PCFGH (Analog inputs) to select my four input pins?
Also I am unsure how to read the voltages of three channels, do i need four ADC read functions where I return 4 separate buffers which will give me my voltage?
Here is what I have for now: (ADC.h)
Code:
void initADC( int amask)
{
AD1PCFGH = amask; // select analog input pins
AD1CON1 = 0x00E0; // data output format-integer, auto convert after end of sampling.12bit Operation mode. No need to include a delay loop to provide time for completion of sampling
AD1CSSL = 0; // no scanning required
AD1CON3 = 0x1FFF ; // Use internal clock, max sample time avail is 31Tad, conversion time Tad = 128*Tcy = 128*(2/Fosc) = 256/8MHz = 32us which is > (required)75ns ); 31 Tad = 31*32us = 992us is the sample time. About 100samples/sec
//Possibly 1FFFF Check Both
AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC
int readADC1( int ch)
{
AD1CHS0 = ch; // select analog input channel
ADC1BUF0 = 0x0000; // Reset Buffer
AD1CON1bits.SAMP = 1; // start sampling, automatic conversion will follow at end of sampling
while (!AD1CON1bits.DONE); // wait to complete the conversion. DONE will be set as soon as conversion ends
return ADC1BUF0; // read the conversion result
} // readADC
//Forth Module is used for now because it is easy to access in hardware setup
int readADC4( int ch)
{
AD1CHS0 = ch; // select analog input channel
ADC1BUF0 = 0x0000; // Reset Buffer
AD1CON1bits.SAMP = 1; // start sampling, automatic conversion will follow at end of sampling
while (!AD1CON1bits.DONE); // wait to complete the conversion. DONE will be set as soon as conversion ends
return ADC1BUF0; // read the conversion result
} // readADC
Obviously I am getting errors that are telling me ADC1BUF1 is undeclared.
Can anyone help me out? Or even lead me in the right direction with an example?
Thank you.
[EDIT] I just realized there is no ADC1BUF0 . I guess Im looking into this all wrong... I set it to ADC1BUF0 and the program ran but I was getting the max voltages (I.e the voltages added together but it clips at 3.3) How do I do this!~!!
Last edited: