How to set ADC of 18F452

Discussion in 'Embedded Systems and Microcontrollers' started by ssm14293, Oct 3, 2011.

  1. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    Hi All,
    I need to set all AN inputs as Analog in PIC18F452. How would i set ADCON1 and ADCON0. Is this ok?

    Code ( (Unknown Language)):
    1.  
    2.      ADRESH = 0xFF;
    3.      ADRESL = 0xFF;
    4.      ADCON1 = 0x80;
    5.      ADCON0 = 0x80;                 //Turn all pins as analog
    6.      CCP1CON = 0x00;                //Turn off all comparators
    7.      CCP2CON = 0x00;
    8.      TRISA = 0xFF;
    9.      TRISB = 0x00;
    10.      TRISC = 0x7D;
    11.      TRISD = 0xFF;
    12.      TRISE = 0xFF;
    13.  
    further more i need to read values of ADC if changed, Continuously in a sync. Like an analog combination lock (itx an example, i dont really want to do that). Advise on that to i you can, it would be a great help!

    PS: I m using MikroC Pro 4.6 for PIC
     
  2. jwilk13

    Member

    Jun 15, 2011
    228
    12
    If I remember correctly, you want your ANSx bits set to disable the digital input buffer and TRISx bits set to disable the digital output driver. This would mean that your ANSEL and ANSELH registers would have 1's for anything set as an analog input and your TRISx registers would have 1's for anything set as an input.

    As far as the ADCONx registers, I would go back through and make sure that you've selected the correct bits that you want. Your settings for ADCON1 are going to give you the following: right justified, FOSC/64 (because of the value in ADCON0 as well), Vdd and Vss as reference voltages.

    For ADCON0, just remember that you have to select the channel that is active before the A/D conversion. This is done with bits 3-5 of ADCON0.

    The ADCON0 and ADCON1 registers are really for you to set. The registers that matter for setting analog inputs are the TRISx and ANSELx registers.
     
  3. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    there is no ANSEL/H in 18F452 SFR, adcon is used for them, from what i know ( im a newbie though);
    As for the ADCON1 i got the right settings, i guess but what bout ADCON0 cant understand how to set those. I need to use all 8 analog inputs. Please advise.
    im using 8Mhz clock.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    According to TABLE 17-1 your ADCS bits look OK. I'm used to MC putting "recommended" settings in there, but if they say Maximum Device Frequency" then max it is.

    However, you should make ADCON0.ADON a one, or "ADCON0 = 0x81;" That bit powers the A2D, always a good thing to do to use it. ;-)

    Which channel is read is set by the CHS2 bits of ADCON0. You have all 8 set to analog using ADCON1 as you have it.

    To change channels in your code you could write some fancy unions to point to the correct bits, but I would just do some #define statements used to load ADCON0 when switching channels:
    Code ( (Unknown Language)):
    1. #define SET_CH0 0x80
    2. #define SET_CH1 0x81
    3. #define SET_CH2 0x90
    4. #define SET_CH3 0x91
    5. #define SET_CH4 0xA0
    6. #define SET_CH5 0xA1
    7. #define SET_CH6 0xB0
    8. #define SET_CH7 0xB1
     
  5. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    doing so will allow me to use ADC all 8 channels ryt??
    sorry still at learning stage.... :D
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Yeah. You set ANCON0 with SET_CHx to pick which channel, then make a reading.

    If this is your first time doing A2D conversions drive one channel with say a pot so you can adjust the inpout and then look at the output on a debugger or an LCD screen or however you can.

    I try to do that whenever I can as a sanity check.
     
    ssm14293 likes this.
  7. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    ok!! thnx!! actually i want to read from IR receivers (8 in #) receiving light from different colors. I want to read them in a series, like 1-2-3 or 3-2-1 or 9-8-7 etc, how will i be able to do that.
    this is my IR Rx grid >> 'O' being the IR.Rx
    O---O---O
    | |
    O O
    | |
    O---O---O
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Do you have a schematic yet? Please post it (or make one up and post).
     
  9. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    an urgent question:
    my PIC is not doing any command... none... its recognized by the programmer and the software, but its not doing any command.

    Code ( (Unknown Language)):
    1.  
    2. void PIC_setting(void){
    3.  ADRESH = 0x00;
    4.  ADRESL = 0x00;
    5.  ADCON0 = 0x45;
    6.  ADCON1 = 0x8E;
    7.  TRISA = 0xFF;
    8.  TRISB = 0x00;
    9.  TRISC = 0x00;
    10.  TRISD = 0x00;
    11.  TRISE = 0xFF;
    12. }
    13.  
    14. void main() {
    15.    PIC_setting;
    16.    PORTB = 0x01;
    17.    delay_ms(500);
    18.    PORTB = 0x02;
    19.    delay_ms(500);
    20.    PORTB = 0x04;
    21.    delay_ms(500);
    22.    PORTB = 0x08;
    23.    delay_ms(500);
    24.    PORTB = 0x01;
    25.    delay_ms(500);
    26.    PORTB = 0x02;
    27.    delay_ms(500);
    28.    PORTB = 0x04;
    29.    delay_ms(500);
    30.    PORTB = 0x08;
    31.    delay_ms(500);
    32.    PORTB = 0x01;
    33.    delay_ms(500);
    34.    PORTB = 0x02;
    35.    delay_ms(500);
    36.    PORTB = 0x04;
    37.    delay_ms(500);
    38.    PORTB = 0x08;
    39.    delay_ms(500);
    40. }
    41.  
    Compile: MikroC for PIC (mE)
     
  10. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    Please reply soon...
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you set the configuration words properly?
     
  12. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    i have posted the code above, what else i have to configure? o_O'
     
  13. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You must read section the data sheet section 19.0 SPECIAL FEATURES OF THE CPU. Then you set set the config bits so they fit your setup. If you use MPLAB you can also set the bits from MPLAB. it is the toolbar configure->configuration bits. The best way is to set the bits in the code. But I do not know your compiler. So I can not tell you how to do it
     
  14. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    i m using MikroC for PIC...
     
  15. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    i dont need:
    Watchdog timer
    Power up timer
    Interrupts
    Brown Out Reset( dont know what this is)
    Not using CCP
    No need of ICSP atm
    Stack Full/Under flow(dont know about this too)
    no code protection needed

    now what should the settings be... please tell...
     
  16. stahta01

    Member

    Jun 9, 2011
    133
    21
    Wrong
    Code ( (Unknown Language)):
    1.  
    2. PIC_setting;
    3.  

    Right way to call a function.
    Code ( (Unknown Language)):
    1.  
    2. PIC_setting();
    3.  
    Tim S.
     
    ssm14293 likes this.
  17. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    u gave me quiet a bit of hope there bro, but still... nothing...

    Code ( (Unknown Language)):
    1.  
    2. void PIC_setting(void){
    3.  ADRESH = 0x00;
    4.  ADRESL = 0x00;
    5.  ADCON0 = 0x45;
    6.  ADCON1 = 0x8E;
    7.  TRISA = 0xFF;
    8.  TRISB = 0x00;
    9.  TRISC = 0x00;
    10.  TRISD = 0x00;
    11.  TRISE = 0xFF;
    12.  UART1_init(9600);
    13. }
    14.  
    15. void main() {
    16.    PIC_setting();
    17.    while (1){
    18.          PORTB = 0x01;
    19.          delay_ms(500);
    20.          PORTB = 0x02;
    21.          delay_ms(500);
    22.          PORTB = 0x04;
    23.          delay_ms(500);
    24.          PORTB = 0x08;
    25.          delay_ms(500);
    26.          PORTB = 0x01;
    27.          UART1_write_text("Hello World");
    28.    }
    29. }
    30.  
     
  18. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    NO you must get your config words in order!!. As I said before I have not used your C compiler. So I can not offer any help. Besides telling you what your setup will not work as is. One more thing it is most common to include a header file which define the PIC internal register addresses.
     
  19. ssm14293

    Thread Starter Member

    Jul 14, 2011
    90
    1
    mikro c add the header itself... other than that i m clueless too... letme try on mpLab
     
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Did you get this to compile? I would suspect that the line:

    PIC_setting;

    Needs to be changed to:

    PIC_setting()


    That way it calls your routine. I have no idea what it does without the ().
     
Loading...