HSMT / (MRT Teil) 27.10.11 Praktikum / Vorlesung / Dipl.Arbeiten Stephan Zielinski Horbacherstr. 116c Tel. 0241 6009 52302 Labor Goethestr, 52072 Aachen 0241 6009 52129 Büro Eupenerstr. Tel. 174173 Fax 0241 6009 52190 Eupenerstr. email: zielinski@fh-aachen.de Homepage : www.zielinski.fh-aachen.de Vorlesung HSMT (MRT - Teil) ab WS2011/2012 Vorwort Der vorliegende Umdruck richtet sich an Studierende der Studienrichtungen Nachrichtentechnik, Energietechnik und Mechatronik. Kenntnisse der Bool'schen Algebra, sowie ihre Anwendung auf kombinatorische und sequentielle Schaltungen werden vorausgesetzt und in soweit wird auf die entsprechenden Vorlesungen verwiesen. Ziel der Vorlesung und des begleitenden Praktikums ist es, dem Studenten das Grundkonzept, Einsatzgebiete und die Einfachheit von Rechnersystemen darzulegen. Die Stellung von Rechnersystemen als alternative zu diskreten Bauelementen oder programmierbaren Schaltkreisen mit teilweise speziellen Rechnerstrukturen soll im Überblick dargestellt werden. Es kann nicht Sinn dieser Vorlesung sein oberflächlich den Pentium zu beschreiben und an ihm ein unrealistisches Praktikum zu absolvieren. Vielmehr soll es anhand eines simplen, überschaubaren Rechners, möglich sein, alle Funktionen im und um den Rechner herum zu begreifen und im Praktikum zu vertiefen. Der Umdruck hat vorlesungsbegleitend zum Ziel, eine Einführung in die Wirkungsweise, Systemkonzeption und Programmierung zu geben. In dem begleitenden Praktikum kann der Prozessor im Rahmen eines Mikrorechnersystems experimentell studiert werden. im Netz downzuloaden! www.zielinski.fh-aachen.de 111006_Umdruck_MRT.doc Seite 1 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) Inhalt Vorlesung HSMT (MRT - Teil) ab WS2011/2012... 1 1. Der ATMEL ATMega32- Mikrocontroller... 3 1.1 Features des Controllers... 3 1.2 Anschlüsse des Controllers (PDIP40 Praktikumsversion)... 4 1.3 Blockschaltbild ATMega32... 5 1.4 Pin Descriptions ATMega32... 6 1.5 Register Summary ATMega32... 7 1.6 Instruction Set Summary ATMega32... 8 2.0 Praktikumssystem... 10 3.0 Interrupttechnik mit dem ATmega32... 12 Zielinski 2011 Seite 2 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 1. Der ATMEL ATMega32- Mikrocontroller 1.1 Features des Controllers High-performance, Low-power Atmel AVR 8-bit Microcontroller Advanced RISC Architecture 131 Powerful Instructions Most Single-clock Cycle Execution 32 x 8 General Purpose Working Registers Fully Static Operation Up to 16 MIPS Throughput at 16 MHz On-chip 2-cycle Multiplier High Endurance Non-volatile Memory segments 32Kbytes of In-System Self-programmable Flash program memory 1024Bytes EEPROM 2Kbyte Internal SRAM Write/Erase Cycles: 10,000 Flash/100,000 EEPROM Data retention: 20 years at 85 C/100 years at 25 C(1) Optional Boot Code Section with Independent Lock Bits In-System Programming by On-chip Boot Program True Read-While-Write Operation Programming Lock for Software Security JTAG (IEEE std. 1149.1 Compliant) Interface Boundary-scan Capabilities According to the JTAG Standard Extensive On-chip Debug Support Programming of Flash, EEPROM, Fuses, and Lock Bits through the JTAG Interface Peripheral Features Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode Real Time Counter with Separate Oscillator Four PWM Channels 8-channel, 10-bit ADC 8 Single-ended Channels 7 Differential Channels in TQFP Package Only 2 Differential Channels with Programmable Gain at 1x, 10x, or 200x Byte-oriented Two-wire Serial Interface Programmable Serial USART Master/Slave SPI Serial Interface Programmable Watchdog Timer with Separate On-chip Oscillator On-chip Analog Comparator Special Microcontroller Features Power-on Reset and Programmable Brown-out Detection Internal Calibrated RC Oscillator External and Internal Interrupt Sources Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby and Extended Standby I/O and Packages 32 Programmable I/O Lines 40-pin PDIP, 44-lead TQFP, and 44-pad QFN/MLF Operating Voltages 2.7V - 5.5V for ATmega32L 4.5V - 5.5V for ATmega32 Speed Grades 0-8MHz for ATmega32L 0-16MHz for ATmega32 Power Consumption at 1 MHz, 3V, 25 C Active: 1.1mA Idle Mode: 0.35mA Power-down Mode: < 1μA 111006_Umdruck_MRT.doc Seite 3 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) 1.2 Anschlüsse des Controllers (PDIP40 Praktikumsversion) Zielinski 2011 Seite 4 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 1.3 Blockschaltbild ATMega32 111006_Umdruck_MRT.doc Seite 5 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) 1.4 Pin Descriptions ATMega32 VCC GND Digital supply voltage. Ground. 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. Port pins can provide internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability. 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). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B also serves the functions of various special features of the ATmega32. Port C (PC7..PC0) Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port C output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The Port C pins are tri-stated when a reset condition becomes active, even if the clock is not running. 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. The TD0 pin is tri-stated unless TAP states that shift out data are entered. Port C also serves the functions of the JTAG interface. Port D (PD7..PD0) Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port D also serves the functions of various special features of the ATmega32. -RESET XTAL1 XTAL2 AVCC AREF 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. Input to the inverting Oscillator amplifier and input to the internal clock operating circuit. Output from the inverting Oscillator amplifier. 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 low-pass filter. AREF is the analog reference pin for the A/D Converter. Zielinski 2011 Seite 6 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 1.5 Register Summary ATMega32 111006_Umdruck_MRT.doc Seite 7 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) 1.6 Instruction Set Summary ATMega32 Zielinski 2011 Seite 8 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 111006_Umdruck_MRT.doc Seite 9 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) 2.0 Praktikumssystem Zielinski 2011 Seite 10 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 111006_Umdruck_MRT.doc Seite 11 von 13 Zielinski 2011
27.10.11 Vorlesungsumdruck HSMT (MRT - Teil) 3.0 Interrupttechnik mit dem ATmega32 Unter einem Interrupt kann man sich einen durch Hardware ausgelösten Unterprogrammaufruf vorstellen. Aufgrund einer Interruptanforderung wird das laufende Programm unterbrochen und das hierfür vorgesehene Unterprogramm ausgeführt. Danach wird das unterbrochene Programm an der Unterbrechungsstelle wieder fortgesetzt. Interrupts werden bei Mikrocontrollern dazu eingesetzt, um auf äußere und interne Systemzustände augenblicklich reagieren zu können. So kann zum Beispiel in einem gesteuerten Prozess ein gefährlicher Betriebszustand eintreten oder ein interner Prozessorzustand erfordert eine Reaktion, zum Beispiel beim Empfang von Daten an der seriellen Schnittstelle. Ein Programm mit Interruptsteuerung hat folgende Merkmale: Es muss eine Interrupt-Service-Routine vorhanden sein (das auszuführende Unterprogramm) Die Interruptsteuerung muss an Anfang des Hauptprogramms aktiviert worden sein. Die folgenden Ereignisse können einen Interrupt auf einem AVR ATmega32 auslösen, wobei die Reihenfolge der Auflistung auch die Priorität der Interrupts entspricht: Allgemeines über die Interruptabarbeitung Wenn ein Interrupt eintrifft, wird automatisch das Global Interrupt Enable Bit GIE im Status Register SREG gelöscht und alle weiteren Interrupts unterbunden. Zielinski 2011 Seite 12 von 13 111006_Umdruck_MRT.doc
Vorlesungsumdruck HSMT (MRT - Teil) 27.10.11 Obwohl es möglich ist, zu diesem Zeitpunkt bereits wieder das GIE-Bit zu setzen, wird dringend davon abgeraten. Dieses wird nämlich automatisch gesetzt, wenn die Interruptroutine beendet wird. Wenn in der Zwischenzeit weitere Interrupts eintreffen, werden die zugehörigen Interrupt-Bits gesetzt und die Interrupts bei Beendigung der laufenden Interrupt-Routine in der Reihenfolge ihrer Priorität ausgeführt. Dies kann eigentlich nur dann zu Problemen führen, wenn ein hoch priorisierter Interrupt ständig und in kurzer Folge auftritt. Dieser sperrt dann möglicherweise alle anderen Interrupts mit niedrigerer Priorität. Dies ist einer der Gründe, weshalb die Interrupt-Routinen sehr kurz gehalten werden sollen. Interrupts mit dem AVR GCC Compiler (WinAVR) Funktionen zur Interrupt-Verarbeitung werden in der Includedatei interrupt.h der avr-libc zur Verfügung gestellt : #include <avr/interrupt.h> // fuer sei(), cli() und ISR(): Das Makro sei() schaltet die Interrupts ein. Eigentlich wird nichts anderes gemacht, als das Global Interrupt Enable Bit im Status Register gesetzt. Das Makro cli() schaltet die Interrupts aus, oder anders gesagt, das Global Interrupt Enable Bit im Status Register wird gelöscht. Die Interrupt-Service_Routinen heißen alle ISR und haben als Parameter für den betreffenden Interrupt einen Interruptvektor. Mit Hilfe des Interruptvektors sorgt der Compiler dafür, dass die ISR an der richtigen Interrupt- Einsprungadresse im Controller platziert wird. Damit eine ISR aufgerufen werden kann, muss der Interrupt im Hauptprogramm aktiviert werden. Diese Aktivierung besteht aus der Aktivierung des jeweiligen Interrupts und aus der allgemeinen Interruptfreigabe mit sei(); _VECTOR(1) INT0_vect External Interrupt Request 0 _VECTOR(2) INT1_vect External Interrupt Request 1 _VECTOR(3) INT2_vect External Interrupt Request 2 _VECTOR(4) TIMER2_COMP_vect Timer/Counter2 Compare Match _VECTOR(5) TIMER2_OVF_vect Timer/Counter2 Overflow _VECTOR(6) TIMER1_CAPT_vect Timer/Counter1 Capture Event _VECTOR(7) TIMER1_COMPA_vect Timer/Counter1 Compare Match A _VECTOR(8) TIMER1_COMPB_vect Timer/Counter1 Compare Match B _VECTOR(9) TIMER1_OVF_vect Timer/Counter1 Overflow _VECTOR(10) TIMER0_COMP_vect Timer/Counter0 Compare Match _VECTOR(11) TIMER0_OVF_vect Timer/Counter0 Overflow _VECTOR(12) SPI_STC_vect Serial Transfer Complete _VECTOR(13) USART_RXC_vect USART, Rx Complete _VECTOR(14) USART_UDRE_vect USART Data Register Empty _VECTOR(15) USART_TXC_vect USART, Tx Complete _VECTOR(16) ADC_vect ADC Conversion Complete _VECTOR(17) EE_RDY_vect EEPROM Ready _VECTOR(18) ANA_COMP_vect Analog Comparator _VECTOR(19) TWI_vect 2-wire Serial Interface _VECTOR(20) SPM_RDY_vect Store Program Memory Ready 111006_Umdruck_MRT.doc Seite 13 von 13 Zielinski 2011