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.

Problem with ADC of potentiometer

Status
Not open for further replies.

Hasher

Junior Member level 1
Joined
Apr 14, 2009
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,458
www.edaboard.com ftopic346990

Hello!

I'm using PIC18F2420 (crystal= 8MHz && HS-PLL) with HiTech C compiler.
I want 50kΩ potentiometer to be converted to digital on two 7-Segments display on PORTB.

Problem==> I got correct o/p but with minus one. I mean if 50KΩ applied I got 0xFE rather than 0xFF and if 0kΩ is applied I got 0x01 rather than 0x00.

Program==>
#include <pic18.h>
#include "config.h"
#include "delay.h"
#include "jwb.h"

int adc_value;
void init(void);
unsigned char ReadAdcChannel(unsigned char c);

void main (void)
{
unsigned char val0;
char i;
unsigned char c;
double dval0;

init();

while(1)
{
val0 = ReadAdcChannel(0);
//dval0 = 5.0 * val0 / 1024; // convert AN0 value to float (assume Vdd=5V)
PORTB = val0;
}

void init(void)
{
PORTA = 0x00;
//SELECT INTERNAL ADC CLOCK AND TURN ON ADC
ADCS2 = 0; ADCS1 = 1; ADCS0 = 1; ADON = 1;

//ONLY CH0 IS SELECTED WITH INTERNAL REF+ =VCC AND REF-=GND
PCFG0=0; PCFG1=1; PCFG2=1; PCFG3=1;

SET_AS_INPUT(TRISA, BIT0 | BIT1);
TRISB = 0x00;

}

unsigned char ReadAdcChannel(unsigned char c)
{
unsigned char temp;

//CLEAR GODONE BIT, CHS0=CHS1=CHS2=000 TO SELECT CHANNEL 0
CHS0=0; CHS1=0; CHS2=0;GODONE=0;

DelayUs(20); // wait 20us for ADC to get ready
GODONE = 1; // start ADC conversion
while(GODONE); // then wait for ADC to finish
if ( ADFM )
return ((ADRESH << 8) | ADRESL);
else
return ((ADRESH << 2) | (ADRESL>>6) );
return ADRESH;
}


=====================================================
jwb.h
=====================================================
#define SET_AS_INPUT(port, bits) port |= (bits)
#define SET_AS_OUTPUT(port, bits) port &= (~(bits))
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
 

potentiometer adresh

Hasher said:
Hello!

I'm using PIC18F2420 (crystal= 8MHz && HS-PLL) with HiTech C compiler.
I want 50kΩ potentiometer to be converted to digital on two 7-Segments display on PORTB.

Problem==> I got correct o/p but with minus one. I mean if 50KΩ applied I got 0xFE rather than 0xFF and if 0kΩ is applied I got 0x01 rather than 0x00.

Program==>
#include <pic18.h>
#include "config.h"
#include "delay.h"
#include "jwb.h"

int adc_value;
void init(void);
unsigned char ReadAdcChannel(unsigned char c);

void main (void)
{
unsigned char val0;
char i;
unsigned char c;
double dval0;

init();

while(1)
{
val0 = ReadAdcChannel(0);
//dval0 = 5.0 * val0 / 1024; // convert AN0 value to float (assume Vdd=5V)
PORTB = val0;
}

void init(void)
{
PORTA = 0x00;
//SELECT INTERNAL ADC CLOCK AND TURN ON ADC
ADCS2 = 0; ADCS1 = 1; ADCS0 = 1; ADON = 1;

//ONLY CH0 IS SELECTED WITH INTERNAL REF+ =VCC AND REF-=GND
PCFG0=0; PCFG1=1; PCFG2=1; PCFG3=1;

SET_AS_INPUT(TRISA, BIT0 | BIT1);
TRISB = 0x00;

}

unsigned char ReadAdcChannel(unsigned char c)
{
unsigned char temp;

//CLEAR GODONE BIT, CHS0=CHS1=CHS2=000 TO SELECT CHANNEL 0
CHS0=0; CHS1=0; CHS2=0;GODONE=0;

DelayUs(20); // wait 20us for ADC to get ready
GODONE = 1; // start ADC conversion
while(GODONE); // then wait for ADC to finish
if ( ADFM )
return ((ADRESH << 8) | ADRESL);
else
return ((ADRESH << 2) | (ADRESL>>6) );
return ADRESH;
}


=====================================================
jwb.h
=====================================================
#define SET_AS_INPUT(port, bits) port |= (bits)
#define SET_AS_OUTPUT(port, bits) port &= (~(bits))
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80

Hi,
I think, there is a need for calibration of the channel being used to read the potentiometer input.
I have no idea of potentiometer ADCs, but if I were you,I would check the ref whether it is stable..

Regards,
Sharon
 

The maximum source impedance of ADC of my PIC18F2420 is 2.5kΩ.

Does that info help you?
 

Hasher said:
The maximum source impedance of ADC of my PIC18F2420 is 2.5kΩ.

Does that info help you?

Is there any ADC calibration register given..?
Just short the input terminal and run the calibration routine..
If you receive 0x00..you have done it..

Regards,
Sharon
 

the maximum impedance may be the factor.... could you test it with a 1k potentiomter???
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top