Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

if else question

Status
Not open for further replies.

treefort

New Member
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. :confused:

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:
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:
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.
 
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.

Edit: Also, do you have MCLRDIS in your __CONFIG statement?

#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;}

}
}


I would suggest you turn off the comparators on the 16F690.
 
Last edited:
Good morning,

I haven't purchased the debugger yet Pommie, but when I get home from work I might be able to step through using the simulator and breakpoints while changing the input value...? I'm just learning so maybe I can't do that.

Hi AllVol, I don't have a __CONFIG statement with my C program. I was under the impression you only needed that when using Assembly. Could you help me out with what to include? I have to go to work now, but I'll likely be thinking about this all day.

I tried with the comparators turned off with the same results. My code now 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
ANSELH=0; // set ports to digital
PORTC=0; // turn off all leds
//CMCON0=7; //set digital I/O
CM1CON0 = 0; // Initialize Comparator 1 off
CM2CON0 = 0; // Initialize Comparator 2 off
}

void main(void)
{
init(); // initialize I/O ports
while(1) // loop forever
{
if(RA3==1)
{PORTC=0x01;}
else
{PORTC=0x02;}

}
}
 
I would suggest you turn off the comparators on the 16F690.

The comparators are off by default but your comment about MCLR made me check the data sheet and it does need to be turned off. Why would Microchip do that?

The code needs a config line added, something like,
Code:
    __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT );

Mike.
 
TreeFort, try this
Code:
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(PORTAbits.RA3==0) // if button is pressed
{
PORTCbits.RC0=1; // turn on led 0
PORTCbits.RC1=0;} // turn off led 1

else // if button is not pressed
{PORTCbits.RC1=1; // turn on led 1
PORTCbits.RC0=0;} // turn off led 0

}
}
 
Thank you AllVol and Pommie. With the __CONFIG (MCLRDIS); the program works like a charm.

I still don't see in the manual where it says to disable it. But, I did see that RA3 can be set up as the MCLR input and you can use it to put the chip in sleep mode. Is that why they make you disable it? to allow you to use it as i/o as well?

After looking on this page I discovered that if I put #include <htc.h> instead of pic.h it works too, without the __CONFIG. Is there a disadvantage to doing this? Should I be including other files as well?

Thanks for all your help again :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top