# ADC Help-Pickit2 Starter Kit Example

Status
Not open for further replies.

#### gamersat678

##### Junior Member level 1
For the past day I've been trying to get adc to work and generate a voltage reading (0-5v) using either pin ra0 or ra2. I just can't get my code to work. I'm programming in Mikrobasic. Does anyone have any source plus a simple circuit schematic to show me how this works, or example source that would work with the built in parts of the pickit2 starter kit?

This is what I have.
Code:
program ADC

main:
ANSEL  = 0xff           ' Configure AN2 pin as analog
TRISA  =%1111111         ' PORTA is input
ANSELH = 0              ' Configure other AN pins as digital I/O
TRISC  = 0
TRISB  = 0              ' PORTB is output
while (TRUE)
portc.1=1
end if
portc.0=1
delay_ms(100)
wend
portc.0=0
end.

#### gamersat678

##### Junior Member level 1
Thanks for the link! Didn't fix my problem though. But after 6 hours of educated guessing I changed the mcu clock frequency from 8mhz to 4mhz. The leds are blinking now (2nd example on your link). Did I have the value set to high, what is the standard value? Sorry I'm new to this stuff. Btw i have two chips 16f690,16f677 different mcu clock speeds for each?

Any code for getting a voltage?

#### Jack// ani

gamersat678 said:
Thanks for the link! Didn't fix my problem though. But after 6 hours of educated guessing I changed the mcu clock frequency from 8mhz to 4mhz. The leds are blinking now (2nd example on your link). Did I have the value set to high, what is the standard value? Sorry I'm new to this stuff. Btw i have two chips 16f690,16f677 different mcu clock speeds for each?

Any code for getting a voltage?

What is source of your uC clock? Are you using 8Mhz crystal or internal software configurable oscillator?

According to the datasheet both uC should work fine at 8Mhz, so my initial guess is there is something wrong with your clock source!

#### gamersat678

##### Junior Member level 1
Wow I just checked again using 8mhz and the code decided to work again???
I guess my only question now is how do you make a voltage reader out of this?
Code:
program ADC_10

main:
TRISA  = %11111111       ' PORTA is input

TRISD  = %00000000       ' PORTD is output

ADCON1 = %1000010        ' PORTA is in analog mode,
'   0 and 5V are referent voltage values,
'   and the result is aligned right
eloop:

PORTD = Lo(AD_Res)       ' Display lower byte of result on PORTD
Delay_ms(500)            ' 500 ms pause
goto eloop               ' Repeat all
end.                     ' End of program

#### Jack// ani

I assume by voltage you are referring to decimal value of your input voltage. Basically you have to convert binary value obtained from ADC to floating decimal.

I don't know how you do that in BASIC, but here is how you that in C. I hope you can tweak to make it work in BASIC.

Code:
#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

void main()
{

int16 value; //Value is a 10bit data sample
float volts;
setup_port_a(ALL_ANALOG); // All the 8 channels are analog

while(1)
{
volts=(float)value*0.0048828125; // 1024 discrete levels, 5/1024 = 0.0048828125
printf("Voltage is : %3.3f\n",volts);// Print upto 3 decimal places
delay_ms(500);
}
}

Good luck

#### gamersat678

##### Junior Member level 1
So I think I got it.
Code:
program ADC_10

dim valu as byte

main:
TRISA  = %11111111       ' PORTA is input

TRISC  = %00000000       ' PORTD is output

ADCON1 = %1000010        ' PORTA is in analog mode,
'   0 and 5V are referent voltage values,
'   and the result is aligned right
eloop:
Select Case valu
Case 4
portc=0xff
Case 3
portc.2=1
Case 2
portc.1=1
Case 1
portc.0=1
Case 0
portc=%00000000
End Select

'PORTC = Lo(AD_Res)       ' Display lower byte of result on PORTD
Delay_ms(500)            ' 500 ms pause
goto eloop               ' Repeat all
end.                     ' End of program

But is there any reason why the volt reading would be off .068 volts per volt (voltmeter vs pic adc, .933v-1v, 1.871v-2v, 2.803v-3v)?
Also what does ADCON1 = %1000010 mean?

#### hugo

##### Full Member level 6
gamersat678 said:
So I think I got it.
Code:
program ADC_10

dim valu as byte

main:
TRISA  = %11111111       ' PORTA is input

TRISC  = %00000000       ' PORTD is output

ADCON1 = %1000010        ' PORTA is in analog mode,
'   0 and 5V are referent voltage values,
'   and the result is aligned right
eloop:
Select Case valu
Case 4
portc=0xff
Case 3
portc.2=1
Case 2
portc.1=1
Case 1
portc.0=1
Case 0
portc=%00000000
End Select

'PORTC = Lo(AD_Res)       ' Display lower byte of result on PORTD
Delay_ms(500)            ' 500 ms pause
goto eloop               ' Repeat all
end.                     ' End of program

But is there any reason why the volt reading would be off .068 volts per volt (voltmeter vs pic adc, .933v-1v, 1.871v-2v, 2.803v-3v)?
Also what does ADCON1 = %1000010 mean?

Try this piece of code : // ( mikroC ) !!!

Volts = Volts / 1024; // Voltage in mV

or this one :

mV = (adc_value * 5000) >> 10; // Voltage in mV

" Also what does ADCON1 = %1000010 mean? "

You have to read the datasheet for the PIC you're using ...

Status
Not open for further replies.