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.

adding 2 adc modules in C coding in MicroC.Need help.

Status
Not open for further replies.

nazstaphobia

Newbie level 5
Joined
Mar 17, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
/**********************************************************/
/***** Robot with Object Detector *************************/
/**********************************************************/

int Adc; // Save analog Data

void Read_Adc()
{
ADCON0=0b10000001; // Select Analog2 and ADON
ADCON0.GO=1; // Start Convert
while(ADCON0.GO); // Wait Until Convert Complete
Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc
}
void main()
{

TRISA=0xFF; //SET PORT A AS INPUT
TRISC = 0X00; //SET PORT C AS OUTPUT
while(1)
{
Read_Adc(); // Read Analog 2


if (Adc>300) // if Detect object in range
{
PortC.F0 = 1;
delay_us(1000); //servo turn left
PortC.F0 = 0;
}
else
{
PortC.F0 = 1;
delay_us(1500); //servo turn to normal position 90 degree
PortC.F0 = 0;
}
}
}
/**********************************************************/


this coding has been tested in MikroC and working. I would like to add 1 more same sensor to detect for right and turn the servo to right.my problem is i stuck on how to declare the sensor in adcon0.i already read the datasheet, search forums, ask my friend and some tutorials.most of them only show 1 adc module.so how to insert 2 adc modules in this coding.please2 help me.i really appreciate it.i'm using sharp IR GP2y0a21yk and pic16f877a.please help me how to integrate 2 adc modules to this coding.thanks.

Added after 2 minutes:

i forgot to tell.the first coding using port AN0.and i would like to insert AN1 for another sensor.so how to configure the ADCON0??
 

matbob

Full Member level 2
Joined
May 15, 2008
Messages
128
Helped
26
Reputation
52
Reaction score
12
Trophy points
1,298
Location
KERALA
Activity points
2,061
Hi nazstaphobia,

Just refer to PIC16F877A datasheet and you would find the answer yourself. It is very simple. Take a look at the ADCON0 register in the datasheet.

Actually, you are not adding another ADC module, but you are going to use one more ADC input channel. There is only one ADC module in the controller and there are 8 inputs to the ADC and these inputs are multiplexed ie; only one input channel is selected at a time. To select a particular channel you need to manipulate few bits in the ADCON0 register.

In the ADCON0 register, bits 5,6,3 (starts at bit7 and ends at bit0) are used to select the channel.

bit 5-3 CHS2:CHS0: Analog Channel Select bits
000 = Channel 0 (AN0)
001 = Channel 1 (AN1)
010 = Channel 2 (AN2)
011 = Channel 3 (AN3)
100 = Channel 4 (AN4)
101 = Channel 5 (AN5)
110 = Channel 6 (AN6)
111 = Channel 7 (AN7)


So for AN0: ADCON0=0b10000001;
AN1: ADCON0=0b10001001;
AN2: ADCON0=0b10010001;

and so on.

In your code, make following modifications:


void Read_Adc()
{
ADCON0.GO=1; // Start Convert
while(ADCON0.GO); // Wait Until Convert Complete
Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc
}

void main()
{

TRISA=0xFF; //SET PORT A AS INPUT
TRISC = 0X00; //SET PORT C AS OUTPUT
while(1)
{
//FOR READING AN0
ADCON0=0b10000001;//SELSECT AN0
Read_Adc();

//CONTROL PART

//FOR READING AN1
ADCON0=0b10001001;//SELSECT AN1
Read_Adc();

//CONTROL PART

//FOR READING AN2
ADCON0=0b10010001;//SELSECT AN2
Read_Adc();

//CONTROL PART
.
.
.
.
.
.
.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,863
Adc=(ADRESH*4)+(ADRESL/64); ??????

This looks odd.
This will generate a lot of machine code for a multiply and a division, and I cant see how it is correct.
The top 2 bits of the result are in ADRESH and the lower 8 bits are in ADRESL.

Surly the accepted way to get the result is:

Adc = (ADRESH << 8 );
Adc += ADRESL;
 

nazstaphobia

Newbie level 5
Joined
Mar 17, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,347
thanks for your help matbob and btbass.i really appreciate it..:D
 

ksathishece

Newbie level 1
Joined
Jul 7, 2010
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
UK
Activity points
1,288
Hi btbass,

Can u explain the lines below, which u had used before.

Adc = (ADRESH << 8 );
Adc += ADRESL;

Also I am using a Sharp IR range sensor GP2D12 and i am getting only maxi 1.8Volts.
I am using PIC 18F46K20 with two of the sensors at right and left side.
It is 3.3 v microcontroller and I set ± VREF internally.
I want to tell the PIC, to change direction when I get 1volt (8cm from object).

in coding posted in this forum, for this line

if(ADC >______) {if(ADC>300) posted before}

I don't know what value to set in this for my case( for 1v) and also how to see the 10 bit value.



pls help me ...

:|
 

millwood

Advanced Member level 3
Joined
Jul 2, 2009
Messages
734
Helped
80
Reputation
164
Reaction score
35
Trophy points
1,308
Activity points
5,088
in general, you should have a routine that initialize a peripheral. for example, adc_init() would put the adc to left justified, set the adc clock frequency, etc.

and then adc_read() to return the result of adc.

I would also give adc_read() an argument so that it can select the channel to be adc'd.

Code:
unsigned int adc_read(unsigned char adc_ch) {
ADCON0 = (ADCON0 & 0b111000111) | (adc_ch << 3);  //adc_ch is bit 5..3 of adcon0
//I would delay a little here for tacq
ADCON0.GO=1; // Start Convert
while(ADCON0.GO) continue; // Wait Until Convert Complete
return (ADRESH*4)|(ADRESL/64); // 10 bit Data ==> Adc, left justified
}

.... in your main....

#define AN0 0b000
#define AN1 0b001
...

adc=adc_read(AN0);  //does adc on an0 and return the result to adc
...

if (adc_read(AN1)) ... //does adc on an1 and branch accordingly.

hope it helps.

I cant see how it is correct.

left justified vs. right justified.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top