Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help Using Push Button With Pic16f883

Status
Not open for further replies.

zain kaleem

New Member
Ok, so here's my question. I just want to run a sequence of LEDs with delays between them in a if statement using push button. When the button is pressed LEDs lights up in a sequence else remain off. I have a while condition(while is used so that the sequence doesn't repeat) and switch denounce in the code which is working perfectly fine. But my problem is that when I press the button lets say for short amount of time(just a click) the whole sequence starts and then completes itself and then end the sequence.
I just want that when I press the button sequence starts but when I release the button (at anytime) it should stop working right away.
Have a look on my code. (I'm using MPLAB X IDE)



C:
void main(void) {
  
    TRISB7=0;
    TRISB6=0;
    TRISB5=0;
    PORTBbits.RB7=0;
    PORTBbits.RB6=0;
    PORTBbits.RB5=0;
    TRISA0=1;
    PORTAbits.RA0=0;
    ANSEL=0X00;
    ANSELH=0X00;
  
       while(1){
        
           if(PORTAbits.RA0==0){
           __delay_ms(70);
           if(PORTAbits.RA0==0){
                 RB5=1;
                 __delay_ms(500);
                 RB6=1;
                 __delay_ms(700);
                 RB7=1;
                 __delay_ms(500);
                 RB7=0;
                 __delay_ms(700);
                 RB6=0;
                 RB5=0;
                  while(PORTAbits.RA0==0);
            }
        }
       else{
           RB5=0;
           RB6=0;
           RB7=0;
        }
       }
}
 
The delay feature is not helpful if you want to monitor something else at the same time. You should use a timer onboard, set timer1 with a prescaller and wither keep checking the timer or use an interrupt flag and check the flag or force an isr. That way, you are not stuck in a delay and you can actually do or check other things at the same time.
If you need a poor-man's (dumbed down option), use a short 5 mSec delay and check if the button was released after each 5 milliseconds). You could embed the short delays in a loop and do it 100 times to make your 500 mSec.
good luck.
 
The delay feature is not helpful if you want to monitor something else at the same time. You should use a timer onboard, set timer1 with a prescaller and wither keep checking the timer or use an interrupt flag and check the flag or force an isr. That way, you are not stuck in a delay and you can actually do or check other things at the same time.
If you need a poor-man's (dumbed down option), use a short 5 mSec delay and check if the button was released after each 5 milliseconds). You could embed the short delays in a loop and do it 100 times to make your 500 mSec.
good luck.



Bro thanks for the help.
But what if there are variable delays.
There is a seven segment display attached to port c and switching the display value with the button to show the value of x and y.
Two more buttons used for increment and decrement the value from 0-9. this 0-9 value changes the delay between LEDs.
and how can I continuously check the button after 5ms???
 
Last edited:
The basic principle is to use an "image" of the input bit the switch is connected to; eg. another variable that you copy the switch state to, after comparing the two.

That way the loop can always run with no holds or long waits for things, so everything in it can be updated regularly.

eg. something like, in pseudocode:
Code:
// near start of the loop:
switch_pressed = switch_input_bit;
// or !switch_input_bit for an active low input

// That guarantees that an change to the input state is only registered
// at the start of the loop, and nothing can change during the loop body.


//The input state test
if(switch_pressed == true && switch_image == false) {
// New switch press, take appropriate action
....
}
else {
// switch not pressed, or already seen.

}

// Rest of code

// and just before the end of the main loop
switch_image = switch_pressed;
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top