1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PIC PORTA digital input HOW

Discussion in 'Microcontrollers' started by LUNAR, Dec 19, 2008.

  1. LUNAR

    LUNAR New Member

    Joined:
    Sep 4, 2008
    Messages:
    33
    Likes:
    0
    hello I am using pic18f4550 and i am trying to use portA as digital input on pins RA0,RA1,RA2,RA3
    i have disabled the analog input
    i have set the pins as output

    i am getting digital input only on RA0 and RA1 but not on RA2 andRA3
    what could be the problem
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi,
    I expect you mean input.:)

    Have you set CMCON as 0x07.?
     
  3. LUNAR

    LUNAR New Member

    Joined:
    Sep 4, 2008
    Messages:
    33
    Likes:
    0
    thanks for reply
    ya thats definitely input.
    yes i have set the cmcon reg.
    but the strange thing is, that in the header file #include <p18f4450.h>
    this register is not defined so i had to use #include <p18cxxx.h> header file.
    do u get anything from this
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,323
    Likes:
    653
    Location:
    Derbyshire, UK

    Try posting your setup code, rather than leaving us all guessing.
     
  6. LUNAR

    LUNAR New Member

    Joined:
    Sep 4, 2008
    Messages:
    33
    Likes:
    0
    this is my code
    Code (text):

     //#include <p18f4450.h>
    #include <p18cxxx.h>
    //#pragma config WDT = OFF, LVP = OFF, FOSC = INTOSC_HS  //internal oscillator

    #pragma config WDT=OFF,LVP=OFF,FOSC=HSPLL_HS,PLLDIV=5,CPUDIV=OSC2_PLL3 //run with external oscillator

    void main (void);
    void InterruptHandlerLow (void);

    #define LCD_DATA   PORTD
    #define LCD_EN    PORTCbits.RC2
    #define LCD_RW    PORTCbits.RC1
    #define LCD_RS     PORTCbits.RC0

    #define A PORTAbits.RA0
    #define B PORTAbits.RA1
    #define C PORTAbits.RA2
    #define D PORTAbits.RA3


    unsigned char data[]="123456789     90";

    unsigned char dd[]="DDDDDDDDDDDDD123";
    unsigned char aa[]="AAAAAAAAAAAAA123";
    unsigned char bb[]="BBBBBBBBBBBBB123";
    unsigned char cc[]="CCCCCCCCCCCCC123";
    unsigned char xx[]="xxxxxxxxxxxxxxxx";



    void delay(int);
    void WRT_COMM(void);
    void WRT_DATA(void);
    unsigned char *varr , var;

    void LCD_sendstring(unsigned char *varr);

    void LCD_dt();
    void LCD_senddata(unsigned char var );

    void initilizeLCD(void);
    void lcd_clear(void);

    void main ()
    {
    char DATA;


    varr=&data[0];
    ADCON1 = 0xFF; // turn all of port a to digital I/O
     

    TRISCbits.TRISC6=1;
    TRISAbits.TRISA3=1;
    TRISAbits.TRISA2=1;
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA0=1;
     

    TRISAbits.TRISA0=1;
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA2=1;
    TRISAbits.TRISA3=1;
     

    CMCON  = 0x07;  // CM0, CM1 and CM2 set to 1
     
    initilizeLCD();
    lcd_clear();
    LCD_dt();



    while(1)
    {
        if(A==1 && B==0 && C==0 && D==0)
            {
        varr=&aa[0];
        LCD_dt();
            }
        else if(A==1 && B==1 && C==0 && D==0)
            {
        varr=&bb[0];
        LCD_dt();
            }
        else
        if(A==1 && B==0 && C==1 && D==0)
            {
        varr=&cc[0];
        LCD_dt();
            }
        else if(A==1 && B==0 && C==0 && D==1)
            {
        varr=&dd[0];
        LCD_dt();
            }
        else
        varr=&xx[0];
        LCD_dt();

     }

    }
     
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi,
    Sorry can't help with 'C', others will.:)
     
  8. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Lunar my datasheet
    PIC18F2455/2550/4455/4550

    Data Sheet

    has info about the CMCON register in many places. What are you looking at ?

    First did you setup TRISA correctly? I found this in your code
    Code (text):
    TRISAbits.TRISA3=1;
    TRISAbits.TRISA2=1;
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA0=1;
     
     
    TRISAbits.TRISA0=1;
    TRISAbits.TRISA1=1;
    TRISAbits.TRISA2=1;
    TRISAbits.TRISA3=1;
    You set RA0-RA3 as inputs twice. Once would have been enough.

    Next check the table 10-2
    SUMMARY OF REGISTERS ASSOCIATED WITH PORTA

    to determine what other registers could be causing problems.

    it looks like you have CMCON correct.

    Look at what bits ADCON1 effects. You used
    Code (text):
    ADCON1 = 0xFF; // turn all of port a to digital I/
    The value 0x0F does that. But 0xFF (bits 4 and 5) makes RA2 and RA3 into voltage ref pins.









     
  9. LUNAR

    LUNAR New Member

    Joined:
    Sep 4, 2008
    Messages:
    33
    Likes:
    0
    thanks for reply
    i have made the changes
    but i am still unable to get input from portA.
     
  10. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    This code works in the simulator. The stimulus file set RA bits to 0x2A after 40 cycles.

    My guess is that you LCD code is changing the TRISA settings or possibly ADCON1 or CMCON. Check initilizeLCD first.

    Code (text):
    //#include <p18f4450.h>  // not sure why this include does not work
    #include <p18cxxx.h>


    void main ()
    {
      unsigned char DATA;
     
      ADCON1 = 0x0F; // *** turn all of port a to digital I/O
      CMCON  = 0x07;  // CM0, CM1 and CM2 set to 1
     
      TRISA=0xFF;
     
     
      DATA = PORTA;  //  Works
      Nop();
      Nop();
      while(1);
    }
     
  11. solarwind

    solarwind New Member

    Joined:
    Dec 22, 2008
    Messages:
    33
    Likes:
    0
    Add this:

    Code (text):

    ANSEL = 0x00;
    ANSELH = 0x00;
     
    Disables analog input.
     
  12. Peter Porkolab

    Peter Porkolab New Member

    Joined:
    Apr 30, 2014
    Messages:
    1
    Likes:
    1
    Hy!
    Maybe your problem is that you made #include <p18f4450.h> instead of #include <p18f4550.h>???
     
    • Like Like x 1
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,304
    Likes:
    914
    Location:
    Rochdale UK
    6 year old thread.... If he hasn't fixed it by now, I'm sure he'll have given up!!
     

Share This Page