very noob LED program in C

Discussion in 'Homework Help' started by eessoo, Jan 30, 2012.

  1. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    i need to make a program that work when i press a button
    [1]first press make all LED ON
    second press make all LED OFF
    third press half on half off
    forth do nothing
    fifth shift right
    Sixth shift left
    seventh go back to [1] BUT(it dose nothing )
     
  2. PaulEE

    Member

    Dec 23, 2011
    423
    32
    This isn't a noob LED program. Done correctly, this is an interrupt-triggered state machine. What chip and what compiler?
     
    eessoo likes this.
  3. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    Micropro for PIC
    pic 16f877A
    i just need the program
    how to do the if statement
     
    Last edited: Jan 30, 2012
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I just need the program! And why should we just do your homework for you, so just could slap a eessoo name sticker on it. And send it in in your name. This is not the way this forum work.
    Why do you not start by showing what you have done so far.
     
    spider87 likes this.
  5. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    okay
    so far i was able to do a program that will make the LED ON when i press the button and keep it pressed
    here is the code (the if statement)
    if(portd.b0==1)
    portb=255;
    else
    portb=0;
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Is this your full program or just a part of it. PIC controllers need a lot configuration in order to work. And I can not see any of this in your example. Make it habit of always posting your complete code.
     
  7. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    is that the full program?
    Code ( (Unknown Language)):
    1.  
    2. void main()
    3. [COLOR="red"]{[/COLOR]
    4. trisd.b0=1;        
    5. portb=0;  
    6. for( ; ; )
    7.          {
    8.            if(portd.b0==1)
    9.                portb=255;
    10.            else
    11.                portb=0;
    12.           }
    13. [COLOR="Red"]}[/COLOR]
    14.  
     
    Last edited: Jan 30, 2012
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I do not think your program will work. Because after reset all the bits in the TRISB register are set to one. So PORTB is all input pins. Then programming PICs attention to details are very important. You can not program a PIC without the datasheet at hands. My experience is that the moment you start to assume things. You will make wrong assumptions by default. In your program start with writing 0b00000001 to the TRISB register. Instead of trisd.b0=1;
     
  9. codehead

    Member

    Nov 28, 2011
    56
    11
    eessoo—you need to implement a finite-state machine. It's not a complicated concept. Look at you the list of states you showed us:


    OK, so when do you turn half on and half off? Answer: When the button has been pressed and your are in the "second" state.

    So, define yourself a variable. Call it "state". Initialize it to zero. When a button gets pressed, execute a big "switch" statement (or "if/then/else" statement). If you're in state 0 (state==0), turn all LEDs on and increment "state". If you're in state 1, turn all LEDs off and increment state...if you're in state 7, set state to 0.
     
    eessoo likes this.
  10. eessoo

    Thread Starter New Member

    Jan 29, 2012
    23
    0
    thanks guys
    :)
     
Loading...