Digital Input PIC16F648A

donalves

Joined Oct 19, 2022
2
Hello everyone!

I'm starting learning PIC microcontrollers and have already done the first project, to blink a LED.
But, as a second one, I decided to try a digital input. So, the idea is, when the push button (connected in RA1) is not pressed, the first LED connected to RB3 receives 1, so it's on, and the second LED, connect to RB2 receives 0, so it's off. When the button is pressed, everything changes, so the first LED turns off, while the other one turns on.
The problem is: When I press the button, nothing happens. I've already saw the voltage on RA1 and the pushbutton is working as I want it to do, but the LEDs doesn't change.
I'm attaching at this trend the code that I'm using and the circuit at the breadboard.

Thank you guys so much for the attention!

Attachments

• 33.2 KB Views: 23
• 138.5 KB Views: 20

JohnInTX

Joined Jun 26, 2012
4,709
Welcome to AAC!

Be sure to turn the comparators off:
CMCON = 0x07;
RA1 will always read '0' otherwise.

EDIT:
Also, remove the green LED and 150ohm resistor from your switch input circuit on RA1. The way it is wired, it won't allow the 10K to pull the input up into a logic '1'.

Last edited:

sarahMCML

Joined May 11, 2019
201
Welcome to AAC!

Be sure to turn the comparators off:
CMCON = 0x07;
RA1 will always read '0' otherwise.

EDIT:
Also, remove the green LED and 150ohm resistor from your switch input circuit on RA1. The way it is wired, it won't allow the 10K to pull the input up into a logic '1'.
Where's your power supply to the chip, I don't see any?

donalves

Joined Oct 19, 2022
2
Welcome to AAC!

Be sure to turn the comparators off:
CMCON = 0x07;
RA1 will always read '0' otherwise.

EDIT:
Also, remove the green LED and 150ohm resistor from your switch input circuit on RA1. The way it is wired, it won't allow the 10K to pull the input up into a logic '1'.
Hey, JohnInTX!

Thank you very much for the help! I've studied the comparator module of my PIC16f648A on datasheet and applied the CMCON with the green led and the 150 ohm resistor.
I've changed the way the circuit should works. Now, when I press the pushbutton, the red LED connected to the PORTBbits.RB3 should turn on or turn off, working like a switch. The blue LED, connected to the PORTBbits.RB2 is useless.
I've got two results, deppending on the existence of the green LED and 150 ohm resistor:
1) WITH green LED and 150 ohm resistor: The red LED starts blinking with the delay that I set on the body of the code. The pushbutton seen to be useless;
2) WITHOUT LED + 150 ohm resistor: THE red LED works as I want it to do. When I press the pushbutton, the LED switches between ON and OFF. If I continue to press the pushbutton, the LED starts to blink in the delay time that I set as a debounce.

So, it worked
Thank you very much guys for the help! If you have any questions of the way that I do that, I'm letting a printscreen of the code as an attatchment here. You guys can write me too anytime you want.
Best Regards!

JohnInTX

Joined Jun 26, 2012
4,709
Glad it is working. Well done!

1) WITH green LED and 150 ohm resistor: The red LED starts blinking with the delay that I set on the body of the code. The pushbutton seen to be useless;
Yes. As I noted above, the voltage divider formed by the 10K pull up and the 150ohm/LED pull down will not get enough voltage on the pin to make a good logic '1' .. the LED / resistor is acting as a pull down and the switch will not be detected. Instead of using an LED to monitor the state of the input pin RA1, it would be better to just use a digital voltmeter. Refer to the PIC datasheet section 17.4 to see what voltages make a logic '0' and a logic ' 1'.

If you post your code using code tags, it is easier for a member to copy it into MPLAB and work with it.
To insert code tags, you can click on the ... (Insert) dropdown on the editor toolbar and select 'CODE'. Copy your source from MPLAB and paste it into the code window. Add a title if you wish and highlight any lines you need to. Select 'C' for the language.
Example:
void main(void){
TRISA = 0xFF;
TRISB = 0x00;  // Highlighted line 3
CMCON = 0x07;

;.. and so on

}
Have fun!

Last edited:

upand_at_them

Joined May 15, 2010
940
You might want to use a shadow register. Newer PIC's have a data latch (LATA, LATB, etc.) for writing to so as to avoid the Read-Modify-Write problem. The solution in older PIC's was to use a shadow register...That is you update the shadow register and then write that entire value to the PORT. Plenty of info online about RMW.

JohnInTX

Joined Jun 26, 2012
4,709
You might want to use a shadow register. Newer PIC's have a data latch (LATA, LATB, etc.) for writing to so as to avoid the Read-Modify-Write problem. The solution in older PIC's was to use a shadow register...That is you update the shadow register and then write that entire value to the PORT. Plenty of info online about RMW.
Yes! Absolutely.