Hasher
Junior Member level 1
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
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