View previous topic :: View next topic |
Author |
Message |
Gwo Guest
|
LED Switching and functions |
Posted: Mon Jul 04, 2005 8:05 am |
|
|
Hello,
i am using a pic 18F458 with ccs 3.206.
What i wanted to know is if i have a function that is doing something for example getc();, can i switch on a led for 100 ms and then switch it off every 2 seconds but doing the getc() anyway and vice versa?
Because if i put delays it won't allow the getc to be done for these 2 seconds.
For example :
Code: | getc();
output_high(LED);
delay_ms(100);
output_low(LED);
delay_ms(2000); |
Won't switch the led if getc didn't receive anything ...
How can i do ?
Many thanks |
|
|
valemike Guest
|
|
Posted: Mon Jul 04, 2005 11:00 am |
|
|
I think you would use the function
if kbhit()
This way, you won't block. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: LED Switching and functions |
Posted: Mon Jul 04, 2005 11:10 am |
|
|
Gwo wrote: | Hello,
i am using a pic 18F458 with ccs 3.206.
What i wanted to know is if i have a function that is doing something for example getc();, can i switch on a led for 100 ms and then switch it off every 2 seconds but doing the getc() anyway and vice versa?
Because if i put delays it won't allow the getc to be done for these 2 seconds.
For example :
Code: | getc();
output_high(LED);
delay_ms(100);
output_low(LED);
delay_ms(2000); |
Won't switch the led if getc didn't receive anything ...
How can i do ?
Many thanks |
There are a couple of ways of doing it. I would suggest that you use a state machine along with a timer. Furthermore, unless you are using a software uart, I'd receive the data using an interrupt. |
|
|
Gwo Guest
|
|
Posted: Tue Jul 05, 2005 1:44 am |
|
|
Many thanks !! |
|
|
Gwo Guest
|
|
Posted: Tue Jul 05, 2005 3:37 am |
|
|
I wondered if i can execute my program not only for the getc function (it was just an example)but with a lot of functions, and what i need is to make flicker a green led of a device that shows that it is ON.
However i want to do what i have to do at the same time continuing switching this led.
You said i could use a state machine along with a timer. Could you tell me more about that ?
Thank you. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jul 05, 2005 6:49 am |
|
|
I divide my program into tasks. I try to keep the tasks as short as possible or make them only do a little bit each time I call them. From the main loop, you can call each of the tasks. This will give the appearance that all tasks are running at the same time. For anything that needs to be done on a time interval, I use a timer variable. I setup a timer to interrupt at some system tick. I usually use 1ms. I keep track of the ms count and decrement/increment the task timers in the main loop based on the count from the isr. Sort of the poor man's RTOS. |
|
|
|