Maker Pro
Maker Pro

adc temperature problem

i have used the following code with my pic 16f73

unsigned char myvalue;
void main()
org 0x10
{
adcon0=0x85;
adcon1=0x01;
trisa=0xff;
trisc=0x00;
while(1)
{
myvalue=adc_read(0);
portc=myvalue;
delay_ms(2000);
}}
but now i want to use it with pic 16f870
but it is not showing the right output as pic 16f870 is 10 bit adc and pic 16f73 is 8 bit adc
so is there any thing i can do to solve it and do i need to adjust the vref value for new pic
 
Assuming adc_read() returns the full 10-bit value, you could simply divide by 4 (or shift right 2) to make it an 8 bit value the same as your old PIC.

Also, with ADFM = 0 (bit 7 of ADCON1), the result is placed with the 8 most significant bits in ADRESH and the 2 least significant bits in ADRESL, giving a result left shifted 6 bits. If you read ADRESH and ignore ADRESL, you'll get the equivalent of an 8-bit A/D conversion.
 

KrisBlueNZ

Sadly passed away in 2015
Great answer kpatz. Did you get the PM I sent you? Just below the Electronics Point banner at the top of any page, at the right side, click on Private Messages.
 
is this code right

unsigned char myvalue;
void main()
org 0x10
{
adcon0=0x85;
adcon1=0x01;
trisa=0xff;
trisc=0x00;
while(1)
{
myvalue=adc_read(0) >> 2;
portc=myvalue;
delay_ms(2000);
}
}
 
I found documentation for ADC_Read() which is a MikroC library function. It returns a 10-bit A/D result so simply shift right 2, like in your example code:

myvalue = ADC_Read(0) >> 2;

should do the trick.

BTW, since you're using the ADC library functions you should use ADC_Init() to set up the ADC instead of setting the ADCON registers yourself.
 
Last edited:
yes, adc_read(0) is reading value from temperature sensor connected to the adc 0 port.
but the code with myvalue=adresh didnt work
 
Last edited:
Top