4620 PORTC issue RC2/CCP1/P1A

poxkix

Joined Nov 28, 2011
42
These are the code snippets:
port settings: I did not include the other port settings
Rich (BB code):
ADCON1 = 0x0F;
INTCON = 0x00;
TRISC = 0x00;
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

Joined Apr 16, 2011
540
I'm using PORTC.F0 up to PORTC.F3.
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.

ErnieM

Joined Apr 24, 2011
8,183
These are the code snippets:
port settings: I did not include the other port settings
Rich (BB code):
ADCON1 = 0x0F;
INTCON = 0x00;
TRISC = 0x00;
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

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.

poxkix

Joined Nov 28, 2011
42
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.

Joined Apr 16, 2011
540
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.

poxkix

Joined Nov 28, 2011
42
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.
Okay. I will try using LAT. It is just that, RC2/CCP1/P1A does not go high

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.
No no, misunderstanding. PORTC is for output. The keypad is interfaced in another set of ports.

Joined Apr 16, 2011
540
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.

Rich (BB code):
// PIC18F4620 and MikroC test program for PORTC
// Use HS oscillator (no PLL)
// be sure to set Frequency in project settings same as your crystal
// turn off watchdog and LVP

main(){

ADCON1 = 0x0F;       // all digital
INTCON = 0x00;
TRISC = 0x00;        // all output
LATC = 0x00;         // initialise PORTC to all 0

Delay_ms(500);
LATC.F0 = 1;
Delay_ms(500);
LATC.F1 = 1;
Delay_ms(500);
LATC.F2 = 1;
Delay_ms(500);
LATC.F3 = 1;
Delay_ms(500);
LATC.F4 = 1;
Delay_ms(500);
LATC.F5 = 1;
Delay_ms(500);
LATC.F6 = 1;
Delay_ms(500);
LATC.F7 = 1;

while(1);            // infinite loop to prevent reset after return from main
}

poxkix

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

poxkix

Joined Nov 28, 2011
42
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?

ErnieM

Joined Apr 24, 2011
8,183
Now, it got me curious I haven't used LAT in setting I/O ports, I used PORT. What is their difference?
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.

poxkix

Joined Nov 28, 2011
42
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.
Thanks for the explanation sir. I think I understand most of it
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