Вы находитесь на странице: 1из 7

Pin No.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

Pin name (XCK/T0) PB0 (T1) PB1 (INT2/AIN0) PB2 (OC0/AIN1) PB3 (SS) PB4 (MOSI) PB5 (MISO) PB6 (SCK) PB7 RESET Vcc GND XTAL2 XTAL1 (RXD) PD0 (TXD) PD1 (INT0) PD2 (INT1) PD3 (OC1B) PD4 (OC1A) PD5 (ICP) PD6 PD7 (OC2) PC0 (SCL) PC1 (SDA) PC2 (TCK) PC3 (TMS) PC4 (TDO) PC5 (TDI) PC6 (TOSC1)

Description I/O PORTB, Pin 0 I/O PORTB, Pin 1 I/O PORTB, Pin 2 I/O PORTB, Pin 3 I/O PORTB, Pin 4 I/O PORTB, Pin 5 I/O PORTB, Pin 6 I/O PORTB, Pin 7 Reset Pin, Active Low Reset Vcc = +5V GROUND

Alternate Function T0: Timer0 External Counter Input. XCK : USART External Clock I/O T1:Timer1 External Counter Input AIN0: Analog Comparator Positive I/P INT2: External Interrupt 2 Input AIN1: Analog Comparator Negative I/P OC0 : Timer0 Output Compare Match Output

In System Programmer (ISP) Serial Peripheral Interface (SPI)

Output to Inverting Oscillator Amplifier Input to Inverting Oscillator Amplifier I/O PORTD, Pin 0 USART Serial Communication Interface I/O PORTD, Pin 1 I/O PORTD, Pin 2 I/O PORTD, Pin 3 I/O PORTD, Pin 4 PWM Channel Outputs I/O PORTD, Pin 5 I/O PORTD, Pin 6 I/O PORTD, Pin 7 I/O PORTC, Pin 0 TWI Interface I/O PORTC, Pin 1 I/O PORTC, Pin 2 I/O PORTC, Pin 3 JTAG Interface I/O PORTC, Pin 4 I/O PORTC, Pin 5 I/O PORTC, Pin 6 Timer Oscillator Pin 1 Timer/Counter1 Input Capture Pin Timer/Counter2 Output Compare Match Output External Interrupt INT0 External Interrupt INT1

29 30 31 32 33 34 35 36 37 38 39 40

PC7 (TOSC2) AVcc GND AREF PA7 (ADC7) PA6 (ADC6) PA5 (ADC5) PA4 (ADC4) PA3 (ADC3) PA2 (ADC2) PA1 (ADC1) PA0 (ADC0)

I/O PORTC, Pin 7

Timer Oscillator Pin 2

Voltage Supply = Vcc for ADC GROUND Analog Reference Pin for ADC I/O PORTA, Pin 7 I/O PORTA, Pin 6 I/O PORTA, Pin 5 I/O PORTA, Pin 4 I/O PORTA, Pin 3 I/O PORTA, Pin 2 I/O PORTA, Pin 1 I/O PORTA, Pin 0 ADC Channel 7 ADC Channel 6 ADC Channel 5 ADC Channel 4 ADC Channel 3 ADC Channel 2 ADC Channel 1 ADC Channel 0

VCC: Digital supply voltage. (+5V) GND:Ground. (0 V)Note there are 2 ground Pins. Port A (PA7 - PA0) Port A serves as the analog inputs to the A/D Converter. Port A also serves as an 8-bit bi-directional I/O port, if the A/D Converter is not used. When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B (PB7 - PB0) Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port B also serves

the functions of various special features of the ATmega16 as listed on page 58 of datasheet. Port C (PC7 - PC0) Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port C also serves the functions of the JTAG interface and otherspecial features of the ATmega16 as listed on page 61 of datasheet. If the JTAG interface is enabled, the pull-up resistors on pins PC5 (TDI), PC3 (TMS) and PC2 (TCK) will be activated even if a reset occurs. Port D (PD7 - PD0) Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). Port D also serves the functions of various special features of the ATmega16 as listed on page 63 of datasheet. RESET: Reset Input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. XTAL1: External oscillator pin 1 XTAL2: External oscillator pin 2 AVCC: AVCC is the supply voltage pin for Port A and the A/D Converter. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a lowpass filter. AREF: AREF is the analog reference pin for the A/D Converter
When an interrupt occurs, the normal flow of instructions is suspended by the microcontroller and the code corresponding to the interrupt, which has occurred, is executed. Once the code corresponding to the interrupt is executed completely the execution again begins from the same instruction where it was stopped.

Following is what happens when an interrupt occurs: 1. 2. Microcontroller normally completes the instruction which is being executed. The program control transfers to Interrupt Service Routine (ISR). Each interrupt have an

associated ISR which is a piece of code which tells the microcontroller what to do when an interrupt has occurred.


Execution of ISR is performed by loading the beginning address of the corresponding ISR into

program counter. 4. 5. Execution of ISR continues until the return from the interrupt instruction (RETI) is encountered. When ISR is complete, the microcontroller resumes processing where it left off before the

interrupt occurred, i.e., program control is reverted back to the main program.

The whole process can be visualized by the following flow diagram:

Atmega16 Interrupts Number of available interrupts varies with different microcontrollers of AVR family. Atmega16 in total has twenty one (21) interrupts available. The available interrupts are categorized in two classes:


External Interrupts- Out of the twenty one interrupts available, four interrupts are directly present

on controller pins to handle the interrupts generated by external sources, so they are called as external interrupts. The four available interrupts and their respective pins are shown in the figure below in their order of priority:


Internal Interrupts- The remaining seventeen (17) interrupts are available for internal use and

support the precise and efficient operation of various peripherals like ADC, Timers, and USARTs etc. The table below describes the available internal interrupts in the order of their priority:

S. No. 1 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.


DEFINITION Timer/Counter2 Compare match interrupt Timer2 Overflow interrupt Timer/Counter1 Capture Event interrupt Timer/Counter1 Compare Match A interrupt Timer/Counter Compare Match B interrupt Timer/Counter1 Overflow interrupt Timer/Counter0 Overflow interrupt Serial Transfer Complete interrupt USART Receive Complete interrupt USART Data Register Empty interrupt USART Transmit Complete interrupt ADC Conversion Complete interrupt EEPROM Ready interrupt

14. 15. 16. 17.


Analog Comparator interrupt Two-wire serial interface interrupt Timer/Countrt0 Compare Match interrupt Store Program Memory Read interrupt

The internal interrupts will be discussed with their respective peripherals. The external interrupts are mainly focused in this article.

External Interrupts Configuration Registers: To configure an external interrupt INT0, INT1 or INT2,it is required to initialize the respective interrupt by doing appropriate bit settings of following 4 registers. The scope of this document is limited to the explanation of the bits corresponding to interrupts only, the detailed description about other bits of these register can be found in the datasheet of Atmega16.


MCUCR (MCU Control register)

The Bit0, Bit1, Bit2 and Bit3 of MCUCR register determines the nature of signal at which the interrupt 0 (INT0) and interrupt 1 (INT1) should occur.


MCUCSR (MCU Control and Status Register)

The Bit6 of MCUCSR register determines the nature of signal at which the external interrupt 2 (INT2) should occur. INT2 is edge triggered only, it cannot be used for level triggering like INT0 and INT1.


GICR (General Interrupt Control Register)

The GICR register Bit5, Bit6 and Bit7 called the interrupt masks are used to disable/enable the respective interrupt. Interrupt is disabled when bit value is set to 0 and enabled when bit value is set to 1. By default all the interrupts are disabled. Above mentioned three registers have to be configured accordingly to initialize a particular interrupt. Also note that in addition to the above mentioned registers, the I-bit (Bit7, Global Interrupt Enable) of SREG register must also be set to 1. If Global Interrupt enable bit is set to 0, none of the interrupts will work irrespective of the other register settings. The set and clear of I-bit is done by SEI and CLI instructions.

Programming Steps: For programming an interrupt, the following steps must be followed: 1. 2. 3. 4. Clear Global Interrupt enable bit in SREG register. Initialize the interrupt by appropriately configuring the MCUCR, MCUCSR and GICR registers. Set Global Interrupt Enable bit in SREG register. Define the appropriate Interrupt service routine (ISR) for the interrupt.

There are two ways of writing ISR, for e.g. ISR for INT0 can be written in following two ways: A. ISR (INT0_vect) B. SIGNAL (SIG_INTERRUPT0)