I'm using PIC16f676 for my project. I want to simply read a switch and turn on/off RC2 and RC3. The switch is connected to RC0 or RA2. I'm using HiTechC pro V9.8 compiler.
I tried "if(RC0==1){RC2=1; RC3=1;}" in the code, but its not working. I can turn on/off the pins set as output directly. But I want to read the switch and its not working. Is there a problem with my configuration bits? The configuration is as shown below:
"__CONFIG(UNPROTECT & BOREN & MCLREN & PWRTDIS & WDTDIS & INTOSCIO);"
How is the switch connected and do you have a pull-up or pull-down resistor to ensure it is in ine state before the switch changes it to the other?
For example, if the switch connects the pin to ground, you need a pull-up resistor to make it high when the switch is open.
Also check the TRIS bits for the pin are set to '1' so it is an input to the PIC.
Also be aware of contact bounce. If you are not debouncing the switch in hardware then you need to do so in software.
What do you mean by "its not working"? Have you set a breakpoint in the code and examined the port register?
It would also help if you showed us the code of a small app that exhibited the problem (given what you are trying to do it should only be a small number of lines long in the 'main' function).
Susan
First of all thanks for the response guys. The pin is pulled down and its not a switch that I connected. Its a signal from an opto-coupler. So there wont be a debouncing issue. I wrote a simple code to turn on an led on RC2 when a push button connected on RC0 (pulled down) is pressed, just to check. But its not working. The code is as follows:
TRISC=0x01;
main()
{
if(RC0)
RC2=1}
Show a schematic. It may be a hand drawing, but shiw it.
You talk about a pushbutton, then about an optocoupler...
An optocoupler doesn't prevent from bouncing...if the input of the optocoupler is bouncing, then the output will also bouncing.
You show code of an "IF" ...
And what happens when the statement is TRUE...
--> But what happens when the statement is NOT TRUE?