CHAP2VER2
CHAP2VER2
Microcontroller’s Advanced
Fonctionalities
•the planned activities
;ISR code
there are two types of hardware interrupts: external and pin change
interrupts.
2. Software interrupts: generated by user-defined program
instructions.
They’re always associated with the microcontroller’s built-in
peripherals and communication port. These interrupts
are not triggered by an external hardware component but by
changes to the built-in peripherals or software configuration.
The interrupt execution response for all the enabled AVR interrupts is
four clock cycles minimum.
During this four clock cycle period, the Program Counter is pushed
onto the Stack.
After four clock cycles, the program vector address for the actual
interrupt handling routine is executed.
After the execution of the ISR the return address is loaded to the
program counter (this is done by the return from interrupt (reti)
instruction in the ISR).
The global interrupt flag in the SREG register is cleared, so that other
interrupts may be treated.
Minimum one instruction from the main program is executed.
Typical and general program setup in assembler for ATmega328P
(including the address; taken from the data sheet):
0x0000 jmp RESET ; Reset Handler
0x0002 jmp EXT_INT0 ; IRQ0 Handler
0x0004 jmp EXT_INT1 ; IRQ1 Handler
0x0006 jmp PCINT0 ; PCINT0 Handler
0x0008 jmp PCINT1 ; PCINT1 Handler
0x000A jmp PCINT2 ; PCINT2 Handler
0x000C jmp WDT ; Watchdog Timer Handler
0x000E jmp TIM2_COMPA ; Timer2 Compare A Handler
0x0010 jmp TIM2_COMPB ; Timer2 Compare B Handler
0x0012 jmp TIM2_OVF ; Timer2 Overflow Handler
0x0014 jmp TIM1_CAPT ; Timer1 Capture Handler
0x0016 jmp TIM1_COMPA ; Timer1 Compare A Handler
0x0018 jmp TIM1_COMPB ; Timer1 Compare B Handler
0x001A jmp TIM1_OVF ; Timer1 Overflow Handler
0x001C jmp TIM0_COMPA ; Timer0 Compare A Handler
0x001E jmp TIM0_COMPB ; Timer0 Compare B Handler
0x0020 jmp TIM0_OVF ; Timer0 Overflow Handler
0x0022 jmp SPI_STC ; SPI Transfer Complete Handler
0x0024 jmp USART_RXC ; USART, RX Complete Handler
0x0026 jmp USART_UDRE ; USART, UDR Empty
Handler
0x0028 jmp USART_TXC ; USART, TX Complete
Handler
0x002A jmp ADC ; ADC Conversion Complete
Handler
0x002C jmp EE_RDY ; EEPROM Ready Handler
0x002E jmp ANA_COMP ; Analog Comparator Handler
0x0030 jmp TWI ; 2-wire Serial Interface Handler
0x0032 jmp SPM_RDY ; Store Program Memory Ready
Handler
0x0033 RESET: ldi r16,high(RAMEND) ; Main program start
0x0034 out SPH,r16 ; Set Stack Pointer to top of RAM
0x0035 ldi r16,low(RAMEND)
0x0036 out SPL,r16
0x0037 sei ; Enable interrupts
0x0038 ... ; next instruction of Main
... ... ... ...
Reset and Interrupt Vectors Placement
MCUCR – MCU Control Register
The MCU Control Register controls the placement of the Interrupt
Vector table.
These bits define the level or edge that triggers the INT0 pin.
ISC11, ISC10 (Interrupt Sense Control bits)
These bits define the level or edge that triggers the INT1 pin.
The main program will keep le blue LED turn on
When push button A is pressed the red LED turns on for second
And when push button B is pressed the green LED turns on for
second
There are two ways to achieve that: Polling and interrupt
The main program will keep the blue LED turn on
When push button A is pressed the red LED turns on for second
And when push button B is pressed the green LED turns on for
second
Programing the Atmega 328 for interrupt
Thus there are 3 banks for pin change interrupt for three ports; B,
C and D.
See the atmega328 interrupt vector table table below.
To use Pin Change Interrupts in ATMega328, you need to configure
the necessary registers and register bits.
This typically involves setting the interrupt mask register (PCMSKx)
to specify which pins in the group you want to monitor, and enabling
the Pin Change Interrupts in the Pin Change Interrupt Control Register
(PCICR)
Optionally there is also pin change interrupt flag register which are set
if interrupt are enabled. You also need to implement the ISR function
in your code, which will be executed when the interrupt is triggered. It
is a bit different from Programming Atmega328P External Interrupt.
The difference being mainly due to masking of bits required and the
pin change interrupt having lower priority than the external hardware
interrupt.
The Pin Change Interrupt Flag Register contains the PCIF1, PCIF2,
PCIF3 flags.
Timer0 and Timer1 can also be used as a counter but we can not use
Tmier2 as a counter
Basic registers and flags of the Timers
Signal Description:
Increment or decrement TCNTn
count
by 1
Select between increment or
direction
decrement
clkTn can be
generated
from an
external or
internal clock
source
comparator
continuously
compares TCNTn
and OCRn.
If equal, the
output compare
flag is set
(OCFn) and an
interrupt can be
issued.
Let's suppose that we want to create a time delay of 50us. Then the
formula to calculate the count value to be loaded into TCNTn register is
as follows.
where, Td = 50us, Fosc = 8MHz, and N is pre-
scalar which can be 1, 8, 64, 256 or 1024
We cannot use pre-scalar of 1 because it will
give negative value for C. So we use next pre-
scalar 8. This then will give value C as,
Basic Mode of Timer Operations
There are basically three types of timer mode of operation which are:
1. Normal Mode
2. CTC Mode
3. PWM
a. Fast PWM
b. Phase Correct PWM
8 bit timers
The ATmega168 has 2, 8-bit timers: Counter0 and Counter2. Each of
these timers are controlled by the following registers.
Counter0 Counter2 Description
TCCR0A TCCR2A Timer/Counter Control Register A
TCCR0B TCCR2B Timer/Counter Control Register B
TCNT0 TCNT2 Timer/Counter Register
OCR0A OCR2A Output Compare Register A
OCR0B OCR2B Output Compare Register B
Timer/Counter Interrupt Mask
TIMSK0 TIMSK2
Register
The counter simply overruns when it passes its maximum 8-bit value
(TOP = 0xFF) and then restarts from the bottom (0x00).
The TOVn Flag, in this case, behaves like a ninth bit, except that it is
only set, not cleared.
1. Load the TCNT0 register with the initial value (let’s take 0x25).
2. For normal mode and the pre-scaler option of the clock, set the value
in the TCCR0 register. As soon as the clock Prescaler value gets
selected, the timer/counter starts to count, and each clock tick causes
the value of the timer/counter to increment by 1.
3.Timer keeps counting up, so keep monitoring for timer overflow i.e.
TOV0 (Timer0 Overflow) flag to see if it is raised.
4. Stop the timer by putting 0 in the TCCR0 i.e. the clock source will
get disconnected and the timer/counter will get stopped.
5. Clear the TOV0 flag. Note that we have to write 1 to the TOV0 bit to
clear the flag.
6. Return to the main function.
. CTC Mode