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.

PIC16F877 ADC coding help

Status
Not open for further replies.

harakiri

Newbie level 3
Joined
May 24, 2008
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,318
pic adc code

hello

i have a GP2D12 infra red sensors 7 of them by SHARP
i am new with PIC16F877. I don't how to configure my PIC in C.
the sensors has a analog voltage output range of 0.5 ~ 4 i need help on how to read this voltage in any form i can handle it. i am a bit familiar with C programing i just do'nt know how to use the ADC of PIC. any help will be appreciated. thank you
 

pic16f877a using adc coding

hi,
which c compiler u r going to use?
 

pic adc in c

if you use the picc compiler you must set the analoge on by write
input_analogs_on or something lik this
 

pic adc c

Symbolic code:
Assuming you use AN0 and want the 10 bits resolution access as a Word
ADCON0=01000001

ADCON1=10000000
all Analogs on Port A ref analog =VDD
If you need specific values of reference ADVISE here

ADCON0.2 Set to start Conversion
check if reset before reading 10 bits on ADRESH and ADRESL

If you want I'll give you the approximate code in Symbolic langage
As it is difficult to do it in a universal C langage(they all got their little additions of Special registers access for the specific C)

here it is

Word Reading=0
Byte adresl,adresl
byte adcon0,adcon1

SetUP: adcon0=01000001B
adcon1=10000000B

Running:

Loop:
...
...
adcon0,2=1
while adcon0,2=1
//wait for conversion to end
wend

Reading=adresh*256+adresl
...
...
goto Loop
gebepic.:D
 

hi-tech c adc

this easy c code for you to read ADC on 16F877:
//---------------read ADC on channel 4-------------
byte ROC()
{
byte oc;
set_adc_channel(4);
delay_ms(1); // option
oc = read_adc();
return oc;
}
good luck!
 

pic adc .c

I am using (HI-TECH PICC Compiler version 8.01PL3)
Sorry guys it took me too long to reply. my PC got seriously bug by viruses,trojan. had to reformat.

about the coding.
i am really sorry and sad to say but i am new with the PIC coding functionalities.
one of my questions would also be that. how does the PIC understand the analog voltage ranges. for example. 0.5 v ~ 3.0 in 10 bits ADC in means 1024 varying information on each voltage. would it mean. at 0.5 the bit is 1, while 0.6 is around 20 i think just an example. as the voltage increase the bit value increases also? to the max of 1024? if so,to determine that i am at 1.0 volts the pic ADC would giv me a value around 200+ or so..... i am not sure... i need help(confused). and how to configure the PIC to use the ports as ADC... Can i use all 7 of the sensors in one PIC? to determine there ADC values? Thank you for your time in reading this. a appriciate it very much.
 

adc pic code

hi, I know. Normally, PIC using GND = Vref- and VDD = Vref+, but i can change position of Vref on pin of PIC by writing another configure, you can use setup command to do that.
 

code picc adc

After extensive reading on the Data sheet i am able to do ADC now.
my code is TEST.c

the code under comments are just my testing code for PROTEUS 7

my circuit in PROTEUS have 7 def dc supply of analog inputs. starting from channel 1-7.

my problem is that now, the data i get from channel 1 only appeares after a second run all the PIC. DO PIC RESETS after main?? and goes back to begining? well as i test the PIC the CHannel 1 data only apperaes on the second run of the PIC. need help. by the way is my delay every after channel change ok? ty

Added after 1 minutes:

DATA
 

how to perform adc in c for pic

goood but we need more
 

@ harakiri

Disable the WATCHDOG timer. I think you have enabled it and are not resetting it. The watchdog timer actually needs to be periodically reset in the software otherwise it resets the microcontroller itself. This is to avoid any unexpected HALT of the microcontroller.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top