problems with AN1 (pin 3 on pic 18f2455)

Author
reggie718
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2007/09/18 00:12:55
  • Location: 0
  • Status: offline
2017/07/17 01:14:46 (permalink)
0

problems with AN1 (pin 3 on pic 18f2455)

Hi.
i am using mplabx 3.26 with pic18f2455.
i am trying to read the value from AN1 (which is from 0 to 5 volt and it should give me a number between 0 to 255) 
i am always getting a maximum number (between 250 to 255) even when i should get a low or medium number.
i am using port A with digital ports ( inputs and output ports) and for analog port (AN1 )
probably my ports configuration is wrong .
what should be the reason that the AN1 always give me a maximum value ?
may be my Adcon0,Adcon1,Adcon2 values are wrong ?
here is my code :
 
include <adc.h>
#include <p18cxxx.h>

void InitADC(int ch)
{
ADCON0=0x01; // ch 0
SetChannel(ch);
ADCON1=0x0b; // Configure RA0 as analog. VSS , VDD
ADCON2=0x87; // Right justigied, 0tad Acq time, clock derived A/D RC osc. 0x87
}
void SetChannel(int ch)
{
ADCON0&=0x03;
switch(ch)
{
case 0:
ADCON0|=0x03;
break;
case 1:
ADCON0|=0x07;
break;
case 2:
ADCON0|=0x0b;
break;
case 3:
ADCON0|=0x0f;
break;
case 4:
ADCON0|=0x13;
break;
case 5:
ADCON0|=0x17;
break;
case 6:
ADCON0|=0x1b;
break;
}
}

void StartADCConversion(void)
{
ADCON0|=0x02; // set the GO bit.
}

int IsADCBusy(void)
{
if((ADCON0&0x02)!=0)
return 1;
return 0;
}
int ReadA2DChannel(int ch)
{
int res;
unsigned char lbyte,hbyte;
SetChannel(ch); // set channel to 0
StartADCConversion(); // start conversion
while(IsADCBusy()){} // wait for conversion.

hbyte=ADRESH; // read the right justify result. 000000xx xxxxxxxx
lbyte=ADRESL;
// res=hbyte*256+lbyte;
lbyte=lbyte&0xfc; // zero 2 LSB bits of 10 bits result
hbyte=hbyte&0x03; // mask 2 MSB bits of 10 bits result.
lbyte=lbyte>>2; // rotate right 2 bits
hbyte=hbyte&0x03; // mask 2 MSB bits of 10 bits result.
hbyte=hbyte<<6; // make 2 LSB bits go up
res=hbyte|lbyte; // OR the result to create a 8 bits result.
return res;
}
 
define cfgTxShutDownPort() TRISAbits.TRISA5 = 0;  
#define SHDN LATAbits.LATA5  
 
#define cfgMonitorOutPort() TRISAbits.TRISA3 = 1;
#define MONITOR_OUT LATAbits.LATA3  
 
#define cfgHPLedPort() TRISAbits.TRISA2 = 0;  
#define HP_LED LATAbits.LATA2  
 
 
void main(void)
{
ADCON1 = 0x0f;
InitADC(2);
TRISAbits.TRISA1=0;  
LATAbits.LATA1 = 0;
 
}
 
 
 
#1

13 Replies Related Threads

    CinziaG
    PORCAMADONNA
    • Total Posts : 2438
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 02:01:29 (permalink)
    +1 (1)
    Yeah, your ADC setting code looks wrong...
    Also this is probabily wrong:
    #define cfgMonitorOutPort() TRISAbits.TRISA3 = 1;
    #define MONITOR_OUT LATAbits.LATA3 
     
    And, do you have a while loop in your main code?

    PORCAMADONNA
    #2
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 04:33:20 (permalink)
    0
    ok.
    what is wrong with my ADC code ? can you please tell me ?
    what isnt right with the next two lines of code ?
    -------------------------------------------------------------
    #define cfgMonitorOutPort() TRISAbits.TRISA3 = 1;
    #define MONITOR_OUT LATAbits.LATA3
    -------------------------------------------------------------
    in my main code i only make this   :
    res=ReadA2DChannel(1);
    _SendOutBuffer_('C','F',res);
    i get into the res value only a number above 250 till 255 ,why cant i get a lower number than 250 ?
     
    thank you very much !
     
    #3
    CinziaG
    PORCAMADONNA
    • Total Posts : 2438
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 04:35:07 (permalink)
    +1 (1)
    You're setting the TRIS as input but you call it an "output" and use LAT on that pin, this is wrong.
     
    As for the Readout I am not sure...

    PORCAMADONNA
    #4
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 04:42:40 (permalink)
    0
    sorry, 
    i forgot a line of code .
    this is the full code for A3 :
    so A3 is an input .
     
    #define cfgMonitorOutPort() TRISAbits.TRISA3 = 1;
    #define MONITOR_OUT LATAbits.LATA3 // RD1
    #define IsMotorOutActivated (0 == MONITOR_OUT)\
     
    how should i use that port instead of  LAT ?
    is there anything wrong with my ADCON variables ? 
    thank you.
     
     
    #5
    rodims
    Super Member
    • Total Posts : 715
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 04:59:26 (permalink)
    +1 (1)
    you forgot to answer
    "And, do you have a while loop in your main code?"
    If you don't have, then describe, how you are testing your code.
    #6
    CinziaG
    PORCAMADONNA
    • Total Posts : 2438
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:13:21 (permalink)
    +1 (1)
    Apart from the rest Smile and apart from this
    ADCON0=0x01; // ch 0
    that you overwrite anyway right down,
    your ADC settings would look good enough.
     
    You may need to set TRIS bits for the analog inputs you plan to use,

    PORCAMADONNA
    #7
    CinziaG
    PORCAMADONNA
    • Total Posts : 2438
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:14:35 (permalink)
    +1 (1)
    Now that I re-check this:
    // res=hbyte*256+lbyte;
    lbyte=lbyte&0xfc; // zero 2 LSB bits of 10 bits result
    hbyte=hbyte&0x03; // mask 2 MSB bits of 10 bits result.
    lbyte=lbyte>>2; // rotate right 2 bits
    hbyte=hbyte&0x03; // mask 2 MSB bits of 10 bits result.
    hbyte=hbyte<<6; // make 2 LSB bits go up
    res=hbyte|lbyte; // OR the result to create a 8 bits result.
    return res;
     
    it looks like you're using the code for left-justified mode, while you're in right-justified (preferred) so this would be enough:
    res=hbyte*256+lbyte;
     

    PORCAMADONNA
    #8
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:18:33 (permalink)
    0
    Yes, i have a while loop in my code.
    my while loop is making this :
     
    res=ReadA2DChannel(1);
    _SendOutBuffer_('C','F',res);
     
    did i configure successfully  the ADCON1,ADCON2,ADCON3  to analog input  ?
    i test my code with my hardware engineers and with a SCOPE.
     
     
    #9
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:21:38 (permalink)
    0
    i am a bit confused:)
    can i please ask you to write me what do i have to change again ?
     
    #10
    mbrowning
    Super Member
    • Total Posts : 389
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:22:10 (permalink)
    +2 (2)
    - the code doesn't do anything - just initializes ADC to AN2 (and simultaneously sets the GO bit)
    - no while loop in main - main is allowed to exit
    - RA1/AN1 is set to output and isn't sampled anyway.
    - RA2/AN2 is the actual sampled pin - is it connected to anything?
    - In the initialization of ADCON0  - you set the GO bit when you set  the channel select bits
    - ADCON2 - acquisition time is set to 0
    - To read an input pin, read the PORT bit, not the LAT bit

    Mark
    #11
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:42:26 (permalink)
    0
    thanks .
    i want to set the AN0 + AN1 + AN2  to be analog input .
    also i want that  A2 + A5 will be output, A3 will be input .
    also i want to read  the A2D value into AN1 ,
    how should i change my code to do that ?
    thanks a lot !
    #12
    rodims
    Super Member
    • Total Posts : 715
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 05:59:34 (permalink)
    +1 (1)
    yes, i have a while loop in my code.
    my while loop is making this :
     
    res=ReadA2DChannel(1);
    _SendOutBuffer_('C','F',res);

     

     
    I suspect that the code you originally posted is not the one which you use yourself for testing, which always is a source of needless misunderstandings.


     
    #13
    reggie718
    Senior Member
    • Total Posts : 169
    • Reward points : 0
    • Joined: 2007/09/18 00:12:55
    • Location: 0
    • Status: offline
    Re: problems with AN1 (pin 3 on pic 18f2455) 2017/07/17 06:21:10 (permalink)
    0
    res=ReadA2DChannel(1);
    _SendOutBuffer_('C','F',res);

    this is the relevant code which i use . i try to test the AN1 . the res value is always above 250 , although i can see via a SCOPE that the AN1 gets all the relevant values
    from 0 volt to 5 volt.

    #14
    Jump to:
    © 2017 APG vNext Commercial Version 4.5