### 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.

Status
Not open for further replies.

#### nazstaphobia

##### Newbie level 5
/**********************************************************/
/***** Robot with Object Detector *************************/
/**********************************************************/

int Adc; // Save analog Data

{
while(ADCON0.GO); // Wait Until Convert Complete
}
void main()
{

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

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;
}
}
}
/**********************************************************/

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
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)

and so on.

In your code, make following modifications:

{
while(ADCON0.GO); // Wait Until Convert Complete
}

void main()
{

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

//CONTROL PART

//CONTROL PART

//CONTROL PART
.
.
.
.
.
.
.

#### btbass

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:

#### nazstaphobia

##### Newbie level 5
thanks for your help matbob and btbass.i really appreciate it..

#### ksathishece

##### Newbie level 1
Hi btbass,

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

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

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

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.

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) {
//I would delay a little here for tacq
while(ADCON0.GO) continue; // Wait Until Convert Complete
}

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

...

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.