# CCS Compiler - Delay

Status
Not open for further replies.

#### Key

##### New Member
Im doing my PIC programming using the CCS Compiler. However, when come to delay, i know how to write a simple program that my output delay for a few second.

I was thinking that my output will always high until the user press a button then only my output will become low.

My question is: do i need to do a loop for my delay so that when user press the button then the delay is equal to zero. or is there anything that i can do to my hardware?

#### 3v0

##### Coop Build Coordinator
Forum Supporter
I read your post a few times and am still confused by it.

First tell us what are you trying to do ?

Then post the code you have so far using the # icon, it will put [CODE][/CODE] tags around the code which will keep the formating and make it easier to read.

#### Key

##### New Member
Previously i set my output that was connected to a LED for 60second.
That means in my program code i write

delay_ms(6000);

Now I want to let my output to be convenient for user. which the LED switch ON until user press a button and then the LED will switch OFF. So how to do for this?

#### Mike - K8LH

##### Well-Known Member
Then it seems you don't really need a delay routine at all. Simply use a loop that tests the push button switch and lights or extinguishes the LED accordingly. Yes, no?

Mike

#### Key

##### New Member
Then it seems you don't really need a delay routine at all. Simply use a loop that tests the push button switch and lights or extinguishes the LED accordingly. Yes, no?

Mike
How??? I mean how to loop the push button? i really have no idea of it and im new in electronic and PIC... Plz do teach me... TQ

#### Mike - K8LH

##### Well-Known Member
Well it depends on how you want the push button to act. If you want it to emulate a toggle switch where you push it to switch the light from on-to-off or from off-to-on then you need a simple latch or memory. Here's an example which acts on a "new press" and ignores the "release" part of the switch cycle;

Code:
while(1)
{
sample = switchpin;    // sample active high switch
if(sample ^ latch)     // if change (press or release)
{
latch = sample;      // update the switch state latch
if(sample & latch)   // if a "new press"
ledpin = ~ledpin;  // toggle the LED
}
}
If you're using an active low push button switch then you'll need to change the first line in the program;

Code:
while(1)
{
sample = ~switchpin;   // sample active low switch
if(sample ^ latch)     // if change (press or release)
{
latch = sample;      // update the switch state latch
if(sample & latch)   // if a "new press"
ledpin = ~ledpin;  // toggle the LED
}
}

Last edited:
Status
Not open for further replies.