Author Topic: ADC on Pic 18f2420  (Read 3252 times)

0 Members and 1 Guest are viewing this topic.

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 600
  • Country: za
    • ShoutingElectronics.com
ADC on Pic 18f2420
« on: October 27, 2013, 07:21:26 pm »
Hi.
I am playing around with the ADC on a Pic Micro (18f2420), using C18 Compiler.
I have an LCD using RA1 to RA3 for the Control Lines,
and PortC is the Data for the LCD.

After many hours, I can now use the LCD fine, using the XLCD libraries.

The problem I have is now,
I have a 10K preset pot between VCC and GND, and that is connected through a resistor into the RA0 port.
When I turn the pot, I only get 2 outputs from the adc, namely 0, or 1023, no intermediate values.

I did accidently have the RA0 set as an output, which was low, whilst I was trying to use it as an adc input (I hope I havent blown something in the uC).
 
I have since fixed that, but still have the same problems (Only giving 0 or 1023)

Can one of you knowledgeable folks please give a brief look at my code, and see if you can spot a mistake.

The ultimate goal for this project is going to be a logging Volt and Ammeter, so my code is sort of going in that direction.

Muchas Gracias (I suck with that spelling, i guess)

Thanks
Peter

Code: [Select]
/*
 * File:   voltlogger.c
 * Author: Peter
 *
 * Created on 26 October 2013, 2:31 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <xlcd.h>
#include <p18f2420.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>

//char text[5]="text";
int result;
char textresult[6]="";
//-------------------------------------------------------------
// CONFIGURATION FUSES
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
//#pragma config PBADEN = OFF


void DelayFor18TCY( void )
{
Delay10TCYx(2);
}

void DelayPORXLCD( void )
{
Delay1KTCYx(10);
}

void DelayXLCD( void )
{
Delay10TCYx(5);
}

int data;
char buffer[16];


void main(void) {
// OSCCONbits.IRCF0=0; //Lower Clock Speed
// OSCCONbits.IRCF1=0; //
// OSCCONbits.IRCF2=0; //
//        ADCON1=0xFF; //TURN OFF ADC's TO GET DIGITAL IO
//        CMCON=0xFF; //TURN OFF ADC's TO GET DIGITAL IO
        TRISC=0;
        LATC=0;
        TRISA=0x01; // ALL OUT EXCEPT RA0
        LATA=0;

while( BusyXLCD() );
        OpenXLCD(  FOUR_BIT & LINES_5X7);
        WriteCmdXLCD( BLINK_OFF & CURSOR_OFF);
        WriteCmdXLCD( SHIFT_DISP_LEFT );
      //  unsigned char x = 'A';
 //       putrsXLCD(text);
        //while(!BusyXLCD() ){
        //putsXLCD( text );
       // }
        putrsXLCD("   VLog V1.0");
       
       // WriteCmdXLCD( SHIFT_CUR_LEFT );
        WriteCmdXLCD((0x80+0x47)); //Move Cursor Position
        putrsXLCD("V");
        WriteCmdXLCD ((0x80+0x42));
        //putrsXLCD ("0");
        while(1) {
        OpenADC( ADC_FOSC_RC &
         ADC_RIGHT_JUST &
         ADC_VREFPLUS_VDD &
         ADC_VREFMINUS_VSS &
         ADC_12_TAD, ADC_CH0 &
         ADC_INT_OFF,
         0);
       
       
        Delay10TCYx( 5 ); // Delay for 50TCY
        ConvertADC(); // Start conversion
        while( BusyADC() ); // Wait for completion
        //result=0;
        //textresult="hello";
        result = ReadADC(); // Read result
        CloseADC(); // Disable A/D converter
        itoa(result,textresult);
        while(BusyXLCD());
        WriteCmdXLCD ((0x80+0x42));
        while(BusyXLCD());
        putrsXLCD("    ");
        while(BusyXLCD());
        WriteCmdXLCD ((0x80+0x42));
        while(BusyXLCD());
        putsXLCD (textresult);
        };

        while(1);

}
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

Offline Clint

  • Regular Contributor
  • *
  • Posts: 119
  • Country: gb
Re: ADC on Pic 18f2420
« Reply #1 on: October 27, 2013, 08:43:03 pm »
I am going blind but I can see where you set it as an analogue input ANSELA
=-=-=-=-=-=-=-=-=
g33K5 L1k3 80085
 

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 600
  • Country: za
    • ShoutingElectronics.com
Re: ADC on Pic 18f2420
« Reply #2 on: October 27, 2013, 09:08:16 pm »
It might be a bit late at night, so I am mybe not thinking clearly,
But please rephrase your last comment,
Also what is ansela?
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

Offline Clint

  • Regular Contributor
  • *
  • Posts: 119
  • Country: gb
Re: ADC on Pic 18f2420
« Reply #3 on: October 27, 2013, 09:14:20 pm »
Sorry should have been clearer;
ANSEL = Analogue Select - otherwise it thinks it digital 0 or 1 0 or 1023 :)

This is what I do to set this up:

Clear the port
PORTA=0x00

Select Port as analogue
ANSEL=0x01

Select all outputs except RA0
TRISA=0x01
=-=-=-=-=-=-=-=-=
g33K5 L1k3 80085
 

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 600
  • Country: za
    • ShoutingElectronics.com
Re: ADC on Pic 18f2420
« Reply #4 on: October 27, 2013, 09:26:49 pm »
Thanks, will give it another shot tomorrow eve.
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

Offline kolbepTopic starter

  • Frequent Contributor
  • **
  • Posts: 600
  • Country: za
    • ShoutingElectronics.com
Re: ADC on Pic 18f2420
« Reply #5 on: October 28, 2013, 05:04:22 pm »
Using ANSEL was only giving errors,
but I searched a bit more, and if I set my openadc to :
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD,
        ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,
        ADC_5ANA & 0xf /*patch di g.dar*/);

Now it works fine.

Dunno why, but it works  :phew:
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf