Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
69 views

5 Unit5 Arduino Interrupt, Timer and Communication

This document discusses interrupts, timers, and serial communication in Arduino. It covers [1] how interrupts work by stopping program execution when an external event is detected to run interrupt service routines, [2] the different types of interrupts and timer registers in Arduino, and [3] examples of using timers to generate interrupts at specific time intervals to toggle an LED. It also provides an example program for serial communication to receive data and control an LED based on the received value.

Uploaded by

patilamrutak2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

5 Unit5 Arduino Interrupt, Timer and Communication

This document discusses interrupts, timers, and serial communication in Arduino. It covers [1] how interrupts work by stopping program execution when an external event is detected to run interrupt service routines, [2] the different types of interrupts and timer registers in Arduino, and [3] examples of using timers to generate interrupts at specific time intervals to toggle an LED. It also provides an example program for serial communication to receive data and control an LED based on the received value.

Uploaded by

patilamrutak2003
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit5: Arduino Interrupt, timer

and communication
Sandip J. Murchite
Arduino Interrupt
• Interrupt
– Interrupts are the section of hardware
and software on microcontroller
which is capable of monitoring the
external event on the input pin.

– any external event is monitor then


the program is stop execution at this
point and jump into their ISR function
which is a interrupt handler.

– after executing the ISR function go


back that point where the external
event is monitor.
Arduino Interrupt
Board Digital pins usable fo
interrupts
• Arduino UNO have two interrupt
Arduino UNO 2,3
ports named as INT1,INT0.
• example of execution so line by
line the code is execute until
interrupt is called on Line3 then
the function jumps down to the
ISR and started executing the
line5 and line 6 than after
executing the lines within the ISR
it jumps back to line4 and
finishing the execution as
routine.
Types of interrupts triggering

• There are five types of triggering Arduino interrupts:


– Change: When signal change even if signal rise or signal
fall.
– Rising: On a rising edge the signal going from low to high
means signal triggering from 0v to 5v.
– Falling: On a falling edge the signal going from high to low
means signal is triggering from 5v to 0v.
– Low: Low is a continuous trigger whenever the signal is
low.
– High: High is a continuous trigger whenever the signal is
high
TIMERS IN ARDUINO
Timers interrupts in Arduino

• a timer / counter is a piece of hardware built-in the Arduino


controller.
• It is like a clock, and can be used to measure time events.
• The timer can be programmed by SFR’s ( special function registers)
• Atmega328 microcontroller have 3 timers, called timer0, timer1
and timer2. Timer0 and timer2 are 8bit timer, where timer1 is a
16bit timer.
• The most important difference between 8bit and 16bit timer is
the timer resolution. 8bits means 256 values where 16bit means
65536 values for higher resolution.
• All timers depends on the system clock of your Arduino system.
Normally the system clock is 16MHz
How do Timer Interrupts work?
• As Timer1 and Timer2 are 16-bit and 8-bit timers respectively
hence they can count from 0-65537 for Timer1 and 0-255 for
Timer2.
• Depending on the timer mode which is selected the timer will
start by increasing its value until it reaches its maximum count
then go back to 0.
• This creates a triangle shaped curve which the timer follows. This
will cause the interrupt to occur as the value increases from 0-255
in the case of Timer2 and then goes back to 0 and repeats again.
How do Timer Interrupts work?

• Timer1 can generate Compare Match, Overflow


interruptions.
• In compare match, the interrupt is triggered
when the timer value in the register is equal to
the compare value. If we set the compare value
equal to 50 then whenever the Timer1 will
reach this value an interrupt will be called.
• Overflow interrupt is triggered whenever the
timer value reaches its maximum value
Timer Register
• The most important timer registers are:
• 1) TCCRx - Timer/Counter Control Register.
• 2)TCNTx - Timer/Counter Register. The actual timer value is stored here. Initially it is 0;
3)OCRx - Output Compare Register. The set timer value is stored here.
4) TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
Timer registers
• Timer modes
• Timers can be configured in different modes.
• PWM mode. Pulse width modulation mode
• CTC mode. Clear timer on compare match. When the timer counter reaches the compare
match register, the timer will be cleared

Link for timer control registers


https://www.tutorialspoint.com/timer-registers-in-arduino
COMPUTATION OF OCR (output compare match register) timer

e.g. Toggle the LED at freq, of 4Hz using


compare match interrupt technique.
Clk freq= 16Mhz
prescaler= 256
target frequency= 4Hz
OCR1A (Output cpompare regi. For
timer1)=?

T=1/f; 1/4hz seconds


OCR1= (16MHz/256)*1/4hz
OCR1=15625;
#define ledPin 13
Void Setuptimer1()
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 15625; // compare match register (16MHz/256)*1/4hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 pre scalar
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable system interrupts
}
oid setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Setuptimer1();
Serial.println("TIMER2 Setup Finished.");
}

ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine


{ digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
}
void loop()
{ }
Arduino timer1 code
Timer 0 and Timer 2:(8 bit timer)
e.g. Toggle the LED after 10ms using
compare match interrupt technique.
Clk freq= 16Mhz
prescaler=1024
target time = 10ms
OCR1A (Output cpompare regi. For
timer2)=?

T= 10ms
OCR2A= (16MHZ/1024) *10ms
=156.25 = 156
#define ledPin 13
Void Setuptimer()
{
noInterrupts(); // disable all interrupts
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 156;
TCCR2A |= (1 << WGM21);
TCCR2B = (1<<CS20) | (0<<CS21)|(1<<CS22);
TIMSK2 |= (1 << OCIE2A)
interrupts();
Serial.println("TIMER2 Setup Finished.");
}

void setup()
{ Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Setuptimer ();
}
ISR(TIMER2_COMPA_vect)
{ digitalWrite(ledPin, digitalRead(ledPin) ^ 1); }

void loop()
{ }
Basic functions related to Serial Communication in Arduino
• Serial.begin(baud_rate)
• baud_rate : The baud rate that will be used for serial communication. Can be 4800, 9600,
14400, 19200, etc.
• This function is used to define the baud rate that will be used for serial communication.
• Example Serial.begin(9600)
defines 9600 baud rate for communication.
• Serial.available()
• This function is used to get the number of bytes available for reading from the serial port. It
gives the number of bytes of data that has arrived and is stored in the serial receive buffer.
• Example if(Serial.available())
If data available at serial port, take action.
• Serial.print(value)
• value : character, string, number to be printed.
• This function is used to print data to a serial port in a form that is human readable
(character, strings, numbers).
• Example Serial.print(“Hi 1234”)
• Serial.write(value)
• value : value to be sent as a single byte.
• This function writes data in binary form to the serial port. Data is sent in form of bytes or series of
bytes.
• Example Serial.write(100)
Serial communication program to transmit
and receive data
int inByte; void loop()
{
if (Serial.available() > 0)
{
void setup() inByte = Serial.read();
{
Serial.begin(9600);
pinMode(13, OUTPUT);
Serial.println("Type 1: LED ON, 0: LED OFF "); if (inByte == '1')
} {
digitalWrite(13, HIGH);
Serial.println("LED - On");
}
else
{
digitalWrite(13, LOW);
Serial.println("LED - off");
}
}
End of unit

You might also like