Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] ADC Channel Selection

Status
Not open for further replies.

rajalakshmiA

Newbie level 6
Joined
Jun 23, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
130
Hi i am trying to read ADC voltage from tw0 channels AN4 and AN3.... Values are updating correctly... I want to detect which channel is using ... Can anyone help me?

Here is My code for pic32Mx795f512l with XC32 Compiler, if am using tempsensor i have to assign channel 1 in variable otherwise channel 2 in that variable..... How to do that....Pls help me it is urgent....

Code:
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#pragma config DEBUG = ON // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
#define SYS_FREQ (8000000L)
#define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_CONVERSION_DONE
#define config2 ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
#define config3 ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
#define configscan SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 |SKIP_SCAN_AN5 |SKIP_SCAN_AN6 |SKIP_SCAN_AN7 | \
SKIP_SCAN_AN8 |SKIP_SCAN_AN9 |SKIP_SCAN_AN10 |SKIP_SCAN_AN11 | \
SKIP_SCAN_AN12 |SKIP_SCAN_AN13 |SKIP_SCAN_AN14 |SKIP_SCAN_AN15
// AN4 = Temperature Sensor, AN3 = POT
#define configport ENABLE_AN3_ANA | ENABLE_AN4_ANA
#define GetPeripheralClock() (SYS_FREQ/(1 << OSCCONbits.PBDIV))
void main(){
TRISB = 0b00011000;
AD1PCFG = 0xFFE7;
CloseADC10(); // Ensure the ADC is off before setting the configuration
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 | ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN3);
OpenADC10( config1, config2, config3, configport,configscan);
  EnableADC10();
while(1){
while(!mAD1GetIntFlag()){
}
offset = 8 * ((~ReadActiveBufferADC10() & 0x01));

// Read the result of temperature sensor conversion from the idle buffer
tempSensor = ReadADC10(offset);

pot1=ReadADC10(offset+1);
 mAD1ClearIntFlag();
 }
}
 
Last edited by a moderator:

Hi ,

can you explain little more is my thought is wrong.
do you want to identify the channel sampled after adc interrupt, right?

first set any one of the channel before start adc conversion. Once adc interrupt triggered copy that value and initiate second one.

i think pic32Mx795f512l having only one adc. so first you need to set which channel need to read.
OR
also you can read AD1CSSL register value (ADC INPUT SCAN SELECT REGISTER) to identify which channel was sampled.

Thanks,
jolly jacob
 

Hi
do you want to identify the channel sampled after adc interrupt, right?
Yess
tempsensor is ADC channel 1 and pot is channel2 ,i can able to read the value in both the channels ...My question is
if i am using the tempsensor ,then i have to set the variable channel = 0x01 otherwise channel 0x02.... So pls help me on that ..
In my project ,sometimes we will use both the channels
As per my identification if i am using channel 2 ,then channel 1 will be zero and viceversa was happened according to that i have set the variable channel as 1 or 2... This method is corrrect or not ...This method will not work if i will use both the channels
 

Hi,

sorry, I am little confused with your requirement.Which one is highest priority, AN3 or AN4?. Are you using only one device at a time(tempsensor or POT)?. do you want to identify the channel only if the tempsensor is present? if not go to below section

Anyway if both device is connected and If you want to identify the channel, use interrupt routine. Inside interrupt routine read 'AD1CSSL' register and take decision on it.

please use this logic


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
unsigned short variable_channel = 0;
 
void interrupt()
{
//clear int flags here
if(AD1CSSL == 0x04)   // for channel AN3
     {
      variable_channel = 0;
     //you read adc result here if required
     }
 
if(AD1CSSL == 0x08)   // for channel AN4
     {
      variable_channel = 1;
      //you read adc result here if required
      }
//return from interupt
}
 
 
 
void main()
{
//initialize PIC and its peripherals(adc and interrupt-global and peripherals)
//Initialize global variables values
//anything else
 
while(true)
    {
 
    //other part of your code like set sequence of channel to be read
    }
 
}
 
//end

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top