I am using a push button to control something on a PIC16F887 but as soon as I let go of the button it reverse back. If I have a variable called switch, how to I toggle it between 1 and 0 each time RB0 is triggered? I need some sort of latch in C
char dir_var
if (RB0)
{
dir_var=!dir_var//flip the dir_var variable
while(RB0)// wait and do nothing until the button is released
}
//Check the dir_var variable
//in code to find out which
// way to rotate
if(dir_var)
{
//rotate left
}
if(!dir_var)
{
//rotate right
}
while(1)
{
while(button == OFF);
while(button == ON) Pin = 1 ;
while(button == OFF);
while(button == ON) Pin = 0 ;
}
/*
* File: Lab4inC.c
* Author: David Baratheon
*
* Created on 18 June 2013, 20:45
*/
#include <xc.h>
#define _XTAL_FREQ 8000000 //Using 8MHz clock speed
void main(void)
{
//Declaring and Defining Variables
char Blink;
char Counter;
char Switch;
Blink = 0x01;
Counter = 0x00;
Switch = 0x00;
//Configuring the Input and Output Ports
PORTD = Blink;
ANSELH = 0b00000000;
ANSEL = 0b00000000;
TRISD = 0x00;
TRISB = 0b11111111;
TRISA = 0b11111111;
//main program
while(1)
{
//Stimulus Dependant Rotating Bit
//Bit shifting left
if(!Switch)
{
if(!RB0)
{
__delay_ms(250); // request a delay
if(!RB0)
{
Switch = !Switch;
do{ __delay_ms(25); }
while(RB0=0);
}
}
if(Counter<7)
{
Blink = Blink << 1; //
PORTD=Blink;
__delay_ms(250); // request a delay
Counter++ ;
//CLRWDT();
}
else
{
Blink = 0x01; //
PORTD=Blink;
__delay_ms(250); // request a delay
Counter = 0x00;
//CLRWDT();
}
}
//Bit shifting right
if(Switch)
{
if(!RB0)
{
__delay_ms(250); // request a delay
if(!RB0)
{
Switch = !Switch;
do{ __delay_ms(250); }
while(RB0=0);
}
}
if(Counter>0)
{
Blink = Blink >> 1; //
PORTD=Blink;
__delay_ms(250); // request a delay
Counter = Counter - 1 ;
//CLRWDT();
}
else
{
Blink = 0x80; //
PORTD=Blink;
__delay_ms(250); // request a delay
Counter = 0x07;
//CLRWDT();
}
}
}
}