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