very noob LED program in C

Thread Starter

eessoo

Joined Jan 29, 2012
23
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 )
 

t06afre

Joined May 11, 2009
5,934
Micropro for PIC
pic 16f877A
i just need the program
how to do the if statement
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.
 

Thread Starter

eessoo

Joined Jan 29, 2012
23
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;
 

t06afre

Joined May 11, 2009
5,934
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;
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.
 

Thread Starter

eessoo

Joined Jan 29, 2012
23
is that the full program?
Rich (BB code):
void main()
{
trisd.b0=1;         
portb=0;  
for( ; ; )
         {
           if(portd.b0==1)
               portb=255;
           else
               portb=0;
          }
}
 
Last edited:

t06afre

Joined May 11, 2009
5,934
is that the full program?
Rich (BB code):
void main()
{
trisd.b0=1;         
portb=0;  
for( ; ; )
         {
           if(portd.b0==1)
               portb=255;
           else
               portb=0;
          }
}
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;
 

codehead

Joined Nov 28, 2011
57
is that the full program?
Rich (BB code):
void main()
{
trisd.b0=1;         
portb=0;  
for( ; ; )
         {
           if(portd.b0==1)
               portb=255;
           else
               portb=0;
          }
}
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:


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 )
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.
 
Top