Exam zone
Intro to AVR ATtiny2313
CS423 Dick Steflik
AVR ATtiny2313
2313 Features
RISC, 120 instructions, most executable in one clock cycle, 32x8 general purpose registers, up to 20 MIPS at 20 Mhz Memory - 2Kbytes Flash, 128 Bytes EEPROM, 128 bytes SRAM Peripherals Features – 1-8bit timer, 1- 16 bit timer, 4- PWM channels, On-chip analog comparitor, watchdog timer, Universal Serial Interface, Full Duplex USART
18 programmable I/O lines
Operating Voltage – 2.7 – 5.5VDC
In System Programmable via SPI (Serial Peripheral Interface)
Basic Pin Descriptions
VCC – Digital Supply Voltage
Gnd – ground
Port A (PA2..PA0) – 3 bit, bi-directional I/O with selectable internal pullups
Port B (PB7..PB0) – 8 bit, bi-directional I/O with selectable internal pullups
Port D (PD6-PD0) - 7 bit, bi-directional I/O with selectable internal pullups
Reset – system reset – active low
XTAL1 – external crystal input (alt. PA0)
XTAL2 – external crystal output (alt. PA1)
Serial Peripheral Interface Bus
SPI is used by all Atmel microcontrollers for loading the flash program memory (many other OEMs also)
Easily implemented in hardware
Faster than other methods (I2C, SMBUS)
Master-slave methodology
Half or Full Duplex operation
SPI with 2 slaves SPI Master
SPI Slave 1
SCLK MOSI
SCLK MOSI
MISO
MISO
SS1
SS
SS2 SPI Slave 2 SCLK MOSI MISO SS
AVR Programming AVR Programmer
PC
AVRDUDE PPT RS232 USB
ATtiny2313
VCC SCLK MOSI
VCC (20) SCLK (19)
MISO
MISO (18)
GND
RESET (1) GND (10)
MOSI (17)
GND
API Applications
Flash, EEPROM, MMC and SD cards
Sensors
Temperature
Pressure (touch screens)
LCD Panels Communications (802.15 (ZigBee), CAN, Ethernet....) Control – D/A, A/D, Digital Pots, position encoders
/* Blinker Demo */ /* Include useful pre-defined functions */ #include <avr/interrupt.h> // Defines pins, ports, etc to make programs easier to read #define F_CPU 100000UL // Sets up the default speed for delay.h #include <util/delay.h> int main(){ DDRD = _BV(PD4); while(1){ PORTD = _BV(PD4); _delay_ms(1000); PORTD &= ~_BV(PD4); _delay_ms(1000); } return(0); }
/* enable output on port D, pin 4 */
Interrupts 1 0x0000 BOR,Watchdog Reset 2 0x0001 0 3 0x0002 1 4 0x0003 Event 5 0x0004 Match A 6 0x0005 7 0x0006 8 0x0007 complete 9 0x0008 Empty 10 0x0009 Complete 11 0x000A 12 0x000B 13 0x000C Match B 14 0x000D Match A
Reset
External Pin, POR,
INT0
External Interrupt Request
INT1
External Interrupt Request
TIMER1 CAPT
Timer/Counter1 Capture
TIMER1 COMPA
Timer/Counter1 Compare
TIMER1 OVF TIMER0 OVF USART0,RX
Timer/Counter1 Overflow Timer/Counter0 Overflow USART0 Receive
USART0 UDRE
USART0 Data Register
USART0 RX
USART0 Transmit
ANALOG COMP PCINT TIMER1 COMPB
Analog Comparitor Pin Change Interrupt Timer/Counter1 Compare
TIMER0 COMPA
Timer/Counter0 Compare
ISR Macro
#include <avr/interrupt.h> Defines the beginning of your Interrupt Servicing Routine
Places the starting address of the ISR code into the interrupt vector ISR(SIG_INT0) { cli(); //disable interrupts . . . sei(); // enable interrupts } places address of the body if the ISR into 0x0001
Programmable Interrupts
INT0 - Pin 6
INT1 – Pin 7
PCINT – Pins 12-19
PCMSK – Pin Change Mask Register
Which pins contribute to the pin change interrupt
4 different pin changes can be detected
Pin goes low Any logical change Falling edge Rising edge
Interrupt Sense Control Interrupt 1 Sense Control ISC11 ISC10 Description 0 0 0 1 1 0 1 1
Low level on INT1 Any logical change on INT1 Falling edge on INT1 Rising Edge on INT1
Interrupt 0 Sense Control ISC01 ISC00 Description 0 0 0 1 on INT0 1 0 1 1 INT0
Low level on INT0 Any logical change Falling edge on INT0 Rising Edge on
MCU Control Register
Defines which of the four states will activate the interrupt
ex. MCUCR = (1<<ISC01) | (1<<ISC00) Interrupt on pin INT0 falling edge
Example #include <avr/interrupt.h> int main(void) { // set Pin 6 (PD02) as the interrupt pin PCMSK |= (1<<PIND2); // interrupt on INT0 falling edge MCUCR = (1<<ISC01) | (1 << ISC00); // turn on interrupts GIMSK |= (1<<INT0); // wait for interrupt while (1) { ... };