View previous topic :: View next topic |
Author |
Message |
deltatech
Joined: 22 Apr 2006 Posts: 87
|
Simple PIC CODE PROBLEM |
Posted: Tue Feb 22, 2011 9:28 am |
|
|
Hi I am having problems with the following code it is not performing as I expected. I am not a C expert and any help in solving this problem will be greatly appreciated.
What I want to do is if the button is pressed and kept pressed then the Pin_B0 should go high and then low only once.
What is happening now is that if I press and release the pin then only the Pin_B0 goes high and low. If I keep the Button pressed the Pin_B0 stays high .
Code: |
#include "G:\PIC-C-CODE\SELONOID\main_877_Pic.h"
#define BUTTON PIN_A0
#define SELONOID PIN_B0
void main()
{
while (true){
if ( !input(BUTTON) ){
output_high(SELONOID);
delay_ms(200);
output_low(SELONOID);
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Tue Feb 22, 2011 10:34 am |
|
|
It will do.
It'll actually go low, but for such a short time, you won't see it.
The code sees the button pressed. Raises B0. Delays 0.2 second. Drops B0, and then _immediately_ tests the input again, and if it is still pressed, raises B0 again. Total 'low' time, probably a handful of machine cycles. Perhaps 1uSec or less (depending on the processor clock).
If you want it to drop just _once_, you need to test for the button being released before accepting the button being pressed again. So something like:
Code: |
#include "G:\PIC-C-CODE\SELONOID\main_877_Pic.h"
#define BUTTON PIN_A0
#define SELONOID PIN_B0
void main() {
int1 last_button=FALSE;
while (true) {
if ( !input(BUTTON) && last_button==FALSE ){
//Only get here when button _goes_ low.
output_high(SELONOID);
delay_ms(200);
output_low(SELONOID);
last_button=TRUE;
}
if (input(BUTTON)) last_button=FALSE; //button has gone high
//so re-enable testing for it going low.
}
}
|
Best Wishes |
|
|
deltatech
Joined: 22 Apr 2006 Posts: 87
|
|
Posted: Tue Feb 22, 2011 11:04 am |
|
|
Ttelmah You are a CCS Super star Thank you very much for your help it is now working exactly as I wanted once again thanks a million |
|
|
|