diff --git a/20181203/hp-20181203.pdf b/20181203/hp-20181203.pdf index 12ffbf8e63fc5e85aa4dd6c525b4a9c8836e1856..577d7a2ace998a6f5d40fcae45febb58084e6315 100644 Binary files a/20181203/hp-20181203.pdf and b/20181203/hp-20181203.pdf differ diff --git a/20181203/hp-20181203.tex b/20181203/hp-20181203.tex index 3ea9739a0530dc66cead837e7f59294e266904b9..ef2ad839a6a90c536b86fb83b317325367f6a468 100644 --- a/20181203/hp-20181203.tex +++ b/20181203/hp-20181203.tex @@ -20,7 +20,7 @@ % 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 +% README: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts \documentclass[10pt,t]{beamer} @@ -583,6 +583,8 @@ \end{frame} +\iffalse + \subsection{volatile-Variable} \begin{frame}[fragile] @@ -699,6 +701,8 @@ \end{frame} +\fi + \nosectionnonumber{\inserttitle} \begin{frame} @@ -717,8 +721,8 @@ \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.5] volatile-Variable \item[4.6] Byte-Reihenfolge -- Endianness \item[4.7] Binärdarstellung von Zahlen \item[4.8] Speicherausrichtung -- Alignment diff --git a/20181210/Makefile b/20181210/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7ae33df99f68fcf460324cfbb008f3f7a3863638 --- /dev/null +++ b/20181210/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/20181210/aufgabe-1.c b/20181210/aufgabe-1.c new file mode 100644 index 0000000000000000000000000000000000000000..2afae773ac2d564771ab6dfb473eed664070d3e2 --- /dev/null +++ b/20181210/aufgabe-1.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <stdint.h> + +int main (void) +{ + uint64_t x = 4262939000843297096; + char *s = &x; + printf ("%s\n", s); + return 0; +} diff --git a/20181210/blink-0.c b/20181210/blink-0.c new file mode 100644 index 0000000000000000000000000000000000000000..b0022c681fd1482ed0a6d9fded7bb0a54699de32 --- /dev/null +++ b/20181210/blink-0.c @@ -0,0 +1,9 @@ +#include <avr/io.h> + +int main (void) +{ + DDRD = 0x40; /* binär: 0100 0000 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-0a.c b/20181210/blink-0a.c new file mode 100644 index 0000000000000000000000000000000000000000..38bc8ab94ce639f21b66d570759414586a1a1b1e --- /dev/null +++ b/20181210/blink-0a.c @@ -0,0 +1,9 @@ +#include <avr/io.h> + +int main (void) +{ + DDRD = 0x7f; /* binär: 0111 1111 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-1.c b/20181210/blink-1.c new file mode 100644 index 0000000000000000000000000000000000000000..6d28dce84f94375094c98479c30a54ace1b2a9d9 --- /dev/null +++ b/20181210/blink-1.c @@ -0,0 +1,18 @@ +#include <avr/io.h> + +#define F_CPU 16000000l +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; + PORTD |= 0x01; + while (1) + { + _delay_ms (500); + PORTD &= ~0x01; + _delay_ms (500); + PORTD |= 0x01; + } + return 0; +} diff --git a/20181210/blink-10.c b/20181210/blink-10.c new file mode 100644 index 0000000000000000000000000000000000000000..1519fd02c731aa0429119d4d142a8c0c9effed98 --- /dev/null +++ b/20181210/blink-10.c @@ -0,0 +1,31 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +#define F_CPU 16000000l +#include <util/delay.h> + +volatile uint8_t key_pressed = 0; + +ISR (INT0_vect) /* PD2 */ +{ + key_pressed = 1; +} + +int main (void) +{ + cli (); + EICRA = 1 << ISC00 | 1 << ISC01; /* INT0: steigende Flanke */ + EIMSK = 1 << INT0; /* INT0 einschalten */ + sei (); + DDRD = 0xfb; /* binär: 1111 1011 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1) + { + while (!key_pressed) + ; /* just wait */ + PORTD ^= 0x40; + key_pressed = 0; + } + return 0; +} diff --git a/20181210/blink-11.c b/20181210/blink-11.c new file mode 100644 index 0000000000000000000000000000000000000000..696b2ed2a5bf07fe08177c3640ecb2db652269d5 --- /dev/null +++ b/20181210/blink-11.c @@ -0,0 +1,32 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +#define F_CPU 16000000l +#include <util/delay.h> + +volatile uint8_t key_pressed = 0; + +ISR (INT0_vect) /* PD2 */ +{ + key_pressed = 1; +} + +int main (void) +{ + cli (); + EICRA = 1 << ISC00 | 1 << ISC01; /* INT0: steigende Flanke */ + EIMSK = 1 << INT0; /* INT0 einschalten */ + sei (); + DDRD = 0xfb; /* binär: 1111 1011 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1) + { + while (!key_pressed) + ; /* just wait */ + _delay_ms (1); + PORTD ^= 0x40; + key_pressed = 0; + } + return 0; +} diff --git a/20181210/blink-2.c b/20181210/blink-2.c new file mode 100644 index 0000000000000000000000000000000000000000..b00eaf522563b1e8fa75f80e784de5b9f1af58e9 --- /dev/null +++ b/20181210/blink-2.c @@ -0,0 +1,16 @@ +#include <avr/io.h> + +#define F_CPU 16000000l +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x02; + PORTD = 0x02; + while (1) + { + _delay_ms (250); + PORTD ^= 0x02; + } + return 0; +} diff --git a/20181210/blink-3.c b/20181210/blink-3.c new file mode 100644 index 0000000000000000000000000000000000000000..5268e7977f0f2a99b2005a81a2fa7560dfea481f --- /dev/null +++ b/20181210/blink-3.c @@ -0,0 +1,17 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; + PORTD = 0x01; + while (1) + { + while ((PIND & 0x02) == 0) + ; /* just wait */ + PORTD ^= 0x01; + } + return 0; +} diff --git a/20181210/blink-3a.c b/20181210/blink-3a.c new file mode 100644 index 0000000000000000000000000000000000000000..c7d9c9994e886973cab41cdfcd08c39fb978c492 --- /dev/null +++ b/20181210/blink-3a.c @@ -0,0 +1,18 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x11; + PORTD = 0x11; + while (1) + { + while ((PIND & 0x02) == 0) + ; /* just wait */ + _delay_ms (100); + PORTD ^= 0x01; + } + return 0; +} diff --git a/20181210/blink-4.c b/20181210/blink-4.c new file mode 100644 index 0000000000000000000000000000000000000000..7344aa7ce19086d6e0a0dc4f8de78499f280931b --- /dev/null +++ b/20181210/blink-4.c @@ -0,0 +1,18 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; + PORTD = 0x01; + while (1) + { + while ((PIND & 0x02) == 0) + ; /* just wait */ + PORTD ^= 0x01; + _delay_ms (200); + } + return 0; +} diff --git a/20181210/blink-4a.c b/20181210/blink-4a.c new file mode 100644 index 0000000000000000000000000000000000000000..f17e333034cce9d3dea78c3b9deb6d8832777212 --- /dev/null +++ b/20181210/blink-4a.c @@ -0,0 +1,18 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; /* 00000001 */ + PORTD = 0x03; /* 00000011 */ + while (1) + { + while ((PIND & 0x02) == 0) + ; /* just wait */ + PORTD ^= 0x01; + _delay_ms (200); + } + return 0; +} diff --git a/20181210/blink-4b.c b/20181210/blink-4b.c new file mode 100644 index 0000000000000000000000000000000000000000..afa99e919b37cf9824f671293313935bf77148f4 --- /dev/null +++ b/20181210/blink-4b.c @@ -0,0 +1,13 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; /* 00000001 */ + PORTD = 0x03; /* 00000011 */ + while (1) + ; /* just wait */ + return 0; +} diff --git a/20181210/blink-4c.c b/20181210/blink-4c.c new file mode 100644 index 0000000000000000000000000000000000000000..a88c76b78c9c73a904dacfff9b60988785bc7653 --- /dev/null +++ b/20181210/blink-4c.c @@ -0,0 +1,16 @@ +#include <avr/io.h> + +#define F_CPU 16000000 +#include <util/delay.h> + +int main (void) +{ + DDRD = 0x01; /* 00000001 */ + PORTD = 0x03; /* 00000011 */ + while (1) + { + _delay_ms (500); + PORTD ^= 0x02; + } + return 0; +} diff --git a/20181210/blink-5.c b/20181210/blink-5.c new file mode 100644 index 0000000000000000000000000000000000000000..bb755f0de02d3e224909f1d2a37789f3c14a0f03 --- /dev/null +++ b/20181210/blink-5.c @@ -0,0 +1,19 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +ISR (TIMER0_COMPB_vect) +{ + PORTD ^= 0x40; +} + +int main (void) +{ + cli (); + TCCR0B = (1 << CS01) | (1 << CS00); /* Takt durch 64 dividieren */ + TIMSK0 = 1 << OCIE0B; /* Interrupt einschalten */ + sei (); + DDRD = 0xfd; /* binär: 1111 1101 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-5a.c b/20181210/blink-5a.c new file mode 100644 index 0000000000000000000000000000000000000000..9f26365fdb65006892febe54b329612af7f9263d --- /dev/null +++ b/20181210/blink-5a.c @@ -0,0 +1,15 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +ISR (TIMER0_COMPB_vect) +{ + PORTD ^= 0x40; +} + +int main (void) +{ + DDRD = 0xfd; /* binär: 1111 1101 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-5b.c b/20181210/blink-5b.c new file mode 100644 index 0000000000000000000000000000000000000000..b3835f9bc88dbc0fd9c2f4921356f974e88aeef8 --- /dev/null +++ b/20181210/blink-5b.c @@ -0,0 +1,15 @@ +#include <avr/io.h> +#include <avr/interrupt.h> + +ISR (TIMER0_COMPB_vect) +{ + PORTD ^= 0x40; +} + +int main (void) +{ + DDRD = 0xfd; /* binär: 1111 1101 */ + PORTD = 0x00; /* binär: 0000 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-6.c b/20181210/blink-6.c new file mode 100644 index 0000000000000000000000000000000000000000..651ab6e4ac926242337a0520c11f2bbd935bdd22 --- /dev/null +++ b/20181210/blink-6.c @@ -0,0 +1,22 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +ISR (TIMER0_COMPB_vect) +{ + static uint8_t counter = 0; + if (counter++ == 0) + PORTD ^= 0x40; +} + +int main (void) +{ + cli (); + TCCR0B = (1 << CS01) | (1 << CS00); /* Takt durch 64 dividieren */ + TIMSK0 = 1 << OCIE0B; /* Interrupt einschalten */ + sei (); + DDRD = 0xfd; /* binär: 1111 1101 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-6a.c b/20181210/blink-6a.c new file mode 100644 index 0000000000000000000000000000000000000000..7f62899349ccddad1777a7c933f1199332e7bab7 --- /dev/null +++ b/20181210/blink-6a.c @@ -0,0 +1,23 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +ISR (TIMER0_COMPB_vect) +{ + static uint8_t counter = 0; + if (counter++ == 0) + PORTD ^= 0x40; +} + +int main (void) +{ + cli (); + TCCR0B = (1 << CS01) | (1 << CS00); /* Takt durch 64 dividieren */ + TIMSK0 = 1 << OCIE0B; /* Interrupt einschalten */ + sei (); + DDRD = 0xfd; /* binär: 1111 1101 */ + PORTD = 0x40; /* binär: 0100 0000 */ + cli (); + while (1); + return 0; +} diff --git a/20181210/blink-7.c b/20181210/blink-7.c new file mode 100644 index 0000000000000000000000000000000000000000..7ed39822752f61b636f001b77eb3742a57e953a9 --- /dev/null +++ b/20181210/blink-7.c @@ -0,0 +1,20 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +ISR (INT0_vect) /* PD2 */ +{ + PORTD ^= 0x40; +} + +int main (void) +{ + cli (); + EICRA = 1 << ISC00 | 1 << ISC01; /* INT0: steigende Flanke */ + EIMSK = 1 << INT0; /* INT0 einschalten */ + sei (); + DDRD = 0xfb; /* binär: 1111 1011 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-8.c b/20181210/blink-8.c new file mode 100644 index 0000000000000000000000000000000000000000..aba94f07176a75656619d1ba09e83093cbc66c89 --- /dev/null +++ b/20181210/blink-8.c @@ -0,0 +1,20 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +ISR (INT0_vect) /* PD2 */ +{ + PORTD ^= 0x40; +} + +int main (void) +{ + cli (); + EICRA = 1 << ISC00 | 1 << ISC01; /* INT0: steigende Flanke */ + EIMSK = 1 << INT0; /* INT0 einschalten */ + sei (); + DDRD = 0xff; /* binär: 1111 1111 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1); + return 0; +} diff --git a/20181210/blink-9.c b/20181210/blink-9.c new file mode 100644 index 0000000000000000000000000000000000000000..0935978651f8a19197904a6f75f73e40a9be825b --- /dev/null +++ b/20181210/blink-9.c @@ -0,0 +1,31 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +#define F_CPU 16000000l +#include <util/delay.h> + +uint8_t key_pressed = 0; + +ISR (INT0_vect) /* PD2 */ +{ + key_pressed = 1; +} + +int main (void) +{ + cli (); + EICRA = 1 << ISC00 | 1 << ISC01; /* INT0: steigende Flanke */ + EIMSK = 1 << INT0; /* INT0 einschalten */ + sei (); + DDRD = 0xfb; /* binär: 1111 1011 */ + PORTD = 0x40; /* binär: 0100 0000 */ + while (1) + { + while (!key_pressed) + ; /* just wait */ + PORTD ^= 0x40; + key_pressed = 0; + } + return 0; +} diff --git a/20181210/download.sh b/20181210/download.sh new file mode 100755 index 0000000000000000000000000000000000000000..770c3b5dca74ac09778be055c9d6f5adb0df293b --- /dev/null +++ b/20181210/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/20181210/hp-20181210.pdf b/20181210/hp-20181210.pdf new file mode 100644 index 0000000000000000000000000000000000000000..bfef43119db2e0de5aeee917ccdc3846cd41200f Binary files /dev/null and b/20181210/hp-20181210.pdf differ diff --git a/20181210/hp-20181210.tex b/20181210/hp-20181210.tex new file mode 100644 index 0000000000000000000000000000000000000000..5286d8be04d90998924580d982fae5bcc52969ea --- /dev/null +++ b/20181210/hp-20181210.tex @@ -0,0 +1,873 @@ +% hp-20181210.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: volatile-Variable, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment + +\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{10.\ 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} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \begin{itemize} + \item[4.1] Bit-Operationen + \color{medgreen} + \item[4.2] Programmierung von Mikrocontrollern + \item[4.3] I/O-Ports + \item[4.4] Interrupts + \color{red} + \item[4.5] volatile-Variable + \item[4.6] Byte-Reihenfolge -- Endianness + \item[4.7] Binärdarstellung negativer 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}] + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\setcounter{section}{3} +\section{Hardwarenahe Programmierung} +\subsection{Bit-Operationen} +\subsubsection{Zahlensysteme} + +\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} + +\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<1->{\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} + +\subsection{Byte-Reihenfolge -- Endianness} +\subsubsection{Konzept} + +\begin{frame}[fragile] + + \showsubsection + \showsubsubsection + + Eine Zahl geht über mehrere Speicherzellen.\\ + Beispiel: 16-Bit-Zahl in 2 8-Bit-Speicherzellen + + \smallskip + + Welche Bits liegen wo? + + \pause + \bigskip + + $1027 = 1024 + 2 + 1 = 0000\,0100\,0000\,0011_2 = 0403_{16}$ + + \pause + \bigskip + Speicherzellen: + + \medskip + \begin{tabular}{|c|c|l}\cline{1-2} + \raisebox{-0.25ex}{04} & \raisebox{-0.25ex}{03} & \strut Big-Endian "`großes Ende zuerst"' \\\cline{1-2} + \multicolumn{2}{c}{} & \pause für Menschen leichter lesbar \pause \\ + \multicolumn{3}{c}{} \\[-5pt]\cline{1-2} + \raisebox{-0.25ex}{03} & \raisebox{-0.25ex}{04} & \strut Little-Endian "`kleines Ende zuerst"' \\\cline{1-2} + \multicolumn{2}{c}{} & \pause bei Additionen effizienter + \end{tabular} + + \pause + \medskip + \textarrow\ Geschmackssache + \pause\\ + \quad\textbf{\dots\ außer bei Datenaustausch!} + +% \pause +% \bigskip +% +% Aber: nicht verwechseln! \qquad $0304_{16} = 772$ + +\end{frame} + +\begin{frame} + + \showsubsection + \showsubsubsection + + Eine Zahl geht über mehrere Speicherzellen.\\ + Beispiel: 16-Bit-Zahl in 2 8-Bit-Speicherzellen + + \smallskip + + Welche Bits liegen wo? + + \medskip + + \textarrow\ Geschmackssache\\ + \textbf{\dots\ außer bei Datenaustausch!} + + \begin{itemize} + \item + Dateiformate + \item + Datenübertragung + \end{itemize} + +\end{frame} + +\subsubsection{Dateiformate} + +\begin{frame} + + \showsubsection + \showsubsubsection + + Audio-Formate: Reihenfolge der Bytes in 16- und 32-Bit-Zahlen + \begin{itemize} + \item + RIFF-WAVE-Dateien (\file{.wav}): Little-Endian + \item + Au-Dateien (\file{.au}): Big-Endian + \pause + \item + ältere AIFF-Dateien (\file{.aiff}): Big-Endian + \item + neuere AIFF-Dateien (\file{.aiff}): Little-Endian + \end{itemize} + + \pause + \bigskip + + Grafik-Formate: Reihenfolge der Bits in den Bytes + \begin{itemize} + \item + PBM-Dateien: Big-Endian\only<4->{, MSB first} + \item + XBM-Dateien: Little-Endian\only<4->{, LSB first} + \end{itemize} + \only<4->{MSB/LSB = most/least significant bit} + +\end{frame} + +\subsubsection{Datenübertragung} + +\begin{frame} + + \showsubsection + \showsubsubsection + + \begin{itemize} + \item + RS-232 (serielle Schnittstelle): LSB first + \item + I$^2$C: MSB first + \item + USB: beides + \pause + \medskip + \item + Ethernet: LSB first + \item + TCP/IP (Internet): Big-Endian + \end{itemize} + +\end{frame} + +\subsection{Binärdarstellung negativer Zahlen} + +\begin{frame}[fragile] + + \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] + + \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] + + \showsubsection + + Frage: \emph{Für welche Zahl steht der Speicherinhalt\, + \raisebox{2pt}{% + \tabcolsep0.25em + \begin{tabular}{|c|c|}\hline + \rule{0pt}{11pt}a3 & 90 \\\hline + \end{tabular}} + (hexadezimal)?} + + \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} + +\subsection{Speicherausrichtung -- Alignment} + +\begin{frame}[fragile] + + \showsubsection + + \begin{lstlisting} + #include <stdint.h> + + uint8_t a; + uint16_t b; + uint8_t c; + \end{lstlisting} + + \pause + \bigskip + + Speicheradresse durch 2 teilbar -- "`16-Bit-Alignment"' + \begin{itemize} + \item + 2-Byte-Operation: effizienter + \pause + \item + \dots\ oder sogar nur dann erlaubt + \pause + \arrowitem + Compiler optimiert Speicherausrichtung + \end{itemize} + + \medskip + + \pause + \begin{minipage}{3cm} + \begin{lstlisting}[gobble=6] + ¡uint8_t a; + uint8_t dummy; + uint16_t b; + uint8_t c;¿ + \end{lstlisting} + \end{minipage} + \pause + \begin{minipage}{3cm} + \begin{lstlisting}[gobble=6] + ¡uint8_t a; + uint8_t c; + uint16_t b;¿ + \end{lstlisting} + \end{minipage} + + \pause + \vspace{-1.75cm} + \strut\hfill + \begin{minipage}{6.5cm} + Fazit: + \begin{itemize} + \item + \textbf{Adressen von Variablen\\ + sind systemabhängig} + \item + Bei Definition von Datenformaten\\ + Alignment beachten \textarrow\ effizienter + \end{itemize} + \end{minipage} + +\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 + \color{red} + \item[4.5] volatile-Variable + \item[4.6] Byte-Reihenfolge -- Endianness + \item[4.7] Binärdarstellung negativer 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}] + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\end{document} diff --git a/20181210/hp-uebung-20181210.pdf b/20181210/hp-uebung-20181210.pdf new file mode 100644 index 0000000000000000000000000000000000000000..1fd38c16a4f70c53035081bd4f76962ea7c928db Binary files /dev/null and b/20181210/hp-uebung-20181210.pdf differ diff --git a/20181210/hp-uebung-20181210.tex b/20181210/hp-uebung-20181210.tex new file mode 100644 index 0000000000000000000000000000000000000000..5d59492749ff8d9ae2651ee57b7f74e16cf2f89d --- /dev/null +++ b/20181210/hp-uebung-20181210.tex @@ -0,0 +1,202 @@ +% hp-uebung-20181210.pdf - Exercises on Low-Level Programming / Applied Computer Sciences +% Copyright (C) 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: Trickprogrammierung, Thermometer-Baustein an I²C-Bus + +\documentclass[a4paper]{article} + +\usepackage{pgscript} +\usepackage{gensymb} + +\newcommand{\ItwoC}{I\raisebox{0.5ex}{\footnotesize 2}C} +\newcommand{\ITWOC}{I\raisebox{0.5ex}{\normalsize 2}C} + +\begin{document} + + \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 10.\ Dezember 2018} + + Diese Übung enthält Punkteangaben wie in einer Klausur. + Um zu "`bestehen"', müssen Sie innerhalb von 60 Minuten + unter Verwendung ausschließlich zugelassener Hilfsmittel + 10 Punkte (von insgesamt \totalpoints) erreichen. + + \exercise{Trickprogrammierung} + + Wir betrachten das folgende Programm (Datei: \gitfile{hp}{20181210}{aufgabe-1.c}): + \begin{lstlisting} + #include <stdio.h> + #include <stdint.h> + + int main (void) + { + uint64_t x = 4262939000843297096; + char *s = &x; + printf ("%s\n", s); + return 0; + } + \end{lstlisting} + Das Programm wird compiliert und auf einem 64-Bit-Little-Endian-Computer ausgeführt: + \begin{lstlisting}[style=terminal] + $ ¡gcc -Wall -O aufgabe-1.c -o aufgabe-1¿ + aufgabe-1.c: In function `main': + aufgabe-1.c:7:13: warning: initialization from incompatible pointer type [...] + $ ¡./aufgabe-1¿ + Hallo + \end{lstlisting} + + \begin{itemize} + \item[(a)] + Erklären Sie die Warnung beim Compilieren. \points{2} + \item[(b)] + Erklären Sie die Ausgabe des Programms. \points{5} + \item[(c)] + Wie würde die Ausgabe des Programms auf einem 64-Bit-Big-Endian-Computer lauten? \points{3} + \end{itemize} + Hinweis: Modifizieren Sie das Programm + und lassen Sie sich Speicherinhalte ausgeben. + + \exercise{Thermometer-Baustein an \ITWOC-Bus} + + Eine Firma stellt einen elektronischen Thermometer-Baustein her, + den man über die serielle Schnittstelle (RS-232) an einen PC anschließen kann, + um die Temperatur auszulesen. + Nun wird eine Variante des Thermo"-meter-Bausteins entwickelt, + die die Temperatur zusätzlich über einen \ItwoC-Bus bereitstellt. + + Um das neue Thermometer zu testen, wird es in ein Gefäß mit heißem Wasser gelegt, + das langsam auf Zimmertemperatur abkühlt. + Alle 10 Minuten liest ein Programm, das auf dem PC läuft, + die gemessene Temperatur über beide Schnittstellen aus + und erzeugt daraus die folgende Tabelle: + + \begin{center} + \renewcommand{\arraystretch}{1.2} + \begin{tabular}{|c|c|c|}\hline + Zeit /\,min. & Temperatur per RS-232 /\,\degree C & Temperatur per \ItwoC\ /\,\degree C \\\hline\hline + \phantom{0}0 & 94 & 122 \\\hline + 10 & 47 & 244 \\\hline + 20 & 30 & 120 \\\hline + 30 & 24 & \phantom{0}24 \\\hline + 40 & 21 & 168 \\\hline + \end{tabular} + \end{center} + + \begin{itemize} + \item[(a)] + Aus dem Vergleich der Meßdaten läßt sich + auf einen Fehler bei der \ItwoC-Übertragung schließen.\\ + Um welchen Fehler handelt es sich, + und wie ergibt sich dies aus den Meßdaten? + \points{5} + \item[(b)] + Schreiben Sie eine C-Funktion \lstinline{uint8_t repair (uint8_t data)}, + die eine über den \ItwoC-Bus empfangene fehlerhafte Temperatur \lstinline{data} korrigiert. + \points{5} + \end{itemize} + +\iffalse + + \exercise{Lauflicht} + + \begin{minipage}[t]{8cm} + 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, + \item + von oben nach unten\\ + an die Bits Nr.\ 0 bis 7. + + \bigskip + + \includegraphics[width=3cm]{leds.jpg} + \end{itemize} + \end{minipage}\hfill + \begin{minipage}[t]{7cm} + Wir betrachten das folgende C-Programm (Datei: \gitfile{hp}{20181210}{aufgabe-2.c}) + für diesen Mikrocontroller: + + \begin{lstlisting}[gobble=6] + #include <avr/io.h> + #include <avr/interrupt.h> + + int counter = 0; + + ISR (TIMER0_COMP_vect) + { + PORTA = 1 << ((counter++ >> 6) & 7); + } + + int main (void) + { + cli (); + TCCR0 = (1 << CS01) | (1 << CS00); + TIMSK = 1 << OCIE0; + sei (); + DDRA = 0xff; + while (1); + return 0; + } + \end{lstlisting} + \end{minipage} + + \medskip + + Das Programm bewirkt ein periodisches Lauflicht in der linken Spalte von oben nach unten. + Eine Animation davon finden Sie in der Datei \gitfile{hp}{20181210}{aufgabe-2.gif}. + + \begin{itemize} + \item[(a)] + Wieso bewirkt das Programm überhaupt etwas, wenn doch das Hauptprogramm + nach dem Initialisieren lediglich eine Endlosschleife ausführt, + in der \emph{nichts\/} passiert? \points 3 + \item[(b)] + Erklären Sie, wie die Anweisung + \begin{lstlisting}[gobble=8] + PORTA = 1 << ((counter++ >> 6) & 7); + \end{lstlisting} + \vspace{-\medskipamount} + das LED-Blinkmuster hervorruft. \points 6 + + Hinweis: Zerlegen Sie die eine lange Anweisung in mehrere kürzere.\\ + Wenn nötig, verwenden Sie zusätzliche Variable für Zwischenergebnisse. + \item[(c)] + Was bedeutet "`\lstinline{ISR (TIMER0_COMP_vect)}"'? \points 1 + \item[(d)] + Wieso leuchten die Leuchtdioden PB2 und PD1? \points 2 + \end{itemize} + +\fi + + \begin{flushright} + \textit{Viel Erfolg!} + \end{flushright} + + \makeatletter + \immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}} + \makeatother + +\end{document} diff --git a/20181210/io-ports-and-interrupts.pdf b/20181210/io-ports-and-interrupts.pdf new file mode 120000 index 0000000000000000000000000000000000000000..bcd46f7afb35605b20bdb05637e6de0a039893ec --- /dev/null +++ b/20181210/io-ports-and-interrupts.pdf @@ -0,0 +1 @@ +../common/io-ports-and-interrupts.pdf \ No newline at end of file diff --git a/20181210/logo-hochschule-bochum-cvh-text-v2.pdf b/20181210/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20181210/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/20181210/logo-hochschule-bochum.pdf b/20181210/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20181210/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20181210/pgscript.sty b/20181210/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20181210/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/20181210/pgslides.sty b/20181210/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20181210/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file diff --git a/README.md b/README.md index 98ff30fdc61aff8039b9d6337a8d316cbf71a63c..21b4f85b1ea7185ccace3c6c3c0a6c6b5a10967d 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ Vortragsfolien: * [12.11.2018: Einführung: Bibliotheken, Differentialgleichungen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181112/hp-20181112.pdf) * [19.11.2018: Präprozessor-Makros, Bibliothek verwenden (Beispiel: GTK+), Differentialgleichungen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181119/hp-20181119.pdf) * [26.11.2018: make; Hardwarenahe Programmierung: Zahlensysteme, Bit-Operationen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181126/hp-20181126.pdf) - * [03.12.2018: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts, volatile-Variable](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181203/hp-20181203.pdf) + * [03.12.2018: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181203/hp-20181203.pdf) + * [10.12.2018: volatile-Variable, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181210/hp-20181210.pdf) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/hp-slides-2018ws.pdf) Übungsaufgaben: @@ -39,6 +40,7 @@ Vortragsfolien: * [19.11.2018: Arrays mit Zahlen, hüpfender Ball](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181119/hp-uebung-20181119.pdf) * [26.11.2018: Zahlensysteme, Mikrocontroller](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181126/hp-uebung-20181126.pdf) * [03.12.2018: XBM-Grafik, LED-Blinkmuster](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181203/hp-uebung-20181203.pdf) + * [10.12.2018: Trickprogrammierung, Thermometer-Baustein an I²C-Bus](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181210/hp-uebung-20181210.pdf) Musterlösungen: --------------- diff --git a/hp-slides-2018ws.pdf b/hp-slides-2018ws.pdf index c77dd4354f802225018dbe94f0d9943f0ba09f35..cdc39ee27d169e8c84d84022161f8d28e9a4de78 100644 Binary files a/hp-slides-2018ws.pdf and b/hp-slides-2018ws.pdf differ diff --git a/hp-slides-2018ws.tex b/hp-slides-2018ws.tex index 14b3d8285cf8a044a680febf12aaa947016453ef..2a2e8ee1b71924318513aa3dd8bdee4ce7331269 100644 --- a/hp-slides-2018ws.tex +++ b/hp-slides-2018ws.tex @@ -26,6 +26,8 @@ \includepdf[pages=-]{20181119/hp-20181119.pdf} \pdfbookmark[1]{26.11.2018: make; Hardwarenahe Programmierung: Zahlensysteme, Bit-Operationen}{20181126} \includepdf[pages=-]{20181126/hp-20181126.pdf} - \pdfbookmark[1]{03.12.2018: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts, volatile-Variable}{20181203} + \pdfbookmark[1]{03.12.2018: Bit-Operationen, Programmierung von Mikrocontrollern, I/O-Ports, Interrupts}{20181203} \includepdf[pages=-]{20181203/hp-20181203.pdf} + \pdfbookmark[1]{10.12.2018: volatile-Variable, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment}{20181210} + \includepdf[pages=-]{20181210/hp-20181210.pdf} \end{document}