4620 PORTC issue RC2/CCP1/P1A

Discussion in 'Embedded Systems and Microcontrollers' started by poxkix, Feb 5, 2012.

  1. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    These are the code snippets:
    port settings: I did not include the other port settings
    Code ( (Unknown Language)):
    1. ADCON1 = 0x0F;
    2. INTCON = 0x00;
    3. TRISC = 0x00;
    4. PORTC = 0;

    I'm using PORTC.F0 up to PORTC.F3. Port F0, F1, and F3 works just fine but at F2 otherwise
    I'm thinking it is because of PORTC.F2 is multiplexed with CCP1/P1A. Is it possible to disable this?

    The ports activate when a corresponding key is pressed from a keypad
     
  2. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Use LATC.F0 to LATC.F3

    For PIC18 and above use PORT only for input, use LAT for output.

    Search for "Read Modify Write" for the explanation.
     
    poxkix likes this.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,607

    It's barely possible to ID what chip you're using from your description. Give the full base number always unless you like to waste a day for someone to ask you WTF you mean.

    From what little code you posted you have set up PortC as 8 output pins all going low. "but at F2 otherwise" is a rather obscure way of indication some sort of undesirable condition, but what is wrong and what could be causing it is anyone's guess.

    Port pin RC2 is multiplexed with the PWM output, but as long as you did not touch the PWM it is off by default.
     
  4. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    I'm sorry. I thought the title is understandable. I'm using P18F4620.

    Yes, I set up all of PORTC to low, it goes HIGH when a button in the keypad is pressed. Lets say button 1 2 3 4 corresponds to PORTC.F0 F1 F2 F3 respectively. When either of the button 1, 2, and 4 is pressed, their corresponding pin goes HIGH except for PORTC.F2. I didn't touch any code/setting for the PWM so that's why I confused why PORTC.F2 won't go high. I think I know the code has no problem because it already works in the simulator. So I'm thinking either hardware problem or software problem.
     
  5. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Ah... you are using PORTC for input...

    In that case

    TRISC = 0x00;

    is wrong. This sets PORTC as all outputs.

    Use TRISC = 0xff; to set all PORTC pins as input.

    You will need a pull-down resistor (typically 10K) for each switch input.
     
  6. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    Okay. I will try using LAT. It is just that, RC2/CCP1/P1A does not go high

    No no, misunderstanding. PORTC is for output. The keypad is interfaced in another set of ports.
     
  7. hexreader

    Active Member

    Apr 16, 2011
    250
    82
    Then your code snippet should work.

    Maybe the problem is with some other part of your code.

    Here is a simple test program that lights LED on each PORTC output, and works fine for me. It is written with MikroC compiler, which I am guessing is what you are using.

    Code ( (Unknown Language)):
    1. // PIC18F4620 and MikroC test program for PORTC
    2. // Use HS oscillator (no PLL)
    3. // be sure to set Frequency in project settings same as your crystal
    4. // turn off watchdog and LVP
    5.  
    6. main(){
    7.  
    8.     ADCON1 = 0x0F;       // all digital
    9.     INTCON = 0x00;
    10.     TRISC = 0x00;        // all output
    11.     LATC = 0x00;         // initialise PORTC to all 0
    12.  
    13.     Delay_ms(500);
    14.     LATC.F0 = 1;
    15.     Delay_ms(500);
    16.     LATC.F1 = 1;
    17.     Delay_ms(500);
    18.     LATC.F2 = 1;
    19.     Delay_ms(500);
    20.     LATC.F3 = 1;
    21.     Delay_ms(500);
    22.     LATC.F4 = 1;
    23.     Delay_ms(500);
    24.     LATC.F5 = 1;
    25.     Delay_ms(500);
    26.     LATC.F6 = 1;
    27.     Delay_ms(500);
    28.     LATC.F7 = 1;
    29.  
    30.     while(1);            // infinite loop to prevent reset after return from main
    31. }
     
    poxkix likes this.
  8. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    Thank you sir hexreader. I will do this tomorrow. Unfortunately sometime ago my hometown got hit with a 6.8 earthquake. wtf
     
  9. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    Unfortunately it was a hardware problem. I tried the code with another P18F4620 and it turned out okay. Thanks for the help.

    Now, it got me curious I haven't used LAT in setting I/O ports, I used PORT. What is their difference?
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,607
    Good question. Usually there is little perceived difference between the two, but it can lead to some problems depending on what you are doing.

    Time to crank up the spec sheet on this device and look at the schematic for "GENERIC I/O PORT OPERATION". The LATCH register for a port holds the value a port will output when configured to be an output. The PORT register is the literal pins on the device.

    This is a subtle difference, but important. You can load the LATCH while the PORT is set to be inputs so when you later set the port to be an output the LATCH value is the one that will appear on the pins.

    There are several machine instructions that work by first reading a register, modifying the value, then writing the modified value back to the register. One example is "BSF" where an individual bit in a register is set. In C if you perform say a "PORTC.F3 = 1;" your compiler is probably using that instruction. This is refered to as "Read-Modify-Write" or R-M-W or RMW.

    There can be a problem using these on the PORT itself as it reads the PORT pins themselves, and in certain cases the pin has not yet reached it's new value. You set the pin as 1, and immediately read it, but it still reads as zero. Another case is when there is some analog function associated with a particular pin: then it only reads as zero even if there is a valid one on the pin.

    The LATCH registers is a latter addition to PICs, originally only the PORT register was used. It is a significant improvement.
     
  11. poxkix

    Thread Starter Member

    Nov 28, 2011
    42
    0
    Thanks for the explanation sir. I think I understand most of it:D
    I tried reading the datasheet but I can't still understand what is their total difference.

    A port has 8 pins right? What if I will use the other half as input and the other as output?
    Is this possible with LAT? What I normally do is:
    ex. TRISC = 0b00001111;
    PORTC = 0; just to clear the register
     
Loading...