Confusion between ANSB and ANSELB on PIC24F

Author
NealR
Super Member
  • Total Posts : 277
  • Reward points : 0
  • Joined: 2008/03/28 10:58:46
  • Location: Hollywood, FL
  • Status: offline
2012/04/11 14:37:48 (permalink)
0

Confusion between ANSB and ANSELB on PIC24F

All,
 
I am trying to configure the ADC port on the PIC24F32KA301.  I looked in the header file and found this (cut and pasted) as shown below.
 
The datasheet has a couple of examples.  One example states: 
 
ANSB = 0x0001; // AN2 as analog, all other pins are digital  (http://ww1.microchip.com/...n/DeviceDoc/39739a.pdf]http://ww1.microchip.com/...n/DeviceDoc/39739a.pdf,  page 54-22)

Another example states:

ANSELB = 0x0001; // Only AN0 as analog input    (http://ww1.microchip.com/...n/DeviceDoc/39739a.pdf]http://ww1.microchip.com/...n/DeviceDoc/39739a.pdf, page 54-39)

So what is the difference between ANSB and ANSELB????????????

Also, in the datasheet, it looks like there are the following coresspondences between RA#/RB# and ANS#: 
 
RA0 <=> AN0 
RA1 <=> AN1 [size=2 font="courier new"]RB0 <=> AN2 
RB1 <=> AN3 
RB2 <=> AN4 
??? <=> AN5 
??? <=> AN6 
??? <=> AN7 
??? <=> AN8 
??? <=> AN9 
RB14 <=> AN10 
RB13 <=> AN11 
RB12 <=> AN12 
RA2 <=> AN13 
RA3 <=> AN14 
RB4 <=> AN15 
  
What if I want to read AN4?  Then what values do I use for ANSELB??? and ANSB#??? 
  
  
extern volatile unsigned int ANSA __attribute__((__sfr__)); 
typedef struct tagANSABITS { 
    unsigned ANSA0:1; 
    unsigned ANSA1:1; 
    unsigned ANSA2:1; 
    unsigned ANSA3:1; 
} ANSABITS; 
extern volatile ANSABITS ANSAbits __attribute__((__sfr__)); 
extern volatile unsigned int ANSELA __attribute__((__sfr__)); 
typedef struct tagANSELABITS { 
    unsigned ANSA0:1; 
    unsigned ANSA1:1; 
    unsigned ANSA2:1; 
    unsigned ANSA3:1; 
} ANSELABITS; 
extern volatile ANSELABITS ANSELAbits __attribute__((__sfr__)); 
extern volatile unsigned int ANSB __attribute__((__sfr__)); 
typedef struct tagANSBBITS { 
    unsigned ANSB0:1; 
    unsigned ANSB1:1; 
    unsigned ANSB2:1; 
    unsigned :1; 
    unsigned ANSB4:1; 
    unsigned :7; 
    unsigned ANSB12:1; 
    unsigned ANSB13:1; 
    unsigned ANSB14:1; 
    unsigned ANSB15:1; 
} ANSBBITS; 
extern volatile ANSBBITS ANSBbits __attribute__((__sfr__)); 
extern volatile unsigned int ANSELB __attribute__((__sfr__)); 
typedef struct tagANSELBBITS { 
    unsigned ANSB0:1; 
    unsigned ANSB1:1; 
    unsigned ANSB2:1; 
    unsigned :1; 
    unsigned ANSB4:1; 
    unsigned :7; 
    unsigned ANSB12:1; 
    unsigned ANSB13:1; 
    unsigned ANSB14:1; 
    unsigned ANSB15:1; 
} ANSELBBITS; 
extern volatile ANSELBBITS ANSELBbits __attribute__((__sfr__)); 
  
 
TIA.
post edited by geometrix - 2012/04/11 14:59:55
#1

14 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 2893
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/11 19:56:57 (permalink)
    0
    The references you have made are to the "Family Reference Manual" which tries to cover all of the PIC24F devices. As such it will take examples from all over the product range. Different devices, even in the same family, use different names and the registers can operate in different ways.

    You need to look at the data sheet for the specific device you are using to find the actual register names. In this case the document with the reference number 39995c lists ANSB as the register that selects the analog/digital operation for the pins on PORTB.

    I use the FRM for conceptual information and the data sheet for the exact details of a register. Then you can use the actual register names for your device based on the concepts and ideas in the FRM.

    Susan
    #2
    NealR
    Super Member
    • Total Posts : 277
    • Reward points : 0
    • Joined: 2008/03/28 10:58:46
    • Location: Hollywood, FL
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/11 20:37:49 (permalink)
    0
    Dear Aussie Susan,
     
    The block of code that I cut and pasted into my post was taken from the "p24f32ka301.h" header file.  I have been struggling with the ADC, and Mike017 suggested looking in the header file.  So I have already done this.
     
    In the header file, I see ANSB and ANSELB.  I don't quite understand what these registers do.  It almost seems that they do the same thing based upon examples that I have seen that use these registers.
     
    Can you please clarify their use?  Thank you.

    Sincerely,
    Neal Rosenblum (KB8PFV) 
    MPLAB X IDE v2.35
    ICD3
    X16 Compiler
    #3
    flubydust
    Super Member
    • Total Posts : 1270
    • Reward points : 0
    • Joined: 2005/05/19 13:44:42
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/11 20:50:00 (permalink)
    0
    If you look at the linker file (.GLD) for the device you will see ANSA and ANSELA have the same address.

    They are alternative names for the same thing. Probably someone got it wrong and they kept the incorrect version for compatibility.
    #4
    DarioG
    Scheisse Menschen
    • Total Posts : 52403
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: online
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/12 00:45:00 (permalink)
    0
    Yeah, a matter of legacy or so.
    #5
    NealR
    Super Member
    • Total Posts : 277
    • Reward points : 0
    • Joined: 2008/03/28 10:58:46
    • Location: Hollywood, FL
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/12 17:49:28 (permalink)
    0
    All,

    I now have this partially working.  I assigned ANSB=0b1111111111111111; and ANSA=0b1111111111111111;

    I have also assigned TRISA=0b1111111111111111; and TRISB=0b1111111111111111;

    This configures all port bits to be analog inputs.

    I am still confused by the correlation between, let's say, PORTB and ANSB.  What if I want to make AN13 an analog input?  Then what setting do I use for TRISA, TRISB, etc.???

    I still do not see the one-to-one correlation between PORT and ANS.  I understand that there are 16 analog channels but I do not know how to configure the appropriate registers.  Can anybody provide any more detail in this area?

    TIA.

    Sincerely,
    Neal Rosenblum (KB8PFV) 
    MPLAB X IDE v2.35
    ICD3
    X16 Compiler
    #6
    Antipodean
    Super Member
    • Total Posts : 1547
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/13 00:56:13 (permalink)
    0
    geometrix


    I still do not see the one-to-one correlation between PORT and ANS.  I understand that there are 16 analog channels but I do not know how to configure the appropriate registers.  Can anybody provide any more detail in this area?

    TIA.


    There may not be a direct correlation. Check the data sheet for which pin each analogue input is on.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #7
    DarioG
    Scheisse Menschen
    • Total Posts : 52403
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: online
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/13 02:18:20 (permalink)
    0
    Hmm, the analog pins are not necessarily in-order with Port pins etc.

    Each ANSx/ANSELx bit refers to a pin, documented in datasheet and pin diagram.
    For an analog input, ANS has to be set to analog and TRIS to input.
    #8
    NealR
    Super Member
    • Total Posts : 277
    • Reward points : 0
    • Joined: 2008/03/28 10:58:46
    • Location: Hollywood, FL
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/16 08:13:47 (permalink)
    0
    Dear DarioG,
     
    I understand that part of it.  But look at this snippet below from the "p24f32ka301.h" header file.  Do you see the disconnect?
     
    This is what I do understand (see correspondences between RA# and AN# below).  I gathered this information from the pinout shown on the datasheet for the PIC24F32KA301.
     
    RA0 corresponds to AN0
    RA1 corresponds to AN1
    RB0 corresponds to AN2
    RB1 corresponds to AN3
    RB2 corresponds to AN4
    which port corresponds to AN5?
    which port corresponds to AN6?
    which port corresponds to AN7?
    which port corresponds to AN8?
    RB15 corresponds to AN9
    RB14 corresponds to AN10
    RB13 corresponds to AN11
    RB12 corresponds to AN12
    RA2 corresponds to AN13
    RA3 corresponds to AN14
    RB4 corresponds to AN15
     
    I do not see the above in the header file as shown below.  Is there something wrong with AN15???
     
    extern volatile unsigned int ANSA __attribute__((__sfr__));
    typedef struct tagANSABITS {
    unsigned ANSA0:1;
    unsigned ANSA1:1;
    unsigned ANSA2:1;
    unsigned ANSA3:1;
    } ANSABITS;
    extern volatile ANSABITS ANSAbits __attribute__((__sfr__));
    extern volatile unsigned int ANSELA __attribute__((__sfr__));
    typedef struct tagANSELABITS {
    unsigned ANSA0:1;
    unsigned ANSA1:1;
    unsigned ANSA2:1;
    unsigned ANSA3:1;
    } ANSELABITS;
    extern volatile ANSELABITS ANSELAbits __attribute__((__sfr__));
    extern volatile unsigned int ANSB __attribute__((__sfr__));
    typedef struct tagANSBBITS {
    unsigned ANSB0:1;
    unsigned ANSB1:1;
    unsigned ANSB2:1;
    unsigned :1;
    unsigned ANSB4:1;
    unsigned :7;
    unsigned ANSB12:1;
    unsigned ANSB13:1;
    unsigned ANSB14:1;
    unsigned ANSB15:1;
    } ANSBBITS;
    extern volatile ANSBBITS ANSBbits __attribute__((__sfr__));
    extern volatile unsigned int ANSELB __attribute__((__sfr__));
    typedef struct tagANSELBBITS {
    unsigned ANSB0:1;
    unsigned ANSB1:1;
    unsigned ANSB2:1;
    unsigned :1;
    unsigned ANSB4:1;
    unsigned :7;
    unsigned ANSB12:1;
    unsigned ANSB13:1;
    unsigned ANSB14:1;
    unsigned ANSB15:1;
    } ANSELBBITS;
    extern volatile ANSELBBITS ANSELBbits __attribute__((__sfr__));
     
    TIA,
    Neal
    post edited by geometrix - 2012/04/16 08:16:12

    Sincerely,
    Neal Rosenblum (KB8PFV) 
    MPLAB X IDE v2.35
    ICD3
    X16 Compiler
    #9
    DarioG
    Scheisse Menschen
    • Total Posts : 52403
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: online
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/16 13:12:30 (permalink)
    0
    Well, the above makes sense (AN15 is RB4 i.e. ANSB4) ...
    only , those unused AN could look strange but anyway it can be! (unused, I mean)
    #10
    WaltR
    Super Member
    • Total Posts : 3759
    • Reward points : 0
    • Joined: 2003/11/07 12:38:21
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/16 13:29:30 (permalink)
    0
    Yep, what Dario said. Carefully study the Pin Diagram and Table in the data sheet for the PIC you are using. This is where the pin to function will be listed.
    #11
    Aussie Susan
    Super Member
    • Total Posts : 2893
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/16 19:44:39 (permalink)
    0
    If you have a look at the table on Page 2 of the data sheet, the row for your processor lists it as having 12 A/D channels - all that means is that for the 20-pin package the A/D channels associated with AN5 to AN8 are not implemented.

    Susan
    #12
    NealR
    Super Member
    • Total Posts : 277
    • Reward points : 0
    • Joined: 2008/03/28 10:58:46
    • Location: Hollywood, FL
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/04/17 08:54:51 (permalink)
    0
    Everybody,
     
    Thank you for your help.  I think that I now have this all ironed out.
     

    Sincerely,
    Neal Rosenblum (KB8PFV) 
    MPLAB X IDE v2.35
    ICD3
    X16 Compiler
    #13
    NealR
    Super Member
    • Total Posts : 277
    • Reward points : 0
    • Joined: 2008/03/28 10:58:46
    • Location: Hollywood, FL
    • Status: offline
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/11/20 17:22:40 (permalink)
    0
    Dear DarioG,
     
    You have responded to many of my posts over the years and have always led me in a good direction.  Thank you.
     
    I reposted this question this afternoon.  I am wondering if you can provide more insite into the assignment of analog pins.
     
    I am VERY confused about analog channels 0 thru 11 on the PIC24F32KA301.  Analog channels 12, 13, 14, & 15 correlate to RB12, 13, 14, & 15.
     
    TIA,
    --Neal

    Sincerely,
    Neal Rosenblum (KB8PFV) 
    MPLAB X IDE v2.35
    ICD3
    X16 Compiler
    #14
    DarioG
    Scheisse Menschen
    • Total Posts : 52403
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: hi there
    • Status: online
    Re:Confusion between ANSB and ANSELB on PIC24F 2012/11/21 01:57:35 (permalink)
    #15
    Jump to:
    © 2017 APG vNext Commercial Version 4.5