CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Unable to increment using push button

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
anilmanisingh



Joined: 31 Mar 2011
Posts: 6

View user's profile Send private message

Unable to increment using push button
PostPosted: Thu Mar 05, 2015 4:01 am     Reply with quote

#include <12F675.h>
//#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5

int seq=0;

void blink_seq(void)
{
switch(seq)
{
case 0:
{
output_high( GP1 ); // turn LED on
delay_ms( 450 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 450 ); // wait 150ms
break;
}
case 1:
{
output_high( GP1 ); // turn LED on
delay_ms( 300 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 300 ); // wait 150ms
break;
}
case 2:
{
output_high( GP1 ); // turn LED on
delay_ms( 150 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 150 ); // wait 150ms
}
}
}
void main()
{
while(TRUE)
{

if(!input(GP5))
seq++;
blink_seq();
}
}


Hello Everyone

I want the push button to change the case each time I press the button.
But it happens in three or fourth attempt.

Please help me to overcome this problem.


Last edited by anilmanisingh on Thu Mar 05, 2015 5:46 am; edited 1 time in total
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 4:57 am     Reply with quote

Learn to use the code button, preserves your code layout and makes it more readable.
My compiler complains button1 is not defined and to your 'if/else if' in the push_button() routine.
So, I can't run your code.

What is supposed to happen as your variable seq advances beyond 2?

Mike
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 5:01 am     Reply with quote

Code:
#include <12F675.h>
//#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5

int seq=0;
int count;

Void Push_Button()
{
   static int1 pushed = false;

   if (!pushed && !button1) // Pressed
   {
      pushed = true;
      if (++count > 4)
         count = 1;
   }
   else
      if (button1) // Released
         pushed = false;
}


void blink_seq(void)
{
   switch(seq)
   {
      case 0:
      {
         output_high( GP1 ); // turn LED on
         delay_ms( 450 ); // wait 150ms
         output_low( GP1 ); // turn LED off
         delay_ms( 450 ); // wait 150ms
         break;
      }
      case 1:
      {
         output_high( GP1 ); // turn LED on
         delay_ms( 300 ); // wait 150ms
         output_low( GP1 ); // turn LED off
         delay_ms( 300 ); // wait 150ms
         break;
      }
      case 2:
      {
         output_high( GP1 ); // turn LED on
         delay_ms( 150 ); // wait 150ms
         output_low( GP1 ); // turn LED off
         delay_ms( 150 ); // wait 150ms
      }
   }
}
 
void main()
{
   while(TRUE)
   {
      Push_button();
      //if(!input(GP5))
      seq++;
      blink_seq();
   }
}
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 5:06 am     Reply with quote

Lots of errors, As Mike said, the issue is seq, which will be incrementing very quickly in the main loop.

I would have thought seq should be incremented by the validated button press.
anilmanisingh



Joined: 31 Mar 2011
Posts: 6

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 5:50 am     Reply with quote

Thanks for your reply.

Actually I want to blink led differently each time I press the push button GP5.

My problem is I am unable to get it correctly.

Hi mike after 2 I want to start over gain from zero and continue unless i cut off the supply .

Also I have seen one off you post like below and I want to do the same.

As I understand it, what you want is:-

(1) Operate in mode1 till button pressed, then advance to mode2.
(2) Operate in mode2 till button pressed, then advance to mode3.
(3) Operate in mode3 till button pressed, then return to mode1.

Thanks for you comment.

Please have a look at the new code:

#include <12F675.h>
//#device ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5

int seq=0;

void blink_seq(void)
{
switch(seq)
{
case 0:
{
output_high( GP1 ); // turn LED on
delay_ms( 450 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 450 ); // wait 150ms
break;
}
case 1:
{
output_high( GP1 ); // turn LED on
delay_ms( 300 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 300 ); // wait 150ms
break;
}
case 2:
{
output_high( GP1 ); // turn LED on
delay_ms( 150 ); // wait 150ms
output_low( GP1 ); // turn LED off
delay_ms( 150 ); // wait 150ms
}
}
}
void main()
{
while(TRUE)
{

if(!input(GP5))
seq++;
blink_seq();
}
}


http://imageshack.com/a/img540/7086/NAT6IY.png

https://imageshack.com/i/f0NAT6IYp


Last edited by anilmanisingh on Thu Mar 05, 2015 6:46 am; edited 2 times in total
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 6:01 am     Reply with quote

If you can't be bothered to used the code button, I can't be bothered to read your code. I already formatted it for you once.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 6:49 am     Reply with quote

You're another one editing code after you've had a response.
Then makes nonsense of our replies.

In your original code (which oxo very kindly and neatly formatted for you) there was a variable count which more or less did what you want seq to do.
You've edited that code so our comments are no longer valid.

I've glanced at your new code (that's all I'm prepared to do) and notice your push_button() routine has gone.
Produce something which handles your buttons, use the "code button", then someone will be able to help you.

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 6:31 pm     Reply with quote

There are many ways to debounce a push-button. The program below
shows one way. Every time you push the button, you will increment the
count and it will be displayed in the terminal window (TeraTerm).
Example:
Quote:
1 2 3 4 5 6 7 8 9 10 11 12 13


The push-button must be connected to the PIC with the circuit shown
below. You can use 4.7K pullup (or 10K or some other value).
Code:

           +5v
            |
            <
            > 4.7K       
            <         ___ Push button switch 
To          |        _|_|_
PIC -----------------o   o------
pin                            |             
B0                            --- GND
                               -   

In the test program below, the internal PortB pullups are used, so the
external pull-up is not needed.

Test program:
Code:

#include <18F4520.h>
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#define BUTTON_PIN   PIN_B0 // This pin must have a pull-up.
#define DEBOUNCE_PERIOD_IN_MS  10
#define DEBOUNCE_COUNT  2

void wait_for_keypress(void);

//====================================
void main()
{
int8 count;

port_b_pullups(TRUE);

count = 0;

while(TRUE)
  {
   wait_for_keypress();
   count++;
   
   printf( "%u  ", count);
  }

}
//==================================

void wait_for_keypress(void)
{
char count;

// First, wait for the button to be released.   With the debounce
// values as given above, the button must be in the "up" state
// for two consecutive readings, spaced 10 ms apart.

count = 0;

while(TRUE)
  {
   if(input(BUTTON_PIN) == 1)
      count++;
   else
      count = 0;
   
   if(count == DEBOUNCE_COUNT)
      break;

   delay_ms(DEBOUNCE_PERIOD_IN_MS);
  }

// Now that the button is up, wait until the user presses it.
// In order for the keypress to be considered valid, based
// on the debounce values listed at the beginning of the
// program, the button must be held down for two consecutive
// readings, spaced 10 ms apart.

count = 0;

while(TRUE)
  {
   if(input(BUTTON_PIN) == 0)
      count++;
   else
      count = 0;
   
   if(count == DEBOUNCE_COUNT)
      break;

   delay_ms(DEBOUNCE_PERIOD_IN_MS);
  }

}
 
 
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group