This may be a RMW problem. Try changing it to,
Code :if(RA3==0) PORTC=0x01; else PORTC=0x02;
Edit, you do have JP5 on don't you?
Mike.
Hi all,
I am trying to set up a simple "if button is pressed, turn on led1 and turn off led2" and vice versa circuit. But for the life of me I can't get it to work properly.
I am using a pic16f690 chip with the lpc board and the pickit2.
What happens is RC0 led is lit unless I push the button(RA3). RA1 led stays off. I must be doing the if else statement wrong, but everything looks good.
My code looks like this:
#include <pic.h>
void init(void)
{
TRISA=0x8; // set RA3 as input
TRISC=0x00; // set PORTC as outputs
ANSEL=0; // set ports to digital
PORTC=0; // turn off all leds
}
void main(void)
{
init(); // initialize I/O ports
while(1) // loop forever
{
if(RA3==0) // if button is pressed
{RC0=1; // turn on led 0
RC1=0;} // turn off led 1
else // if button is not pressed
{RC1=1; // turn on led 1
RC0=0;} // turn off led 0
}
}
Thanks in advance
This may be a RMW problem. Try changing it to,
Code :if(RA3==0) PORTC=0x01; else PORTC=0x02;
Edit, you do have JP5 on don't you?
Mike.
Last edited by Pommie; 30th November 2009 at 05:20 AM.
Hi Pommie,
I do have JP5 on. I changed the code to your advice and the results are the same. RC0 led stays on unless I push the button. Then it turns off. RC1 never comes on.
Just to be sure I am doing things right: I wrote and built the program in mplab. I then exported with config files to a file, and loaded that file with the pickit2 loader.
I'll also post the new code with your changes.
#include <pic.h>
void init(void)
{
TRISA=0x8; // set RA3 as input
TRISC=0x00; // set PORTC as outputs
ANSEL=0; // set ports to digital
PORTC=0; // turn off all leds
}
void main(void)
{
init(); // initialize I/O ports
while(1) // loop forever
{
if(RA3==0)
{PORTC=0x01;}
else
{PORTC=0x02;}
}
}
If I change it to:
if(RA3==1)
{PORTC=0x01;}
else
{PORTC=0x02;}
then RA1 led comes on, unless I push the button.
Any help is most appreciated,
Edit: I'm also getting a warning when I build, is this a concern?
Executing: "C:\Program Files\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" -q -g --asmlist --chip=16F690 "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" -P --char=unsigned --pass1 "C:\Program Files\Microchip\Lessons\c test\ok.c"
Executing: "C:\Program Files\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" -q -g --asmlist --chip=16F690 "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" -P --char=unsigned -ook1.cof -mok1.map ok.p1
Last edited by treefort; 30th November 2009 at 06:24 AM.
Do you have JP1 and JP2 on as well? If you do then I have now idea why it's not working. Maybe someone with an LPC board could help.
Mike.
I do have JP1 and JP2 installed.
I don't understand. With the following code, why is RC1 on when the switch isn't pressed? And off when it is?
if(RA3==1)
{PORTC=0x01;}
else
{PORTC=0x02;}
The instruction if(RA3==1) will be true if the button isn't pressed as the pin is tied to 5V via a resistor. So when the button isn't pressed the PORTC=0x01 should be executed, so, RC0 should be on and RC1 should be off.
Are you able to use a debugger?
Mike.
shouldnt it be something like PORTA.RA3 ?
Roman Vaughan
Also, do you have MCLRDIS in your __CONFIG statement?
| Tags |