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.

ADC Help-Pickit2 Starter Kit Example

Status
Not open for further replies.

gamersat678

Junior Member level 1
Joined
Jan 18, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,407
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
dim adc_rd as word

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
    'ADCON1 = %01100000
  while (TRUE)
  'Read pin ra2
    adc_rd = ADC_Read(2)  '*5/1023    ' get ADC value from 2nd channel
    if adc_rd>1 then
       portc.1=1
    end if
    portc.0=1
    delay_ms(100)
  wend
    portc.0=0
end.
 

gamersat678

Junior Member level 1
Joined
Jan 18, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,407
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

Advanced Member level 3
Joined
Dec 2, 2004
Messages
759
Helped
106
Reputation
220
Reaction score
58
Trophy points
1,308
Activity points
5,010
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
Joined
Jan 18, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,407
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

dim AD_Res as word


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:
  AD_Res = ADC_read(0)   ' Execute conversion and store result
                         '   in variable AD_Res.

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

Advanced Member level 3
Joined
Dec 2, 2004
Messages
759
Helped
106
Reputation
220
Reaction score
58
Trophy points
1,308
Activity points
5,010
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>
#device ADC=10
#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 
   setup_adc(ADC_CLOCK_INTERNAL); // Internal ADC Clock
   set_adc_channel(0); // RA0/AN0(Pin2) is analog input channel

   while(1)
   {
      value=read_adc();
      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
Joined
Jan 18, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,407
So I think I got it.
Code:
program ADC_10

dim AD_Res as word
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:
  AD_Res = ADC_read(0)'Lo(ADC_read(0))*-1/256*0.6/4.65'/1023*50   ' Execute conversion and store result
                         '   in variable AD_Res.
AD_Res=(Ad_Res*0.0048828125) '.00488 for 5v
valu=AD_Res div 1
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
Joined
Dec 31, 1999
Messages
323
Helped
49
Reputation
98
Reaction score
23
Trophy points
1,298
Location
canada
Activity points
2,152
gamersat678 said:
So I think I got it.
Code:
program ADC_10

dim AD_Res as word
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:
  AD_Res = ADC_read(0)'Lo(ADC_read(0))*-1/256*0.6/4.65'/1023*50   ' Execute conversion and store result
                         '   in variable AD_Res.
AD_Res=(Ad_Res*0.0048828125) '.00488 for 5v
valu=AD_Res div 1
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 ) !!!

adc_value = read_adc();
Volts = 5000*adc_value;
Volts = Volts / 1024; // Voltage in mV

or this one :

adc_value = read_adc();
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top