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 CCS Technical Support

Getting acquisition to start

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 12, 2004 1:30 am     Reply with quote

Here is a very simple (non-multitasking) approach. A button is polled
until a valid (debounced) down state is detected. At that point, the
program will proceed. The button must first be released by the
user before the program will look for it to be pressed. I believe this
will solve one of your problems.

One side of your button (ie., switch) should go to Pin C1. You must
also place a pull-up resistor (10K) on that side of the button.
The other side of the button must be connected to ground.
So the idle state (ie, button not pressed) is a logic '1' at pin C1.
When the button is pressed, it connects pin C1 to ground, and
this is read as a logic '0' at that time.

I'm not at a location where I can test this program in hardware,
but it should probably work.

Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

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

void my_function(void);
void wait_for_keypress(void);

//====================================
void main()
{

while(1)
  {
   wait_for_keypress();
   
   my_function();
  }

while(1);
}
//==================================

void my_function(void)
{
printf("Executing my function\n\r");

}


//-------------------------------
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(1)
  {
   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(1)
  {
   if(input(BUTTON_PIN) == 0)
      count++;
   else
      count = 0;
   
   if(count == DEBOUNCE_COUNT)
      break;

   delay_ms(DEBOUNCE_PERIOD_IN_MS);
  }

}
carlosma



Joined: 24 Mar 2004
Posts: 53
Location: Portugal

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Mon Jul 12, 2004 2:42 am     Reply with quote

hello

see this

http://www.ccsinfo.com/forum/viewtopic.php?t=17189
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 12, 2004 7:48 am     Reply with quote

Code:
void main(void)
{
   InitializePic(); // all your "passive" initialization code

   // loop forever
   while(1)
   {
      if (input(PIN_C1)
      {
         // do all your wonderful data acquisition tasks
      }
      // put non-data acquisition tasks here, perhaps kick the watchdog
      // or send out periodic status information based on a timer counter
      // or alarm from your RTC
   }
}


PCM Programmer's example is about the same as this, and the link carlosma provided is similar too.

There are many situations where using interrupts is not necessarily the best way to accomplish a task. A simple loop can do the job.

One thing I would recomment is that you consider some kind of debouncing for your PIN_C1 signal. Even if it is a toggle switch as you described in your example, it can still bounce during transitions. If bouncing won't cause spurious data then don't worry about it.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Guest
Guest







Thanks for responses
PostPosted: Mon Jul 12, 2004 7:55 am     Reply with quote

Thanks for the responses. After I posted my query, I realized that one EZ way to get a conditional while to get revisited is to wrap the conditional while statement in an always true while:

while(1) {

while(did something become true?) {
} // end of conditional while

} // end of always true while


And very good point regarding the pullup and/or hard ground when assuring the state of an input pin. Even the best code won't work reliably when the pin is flopping around at some unknown potential. I think many people don't believe this until they find their code doesn't work, dig out that resistor, and hook it up. It seems magical first time around.....

Thanks,
Bill
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