How to Use Timer in Raspberry Pi




The Raspberry pi is a device which uses the Broadcom controller chip which is a SoC (System on Chip). This SoC has the powerful ARM11 processor which runs on 700 MHz at its core. This powerful processor and the controller having the peripherals like timers, interrupt controller, GPIO, PCM / I2S, DMA controller, I2C, SPI slave, PWM, UART, USB, graphical processing unit (GPU) which includes VideoCore, MPEG-2 and MPEG-4 and a 512 MB SDRAM makes it a mini-computer.

The Raspberrypi board is powerful enough to run large operating systems like Linux, Mac and Windows. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. The immediate advantage of having an Operating System like Ubuntu running on an embedded system device like Raspberrypi is Multi-User-Multitasking.How to Use Timer in Raspberry Pi

In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.  To access the pins that coming out of the Broadcom controller of the Raspberrypi board using C language, a C library is available called “bcm2835” which has been downloaded and installed.

A signal is sent for the purpose of notifying the process about something that required immediate attention. Different signals are used to notify different events and the signals are differentiated by their signal numbers. The list of all the available signals in the OS and their signal numbers can be obtained using the following command;

kill -l

The following table gives a list of the most common signals that a process might encounter in an Operating System;

 NAMENUMBERDESCRIPTION
SIGHUP1Linux sends a process this signal when it becomes disconnected from a terminal.
SIGINT2Linux sends a process this signal when the user tries to end it by

pressing CTRL+C.

SIGILL4Linux sends a process this signal when it attempts to execute an illegal instruction.
SIGABRT6Linux sends a process this signal to the process when the process calls the ‘abort ()’ function
SIGFPE8Linux sends a process this signal when it has executed an invalid floating-point math instruction
SIGKILL9Linux sends a process this signal to end it immediately
SIGUSR110User programs can send this signal to other process
SIGUSR212User programs can send this signal to other process
SIGSEGV11Linux sends a process this signal when the program has attempted an invalid memory access
SIGPIPE13Linux sends a process this signal when the program has attempted to access a broken data stream, such as a socket connection that has been already closed
SIGALRM14A process can receive this signal from the Linux using the function alarm(), after a time period mentioned in its argument.
SIGTERM15Linux sends a process this signal requesting it to terminate
SIGCHLD17Linux sends a process this signal when a child process exits
SIGXCPU24Linux sends a process this signal when it exceeds the limit of

CPU time that it can consume.

SIGVTALRM26A process can receive this signal from the Linux using the function setitimer (), after a time period mentioned in its argument.

This particular project is based on continuously receiving and handling the signal number 14, the SIGALRM. A process can receive the SIGALRM signal from the OS by calling a function named ‘alarm ()’, the prototype of which is defined in the header file <signal.h>.

The OS will send the SIGALRM to the process after a time period mentioned in the parameter passed to the function during the function call. A single call to the alarm () will receive a single SIGALRM signal only. To receive a SIGALRM at continuous interval of time, the same function needs to be called after receiving each SIGALRM. The better method is to use set a timer which can continuously generate the SIGALRM signals at specified intervals of time.

The ‘set interval timer’ function can be used to set a timer which can continuously generate the SIGALRM signals at specified intervals of time.

#include <bcm2835.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define PIN1 RPI_GPIO_P1_11
#define PIN2 RPI_GPIO_P1_12
#define PIN3 RPI_GPIO_P1_24
#define PIN4 RPI_GPIO_P1_15
#define PIN5 RPI_GPIO_P1_16
#define PIN6 RPI_GPIO_P1_18
#define PIN7 RPI_GPIO_P1_22
#define PIN8 RPI_GPIO_P1_07
void sig_handler ( int signo );
void set_pins_output ( void );
void set_all_pin_low ( void );
void timer_seconds ( long int seconds );
void timer_useconds ( long int useconds );
int main(){if (!bcm2835_init())
return 1;set_pins_output ();set_all_pin_low ();
 signal ( SIGALRM, sig_handler );timer_seconds ( 2 );while ( 1 );
bcm2835_close();return 0;}void timer_seconds ( long int seconds ){struct itimerval timer1;How to Use Timer in Raspberry Pi

timer1 . it_interval . tv_usec = 0;
timer1 . it_interval . tv_sec = seconds;
timer1 . it_value . tv_usec = 0;
timer1 . it_value . tv_sec = seconds;
setitimer ( ITIMER_REAL, &timer1, NULL );}
void timer_useconds ( long int useconds ){struct itimerval timer2;
timer2 . it_interval . tv_usec = useconds;
timer2 . it_interval . tv_sec = 0;
timer2 . it_value . tv_usec = useconds;
timer2 . it_value . tv_sec = 0;
setitimer ( ITIMER_REAL, &timer2, NULL );}
void set_all_pin_low ( void ){
bcm2835_gpio_write(PIN1, LOW);
bcm2835_gpio_write(PIN2, LOW);
bcm2835_gpio_write(PIN3, LOW);
bcm2835_gpio_write(PIN4, LOW);
bcm2835_gpio_write(PIN5, LOW);
bcm2835_gpio_write(PIN6, LOW);
bcm2835_gpio_write(PIN7, LOW);
bcm2835_gpio_write(PIN8, LOW);}

 

For mroe detail: How to Use Timer in Raspberry Pi




© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top