L13 interrupts-in-atmega328 p

15

Click here to load reader

Transcript of L13 interrupts-in-atmega328 p

Page 1: L13 interrupts-in-atmega328 p

Interrupts in Atmega328P(Lecture-13)

R S Ananda Murthy

Associate Professor and HeadDepartment of Electrical & Electronics Engineering,

Sri Jayachamarajendra College of Engineering,Mysore 570 006

R S Ananda Murthy Interrupts in Atmega328P

Page 2: L13 interrupts-in-atmega328 p

Methods of I/O Operation

Synchronous I/O operation.

This is possible when the I/O device operates at the samespeed as that of MCU.In this case the I/O operation with the device is performedby the MCU assuming that the device is always ready.

Asynchronous I/O Operation.

In this case, generally the I/O device is very slow whencompared to the MCU. Then, it can perform I/O operationby the following methods:

By polling.By using interrupts.

R S Ananda Murthy Interrupts in Atmega328P

Page 3: L13 interrupts-in-atmega328 p

I/O Operation by Polling

Isthe device

ready?

Request device to get ready

Service the device

No

Yes

Polling results in waste of MCU time and power and doesnot permit masking of devices since all devices have to bechecked.

R S Ananda Murthy Interrupts in Atmega328P

Page 4: L13 interrupts-in-atmega328 p

Polling Example

R S Ananda Murthy Interrupts in Atmega328P

Page 5: L13 interrupts-in-atmega328 p

I/O Operation by Interrupts

The MCU keeps on doing some useful operation, or itremains in a low-power state after enabling interrupts.When I/O device is ready, it interrupts the MCU.The MCU completes execution of the current instruction,saves the return address on the stack, and then jumps tothe Interrupt Service Routine (ISR) to serve the device.After this, the MCU returns back to the interrupted programby loading the return address from the stack to theProgram Counter.Interrupts can be enabled or disabled dynamically andwhen multiple interrupts come simultaneously prioritiescan be assigned.

R S Ananda Murthy Interrupts in Atmega328P

Page 6: L13 interrupts-in-atmega328 p

Interrupt Example

R S Ananda Murthy Interrupts in Atmega328P

Page 7: L13 interrupts-in-atmega328 p

Default Interrupt Vectors in Atmega328P

R S Ananda Murthy Interrupts in Atmega328P

Page 8: L13 interrupts-in-atmega328 p

Interrupt Vectors in Atmega328P

Interrupt vector is a definite address in the programmemory where a JMP instruction should be written to jumpto the ISR corresponding to the interrupt.Each interrupt vector occupies two program memory wordsin order to provide space for JMP instruction.In Atmega328P, the RESET Vector is affected byBOOTRST fuse and the Interrupt Vector start address isaffected by IVSEL bit in the MCUCR.

R S Ananda Murthy Interrupts in Atmega328P

Page 9: L13 interrupts-in-atmega328 p

Enabling and Disabling Interrupts in Atmega328P

In SREG if I = 1, then all interrupts are enabled.We can make I = 1 by using SEI instruction in ALP andsei() function in C program.In SREG if I = 0, then all interrupts are disabled.We can make I = 0 by using CLI instruction in ALP andcli() function in C program.Upon reset I = 0 so all interrupts are disabled.

R S Ananda Murthy Interrupts in Atmega328P

Page 10: L13 interrupts-in-atmega328 p

Bits of EICRA Control Nature of INT1 and INT0

Bits 7, 6, 5, and 4 are not used and are always read as 0.

R S Ananda Murthy Interrupts in Atmega328P

Page 11: L13 interrupts-in-atmega328 p

Bits of EIMSK Mask/Unmask INT1 and INT0

Masking/unmasking means disabling/enabling respectively.When INT1 = 1 INT1 is unmasked if I = 1 in SREG.When INT1 = 0 INT1 is masked even if I = 1 in SREG.When INT0 = 1 INT0 is unmasked if I = 1 in SREG.When INT0 = 0 INT0 is masked even if I = 1 in SREG.Activity on the pins of INT1/INT0 will cause an interruptrequest even if the corresponding pins are configured asoutput pins.Bits 2 to 7 are unused and will always be read as 0.

R S Ananda Murthy Interrupts in Atmega328P

Page 12: L13 interrupts-in-atmega328 p

Bits of EIFR Show Status of INT1 and INT0

Bits 2 to 7 are unused and will always be read as 0.When INT1 interrupt is triggered, INTF1 becomes 1. Thisflag is cleared when ISR is executed.When INT0 interrupt is triggered, INTF0 becomes 1. Thisflag is cleared when ISR is executed.INTF1/INTF0 flags can be cleared by writing a logical 1 tothe respective bit.INTF1/INTF0 flags are always cleared when the respectiveinterrupts are configured as a level interrupt.

R S Ananda Murthy Interrupts in Atmega328P

Page 13: L13 interrupts-in-atmega328 p

C-Code to use INT0

R S Ananda Murthy Interrupts in Atmega328P

Page 14: L13 interrupts-in-atmega328 p

Problem Statement to use INT0/INT1

Assume that a push button is connected to the INT0/PD2 pin ofAtmega328P MCU with internal pull-up resistor enabled andthat a 7-segment common cathode display is connected toPORTB lines of the MCU through 330Ω current limitingresistors. Let a high-to-low logic change on this pin cause anINT0 interrupt. Sketch a diagram showing the hardwareconnections. Write a program in C which displays on the LEDdisplay any integer in the range 1 to 6, both inclusive, whenever the push button is pressed, thus simulating throwing of adie. Sketch a flow chart to show the logic.

Also write ALP to achieve the task mentioned above.

Modify the programs written above to use INT1/PD3.

R S Ananda Murthy Interrupts in Atmega328P

Page 15: L13 interrupts-in-atmega328 p

License

This work is licensed under aCreative Commons Attribution 4.0 International License.

R S Ananda Murthy Interrupts in Atmega328P