PIC16F877A PUSHBUTTON

Thread Starter

namratha_r

Joined Jan 27, 2015
23
I am working on pic16f877a and using XC8 complier. Have interfaced pushbutton if I press the button value should increment by 1.. its working fine but one more condition - If I keep press and hold down for long time fastely value should increment how to do this I dont no am trying but not achieved. Below is my button code

void main()
{
int oldstate;
oldstate = 0;

while(1)
{
if(RA4 == 1) // switch

{

oldstate = 1; // update flag

}

if(oldstate &&(RA4 == 0))

{

val++;

if(val > 10)

{

val = 1;

}

oldstate = 0; // update flag

}
}
 

sevenfold4

Joined Jan 12, 2015
80
Please make your code more readable


Code:
void main()
{
    int oldstate;
    oldstate = 0;
    while(1)
    {
        if(RA4 == 1) // switch
        {
            oldstate = 1; // update flag
        }
        if(oldstate &&(RA4 == 0))
        {
            val++;
            if(val > 10)
            {
                val = 1;
            }

            oldstate = 0; // update flag
        }
    }
}
I would personally recommend using an interrupt for the button instead. If more code is added it will simplify and make everything faster.

I can not really understand your question, but this should increment variable v for as long as the button is held down.


Code:
void main()
{
    int v = 0;
    while(1)
    {
        if(RA4 == 1) // switch
        {
            v++;
        }
    }
}
For simplicity sake you can add a delay to take care of bouncing and if you want to increment it slower, while held down
 
Last edited:

Thread Starter

namratha_r

Joined Jan 27, 2015
23
Code:
void main()
{
int oldstate;                                              // old state flag
oldstate = 0;

while(1)
{
if(RA4 == 1)                                           // [I]Detect logical one (switch)[/I]

{

oldstate = 1;                                          // update flag

}

if(oldstate &&(RA4 == 0))                   // [I]Detect one-to-zero transition[/I]

{

val++;                                                  // increment value by 1

if(val > 10)                                         

{

val = 1;                                              // counts upto 10 values

}

oldstate = 0;                                     // update flag

}
}
Moderators note: Please use code tags to preserve the spaces
 
Last edited by a moderator:

Thread Starter

namratha_r

Joined Jan 27, 2015
23
if i press the button value will be incremented by 1 . And in my code if i press down and hold button for long time value will be same here i have to change the logic . If i press down the button for long time value should fastely increment ...
 

sevenfold4

Joined Jan 12, 2015
80
Please use tabs...

Code:
void main()
{
    int oldstate; // old state flag
    oldstate = 0;
    while(1)
    {
        if(RA4 == 1) // Detect logical one (switch)
        {
            oldstate = 1; // update flag
        }
        if(oldstate &&(RA4 == 0)) // Detect one-to-zero transition
        {
            val++; // increment value by 1
            if(val > 10)
            {
                val = 1; // counts upto 10 values
            }
            oldstate = 0; // update flag
        }
    }
}
Of course the value will remain the same, because of the variable oldstate, it can not increment until you have released the button and the variable resets. You can have the variable increment once when pressed, or you can use my code and have it increment for as long as the button is pressed...
 

Thread Starter

namratha_r

Joined Jan 27, 2015
23
Hi..,
Here is my complete code . What am trying to do actually is AC dimming. Have used TRIAC and zerocrossing detector in my hardware connected to ac bulb and interfaced 2 pushbuttons. Variable (val) is incremented/decremented when switch is pressed and later used in delay so dimming is happening . Code is working fine for - > If i press the button once val will inc/dec one time so dimming will happen step by step. But, > If i press the button down for longer time val should fastely inc/dec and dimming should happen smoothly without flickering. But here in my code it will be in same state as perilously discussed.

Code:
void interrupt RB0_int(void)
{
    if (INTF)
    {
        RB0 = 1;
        INTF = 0;
    }
}

void main(void)
{
    int val;
    int oldstate;
    int oldstate1;

    PORTB = 0;
    TRISB = 0xFF; //RB0 input for interrupt
    TRISD = 0;
    PORTA = 0;
    TRISA = 0xFF;
    OPTION_REG = 1;

     val = 1;
     oldstate = 0;
     oldstate1 =0;
    
    while(1)
    {
        if (RB0)                       // interrupt
        {
            if(RA4 == 1)           // sw1
            {
                oldstate = 1;
            }
                if(oldstate &&(RA4 == 0))
                {
                    val++;              
                    if(val > 10)
                    {
                        val = 1;
                    }

                    oldstate = 0;

               }

            if(RB4 == 1)                   // sw2
            {
                oldstate1 = 1;
            }
            if(oldstate1 &&(RB4 == 0))
            {
                val--;
                if(val < 1)
                {
                    val = 10;
                }
                oldstate1 = 0;

            }
                    DelayMs(val);                  
                    RD1 = 0;                                     //light on                          
                    DelayMs(1);
                    RD1 = 1;                                    //light off

                    while (RB0)
                    {

                    }
            }

    }
    return;
}
}
 

sevenfold4

Joined Jan 12, 2015
80
And what was your question? It does not dim smoothly?
The val will increment/decrement once each time you press the button, because of the variable oldstate0 and 1.
 

sevenfold4

Joined Jan 12, 2015
80
And what was your question? It does not dim smoothly?
The val will increment/decrement once each time you press the button, because of the variable oldstate0 and 1.

Code:
if(RA4 == 1)           // sw1
            {
          
                if((RA4 == 0))
                {
                    val++;      
//Insert some kind of delay.      
                    if(val > 10)
                    {
                        val = 1;
                    }
                }
            }

               }
You probably will want to make val a bigger value, to ensure more smoothness.
 

Thread Starter

namratha_r

Joined Jan 27, 2015
23
Sorry for disturbing :( ... If button pressed down its flickering . I changed delay as well as changed val value

Code:
if(RA4 == 0)
            {
                DelayMs(100);
                if((RA4 == 0))
                {
                    val++;
                    DelayMs(50);
                    if(val > 10)
                    {
                        val = 1;
                    }
                }
            }
 

sevenfold4

Joined Jan 12, 2015
80
Well of course it will be flickering, You are not using a Digital to Analog to power the LED. You are simply turning it on and off at different time periods.
Code:
   DelayMs(val);                 
                    RD1 = 0;                                     //light on                         
                    DelayMs(1);
                    RD1 = 1;                                    //light off
 

ErnieM

Joined Apr 24, 2011
8,377
When you use "TRIAC and zerocrossing detector in my hardware connected to ac bulb" you control the brightness by delaying the TRIAC turn on from each and every zero crossing. Do not think PWM (though this is a close cousin to that) as there is no linear relationship between delay and brightness.

Do not compute the delay but use a look up table as this is much faster: when you have 100% brightness you have zero time to calculate that delay!

However, a clever coder could calculate the delay for the next half cycle, and have almost the full half cycle of time to compute that delay.

(I would ask to see your schematic but that might open an unneccessary TOS (Terms of Service) discission: just be careful working on this!)
 
Top