pic16f887

Alho

Joined Nov 3, 2011
12
hello.
i am using PIC16F887 microcontroller for my project.

i suppose to turn on two LEDs (red and green) with two push button switches. if i press switch 1, the red LED turns on and should stay on even if i release the button. if i press switch 2, the red LED turns off and the green LED turns on and should stay on even if i release the button. can anybody help me right the c program for thing simple project. i am just new begginer for pic microcontrollers......

thanks

MrChips

Joined Oct 2, 2009
22,886
We are here to help you, not do your homework for you. Start writing the code and post it here. Enclose the program in code /code tags in brackets [ ]. Then we'll guide you along the way.

thatoneguy

Joined Feb 19, 2009
6,359
That would almost be simpler to implement with a couple latches than a uC

It's very straightforward to code though.

What language and programming tool do you use?

Alho

Joined Nov 3, 2011
12
yeah i actually don't know how to latch inputs.
I am using microC for pro.

Alho

Joined Nov 3, 2011
12
and i use PICKIT2

nerdegutta

Joined Dec 15, 2009
2,676
A wise man said:
"First comes the hardware, then the software."

Do you have schematic?

debjit625

Joined Apr 17, 2010
790
Of course its will be easy,but as it has been said you have to show us ,what you have done this far then only we could help... like any code you have written..

Alho

Joined Nov 3, 2011
12
Below is the code i wrote in microC for pro. I need to use only two buttons. Pressing button1 for the first time clears the portd.f0 and sets portd.f7. As long as portd.f7 is high, portd.f5 goes high after 20s. If button1 is pressed again, portd.f5 goes low and after 20s portd.f5 goes high. This continues as long as portd.f7 is high. If i press button2, portd.f7 and portd.f5 clears and portd.f0 sets. this procedure repeats as endless while loop.

could anyone help me whats wrong with my code? The problem starts with the second "if statement" marked in blue. When i pressed button1, i have to press several times or sometimes once with some delay. It doen't work with one shot.............can anyone help me with this...... thanks

Rich (BB code):
=============================================================================

bit oldstate;                                    // Old state flag

void main() {

ANSEL  = 0;                                    // Configure all pins as digital I/O
ANSELH = 0;
C1ON_bit = 0;                                  // Disable comparators
C2ON_bit = 0;

TRISA4_bit = 1;                             // set RA4 pin as input
TRISA5_bit = 1;

TRISD = 0x00;                                  // Configure PORTD as output
PORTD = 0x01;                                  // Initial PORTD value
oldstate = 0;

do
{
if (Button(&PORTA, 4, 1, 1))             // if button1 is pressed
{
PORTD.F7 = 1;                          // portd.f7 goes high
PORTD.F0 = 0;                         // portd.f0 goes low

do
{
Delay_ms(20000);                     // delay 20 seconds
PORTD.F5 = 1;                        // portd.f5 goes high

if (Button(&PORTA, 4, 10, 1))        // button1 is pressed again
{
PORTD.F5 = 0;                      // portd.f5 goes low
}

if (Button(&PORTA, 5, 1, 1))       // button2 is pressed
{
PORTD.F0 = 1;                    // portd.f0 goes high
PORTD.F5 = 0;                    // portd.f5 goes low
PORTD.F7 = 0;                    // portd.f7 goes low
}

} while(PORTD.F7 == 1);           // while portd.f7 is high
}
} while(1);
}

Last edited by a moderator:

MrChips

Joined Oct 2, 2009
22,886
Put your code in between {code} and {/code} tags. Replace the {} around the word "code" and "/code" with [].

Your program will be sitting in Delay_ms(20000) for 20 seconds most of the time doing nothing.