[vc_row][vc_column width=”1/1″][vc_column_text]Timer interrupts allow you to perform a task at very specifically timed intervals regardless of what else is going on in your code.[/vc_column_text][vc_tour][vc_tab title=”How to use timer interrupt on pcDuino” tab_id=”1387838249-1-51″][vc_column_text]When the pcDuino runs Ubuntu, which is a version of Linux, Linux provides a timer mechanism, which is to evoke a certain process at certain interval, and use the process to do some predefined jobs.
The structure that is used by the timer is shown below:
struct itimerval
{
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
}
struct timeval
{
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
}
it_value: Set the first time that the timer alarms and interrupts.
it_interval: Set the reset value of the timer. If it is zero, the timer will be invalid after the first alarm. If not zero, the timer will be loaded with this value when after the first alarm.
tv_sec: Set the time parameter of the timer in unit of seconds.
tv_usec: Set the time parameter of the timer in units of microseconds.
The function to set timer:
int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value)
where the variable ‘which’ takes the following value:
- ITIMER_REAL: Use the real time of system to compute, it will send a signal named SIGALRM.
- ITIMER_VIRTUAL: Use the time spent in user status to compute, it will send a signal named SIGVTALRM.
- ITIMER_PROF: Use the total time spent in user status and kernel status to compute, it will send a signal named SIGPROF.
We also need to configure the corresponding processing function of the interrupt signal (in header file signal.h):
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
signum: set the interrupt signal that needs to be processed. The interrupt signal can be: SIGALRM, SIGVTALRM, and SIGPROF.
handler: function that is used to process the interrupt signal.
[/vc_column_text][/vc_tab][vc_tab title=”Parts list” tab_id=”1387842179394-3-4″][vc_column_text]
- 1 x pcDuino
- 1 x LED
- Several jump wires
[/vc_column_text][/vc_tab][vc_tab title=”Sample Code” tab_id=”1387838249-2-81″][vc_column_text]
#include <core.h>
#include <signal.h>
#define REFRESH_TIME_S 1
#define REFRESH_TIME_US 500000
void time_initialization(long s,long us)
{
struct itimerval interrupt_time;
interrupt_time.it_value.tv_sec = s;
interrupt_time.it_value.tv_usec =us;
interrupt_time.it_interval.tv_sec = s;
interrupt_time.it_interval.tv_usec = us;
setitimer(ITIMER_REAL, &interrupt_time, NULL);
signal(SIGALRM, timer_handler);
printf(“set interrupt time: %lds , %ldus\n”, s, us);
}
void timer_handler(int irq)
{
switch(irq)
{
case SIGALRM:
digitalWrite(13,(digitalRead(13)^0×01));
break;
default:
break;
}
}
void setup()
{
pinMode(13,OUTPUT);
time_initialization(REFRESH_TIME_S,REFRESH_TIME_US);
}
void loop()
{
}
[/vc_column_text][/vc_tab][vc_tab title=”Run the sample code” tab_id=”1387841869015-2-10″][vc_column_text]The sample code presented in the above section implements a LED blinking function. It uses timer interrupt to implement it instead of the poll that we often see. Here we can see that the function loop() is blank. The blink is implemented in the processing function of the interrupt void timer_handler(int irq).
The timer is configured through REFRESH_TIME_S and REFRESH_TIME_US. In the above sample code, we set it to be 1.5 seconds.
Copy and paste the sample code to the Arduino IDE that is included in pcDuino:
We connect a LED to pin D13, and can observe that the LED is blinking every 1.5 seconds:
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.