pic16f887

Discussion in 'General Electronics Chat' started by Alho, Dec 14, 2011.

  1. Alho

    Thread Starter New Member

    Nov 3, 2011
    12
    0
    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
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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.
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
  4. Alho

    Thread Starter New Member

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

    Thread Starter New Member

    Nov 3, 2011
    12
    0
    and i use PICKIT2
     
  6. nerdegutta

    Moderator

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

    Do you have schematic?
     
  7. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    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..
     
  8. Alho

    Thread Starter New Member

    Nov 3, 2011
    12
    0
    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 :)

    Code ( (Unknown Language)):
    1.  
    2. =============================================================================
    3.  
    4. bit oldstate;                                    // Old state flag
    5.  
    6. void main() {
    7.  
    8.   ANSEL  = 0;                                    // Configure all pins as digital I/O
    9.   ANSELH = 0;
    10.   C1ON_bit = 0;                                  // Disable comparators
    11.   C2ON_bit = 0;
    12.  
    13.   TRISA4_bit = 1;                             // set RA4 pin as input
    14.   TRISA5_bit = 1;
    15.  
    16.   TRISD = 0x00;                                  // Configure PORTD as output
    17.   PORTD = 0x01;                                  // Initial PORTD value
    18.   oldstate = 0;
    19.  
    20.   do
    21.   {
    22.     if (Button(&PORTA, 4, 1, 1))             // if button1 is pressed
    23.     {
    24.       PORTD.F7 = 1;                          // portd.f7 goes high
    25.       PORTD.F0 = 0;                         // portd.f0 goes low
    26.  
    27.    
    28.       do
    29.       {
    30.         Delay_ms(20000);                     // delay 20 seconds
    31.         PORTD.F5 = 1;                        // portd.f5 goes high
    32.      
    33.         if (Button(&PORTA, 4, 10, 1))       [COLOR=Blue] // button1 is pressed again[/COLOR]
    34.         {
    35.           PORTD.F5 = 0;                      // portd.f5 goes low
    36.         }
    37.        
    38.         if (Button(&PORTA, 5, 1, 1))       // button2 is pressed
    39.         {
    40.           PORTD.F0 = 1;                    // portd.f0 goes high
    41.           PORTD.F5 = 0;                    // portd.f5 goes low
    42.           PORTD.F7 = 0;                    // portd.f7 goes low
    43.         }
    44.        
    45.       } while(PORTD.F7 == 1);           // while portd.f7 is high
    46.     }
    47.   } while(1);
    48. }
    49.  
    50.  
     
    Last edited by a moderator: Dec 19, 2011
  9. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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.
     
Loading...