diff --git a/20181126/hp-uebung-20181126.pdf b/20181126/hp-uebung-20181126.pdf index c1faacd7fd46235ec5e86a0b376a085b2a36d1df..61d9066d148a5acd37eb44d3e26b817dc52bf50e 100644 Binary files a/20181126/hp-uebung-20181126.pdf and b/20181126/hp-uebung-20181126.pdf differ diff --git a/20181126/hp-uebung-20181126.tex b/20181126/hp-uebung-20181126.tex index 0ca78249922a8926c7499db5f40133e49d6fd54c..0985fd9e393c0bef8aac9d3ee42f52dbba1b219d 100644 --- a/20181126/hp-uebung-20181126.tex +++ b/20181126/hp-uebung-20181126.tex @@ -20,7 +20,7 @@ % Attribution-ShareAlike 3.0 Unported License along with this % document. If not, see <http://creativecommons.org/licenses/>. -% README: Zahlensysteme, Mikro-Controller +% README: Zahlensysteme, Mikrocontroller \documentclass[a4paper]{article} @@ -103,10 +103,10 @@ (In der Klausur sind Hilfsmittel zugelassen, daher ist dies \emph{keine\/} typische Klausuraufgabe.) - \exercise{Mikro-Controller} + \exercise{Mikrocontroller} \begin{minipage}[t]{10cm} - An die vier Ports eines ATmega16-Mikro-Controllers sind Leuchtdioden angeschlossen: + An die vier Ports eines ATmega16-Mikrocontrollers sind Leuchtdioden angeschlossen: \begin{itemize} \item von links nach rechts an die Ports A, B, C und D, diff --git a/20181203/400_points_breadboard.jpg b/20181203/400_points_breadboard.jpg new file mode 100644 index 0000000000000000000000000000000000000000..8ba048255851581a84db16c4d5a50802f7521486 Binary files /dev/null and b/20181203/400_points_breadboard.jpg differ diff --git a/20181203/Makefile b/20181203/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7ae33df99f68fcf460324cfbb008f3f7a3863638 --- /dev/null +++ b/20181203/Makefile @@ -0,0 +1,8 @@ +%.elf: %.c + avr-gcc -Wall -Os -mmcu=atmega328p $< -o $@ + +%.hex: %.elf + avr-objcopy -O ihex $< $@ + +download: + ./download.sh diff --git a/20181203/download.sh b/20181203/download.sh new file mode 100755 index 0000000000000000000000000000000000000000..770c3b5dca74ac09778be055c9d6f5adb0df293b --- /dev/null +++ b/20181203/download.sh @@ -0,0 +1,3 @@ +port=$(ls -rt /dev/ttyACM* | tail -1) +echo avrdude -P $port -c arduino -p m328p -U flash:w:$(ls -rt *.hex | tail -1) +avrdude -P $port -c arduino -p m328p -U flash:w:$(ls -rt *.hex | tail -1) 2>/dev/null diff --git a/20181203/hp-20181203.pdf b/20181203/hp-20181203.pdf new file mode 100644 index 0000000000000000000000000000000000000000..b72be290152b4e33116e29069103ad41556e0c26 Binary files /dev/null and b/20181203/hp-20181203.pdf differ diff --git a/20181203/hp-20181203.tex b/20181203/hp-20181203.tex new file mode 100644 index 0000000000000000000000000000000000000000..0a3acf1e1b50922248c20c61362e90e1c04db226 --- /dev/null +++ b/20181203/hp-20181203.tex @@ -0,0 +1,838 @@ +% hp-20181203.pdf - Lecture Slides on Low-Level Programming +% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or under the terms of the +% GNU General Public License as published by the Free Software +% Foundation, either version 3 of the License, or (at your option) +% any later version. +% +% This document is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts, volatile-Variable + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{pdftricks} +\usepackage{tikz} + +\begin{psinputs} + \usepackage[utf8]{inputenc} + \usepackage[german]{babel} + \usepackage[T1]{fontenc} + \usepackage{helvet} + \renewcommand*\familydefault{\sfdefault} + \usepackage{pstricks,pst-grad} +\end{psinputs} + +\title{Hardwarenahe Programmierung} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{3.\ Dezember 2018} + +\begin{document} + +\maketitleframe + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \begin{itemize} + \item[3.1] Der Präprozessor + \item[3.2] Bibliotheken einbinden + \item[3.3] Bibliotheken verwenden + \color{medgreen} + \item[3.4] Projekt organisieren: make + \end{itemize} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \begin{itemize} + \color{orange} + \item[4.1] Bit-Operationen + \color{red} + \item[4.2] Programmierung von Mikrocontrollern + \item[4.3] I/O-Ports + \item[4.4] Interrupts + \item[4.5] volatile-Variable + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \item[5.1] Differentialgleichungen + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\setcounter{section}{2} +\section{Bibliotheken} +\setcounter{subsection}{3} +\subsection{Projekt organisieren: make} + +\begin{frame}[fragile] + + \showsubsection + \begin{itemize} + \item + \only<3->{explizite und implizite} Regeln + \begin{onlyenv}<1> + \smallskip + \begin{lstlisting}[language=make,gobble=10] + philosophy: philosophy.o answer.o + gcc philosophy.o answer.o -o philosophy + + answer.o: answer.c answer.h + gcc -Wall -O answer.c -c + + philosophy.o: philosophy.c answer.h + gcc -Wall -O philosophy.c -c + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<3> + \smallskip + \begin{lstlisting}[language=make,gobble=10] + TARGET = philosophy + OBJECTS = philosophy.o answer.o + HEADERS = answer.h + CFLAGS = -Wall -O + + $(TARGET): $(OBJECTS) + gcc $(OBJECTS) -o $(TARGET) + + %.o: %.c $(HEADERS) + gcc $(CFLAGS) $< -c + + clean: + rm -f $(OBJECTS) $(TARGET) + \end{lstlisting} + \end{onlyenv} + \item + Makros + \begin{onlyenv}<2> + \smallskip + \begin{lstlisting}[language=make,gobble=10] + TARGET = philosophy + OBJECTS = philosophy.o answer.o + HEADERS = answer.h + CFLAGS = -Wall -O + + $(TARGET): $(OBJECTS) + gcc $(OBJECTS) -o $(TARGET) + + answer.o: answer.c $(HEADERS) + gcc $(CFLAGS) answer.c -c + + philosophy.o: philosophy.c $(HEADERS) + gcc $(CFLAGS) philosophy.c -c + + clean: + rm -f $(OBJECTS) $(TARGET) + \end{lstlisting} + \vspace*{-1cm} + \end{onlyenv} + \begin{onlyenv}<4-> + \smallskip + \arrowitem + 3 Sprachen: C, Präprozessor, make + \end{onlyenv} + \end{itemize} + +\end{frame} + +\section{Hardwarenahe Programmierung} +\subsection{Bit-Operationen} +\subsubsection{Zahlensysteme} + +\begin{frame}[fragile] + + \showsection + \vspace*{-\smallskipamount} + \showsubsection + \vspace*{-\medskipamount} + \showsubsubsection + + \begin{tabular}{rlrl} + Basis & Name & Beispiel & Anwendung \\[\smallskipamount] + 2 & Binärsystem & 1\,0000\,0011 & Bit-Operationen \\ + 8 & Oktalsystem & \lstinline,0403, & Dateizugriffsrechte (Unix) \\ + 10 & Dezimalsystem & \lstinline,259, & Alltag \\ + 16 & Hexadezimalsystem & \lstinline,0x103, & Bit-Operationen \\ + 256 & (keiner gebräuchlich) & 0.0.1.3 & IP-Adressen (IPv4) + \end{tabular} + + \bigskip + + \begin{itemize} + \item + Computer rechnen im Binärsystem. + \item + Für viele Anwendungen (z.\,B.\ I/O-Ports, Grafik, \dots) ist es notwendig,\\ + Bits in Zahlen einzeln ansprechen zu können. + \end{itemize} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsubsection + + \begin{tabular}{rlrlrc} + \qquad 000 & \bf 0 \hspace*{1.5cm} & 0000 & \bf 0 & \quad 1000 & \bf 8\\ + 001 & \bf 1 & 0001 & \bf 1 & 1001 & \bf 9\\ + 010 & \bf 2 & 0010 & \bf 2 & 1010 & \bf A\\ + 011 & \bf 3 & 0011 & \bf 3 & 1011 & \bf B\\[\smallskipamount] + 100 & \bf 4 & 0100 & \bf 4 & 1100 & \bf C\\ + 101 & \bf 5 & 0101 & \bf 5 & 1101 & \bf D\\ + 110 & \bf 6 & 0110 & \bf 6 & 1110 & \bf E\\ + 111 & \bf 7 & 0111 & \bf 7 & 1111 & \bf F\\ + \end{tabular} + + \medskip + + \begin{itemize} + \item + Oktal- und Hexadezimalzahlen lassen sich ziffernweise\\ + in Binär-Zahlen umrechnen. + \item + Hexadezimalzahlen sind eine Kurzschreibweise für Binärzahlen,\\ + gruppiert zu jeweils 4 Bits. + \item + Oktalzahlen sind eine Kurzschreibweise für Binärzahlen,\\ + gruppiert zu jeweils 3 Bits. + \item + Trotz Taschenrechner u.\,ä.\ lohnt es sich,\\ + die o.\,a.\ Umrechnungstabelle \textbf{auswendig} zu kennen. + \end{itemize} + +\end{frame} + +\subsubsection{Bit-Operationen in C} + +\begin{frame}[fragile] + + \showsubsubsection + + \begin{tabular}{lll} + C-Operator & Verknüpfung & Anwendung \\[\smallskipamount] + \lstinline,&, & Und & Bits gezielt löschen \\ + \lstinline,|, & Oder & Bits gezielt setzen \\ + \lstinline,^, & Exklusiv-Oder & Bits gezielt invertieren \\ + \lstinline,~, & Nicht & Alle Bits invertieren \\[\smallskipamount] + \lstinline,<<, & Verschiebung nach links & Maske generieren \\ + \lstinline,>>, & Verschiebung nach rechts & Bits isolieren + \end{tabular} + + \bigskip + + Numerierung der Bits: von rechts ab 0 + + \medskip + + \begin{tabular}{ll} + Bit Nr.\ 3 auf 1 setzen: & + \lstinline,a |= 1 << 3;, \\ + Bit Nr.\ 4 auf 0 setzen: & + \lstinline,a &= ~(1 << 4);, \\ + Bit Nr.\ 0 invertieren: & + \lstinline,a ^= 1 << 0;, + \end{tabular} + + \smallskip + + ~~Abfrage, ob Bit Nr.\ 1 gesetzt ist:\quad + \lstinline{if (a & (1 << 1))} + +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \begin{itemize} + \item[3.1] Der Präprozessor + \item[3.2] Bibliotheken einbinden + \item[3.3] Bibliotheken verwenden + \color{medgreen} + \item[3.4] Projekt organisieren: make + \end{itemize} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \begin{itemize} + \color{orange} + \item[4.1] Bit-Operationen + \color{red} + \item[4.2] Programmierung von Mikrocontrollern + \item[4.3] I/O-Ports + \item[4.4] Interrupts + \item[4.5] volatile-Variable + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \item[5.1] Differentialgleichungen + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsubsection + + C-Datentypen für Bit-Operationen: + \smallskip\par + \lstinline{#include <stdint.h>} + \medskip\par + \begin{tabular}{lllll} + & 8 Bit & 16 Bit & 32 Bit & 64 Bit \\ + mit Vorzeichen & \lstinline,int8_t, + & \lstinline,int16_t, + & \lstinline,int32_t, + & \lstinline,int64_t, \\ + ohne Vorzeichen & \lstinline,uint8_t, + & \lstinline,uint16_t, + & \lstinline,uint32_t, + & \lstinline,uint64_t, + \end{tabular} + + \bigskip + \bigskip + + Ausgabe: + \smallskip\par + \begin{lstlisting} + #include <stdio.h> + #include <stdint.h> + #include <inttypes.h> + ... + uint64_t x = 42; + printf ("Die Antwort lautet: %" PRIu64 "\n", x); + \end{lstlisting} + +\end{frame} + +\subsection{Programmierung von Mikrocontrollern} + +\begin{frame}[fragile] + \showsubsection + + Cross-Entwicklungswerkzeuge + \begin{itemize} + \item + laufen auf "`normalem"' Computer,\\ + erzeugen Software für anderen Computer oder Mikrocontroller + \item + Cross-Compiler, -Assembler und -Linker + \begin{lstlisting}[style=cmd,gobble=8] + avr-gcc -Wall -Os -mmcu=atmega328p blink.c -o blink.elf + \end{lstlisting} + \item + Werkzeuge zum Herunterladen\footnote{So ist die Sprechweise.} + auf den Mikrocontroller + \begin{lstlisting}[style=cmd,gobble=8] + avr-objcopy -O ihex blink.elf blink.hex + avrdude -P /dev/ttyACM0 -c arduino -p m328p \ + -U flash:w:blink.hex + \end{lstlisting} + \end{itemize} + \begin{picture}(0,0) + \color{red} + \put(9,-1.2){\makebox(0,0)[bl]{\tikz{\draw[-latex](0,0)--(1,2);}}} + \put(9,-1.3){\makebox(0,0)[t]{\shortstack{"`Es geht in der\\ + nächsten Zeile\\ + weiter."'}}} + \end{picture} + +\end{frame} + +\subsection{I/O-Ports} + +\begin{frame}[fragile] + +% \showsection + \showsubsection + \vspace*{-1.5\medskipamount} + {\large\textbf{\color{structure}4.4\quad Interrupts}} + + \bigskip + + Kommunikation mit externen Geräten + + \bigskip + + \begin{center} + \includegraphics{io-ports-and-interrupts.pdf} + \end{center} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + In Output-Port schreiben = Aktoren ansteuern + + Beispiel: LED + + \medskip + + \begin{lstlisting} + #include <avr/io.h> + ... + DDRC = 0x70; + PORTC = 0x40; + \end{lstlisting} + \begin{picture}(0,0) + \put(3,0.67){\begin{minipage}{3cm} + \color{red}% + binär: 0111\,0000\\ + binär: 0100\,0000 + \end{minipage}} + \put(10,0.67){\makebox(0,0)[r]{\color{red}Herstellerspezifisch!}} + \end{picture} + + \bigskip + + \lstinline{DDR} = Data Direction Register\\ + Bit = 1 für Output-Port\\ + Bit = 0 für Input-Port + + \bigskip + + \emph{Details: siehe Datenblatt und Schaltplan} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Aus Input-Port lesen = Sensoren abfragen + + Beispiel: Taster + + \medskip + + \begin{lstlisting} + #include <avr/io.h> + ... + DDRC = 0xfd; + while (PINC & 0x02 == 0) + ; /* just wait */ + \end{lstlisting} + \begin{picture}(0,0)(-1.5,-0.42) + \put(3,0.67){\begin{minipage}{3cm} + \color{red}% + binär: 1111\,1101\\ + binär: 0000\,0010 + \end{minipage}} + \put(10,0.67){\makebox(0,0)[r]{\color{red}Herstellerspezifisch!}} + \end{picture} + + \bigskip + + \lstinline{DDR} = Data Direction Register\\ + Bit = 1 für Output-Port\\ + Bit = 0 für Input-Port + + \bigskip + + \emph{Details: siehe Datenblatt und Schaltplan} + + \bigskip + + Praktikumsaufgabe: Druckknopfampel + +\end{frame} + +\subsection{Interrupts} + +\begin{frame}[fragile] + + \showsubsection + + Externes Gerät ruft (per Stromsignal) Unterprogramm auf + + Zeiger hinterlegen: "`Interrupt-Vektor"' + + Beispiel: eingebaute Uhr\hfill + \makebox(0,0)[tr]{% + \only<2->{\begin{minipage}[t]{4.7cm} + \vspace*{-0.3cm}% + statt Zählschleife (\lstinline{_delay_ms}):\\ + Hauptprogramm kann\\ + andere Dinge tun + \end{minipage}}% + } + + \medskip + + \begin{lstlisting} + #include <avr/interrupt.h> + + ... + + + ISR (TIMER0B_COMP_vect) + { + PORTD ^= 0x40; + } + \end{lstlisting} + \begin{picture}(0,0) + \color{red} + \put(1.9,3.1){\makebox(0,0)[tr]{\tikz{\draw[-latex](0,0)--(-1.4,-1.0);}}} + \put(2.0,3.2){\makebox(0,0)[l]{"`Dies ist ein Interrupt-Handler."'}} + \put(2.3,2.6){\makebox(0,0)[tr]{\tikz{\draw[-latex](0,0)--(-0.6,-0.55);}}} + \put(2.4,2.6){\makebox(0,0)[l]{Interrupt-Vektor darauf zeigen lassen}} + \end{picture} + + Initialisierung über spezielle Ports: + \lstinline{TCCR0B}, \lstinline{TIMSK0} + + \bigskip + + \emph{Details: siehe Datenblatt und Schaltplan} + + \vspace*{-2.5cm}\hfill + {\color{red}Herstellerspezifisch!}% + \hspace*{1cm} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Externes Gerät ruft (per Stromsignal) Unterprogramm auf + + Zeiger hinterlegen: "`Interrupt-Vektor"' + + Beispiel: Taster\hfill + \makebox(0,0)[tr]{% + \begin{minipage}[t]{4.7cm} + \vspace*{-0.3cm}% + statt \newterm{Busy Waiting\/}:\\ + Hauptprogramm kann\\ + andere Dinge tun + \end{minipage}} + + \medskip + + \begin{lstlisting} + #include <avr/interrupt.h> + ... + + ISR (INT0_vect) + { + PORTD ^= 0x40; + } + \end{lstlisting} + + \medskip + + Initialisierung über spezielle Ports: + \lstinline{EICRA}, \lstinline{EIMSK} + + \bigskip + + \emph{Details: siehe Datenblatt und Schaltplan} + + \vspace*{-2.5cm}\hfill + {\color{red}Herstellerspezifisch!}% + \hspace*{1cm} + +\end{frame} + +\subsection{volatile-Variable} + +\begin{frame}[fragile] + + \showsubsection + + Externes Gerät ruft (per Stromsignal) Unterprogramm auf + + Zeiger hinterlegen: "`Interrupt-Vektor"' + + Beispiel: Taster + + \vspace*{-2.5pt} + + \begin{minipage}[t]{5cm} + \begin{onlyenv}<1> + \begin{lstlisting}[gobble=8] + ¡#include <avr/interrupt.h> + ... + + uint8_t key_pressed = 0; + + ISR (INT0_vect) + { + key_pressed = 1; + }¿ + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<2> + \begin{lstlisting}[gobble=8] + ¡#include <avr/interrupt.h> + ... + + volatile uint8_t key_pressed = 0; + + ISR (INT0_vect) + { + key_pressed = 1; + }¿ + \end{lstlisting} + \end{onlyenv} + \end{minipage}\hfill + \begin{minipage}[t]{6cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + ... + + while (1) + { + while (!key_pressed) + ; /* just wait */ + PORTD ^= 0x40; + key_pressed = 0; + } + return 0; + }¿ + \end{lstlisting} + \end{minipage} + + \pause + \begin{picture}(0,0) + \color{red} + \put(10.3,4.0){\makebox(0,0)[b]{\begin{minipage}{6cm} + \begin{center} + \textbf{volatile}:\\ + Speicherzugriff\\ + nicht wegoptimieren + \end{center} + \end{minipage}}} + \put(10.3,3.95){\makebox(0,0)[tr]{\tikz{\draw[-latex](0,0)--(-0.5,-0.9);}}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Was ist eigentlich \lstinline{PORTD}? + + \bigskip + \pause + + \lstinline[style=cmd]{avr-gcc -Wall -Os -mmcu=atmega328p blink-3.c -E} + + \bigskip + \pause + \lstinline{PORTD = 0x01;}\\ + \textarrow\quad + \lstinline[style=terminal]{(*(volatile uint8_t *)((0x0B) + 0x20)) = 0x01;}\\ + \pause + \begin{picture}(0,2)(0,-1.7) + \color{red} + \put(5.75,0.3){$\underbrace{\rule{2.95cm}{0pt}}_{\mbox{Zahl: \lstinline|0x2B|}}$} + \pause + \put(1.55,0.3){$\underbrace{\rule{4.0cm}{0pt}}_{\mbox{\shortstack[t]{Umwandlung in Zeiger\\ + auf \lstinline|volatile uint8_t|}}}$} + \pause + \put(1.32,-1){\makebox(0,0)[b]{\tikz{\draw[-latex](0,0)--(0,1.3)}}} + \put(1.12,-1.1){\makebox(0,0)[tl]{Dereferenzierung des Zeigers}} + \end{picture} + + \pause + \textarrow\quad + \lstinline|volatile uint8_t|-Variable an Speicheradresse \lstinline|0x2B| + + \pause + \bigskip + \bigskip + + \textarrow\quad + \lstinline|PORTA = PORTB = PORTC = PORTD = 0| ist eine schlechte Idee. + +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \begin{itemize} + \color{medgreen} + \item[4.1] Bit-Operationen + \item[4.2] Programmierung von Mikrocontrollern + \item[4.3] I/O-Ports + \item[4.4] Interrupts + \item[4.5] volatile-Variable + \color{red} + \item[4.6] Byte-Reihenfolge -- Endianness + \item[4.7] Binärdarstellung von Zahlen + \item[4.8] Speicherausrichtung -- Alignment + \end{itemize} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \item[5.1] Differentialgleichungen + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \item[\textbf{\dots}] +% \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} +% \item[\textbf{5}] \textbf{Algorithmen} +% \item[\textbf{6}] \textbf{Ergänzungen und Ausblicke} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\iffalse + +\subsection{Binärdarstellung von Zahlen} + +\begin{frame}[fragile] + + \vspace*{-2pt} + \showsubsection + + Speicher ist begrenzt!\\ + \textarrow\ feste Anzahl von Bits + + \medskip + + 8-Bit-Zahlen ohne Vorzeichen: \lstinline{uint8_t}\\ + \textarrow\ Zahlenwerte von \lstinline{0x00} bis \lstinline{0xff} = 0 bis 255\\ + \pause + \textarrow\ 255 + 1 = 0 + + \pause + \medskip + + 8-Bit-Zahlen mit Vorzeichen: \lstinline{int8_t}\\ + \lstinline{0xff} = 255 ist die "`natürliche"' Schreibweise für $-1$.\\ + \pause + \textarrow\ Zweierkomplement + + \pause + \medskip + + Oberstes Bit = 1: negativ\\ + Oberstes Bit = 0: positiv\\ + \textarrow\ 127 + 1 = $-128$ + +\end{frame} + +\begin{frame}[fragile] + + \vspace*{-2pt} + \showsubsection + + Speicher ist begrenzt!\\ + \textarrow\ feste Anzahl von Bits + + \medskip + + 16-Bit-Zahlen ohne Vorzeichen: + \lstinline{uint16_t}\hfill\lstinline{uint8_t}\\ + \textarrow\ Zahlenwerte von \lstinline{0x0000} bis \lstinline{0xffff} + = 0 bis 65535\hfill 0 bis 255\\ + \textarrow\ 65535 + 1 = 0\hfill 255 + 1 = 0 + + \medskip + + 16-Bit-Zahlen mit Vorzeichen: + \lstinline{int16_t}\hfill\lstinline{int8_t}\\ + \lstinline{0xffff} = 66535 ist die "`natürliche"' Schreibweise für $-1$.\hfill + \lstinline{0xff} = 255 = $-1$\\ + \textarrow\ Zweierkomplement + + \medskip + + Oberstes Bit = 1: negativ\\ + Oberstes Bit = 0: positiv\\ + \textarrow\ 32767 + 1 = $-32768$ + + \bigskip + Literatur: \url{http://xkcd.com/571/} + +\end{frame} + +\begin{frame}[fragile] + + \vspace*{-2pt} + \showsubsection + + Frage: \emph{Für welche Zahl steht der Speicherinhalt \lstinline{0x90a3}?} + + \pause + \smallskip + Antwort: \emph{Das kommt darauf an.} ;--) + + \pause + \bigskip + \begin{tabular}{lrl} + als \lstinline,int8_t,: & $-93$ & (nur unteres Byte, Little-Endian)\\ + als \lstinline,uint8_t,: & $163$ & (nur unteres Byte, Little-Endian)\\ + als \lstinline,int16_t,: & $-28509$\\ + als \lstinline,uint16_t,: & $37027$\\ + \lstinline,int32_t, oder größer: & $37027$ + & (zusätzliche Bytes mit Nullen aufgefüllt) + \end{tabular} + +\end{frame} + +\fi + +\end{document} diff --git a/20181203/hp-2018ws-p3.pdf b/20181203/hp-2018ws-p3.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4ac609957322fd09079c33d4a91d6af4e96b810c Binary files /dev/null and b/20181203/hp-2018ws-p3.pdf differ diff --git a/20181203/hp-2018ws-p3.tex b/20181203/hp-2018ws-p3.tex new file mode 100644 index 0000000000000000000000000000000000000000..03a7961e97d6a027c676fddfae8d0c7d545861c2 --- /dev/null +++ b/20181203/hp-2018ws-p3.tex @@ -0,0 +1,172 @@ +% hp-2018ws-p3.pdf - Labor Notes on Low-Level Programming +% Copyright (C) 2014, 2015, 2016, 2017, 2018 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or under the terms of the +% GNU General Public License as published by the Free Software +% Foundation, either version 3 of the License, or (at your option) +% any later version. +% +% This document is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: Versuch 3, 5.12.2017 und Januar 2018: Druckknopfampel + +\documentclass[a4paper]{article} + +\usepackage{pgscript} +\usepackage{multicol} +\usepackage{amsmath} +\usepackage{sfmath} + +\sloppy +\pagestyle{empty} +\newcommand{\sep}{~$\cdot$~} +\newcommand{\mylicense}{CC-by-sa (Version 3.0) oder GNU GPL (Version 3 oder höher)} + +\begin{document} + + \makebox(0,0.005)[tl]{\includegraphics[scale=0.72]{logo-hochschule-bochum-cvh-text-v2.pdf}}\hfill + \makebox(0,0)[tr]{\includegraphics[scale=0.5]{logo-hochschule-bochum.pdf}} + \par\bigskip\bigskip + \begin{center} + \Large\textbf{Praktikumsversuch 3: Druckknopfampel} + \par\medskip + \normalsize Hardwarenahe Programmierung\sep + Wintersemester 2018/19\sep + Prof.~Dr.~Peter Gerwinski + \end{center} + + Aufgabe: Programmieren Sie einen Mikrocontroller so, + daß er eine Druckknopfampel steuern kann. + + \bigskip + + Hinweise: + \vspace*{-\medskipamount} + + \begin{multicols}{2} + + \begin{itemize} + \item + Normalerweise zeigt eine Druckknopfampel rotes Licht für die + Fußgänger und grünes Licht für die Straße. + \item + Nach Knopfdruck wechselt die Straße über Gelb nach Rot. + Danach bekommen die Fußgänger Grün. + Nach einer gewissen Zeit bekommen die Fußgänger wieder Rot, + und die Straße wechselt über Rot-Gelb wieder zu Grün. + + \bigskip + \item + Als Mikrocontroller-Entwicklungsplattform stellen wir Ihnen + einen \emph{Arduino Uno\/} zur Verfügung. + \item + Software-Entwiklung für den Arduino geschieht üblicherweise + mit Hilfe der Entwicklungswerkzeuge \emph{GCC, binutils und + glibc für AVR} sowie \emph{avrdude\/} für das Herunterladen + des geschriebenen Programms vom PC auf den Mikrocontroller. + + Die Arduino-Entwicklungsumgebung (siehe z.\,B.\ + \url{https://www.arduino.cc/en/Main/Software}) integriert + alle diese Komponenten. + \item + Den Schaltplan des Arduino Uno sowie das Datenblatt des + ATmega328p finden Sie per Web-Recherche. + + \columnbreak + + \item + \lstinline[style=cmd]{avr-gcc -Wall -Os -mmcu=atmega328p \}\\ + \lstinline[style=cmd]{dateiname.c -o dateiname.elf} + + \lstinline[style=cmd]{avr-objcopy -O ihex \}\\ + \lstinline[style=cmd]{dateiname-4.elf dateiname-4.hex} + + \lstinline[style=cmd]{avrdude -P /dev/ttyACM0 -c arduino \}\\ + \lstinline[style=cmd]{-p m328p -U flash:w:dateiname.hex} + + Notfalls: \lstinline[style=cmd]{sudo chmod 666 /dev/ttyACM0} + + \bigskip + \item + Die Verdrahtung erfolgt auf Steckplatinen -- siehe + Abb.~\ref{Steckplatine}. + Die mit roten und blauen Streifen markierten Lochreihen sind + in Längsrichtung elektrisch verbunden, alle anderen in + Querrichtung mit einer Trennung in der Mitte. Üblicherweise + verwendet man die Längsrichtung für die Stromversorgung: Rot + = Versorgungsspannung, Blau = 0\,V. + \item + LEDs sind keine Glühlampen! Sie haben einen Pluspol (länger) + und einen Minuspol (kürzer, Gehäuse abgeflacht), und sie dürfen + \textbf{nur mit Vorwiderstand} betrieben werden, + andernfalls besteht die Gefahr einer Zerstörung der LED + und/oder des Arduino. + \item + Anstelle eines Drucktasters verwenden wir ein Stück Draht. + \item + Bevor Sie Ihre Schaltung unter Strom setzen, lassen Sie + diese \textbf{durch einen Betreuer prüfen}. + \end{itemize} + + \vspace*{-\medskipamount} + + \strut\hfill\emph{Viel Erfolg!}\\[-3.5cm] + + \end{multicols} + + \vspace*{-1.5\bigskipamount} + + \begin{figure}[h] + \begin{minipage}{7.3cm} + \includegraphics[width=8cm]{400_points_breadboard.jpg} + \vspace*{-1cm} + \caption{Steckplatine\label{Steckplatine}} + \vspace*{1cm} + \end{minipage}% + \begin{minipage}{9cm} + \small\raggedright + \vspace*{1cm} + Bildquelle: + \href{https://commons.wikimedia.org/wiki/File:400_points_breadboard.jpg}% + {\nolinkurl{https://commons.wikimedia.org/}\\ + \nolinkurl{wiki/File:400_points_breadboard.jpg}}\\ + Autor: \url{https://www.flickr.com/people/33504192@N00}\\ + Lizenz: CC-BY-SA 2.0 Generic + \end{minipage} + \end{figure} + + \vspace*{-1.5\bigskipamount} +% \vfill + + \begingroup + + \small + + \setlength{\leftskip}{3cm} + + Stand: 1.\ Dezember 2018 + +% Soweit nicht anders angegeben:\\ + Copyright \copyright\ 2014, 2015, 2016, 2017\quad Peter Gerwinski\\ + Lizenz: \mylicense + + Sie können diese Praktikumsunterlagen einschließlich Quelltext +% und Beispielprogramme\\ + herunterladen unter:\\ + \url{https://gitlab.cvh-server.de/pgerwinski/hp} + + \endgroup + +\end{document} diff --git a/20181203/io-ports-and-interrupts.pdf b/20181203/io-ports-and-interrupts.pdf new file mode 120000 index 0000000000000000000000000000000000000000..bcd46f7afb35605b20bdb05637e6de0a039893ec --- /dev/null +++ b/20181203/io-ports-and-interrupts.pdf @@ -0,0 +1 @@ +../common/io-ports-and-interrupts.pdf \ No newline at end of file diff --git a/20181203/logo-hochschule-bochum-cvh-text-v2.pdf b/20181203/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20181203/logo-hochschule-bochum-cvh-text-v2.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum-cvh-text-v2.pdf \ No newline at end of file diff --git a/20181203/logo-hochschule-bochum.pdf b/20181203/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20181203/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20181203/pgscript.sty b/20181203/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20181203/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/20181203/pgslides.sty b/20181203/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20181203/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file diff --git a/script/hp-2018ws.pdf b/script/hp-2018ws.pdf index 27a7a4deadea401f65f577789974dc6ca49b934a..7e5984ff3f3cddf957034b1d6e06caa0feb86cf3 100644 Binary files a/script/hp-2018ws.pdf and b/script/hp-2018ws.pdf differ diff --git a/script/hp-2018ws.tex b/script/hp-2018ws.tex index a1c2f3f9fa4a79a18f47ac4f1a362591170a206a..95c14f213ded265073eebfc23379d370acfc35cf 100644 --- a/script/hp-2018ws.tex +++ b/script/hp-2018ws.tex @@ -252,7 +252,7 @@ normalerweise \emph{keine\/} Gefahr für den Rechner. Moderne PC-Betriebssysteme überwachen die aufgerufenen Programme und beenden sie notfalls mit einer Fehlermeldung ("`Schutzverletzung"'). - Experimente mit Mikro-Controllern, die im Rahmen dieser Lehrveranstaltung stattfinden werden, + Experimente mit Mikrocontrollern, die im Rahmen dieser Lehrveranstaltung stattfinden werden, erfolgen ebenfalls in einer Testumgebung, in der kein Schaden entstehen kann. Bitte nutzen Sie die Gelegenheit, in diesem Rahmen Ihre Programmierkenntnisse zu trainieren, @@ -491,7 +491,7 @@ und bricht das Programm mit einer Fehlermeldung ("`Speicherzugriffsfehler"', "`Schutzverletzung"' o.\,ä.) ab. - Auf einer Plattform ohne derartige Schutzmechanismen (z.\,B.\ einem Mikro-Controller) + Auf einer Plattform ohne derartige Schutzmechanismen (z.\,B.\ einem Mikrocontroller) wird das fehlerhafte Programm hingegen klaglos ausgeführt. Es werden dann sinnlose Texte, die sich zufällig an Speicheradresse Nr.\ 42 befinden, auf dem Standardausgabegerät ausgegeben. @@ -3443,7 +3443,7 @@ Beispiel: \file{Makefile.blink} Die Beispielprogramme \file{blink-\lstinline{*}.c} sind dafür gedacht, - auf einem Mikro-Controller zu laufen. + auf einem Mikrocontroller zu laufen. Der Compiler-Aufruf erfordert zusätzliche Optionen (z.\,B.\ \lstinline[style=cmd]{-Os -mmcu=atmega32}), und es müssen zusätzliche Entwicklungswerkzeuge @@ -3978,7 +3978,7 @@ Die Variable \lstinline|PORTC| ist ein Output-Port. Durch Manipulation einzelner Bits in dieser Variablen - ändert sich die Spannung an den elektrischen "`Beinchen"' des Mikro-Controllers. + ändert sich die Spannung an den elektrischen "`Beinchen"' des Mikrocontrollers. Hierdurch wird die Beschaltung von Elektromotoren umgepolt. (Die Konstanten \lstinline|DIR_L| und \lstinline|DIR_R| sind "`Bitmasken"', @@ -3987,7 +3987,7 @@ \bigskip - Die direkte Ansteuerung von I/O-Ports ist nur auf Mikro-Controllern üblich. + Die direkte Ansteuerung von I/O-Ports ist nur auf Mikrocontrollern üblich. Auf Personal-Computern erfolgt die gesamte Ein- und Ausgabe über Betriebssystem-"`Treiber"'. Anwenderprogramme greifen dort i.\,d.\,R.\ nicht direkt auf I/O-Ports zu.