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.

Help need in PIC16F877A ADC values..

Status
Not open for further replies.

chwoei

Newbie level 6
Newbie level 6
Joined
Dec 14, 2006
Messages
12
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,367
adresh adresl

hi everyone,

i got some problem in getting the values in c programming for analog to digital input for PIC16F877A. Do anyone knows how to get the values when a infrared sensor(analog) is converted in Digital values? how to set the ports or any alogorithms needed in programing to get the values when different distance is detected.

Hope someone good in PIC could help!! :p... hope to hear from u guys soon..

Kind regards,
CHANG
 

adresl adresh

chwoei said:
hi everyone,

i got some problem in getting the values in c programming for analog to digital input for PIC16F877A. Do anyone knows how to get the values when a infrared sensor(analog) is converted in Digital values? how to set the ports or any alogorithms needed in programing to get the values when different distance is detected.

Hope someone good in PIC could help!! :p... hope to hear from u guys soon..

Kind regards,
CHANG

This code for CSS c compiler
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif


void main() {

int i, value, min, max;

printf("Sampling:");

setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );

do {
min=255;
max=0;
for(i=0; i<=30; ++i) {
delay_ms(100);
value = Read_ADC();
if(value<min)
min=value;
if(value>max)
max=value;
}
printf("\n\rMin: %2X Max: %2X\n\r",min,max);

} while (TRUE);
}

i hope it can help you
 

adresl

hi there,
thanks for the respond, but i am using a picc compiler, can i have a picc one? and also comments on the code... thanks.
 

adresl and adresh

hi, can i as wat is the value = Read_ADC(); <--- the function read_adc() from? issit form the header file? and how was it actually in a pic.. could u tell me which register in pic?
 

adresh ccs pic c + 10 bits

initialise PORTA's appropiate pin as "analog & input" in ADCON1 register and TRISA register.


select ADC Pin from which Analog value to read in ADCON0
a small delay
set ADGO bit
wait untill ADGO bit is cleared by hardware ( means ADC data is ready )
ADC Value = ADRESH * 256 + ADRESL
 

16f877a picc adc

hi,
thanks for the explaination. can u tell me how this works

ADC Value = ADRESH * 256 + ADRESL


thanks
 
pic16f877 do not responding

Hi!
The result generated by the ADC is 10 bit and is held in the ADRESH(high byte) and ADRESL(low byte). The result can be combined into a single 16bit variable(i.e integer)by using

ADC Value = ADRESH * 256 + ADRESL

when u multiply the contents of ADRESH with 256 it is actually shifted 8 times to the left and then u simply add the lower register to it to get the actual value.

i.e e.g if ADRESH=0X03; and ADRESL=0X0F;

then

ADC Value = (ADRESH<<8)|ADRESL;

will give the value of ADC Value = 0x030F;

which was the actual result.

hope this helps.

Regards.

Added after 2 minutes:

Hi!
Sorry for the smiley, i donot know how it got into that equation. The actual equation is

ADC Value = (ADRESH<<8)|ADRESL;
Regards.

Added after 4 minutes:

Sorry once again i cannot figure out why it is hapenning.

ADC Value = (ADRESH<<8)|ADRESL;

Regards.
 
Hello,

I'll tell you how to do this in assembly:

Provided that PIC16f877 uses 10 bit resolution for ADC, it implies that two registers ADRESH and ADRESL represent analog data with a maximum of (2^10) - 1 = 1023.

Now here's what to do:

Step 0. Make the ADRESH & ADRESL Right Justified (now 00000011 11111111 is same as 1023 in decimal)
Step 1. Store ADRESH to a local an 8 bit variable say R1
Step 2. Store ADRESL to another 8 bit local variable say R2

Given the above steps, you have R1 R2 which are two eight bit registers having the actual digital value of the analog input.

Step 3. Do a 16 bit subtraction between the read value and 03 FF (1023 in decimal) just like in the classical 8086 style and convert the difference into ASCII. At this point you can do a multiplication procedure to get the input voltage in decimal form.

Step 4. To convert to ASCII (A tip) you'll have to do 1000's, 100's, 10's and 1's subtraction from the two 8 bit registers one by one to get the exact ASCII values of the individual digits. These individual digits can then be sent directly to your lcd or be stored to RAM for later use.

Congratulations! You've just learnt how to handle 16 bit data in PIC assembly :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top