Lecture 6 – ATmega328 Timers And Interrupts

2y ago
9 Views
2 Downloads
1.37 MB
48 Pages
Last View : 14d ago
Last Download : 3m ago
Upload by : Pierre Damon
Transcription

Lecture 6 – ATmega328 Timersand InterruptsCSE P567

Arduino Digital and Analog I/O Pins Digital pins: Pins 0 – 7: PORT D [0:7]Pins 8 – 13: PORT B [0:5]Pins 14 – 19: PORT C [0:5] (Arduino analog pins 0 – 5)digital pins 0 and 1 are RX and TX for serial communicationdigital pin 13 connected to the base board LEDDigital Pin I/O Functions pinMode(pin, mode) digitalWrite(pin, value) Sets pin to INPUT or OUTPUT modeWrites 1 bit in the DDRx registerSets pin value to LOW or HIGH (0 or 1)Writes 1 bit in the PORTx registerint value digitalRead(pin) Reads back pin value (0 or 1)Read 1 bit in the PINx register

Port Pin Definitions#define PINB SFR IO8(0x03)#define PINB0 0.#define PINB7 7#define PORTC SFR IO8(0x08)#define PORTC0 0.#define PORTC6 6#define DDRB SFR IO8(0x04)#define DDB0 0.#define DDB7 7#define PIND SFR IO8(0x09)#define PIND0 0.#define PIND7 7#define PORTB SFR IO8(0x05)#define PORTB0 0.#define PORTB7 7#define DDRD SFR IO8(0x0A)#define DDD0 0.#define DDD7 7#define PINC SFR IO8(0x06)#define PINC0 0.#define PINC6 6#define PORTD SFR IO8(0x0B)#define PORTD0 0.#define PORTD7 7#define DDRC SFR IO8(0x07)#define DDC0 0.#define DDC6 6

Interrupts Allow program to respond to events when they occurAllow program to ignore events until the occurExternal events e.g.: UART ready with/for next characterSignal change on pin Action depends on context# of edges arrived on pinInternal events e.g.: Power failureArithmetic exceptionTimer “tick”

ATmega328 Interrupts

ATmega328 Interrupts (cont)

Interrupt Model When an interrupt event occurs: Processor does an automatic procedure callCALL automatically done to address for that interrupt Each event has its own interrupt addressThe global interrupt enable bit (in SREG) is automatically cleared Push current PC, Jump to interrupt addressi.e. nested interrupts are disabledSREG bit can be set to enable nested interrupts if desiredInterrupt procedure, aka “interrupt handler” Does whatever it needs to, then returns via RETIThe global interrupt enable bit is automatically set on RETIOne program instruction is always executed after RETI

Interrupts Type 1 – Event is remembered when interrupt is disabled If interrupt is not enabled, flag is set When interrupt is enabled again, interrupt takes place,and flag is resetType 2 – Event is not remembered when interrupt isdisabled Signal level causes interruptIf level occurs when interrupt is enabled, interrupt takes placeIf interrupt is not enabled, and level goes away before theinterrupt is enabled, nothing happens

Interrupt Model Interrupt hander is invisible to program Except through side-effects, e. g. via flags or variablesChanges program timing Can’t rely on “dead-reckoning” using instruction timingMust be written so they are invisible Cannot stomp on program state, e. g. registersSave and restore any registers used Including SREG

Interrupt Vectors Table in memory containing the first instruction of eachinterrupt handlerTypically at program address 0

Interrupt Vectors If interrupts are not used, this memory can be used aspart of the program i.e. nothing special about this part of memoryExample interrupt routine RESET: Sets up the stack pointer

Defined fine#define#define#define#defineINT0 vectINT1 vectPCINT0 vectPCINT1 vectPCINT2 vectWDT vectTIMER2 COMPA vectTIMER2 COMPB vectTIMER2 OVF vectTIMER1 CAPT vectTIMER1 COMPA vectTIMER1 COMPB vectTIMER1 OVF vectTIMER0 COMPA vectTIMER0 COMPB vectTIMER0 OVF vectSPI STC vectUSART RX vectUSART UDRE vectUSART TX vectADC vectEE READY vectANALOG COMP vectTWI vectSPM READY nal Interrupt Request 0 */External Interrupt Request 1 */Pin Change Interrupt Request 0 */Pin Change Interrupt Request 0 */Pin Change Interrupt Request 1 */Watchdog Time-out Interrupt */Timer/Counter2 Compare Match A */Timer/Counter2 Compare Match A */Timer/Counter2 Overflow */Timer/Counter1 Capture Event */Timer/Counter1 Compare Match A */Timer/Counter1 Compare Match B */Timer/Counter1 Overflow */TimerCounter0 Compare Match A */TimerCounter0 Compare Match B */Timer/Couner0 Overflow */SPI Serial Transfer Complete */USART Rx Complete */USART, Data Register Empty */USART Tx Complete */ADC Conversion Complete */EEPROM Ready */Analog Comparator */Two-wire Serial Interface */Store Program Memory Read */

Interrupts Global interrupt enable Interrupt priority is determined by order in table Bit in SREGAllows all interrupts to be disabled with one bitsei() – set the bitcli() – clear the bitLower addresses have higher priorityISR(vector) – Interrupt routine definitionreti() – return from interrupt automatically generated for ISR

External Interrupts Monitors changes in signals on pinsWhat causes an interrupt can be configured by setting control registers appropriatelyPins: INT0 and INT1 – range of event options PCINT[23:0] – any signal change (toggle) INT0 – PORT D [2]INT1 – PORT D [3]PCINT[7:0] – PORT B [7:0]PCINT[14:8] – PORT C [6:0]PCINT[23:16] – PORT D [7:0]Pulses on inputs must be slower than I/O clock rate

INT0 and INT1 External Interrupt Control Register: Sense Control (INT0 is the same)

INT0 and INT1 External Interrupt Mask Register If INT# bit is set (and the SREG I-bit is set), then interrupts areenabled on pin INT#External Interrupt Flag Register Interrupt flag bit is set when a change triggers an interruptrequestFlag is cleared automatically when interrupt routine is executedFlag can be cleared by writing a 1 to it

Arduino Language Support forExternal Interrupts attachInterrupt(interrupt, function, mode) interrupt: 0 or 1function: interrupt function to callmode: LOW, CHANGE, RISING, FALLINGdetachInterrupt(interrupt)interrupts( ) – Enable interrupts : sei( )noInterrupts( ) – Disable interrupts : cli( )

PCINT[23:0] Pin Change Interrupt Control Register PCIE2 enables interrupts for PCINT[23:16]PCIE1 enables interrupts for PCINT[14:8]PCIE0 enables interrupts for PCINT[7:0]Pin Change Interrupt Flag Register PCIF# set if corresponding pins generate an interrupt requestCleared automatically when interrupt routine is executed

PCINT[23:0] Pin Change Mask Register 2 Each bit controls whether interrupts are enabled for thecorresponding pinChange on any enabled pin causes an interrupt(Mask registers 1 and 0 are similar)

8-bit Timer/Counter 0(1 and 2 are very similar)

Prescaler for Timer/Counter 0 & 1

Clock Source Select (CS0[2:0]) T0 pin – PORT D[4]T1 pin – PORT D[5]Pin can be configured as output pin Program can generate clock

External Clock Source

Timer/Counter Registers TCNT0 – Timer/Counter Register (8-bit)OCR0A – Output Compare Register AOCR0B – Output Compare Register BTCCR0A/B – Timer/Counter Control RegistersTIMSK0 – Timer/Counter Interrupt Mask Register TOV interruptCompare A&B interruptsTIFR0 – Timer/Counter Interrupt Flag Register TOV interruptCompare A&B interrupts

Normal Mode (0) Timer incrementsWraps around at TOP 0xFFStarts again at 0TOV0 interrupt flag set when TCNT0 reset to 0Useful for generating interrupts every N time unitsUseful for generating an interrupt in N time units Set TCNT0 to an initial value (255 – N)

CTC (Clear Timer on Compare) Mode (2) Timer incrementsWraps around at OCR0A OCR0A defines top value of counterStarts again at 0OCF0A interrupt flag set when TCNT0 reset to 0Pin OC0A can be made to toggle when counter resets Generate output waveform

Fast PWM Mode (3/7) Timer incrementsWraps around at 0xFF (3) or OCR0A (7)Start again at 0Pin OC0x generates waveform Set (reset) when timer 0Reset (set) when timer OCR0x

Phase-Correct PWM Mode (1/5) Timer increments then decrementsIncrements from 0Up to 0xFF (1) or OCR0A (5)Than back down to 0Pin OC0x generates waveform Set when timer OCR0x while incrementingReset when timer OCR0x while decrementing

Timer/Counter Control Registers Timer/Counter Control Register A Timer/Counter Control Register B

Mode Summary

COM0A[1:0] (COM0B[1:0] similar)

Timer/Counter 0 Interrupts Timer/Counter 0 Interrupt Mask TOIE0 – Timer Overflow interruptOCIE0A/B – Compare A/B interruptTimer/Counter 1 Interrupt Flags TOV0 – Timer Overflow flagOCF0A/B – Compare A/B interrupt flag

Timer/Counter 2 (16-bit) Similar to Timer/Counter 1 16-bit counter vs. 8-bit counter16-bit registers Uses shared temporary 8-bit register to enable 16-bit read/writeInput capture register

Input Capture Unit

Input Capture Unit Event on input causes: Counter value (TCNT1) to be written to ICR1 Interrupt flag ICF1 to be set Causing an interrupt, if enabledPin ICP1 – Port B [0]Noise Canceller Time-stampPulses less than 4 clock cycles long are filteredUseful for measuring frequency and duty cycle PWM inputs

Timer/Counter 1 ICR can also be used as the TOP value Allows OCR1A to be used for PWM generation

Timer/Counter 1 Modes

Timer/Counter 2 (8-bit) Identical to Timer/Counter 1Except for clock sources

External and Pin Change IF1PCIF2SFR IO8(0x1B)012#define EIFR SFR IO8(0x1C)#define INTF0 0#define INTF1 1#define EIMSK SFR IO8(0x1D)#define INT0 0#define INT1 FR CRAISC00ISC01ISC10ISC11SFR MEM8(0x69)0123#define PCMSK0 SFR MEM8(0x6B)#define PCINT0 0.#define PCINT7 7#define PCMSK1 SFR MEM8(0x6C)#define PCINT8 0.#define PCINT14 6#define PCMSK2 SFR MEM8(0x6D)#define PCINT16 0.#define PCINT23 7

Timer/Counter 0 e#defineTCCR0A SFR IO8(0x24)WGM00 0WGM01 1COM0B0 4COM0B1 5COM0A0 6COM0A1 TCCR0B SFR IO8(0x25)CS00 0CS01 1CS02 2WGM02 3FOC0B 6FOC0A 7#define TCNT0 SFR IO8(0x26)#define TCNT0 0 0.#define TCNT0 7 7#define OCR0A SFR IO8(0x27)#define OCROA 0 0.#define OCROA 7 7#define OCR0B SFR IO8(0x28)#define OCR0B 0 0.#define OCR0B 7 7

Timer/Counter Interrupts#define#define#define#defineTIFR0 SFR IO8(0x15)TOV0 0OCF0A 1OCF0B 2#define#define#define#defineTIMSK0 SFR MEM8(0x6E)TOIE0 0OCIE0A 1OCIE0B 2#define#define#define#define#defineTIFR1 SFR IO8(0x16)TOV1 0OCF1A 1OCF1B 2ICF1 5#define#define#define#define#defineTIMSK1 SFR MEM8(0x6F)TOIE1 0OCIE1A 1OCIE1B 2ICIE1 5#define#define#define#defineTIFR2 SFR IO8(0x17)TOV2 0OCF2A 1OCF2B 2#define#define#define#defineTIMSK2 SFR MEM8(0x70)TOIE2 0OCIE2A 1OCIE2B 2

Timer/Counter defineTCCR1A SFR MEM8(0x80)WGM10 0WGM11 1COM1B0 4COM1B1 5COM1A0 6COM1A1 7TCCR1B SFR MEM8(0x81)CS10 0CS11 1CS12 2WGM12 3WGM13 4ICES1 6ICNC1 7#define TCCR1C SFR MEM8(0x82)#define FOC1B 6#define FOC1A 7#define TCNT1 SFR MEM16(0x84)#define TCNT1L SFR MEM8(0x84)#define TCNT1L0 0.#define TCNT1L7 7#define TCNT1H SFR MEM8(0x85)#define TCNT1H0 0.#define TCNT1H7 7#define ICR1 SFR MEM16(0x86)#define ICR1L SFR MEM8(0x86)#define ICR1L0 0.#define ICR1L7 7#define ICR1H SFR MEM8(0x87)#define ICR1H0 0.#define ICR1H7 7

Timer/Counter 1 (cont)#define OCR1A SFR MEM16(0x88)#define OCR1AL SFR MEM8(0x88)#define OCR1AL0 0.#define OCR1AL7 7#define OCR1AH SFR MEM8(0x89)#define OCR1AH0 0.#define OCR1AH7 7#define OCR1B SFR MEM16(0x8A)#define OCR1BL SFR MEM8(0x8A)#define OCR1BL0 0.#define OCR1BL7 7#define OCR1BH SFR MEM8(0x8B)#define OCR1BH0 0.#define OCR1BH7 7

Timer/Counter TCCR2A SFR MEM8(0xB0)WGM20 0WGM21 1COM2B0 4COM2B1 5COM2A0 6COM2A1 TCCR2B SFR MEM8(0xB1)CS20 0CS21 1CS22 2WGM22 3FOC2B 6FOC2A 7#define TCNT2 SFR MEM8(0xB2)#define TCNT2 0 0.#define TCNT2 7 7#define OCR2A SFR MEM8(0xB3)#define OCR2 0 0.#define OCR2 7 7#define OCR2B SFR MEM8(0xB4)#define OCR2 0 0.#define OCR2 7 #defineASSR SFR MEM8(0xB6)TCR2BUB 0TCR2AUB 1OCR2BUB 2OCR2AUB 3TCN2UB 4AS2 5EXCLK 6

Timer Interrupt Program Examplevoid setup(){DDRB ;// Pin 13 as output// Using timer 2// Set to Normal mode, Pin OC0A disconnectedTCCR2A ;// Prescale clock by 1024// Interrupt every 256K/16M sec 1/64 secTCCR2B ;// Turn on timer overflow interrupt flagTIMSK2 ;// Turn on global interruptssei();}char timer 0;ISR(timer ;PORTB }vect) {void loop(){// Nothing to do};

Timer Example #2void setup(){DDRB ; // Pin 13 OUTPUT// Using timer 2// Set to CTC mode, Pin OC0A disconnectedTCCR2A ;// Prescale clock by 1 (no prescale)TCCR2B ;// Set compare registerOCR2A ;// Turn on timer compare A interrupt flagTIMSK2 ;// Turn on global interruptssei();}char timer 0;ISR(vect) {timer ;PORTB ;}void loop(){// Nothing to do}

External Interrupt Example#define pinint0#define pinint1void .begin(9600);// External interrupts 0 and 1// Interrupt on rising edgeEICRA ;// Enable both interruptsEIMSK ;// Turn on global interruptssei();}ISR(vect) {}ISR(vect) {}// Print out the informationvoid loop(){Serial.print("X: ");Serial.print(percent0);Serial.print(" Y: ");Serial.println(percent1);}

Interrupt Model When an interrupt event occurs: Processor does an automatic procedure call CALL automatically done to address for that interrupt Push current PC, Jump to interrupt address Each event has its own interrupt address The global interrupt enable bit (in SREG) is

Related Documents:

Introduction of Chemical Reaction Engineering Introduction about Chemical Engineering 0:31:15 0:31:09. Lecture 14 Lecture 15 Lecture 16 Lecture 17 Lecture 18 Lecture 19 Lecture 20 Lecture 21 Lecture 22 Lecture 23 Lecture 24 Lecture 25 Lecture 26 Lecture 27 Lecture 28 Lecture

Lecture 1: A Beginner's Guide Lecture 2: Introduction to Programming Lecture 3: Introduction to C, structure of C programming Lecture 4: Elements of C Lecture 5: Variables, Statements, Expressions Lecture 6: Input-Output in C Lecture 7: Formatted Input-Output Lecture 8: Operators Lecture 9: Operators continued

Lecture 1: Introduction and Orientation. Lecture 2: Overview of Electronic Materials . Lecture 3: Free electron Fermi gas . Lecture 4: Energy bands . Lecture 5: Carrier Concentration in Semiconductors . Lecture 6: Shallow dopants and Deep -level traps . Lecture 7: Silicon Materials . Lecture 8: Oxidation. Lecture

TOEFL Listening Lecture 35 184 TOEFL Listening Lecture 36 189 TOEFL Listening Lecture 37 194 TOEFL Listening Lecture 38 199 TOEFL Listening Lecture 39 204 TOEFL Listening Lecture 40 209 TOEFL Listening Lecture 41 214 TOEFL Listening Lecture 42 219 TOEFL Listening Lecture 43 225 COPYRIGHT 2016

Partial Di erential Equations MSO-203-B T. Muthukumar tmk@iitk.ac.in November 14, 2019 T. Muthukumar tmk@iitk.ac.in Partial Di erential EquationsMSO-203-B November 14, 2019 1/193 1 First Week Lecture One Lecture Two Lecture Three Lecture Four 2 Second Week Lecture Five Lecture Six 3 Third Week Lecture Seven Lecture Eight 4 Fourth Week Lecture .

Water Level Indicator with Temperature Sensor . The Arduino Uno is a microcontroller board based on the ATmega328.ATmega328 has 32 KB (with 0.5 KB used for the boot loader). It also has 2 KB of SRAM and 1 KB of EEPROM .It has 14 digital . Thus this project is a very essential protection circuit that can be implemented in a wide range of

V13-T1-53 1/16 DIN LCD Preset Counters with Rate and Time. . . . . . . . . . . . . . . . Timers and Tachometers CA08100015E—April 2012 www.eaton.com V13-T1-3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Counters, Panel Meters, Tachometers and Timers 1.1 . Model X Serie

Timer0 Interrupt Mask Register Bit 7 OCIE0 B OCIE0 TOIE0 A 8-bit Timers can only count up to 255. Be sure to select a prescalarsuch that your OCR value will fit in 8-bits. C.18 ISR Names In CTC mode, an "Output Compare A Match Interrupt" will vector to an ISR with thes