View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Problem with synchronization of led blinking |
Posted: Sat Mar 19, 2011 9:20 am |
|
|
I have a problem here....I have to develop an I/O module wherein when 1st input on Pin_A2 appears, Led on C5 should start blinking say with a delay of 2secs.
Then when input on Pin_a1 goes high, Led on C6 should start blinking with a delay of 1 sec along with the led which is previously blinking.
Again the same case of PIN_A0 and C7. with a delay of half a sec, with previous led blinking at a same rate.
When I press a button B0 the led becomes steady, and when the conditions goes low...the led stop blinking.
The code is as follows:- (Delays in programs are assumed but still they are not much of a help :( )
please suggest something:-
Code: |
#include <18F2520.h>
#device ICD=TRUE
#fuses HS,NOLVP,WDT128 //INTRC_IO
#use delay (clock=40000000)
#include <button.c>
int8 B0 = 0;
int flag=0;
void main()
{
PORT_B_PULLUPS(true);
output_low(PIN_C5);
output_low(PIN_C6);
output_low(PIN_C7);
while(1)
{
if(input(Pin_A0)==0)
output_low(PIN_C5);
if(input(Pin_A1)==0)
output_low(PIN_C6);
if(input(Pin_A2)==0)
{
output_low(PIN_C7);
flag=0;
}
while(flag==0)
{
if(input(Pin_A2)==1)
{
while(1)
{
output_high(PIN_C5);
delay_ms(50);
output_low(PIN_C5);
delay_ms(50);
if(input(pin_A1)==1)
{
while(1)
{
output_high(PIN_C5);
delay_ms(50);
output_low(PIN_C5);
delay_ms(50);
output_high(PIN_C6);
delay_ms(25);
output_low(PIN_C6);
delay_ms(25);
if(input(Pin_A0)==1)
{
while(1)
{
output_high(PIN_C7);
delay_ms(50);
output_low(PIN_C7);
delay_ms(50);
output_high(PIN_C5);
delay_ms(100);
output_low(PIN_C5);
delay_ms(100);
output_high(PIN_C6);
delay_ms(150);
output_low(PIN_C6);
delay_ms(150);
if(button(PIN_B0, 0, 50, 10, B0, 1))
{
flag=1;
if(input(Pin_A0)==1)
{
output_high(PIN_C7);
}
if(input(Pin_A1)==1)
{
output_high(PIN_C6);
}
if(input(Pin_A2)==1)
{
output_high(PIN_C5);
}
break;
}
}
}
break;
}
}
break;
}
}
}
}
} |
Waiting for your reply.
Thanks
Sid |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 19, 2011 2:49 pm |
|
|
You have several inputs being monitored and several processes going
at the same time. You should probably look at this multi-tasking thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=17189
For more ideas, use the forum's search page to search for all threads
with this word:
|
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Mon Mar 21, 2011 1:47 am |
|
|
Thanks PCM,
However, I would like to know how the flows goes....I have never used interrupts and timers before...
should i use the inputs are the interrupts and call the timers onces i get the interrupt.
I'm clueless about it...
Please show some way..
thanks
sid |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Thu Apr 07, 2011 4:24 am |
|
|
Can anyone please help me show how to blink 2 leds at a same time with different frequency....
I can manage the C code but, need a help with timers and interrupts... |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Thu Apr 07, 2011 10:07 am |
|
|
Well think about the timing first for example say 50 ms is your time quanta
one led will blink at this rate the next at 2x this rate and so on
Next think about what needs to happen as the time progresses
again:
turn on led1 and led 2
delay 50 ms
turn off led1
delay 50 ms // 100ms elapsed
turn on led1 turn off led 2
delay 50 ms /// 150ms elapsed
turn off led1
delay 50 ms // 200 ms
goto again
4 delay statements to control two leds
If you get the idea then extend it to 3 leds |
|
|
scottc
Joined: 16 Aug 2010 Posts: 95
|
|
Posted: Thu Apr 07, 2011 11:01 am |
|
|
I dont think you need to use a ISR or timer considering that your
code is running at 40Mhz.
Have you tried creating a couple of functions to handle the actual
led blinking instead of putting everything in main ?
Basically I suggest, put the Led blinking parts in their own function.
Then make a call to the function as required. Modularise the code..
Thanks Scott |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Thu Apr 07, 2011 11:42 pm |
|
|
Thanks Douglas Kennedy and scottc,
That makes sense...and as scottc suggested calling function can actually work!
I will just write down a sample code an see how it executes!
thanks alot for your efforts.
Sid |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Apr 08, 2011 7:36 am |
|
|
For an interrupt routine with a corresponding timer it is often useful to first decide on the interrupt interval ( time quanta) say 50ms and just blink a single led before mixing things up. A very good rule with interrupt service routines (isr) is to have them as frugal as possible. Avoid printf in an isr and keep the code short further a delay_ms call in any isr is rarely valuable.
Most often the code in main does all the heavy work and the isr just sets flags for the main routine. In your isr your flags would be the on/off state of your led. Now since changing the state of a pin connected to a led is very short code the isr rule could be broken and the work done there. Be careful not to get drawn in and bloat the isr so that it loses its functional value. |
|
|
|