38
08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 1 Programmieren in C Teil 3: Mikrocontrollerprogrammierung

Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

  • Upload
    lediep

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 1

Programmieren in CTeil 3: Mikrocontrollerprogrammierung

Page 2: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 2

Tag 1Hello World

Page 3: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3

Mikrocontroller

Im Gegensatz zum Mikroprozessor Peripherie integriert Speicher Takterzeugung Interrupt-Controller Timer-Baustein Schnittstellen Single-Chip-Betrieb möglich

Angepasster Befehlssatz für Steueraufgaben (in, out)

Wenig Speicher

Geringer Stromverbrauch

Versteckt in Alltagsgeräten (Kaffeemaschine, Handy, Smartcard,...)

Page 4: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 4

ATmega640

Features max. 16 MHz Taktfrequenz (aber fast 16 MIPS!) Hardwaremultiplizierer 64 kB Flash-Speicher (Programmspeicher) 8 kB SRAM 4 kB EEPROM 16-Kanal 10-Bit-ADC 6 Counter 4 Hardware-PWM-Ausgänge 4 USARTs ...

Page 5: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 5

ATmega640

Programm ist im Flash-Speicher abgelegt

Program Counter zeigt auf aktuellen Befehl im Speicher

Recheneinheit(“Arithmetic Logic Unit”, ALU) Führt alle Rechenoperationen

aus (Addieren, Subtrahieren,Shiften,...)

Liest Operanden aus Register und schreibt Ergebnis in Register

Operation wird durch Inhalt des Instruction Registers vorgegeben

Abbildung: Datenblatt ATmega640

Page 6: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 6

myAVR-Board

“Stamp” mit Mikrocontroller

Page 7: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 7

myAVR-Board

Programmieradapter

Page 8: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 8

myAVR-Board

LEDsLEDs

Page 9: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 9

myAVR-Board

Sieben-Segment-Anzeige

Page 10: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 10

myAVR-Board

DIP-Schalter

Page 11: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 11

myAVR-Board

Taster

Page 12: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 12

myAVR-Board

Joystick

Page 13: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 13

myAVR-Board

Lautsprecher

Page 14: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 14

myAVR-Board

Potentiometer

Page 15: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 15

myAVR-Board

Fotosensor

Page 16: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 16

myAVR-Board

Graphikdisplay

Page 17: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 17

Hello World

Ein einfaches Programm Programmierung in Assembler Einbinden von Header-Dateien: .include “<Dateiname>”

Befehle: ldi <reg>, <val> (load immediate) Lädt Wert <val> in Register <reg> out <port>, <reg> (output) Schreibt Inhalt eines Registers in Port rjmp <label> (relative jump) Springt zum Label

Kommentare beginnen mit “;” Labels definieren Abschnitte im Code ende: (Markiert das Ende)

Page 18: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 18

Hello World

Das Programm

.include "m640def.inc"

.org 0x000

; Data Direction Register setzenldi r16, 0xFF ; In Register 16 Wert 0xFF ladenout DDRD, r16 ; Inhalt von r16 in DDRD schreiben

; LED einschaltenldi r16, 0x01 ; In Register 16 Wert 1 ladenout PORTD, r16 ; Inhalt von r16 in PORT D schreiben

ende: rjmp ende ; Endlosschleife

Page 19: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 19

AVR Studio

AVR Studio (IDE) starten Neues Projekt erstellen Atmel AVR Assembler Name des Projekts eintragen AVR Simulator auswählen Atmega640 als Device wählen

Programm schreiben oder .c-Datei laden Assemblieren (F7)

:020000020000FC:0A0000000FEF0AB901E00BB9FFCFC2:00000001FF

Page 20: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 20

Simulation

Simulation (Debugging) starten (Strg-Shift-Alt-F5) Schrittweise den Code ausführen (F11) I/O-View (rechts) zeigt die Änderungen im Controller an

Page 21: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 21

Programm auf myAVR Board MK3 brennen

Alle blauen Jumper öffnen

Programm assemblieren (F7) Hexfile→ Tools Program AVR Connect...→ → STK500 und COM-Port wählen Unter 'Flash' das Input Hexfile auswählen 'Program' wählen, um Hexfile zu brennen

Programm wird sofort ausgeführt Resetknopf startet Programm neu

Page 22: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 22

Hello World in AVR-GCC

Gleiche Funktionalität in C programmieren: Neues Projekt erstellen, wie zuvor Aber 'AVR GCC' auswählen

Programmcode:#include <avr/io.h> // Enthält alle Definitionen

int main(void){ // LED einschalten

DDRD = 0xff; // Setze Port D auf AusgangPORTD = 0x1; // Schreibe 1 an Port D

while(1) ; // Endlosschleife

return 0; // Wird nie erreicht}

Page 23: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 23

Kompilieren und brennen

Kompiliervorgang starten (F7) Hex-File brennen wie zuvor Wichtig: Hex-File muss für jedes Projekt neu ausgewählt werden! STK500 Fenster nicht schließen, sondern minimieren,

dann kann wieder mit programmiert werden Simulation ist auch wie zuvor möglich

Page 24: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 24

Port I/O

Ermöglicht Digitale Ein-/Ausgabe über einzelne Anschlüsse (“Pins”) des Controllers

Pins sind zu Gruppen (“Ports”) zusammengefasst (meist 8er-Gruppen)

Zuschaltbare interne Pull-up-Widerstände Schaltungsprinzip (für Anschluss eines Tasters):

Abbildung: http://www.mikrocontroller.net/articles/AVR-Tutorial:_IO-Grundlagen

Page 25: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 25

Port I/O

Gesteuert über drei Register: Data Direction Register (DDxn) “0” bedeutet Eingang “1” bedeutet Ausgang

Port Input Pins (PINxn) Im Prinzip nur lesbar Schreiben ändert Zustand von DDxn

Data Register (PORTxn) Wert der ausgegeben wird

Page 26: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 26

Port I/O

Globale “Pull-up disable”-Funktion (PUD) im MCUCR-Register

Ausgänge zeigen im Detail folgendes Verhalten:

Tabelle: Datenblatt ATmega640

Page 27: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 27

Port I/O

Bildquelle: Datenblatt ATmega640

Pull-up-Widerstand

Page 28: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 28

Port I/O

Bildquelle: Datenblatt ATmega640

Data Register

Page 29: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 29

Port I/O

Bildquelle: Datenblatt ATmega640

Port Input Register

Page 30: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 30

Port I/O

Bildquelle: Datenblatt ATmega640

Data Direction Register

Page 31: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 31

Bitmanipulationen

8-Bit Register Darstellung im Binär-

system Nach Reset genullt

Setzen des Wertes entweder dezimal oder hexadezimal Bsp: PORTA = 11; oder PORTA = 0xB;

7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0

7 6 5 4 3 2 1 0

0 0 0 0 1 0 1 1

Page 32: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 32

Bit-Tabelle

Eine Stelle hexadezimalentspricht vier Bits Deshalb Umrechnung

recht einfach Selten Benutzung von

Dezimalwerten fürBitmanipulationen

Intuitive Variante:Bitverschiebungen

Dez Hex Binär

0 0x0 0000

1 0x1 0001

2 0x2 0010

3 0x3 0011

4 0x4 0100

5 0x5 0101

6 0x6 0110

7 0x7 0111

8 0x8 1000

9 0x9 1001

10 0xA 1010

11 0xB 1011

12 0xC 1100

13 0xD 1101

14 0xE 1110

15 0xF 1111

Page 33: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 33

Bit-Shifts (<<, >>)

Shift nach links Entspricht Multiplikation

mit 2 Bsp: a = 0x2; a << 1; Von rechts mit 0 gefüllt

Shift nach rechts Entspricht Division

durch 2 Bsp: a = 0x10; (16)

a >> 2; (4) Von links mit 0 gefüllt

7 6 5 4 3 2 1 0

0 0 0 0 0 0 1 0

7 6 5 4 3 2 1 0

0 0 0 0 0 1 0 0

7 6 5 4 3 2 1 0

0 0 0 0 0 1 0 0

7 6 5 4 3 2 1 0

0 0 0 1 0 0 0 0

Page 34: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 34

Bits einzeln setzen

Einfache Zuweisungüberschreibt Zustanddes Registers Bsp: a = 0x4;

a = 0x2;

Kombinierte Operationaus Shift und ODER Setzt ein bestimmtes

Bit und lässt andereunberührt

Bsp: a |= (1 << 4); Setzt Bit Nr. 4

zusätzlich wegen ODER-Verknüpfung

7 6 5 4 3 2 1 0

0 0 0 0 0 1 0 0

7 6 5 4 3 2 1 0

0 0 0 0 0 0 1 0

7 6 5 4 3 2 1 0

0 0 0 1 0 0 1 0

7 6 5 4 3 2 1 0

0 0 0 0 0 0 1 0

Page 35: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 35

Bits einzeln löschen

Einfache Zuweisungüberschreibt Zustanddes Registers Bsp: a = 0x7;

a = 0x0;

Kombinierte Operationaus Shift, NOT und UND Löscht ein bestimmtes

Bit und lässt andereunberührt

Bsp: a = 0x7;a &= ~(1 << 2);

Löscht Bit Nr. 2

7 6 5 4 3 2 1 0

0 0 0 0 0 1 1 1

7 6 5 4 3 2 1 0

0 0 0 0 0 0 0 0

7 6 5 4 3 2 1 0

0 0 0 0 0 0 1 1

7 6 5 4 3 2 1 0

0 0 0 0 0 1 1 1

0 0 0 0 0 1 0 0

1 1 1 1 1 0 1 1

Page 37: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 37

Links (Entwicklungstools)

Für Windows:

WinAVR

AVR Studio

Für Linux:

AVR GCC

avrdude

KontrollerLab (IDE für Linux/KDE)

Sonstiges:

AVR libc Dokumentation

Page 38: Programmieren in C - ikp.tu-darmstadt.de · 08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 3 Mikrocontroller Im Gegensatz zum Mikroprozessor

08/30/10 | Fachbereich Physik | Institut für Kernphysik | Bastian Löher, Martin Konrad | 38

Aufgaben

Zweite und dritte LED an Port D leuchten lassen Mehrere LEDs leuchten lassen LEDs an Port L gezielt leuchten lassen (nur die 4. oder 5., …) Bit setzen: PORTx |= (1 << bit) Bit löschen: PORTx &= ~(1 << bit)

LEDs an Port L hochzählen lassen (binär oder Lauflicht) Variablen werden wie in C definiert, allerdings Typen mit definierter

Größe: uint8_t (8 bit unsigned), uint16_t (16 bit unsigned)

LEDs an Port L mit Delay hochzählen lassen Delay: _delay_ms(millisekunden)

_delay_us(mikrosekunden)

Was passiert wenn man die Endlosschleife am Ende weglässt?