Electronic Circuits and Projects Forum



if else question

12 Last »
  1. #1
    treefort treefort is offline

    if else question

    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

  2. #2
    Pommie Pommie is online now
    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.
    0
    Last edited by Pommie; 30th November 2009 at 05:20 AM.

  3. #3
    treefort treefort is offline
    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;}

    }
    }
    0

  4. #4
    treefort treefort is offline
    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
    0
    Last edited by treefort; 30th November 2009 at 06:24 AM.

  5. #5
    Pommie Pommie is online now
    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.
    0

  6. #6
    treefort treefort is offline
    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;}
    0

  7. #7
    Pommie Pommie is online now
    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.
    0

  8. #8
    Smartie Smartie is online now
    shouldnt it be something like PORTA.RA3 ?
    0
    Roman Vaughan

  9. #9
    AllVol AllVol is offline
    Quote Originally Posted by treefort View Post
    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.
    0
    Last edited by AllVol; 30th November 2009 at 10:07 AM.

  10. #10
    AllVol AllVol is offline
    Also, do you have MCLRDIS in your __CONFIG statement?
    0

12 Last »
Tags
Electronic Circuits  |  Learning Electronics

Join our community with over 100,000 Members! It's free, easy and when you're logged in you have many more features! Click to register.
Page Time: 0.04896 seconds      Memory: 7,262 KB      Queries: 15      Templates: 0