8-bit AVR Interrupts Example
Objective
This page provides a basic interrupt code example for the ATmega328PB MCU. The project configures the Timer/Counter1 module to operate in Clear-Timer-On-Compare (CTC) mode, and, on a period match, generates an interrupt event every 100 mS. The ISR manipulates a "tick" signal variable that is used by the main loop to toggle LED0 every 100 mS.
Resources
ATmega328PB Xplained Mini
MPLAB® X IDE
MPLAB® X XC8
Connection Diagram
The USART0 module on the target ATmega328PB device is connected to the USART interface on the mEDBG chip. The mEDBG chip performs USB-serial conversion by enumerating as a CDC-class virtual COM port on the PC and presenting the target USART data on this interface. The mEDBG also controls the programming/debug interface on the target device, as well as supplying a 16MHz clock when the Xplained board is connected via USB cable to a PC. The LED0 is connected to port PB5 as shown:
Procedure
Attach the ATmega328PB Xplained Mini board
Using a USB-A-male-to-Micro-B-male cable, attached the Xplained Mini to your computer. Start MPLAB X. If the board has been successfully enumerated, you should see the board image come up in Studio.
Create a New Project
In MPLAB X IDE, select File > New Project.
Choose Project
Select Microchip Embedded and then Standalone Project and then select Next.
Select Device
Use the pulldown or type into the boxes to indicate the Family, ATmega328PB, and Tool. Then select Next.
Select Compiler
Select the XC8 Compiler and then Next.
Name the Project
Choose a name for the project, browse to a location to save it, and select Finish.
Create a new main.c file
Right-click on Source Files in the file explorer window and select New > avr-main.c and rename the File Name to main. Then select Finish.
Add the Code
Delete the contents of the newly created main.c file. Copy and paste the following code into it.
* File: main.c
*
* Project: 8avr-mega-int-usage-complete
*
* Description: Timer1 ISR used to provide 100mS "tick" signal
* Main loop toggles LED0 on every "tick" event
*
* Date: 31-May-2017
* Revision:
* Author:
*
* Hardware: ATmega328PB Xplained Mini PCB Rev 3
*
* Clock: 16MHz (External - from EDBG IC)
* LED0: PB5
* SW0: PB7
* UART_TX: TXD/PD1 (Connected to EDBG IC RXD)
* UART_RX: RXD/PD0 (Connected to EDBG IC TXD)
*
* Fuses: High: 0xDF
* Low: 0xC0
* Ext: 0xFC
******************************************************************************/
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
// define shared variable used for signaling
volatile uint8_t tick_signal = 0;
// Define An Interrupt Handler for TIMER1_COMPA Interrupt (Nesting Disabled)
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
{
tick_signal++; // signal "tick" event
// Timer1 CTC flag auto-cleared by hardware
}
int main(void)
{
// Initialization
// Set LED as output
DDRB |= (1<<PORTB5); // Configure PB5 as digital output
PORTB &= ~(1<<PORTB5); // Set initial level for PB5
// Set up Timer/Counter1
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
OCR1A = 25000; // Set CTC compare value to 10Hz (100mS) at
// 16MHz AVR clock , with a prescaler of 64
TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start Timer/Counter1 at F_CPU/64
// Enable global interrupts
sei();
while(1){
if(tick_signal){
PORTB ^= (1 << PORTB5); // Toggle LED0
tick_signal = 0;
}
}
}
/*******************************************************************************
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP TECHNOLOGY INC OR ITS LICENSORS BE LIABLE OR
OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF
WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR
EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT,
PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD
PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR
COSTS.
*******************************************************************************/
Program the Device
At the top of the MPLAB X IDE, select the Make and Program device button.
Observe the Results
Conclusions
This project has provided an example of how to set up and use interrupts on the megaAVR MCU.
Learn More
megaAVR Interrupt Overview
megaAVR Interrupt Configuration
Special Considerations