|
|
View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Getting acquisition to start |
Posted: Mon Jul 12, 2004 12:00 am |
|
|
Hello -
My question is kind of general, not code specific, but I would like to poll the group regarding techniques used in the scenario described below.
I am grabbing various types of sensor signals, and storing these data to external EEPROM. When the acquisition starts, I check the real time from a RTC and store it, and store the time interval between acquisitions as well. This way, I can log remote data for days or even weeks, and have a time stamp on it.
My question concerns the start of the acquisition. Let's assume I have the board powered up - the board has a PIC, sensors, EEPROM, etc. Now, I want the data acquisition to start when the user throws a switch - say its connected to PIN_C1 for the sake of argument. So I have something in code like this:
main() {
// Bunch of mostly passive code here.....
while( input(PIN_C1) ) {
// Here is where the data is grabbed and stored to EEPROM.
} // end the while
} // end of main()
If I start the program running when PIN_C1 is low, and then throw the switch to set PIN_C1 high, no data is acquired. I guess the "while" statement is checked and "bypassed" when the "while" argument is not true, and the program never returns to the "while" to check again. However, if I power the board down, set PIN_C1 high so that it is high when the board powers up, then the "while" is entered and data is acquired, of course. And, once data is being acquired and I set PIC_C1 low, data acquisition stops. Ok, so the dumb question is this: What's the best way to have an application running and "idling" until the user throws a switch? Must interrupts be used?
Thanks very much,
Bill |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 12, 2004 1:30 am |
|
|
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
|
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Jul 12, 2004 7:48 am |
|
|
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 |
Posted: Mon Jul 12, 2004 7:55 am |
|
|
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 |
|
|
|
|
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
|