I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
Hello,
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
Hello,
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
also see usleep() and nanosleep()
despite being obsolete they may do what you want if available.
On 04/11/2021 09:03, zeneca wrote:
Hello,
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
sleep()
On Thu, 4 Nov 2021 10:03:46 +0100, zeneca wrote:
Hello,
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
You could always use the poll() function, though the advisability of this depends on what else the program is doing apart from making an LED blink.
For instance, if poll() watches stdin and has a timeout value of 1
second, it would be trivial to use poll() to turn the LED on for a
second, then off for a second until a key is hit, and then to write
"Exiting NOW" to stdout and exit.
IME its useful to understand how to use poll(). This is because writing
and debugging a program that uses it to handle input from several files
is a lot simpler and less error-prone than using threads to do the same thing.
Many thanks, but for the moment I m only testing /evaluating
possibilities. I often use timer wile programing microcontrolers (Atmel)
but here poll doesn't fit because the target is free running procces in backgound supposed to take some action every second?.
Hello,
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
On Thu, 4 Nov 2021 10:11:26 +0000
nev young <newsforpasiphae1953@yahoo.co.uk> wrote:
also see usleep() and nanosleep()
despite being obsolete they may do what you want if available.
Not strictly true.
Windows has never supported nanosleep.
On Posix, although deprecated, these days the OS actually translates it as 1000 * nanosleep (which is very much current).
On 04/11/2021 12:14, Folderol wrote:
On Thu, 4 Nov 2021 10:11:26 +0000
nev young <newsforpasiphae1953@yahoo.co.uk> wrote:
also see usleep() and nanosleep()
despite being obsolete they may do what you want if available.
Not strictly true.
Windows has never supported nanosleep.
On Posix, although deprecated, these days the OS actually translates
it as
1000 * nanosleep (which is very much current).
Just make sure you check that whatever sleep function you are using in whatever language, is doing what you expect for sub-second values, by
timing how long it takes do to a few thousand calls.
For example sleeps of down to 1ms may sleep for the expected time, but
under that it may either return straight away causing the loop to run
too fast, or always sleep for a minimum of 1ms, causing the loop to be
too slow.
I've seen this change from platform to platform, and language to
language, including on Windows differences between Python 2 (<1ms
returned immediately) and Python 3 (minimum sleep of 1ms).
Many thanks, but for the moment I m only testing /evaluating
possibilities. I often use timer wile programing microcontrolers (Atmel)
but here poll doesn't fit because the target is free running procces in backgound supposed to take some action every second?.
On Thu, 4 Nov 2021 10:11:26 +0000
nev young <newsforpasiphae1953@yahoo.co.uk> wrote:
also see usleep() and nanosleep()
despite being obsolete they may do what you want if available.
Not strictly true.
Windows has never supported nanosleep.
AFAIK Pi's don't run windows.
The distance between two code executions seperated by sleep()s is
whatever time you set sleep() to *plus* the time your code takes to do
its thing
On 05/11/2021 10:26, A. Dumas wrote:
Sure. I guess that is why they asked about timer interrupts. But can
you give an example of how to use timer interrupts (hardware or
otherwise) using C on Raspberry Pi OS? Easy enough on a
microcontroller, but ...
As I said already, you need to set a signal and let the kernel return a signal to your process after a timeout. You cannot access interrupts
directly from user space in Linux...
A word of warning :
Most of the people here have adviced to use sleep(). There is something you should be aware of when you chose to use that method :
The distance between two code executions seperated by sleep()s is whatever time you set sleep() to *plus* the time your code takes to do its thing
(and before you ask : trying to recalculate the sleep time each step to account for the code-execution delay comes with its own problems).
Opposed to that an event from a system timer will /allways/ be the set time apart [1], regardless of how much time the fired-up code takes.
[1] even though the to-be-fired-up code may be delayed due to another processes timeslice still running.
Ofcourse, this is also a timers drawback : if the fired-up code takes more than the set time the next iteration of the fired-up code will start to run when the last one has not yet finished. And that can give .... interresting results. :-)
Bottom line:
Using Sleep()s in a loop works good enough when the timing isn't all that important.
On 05-11-2021 10:29, R.Wieser wrote:
A word of warning :
Most of the people here have adviced to use sleep(). There is
something you
should be aware of when you chose to use that method :
The distance between two code executions seperated by sleep()s is
whatever
time you set sleep() to *plus* the time your code takes to do its thing
(and before you ask : trying to recalculate the sleep time each step to
account for the code-execution delay comes with its own problems).
Opposed to that an event from a system timer will /allways/ be the set
time
apart [1], regardless of how much time the fired-up code takes.
[1] even though the to-be-fired-up code may be delayed due to another
processes timeslice still running.
Ofcourse, this is also a timers drawback : if the fired-up code takes
more
than the set time the next iteration of the fired-up code will start
to run
when the last one has not yet finished. And that can give ....
interresting
results. :-)
Bottom line:
Using Sleep()s in a loop works good enough when the timing isn't all that
important.
Sure. I guess that is why they asked about timer interrupts. But can you
give an example of how to use timer interrupts (hardware or otherwise)
using C on Raspberry Pi OS? Easy enough on a microcontroller, but ...
Yes, but I wanted to hear Rudy say that. Also, it's still not trivial,
is it? It has been too long since I did it, or tried it.
In essence signals are the asynchronous message from kernel to user
space I guess. Not hardware interrupts!
On 05-11-2021 11:48, The Natural Philosopher wrote:
On 05/11/2021 10:26, A. Dumas wrote:
Sure. I guess that is why they asked about timer interrupts. But can
you give an example of how to use timer interrupts (hardware or
otherwise) using C on Raspberry Pi OS? Easy enough on a
microcontroller, but ...
As I said already, you need to set a signal and let the kernel return
a signal to your process after a timeout. You cannot access interrupts
directly from user space in Linux...
Yes, but I wanted to hear Rudy say that. Also, it's still not trivial,
is it? It has been too long since I did it, or tried it.
Ofcourse, this is also a timers drawback : if the fired-up code takes more than the set time the next iteration of the fired-up code will start to run when the last one has not yet finished. And that can give .... interresting results. :-)
Sure. I guess that is why they asked about timer interrupts.
But can you give an example of how to use timer interrupts (hardware or otherwise) using C on Raspberry Pi OS?
Unless the process gets a signal and doesn't ignore it, in which
case the sleep returns as soon as the signal handler is done.
That's why the fired-up code sets a global flag when it begins,
and resets it when it ends.
Charlie,
That's why the fired-up code sets a global flag when it begins,
and resets it when it ends.
:-) Such a basic solution is ofcourse easy enough. But the OP would first
need to know about the possibility of the problem - which is all I wanted to do.
Most of the people here have adviced to use sleep(). There is something you should be aware of when you chose to use that method :
The distance between two code executions seperated by sleep()s is whatever time you set sleep() to *plus* the time your code takes to do its thing
(and before you ask : trying to recalculate the sleep time each step to account for the code-execution delay comes with its own problems).
Opposed to that an event from a system timer will /allways/ be the set time apart [1], regardless of how much time the fired-up code takes.
Bottom line:
Using Sleep()s in a loop works good enough when the timing isn't all that important.
Hello,This is how I did it:
I am looking for information and example on programing a led that's is
on / off every second, based on a 'timer' interrupt.
So far I found very few information on timers.
By the way I am programing in C.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 371 |
Nodes: | 16 (2 / 14) |
Uptime: | 175:07:00 |
Calls: | 7,915 |
Files: | 12,983 |
Messages: | 5,797,724 |