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

Simple PIC CODE PROBLEM

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



Joined: 22 Apr 2006
Posts: 87

View user's profile Send private message

Simple PIC CODE PROBLEM
PostPosted: Tue Feb 22, 2011 9:28 am     Reply with quote

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: 19369

View user's profile Send private message

PostPosted: Tue Feb 22, 2011 10:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 22, 2011 11:04 am     Reply with quote

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
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