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 CCS Technical Support

Timers to keep lights on

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



Joined: 21 Feb 2010
Posts: 11

View user's profile Send private message

Timers to keep lights on
PostPosted: Fri May 17, 2013 5:47 am     Reply with quote

I have a system with two light bulbs and two buttons. When a button is pressed the corresponding light bulb should be on. But after the button is released the light must stay on for a specific time. I can make the system for only one light but for two lights i need to use timers. Does anyone have an example of how to use timers to just light a led for a specific time. I've read the example from the site but i don't understood how timers are working. Is there other way to do what i what or timers is the only solution. It doesn't matter what PIC i use. I was thinking at a pic12f615 because it have less pins. The thing i want to do can be done with two 555 or with just a 556 but i want to do it with a microcontroller. The main problem is how to separate the processes. I haven't used timers since now so a basic hello world application that blink a led using a timer would help me.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri May 17, 2013 6:44 am     Reply with quote

Probably easiest and simplest to use interrupts for both buttons and timers.

You could use small delay_ms(xx)'s multiple times, for the timings.
Create counters to set the times.
Poll the buttons.

Mike
A_L_E_X



Joined: 21 Feb 2010
Posts: 11

View user's profile Send private message

PostPosted: Fri May 17, 2013 7:31 am     Reply with quote

The maximum time is 12 seconds. Do you know any example similar to my case ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Fri May 17, 2013 8:38 am     Reply with quote

All you need is to have a 'tick' interrupt at some frequency that suits, then whenever the button is seen as 'on', load a counter with the number of counts of this interrupt you want it to stay on for. So (pseudo code):
Code:

int16 count1=0,count2=0;
#define TicksToStayOn (NoOfSecondsYouWant*InterruptsPerSecond)

............
//In your main loop
if (button1IsOn)
{
    count1=TicksToStayOn;
}
if (button2IsOn)
{
    count2=TicksToStayOn;
}

If (count1>0)
    output1_on;
else
    output1_off;

If (count2>0)
    output2_on;
else
    output2_off;
.............


void your_tick_interrupt(void)
{
    if (count1>0)
       --count1;
    if (count2>0)
       --count2;
}


So whenever the button is seen as 'pressed', the counter is loaded (and reloaded so long as the button is pressed). When the button is released the counter starts counting down at the interrupt rate. When it gets to zero, the output is turned off.

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