diff --git a/20181203/aufgabe-1.png b/20181203/aufgabe-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e655af0096cc6e50da81c8f820395dfaed27277f Binary files /dev/null and b/20181203/aufgabe-1.png differ diff --git a/20181203/aufgabe-1.xbm b/20181203/aufgabe-1.xbm new file mode 100644 index 0000000000000000000000000000000000000000..0d1c8d4f622cb6b703e2ce0e2f434f5d2f6ad216 --- /dev/null +++ b/20181203/aufgabe-1.xbm @@ -0,0 +1,6 @@ +#define aufgabe_1_width 14 +#define aufgabe_1_height 14 +static unsigned char aufgabe_1_bits[] = { + 0x00, 0x00, 0xf0, 0x03, 0x08, 0x04, 0x04, 0x08, 0x02, 0x10, 0x32, 0x13, + 0x22, 0x12, 0x02, 0x10, 0x0a, 0x14, 0x12, 0x12, 0xe4, 0x09, 0x08, 0x04, + 0xf0, 0x03, 0x00, 0x00 }; diff --git a/20181203/aufgabe-2.c b/20181203/aufgabe-2.c new file mode 100644 index 0000000000000000000000000000000000000000..4c95eb6ad4881c904448309c6b7c46fa101cc109 --- /dev/null +++ b/20181203/aufgabe-2.c @@ -0,0 +1,33 @@ +#include <stdint.h> +#include <avr/io.h> +#include <avr/interrupt.h> + +uint8_t counter = 1; +uint8_t leds = 0; + +ISR (TIMER0_COMP_vect) +{ + if (counter == 0) + { + leds = (leds + 1) % 8; + PORTC = leds << 4; + } + counter++; +} + +void init (void) +{ + cli (); + TCCR0 = (1 << CS01) | (1 << CS00); + TIMSK = 1 << OCIE0; + sei (); + DDRC = 0x70; +} + +int main (void) +{ + init (); + while (1) + ; /* do nothing */ + return 0; +} diff --git a/20181203/hp-uebung-20181203.pdf b/20181203/hp-uebung-20181203.pdf new file mode 100644 index 0000000000000000000000000000000000000000..895ecf5fa9746d6d540b91041aaafe6656af2713 Binary files /dev/null and b/20181203/hp-uebung-20181203.pdf differ diff --git a/20181203/hp-uebung-20181203.tex b/20181203/hp-uebung-20181203.tex new file mode 100644 index 0000000000000000000000000000000000000000..e4a6f0d2fc713ae5a9ceb7ef30ca716b0ef2b5d9 --- /dev/null +++ b/20181203/hp-uebung-20181203.tex @@ -0,0 +1,284 @@ +% hp-uebung-20181203.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: XBM-Grafik, LED-Blinkmuster + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + +% \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 3.\ Dezember 2018} + + Diese Übung enthält Punkteangaben wie in einer Klausur. + Um zu "`bestehen"', müssen Sie innerhalb von 45 Minuten + unter Verwendung ausschließlich zugelassener Hilfsmittel + 8 Punkte (von insgesamt \totalpoints) erreichen. + + \exercise{XBM-Grafik} + + Bei einer XBM-Grafikdatei handelt es sich + um ein als C-Quelltext abgespeichertes Array, + das die Bildinformationen enthält: + \begin{itemize}\itemsep0pt + \item Jedes Bit entspricht einem Pixel. + \item Nullen stehen für Weiß, Einsen für Schwarz. + \item Das Bit mit Zahlenwert 1 steht für einen Bildpunkt ganz links im Byte, + das Bit mit Zahlenwert 128 für einen Bildpunkt ganz rechts. + (Diese Bit-Reihenfolge heißt \newterm{LSB first}.) + \item Jede Zeile des Bildes wird auf ganze Bytes aufgefüllt. + \item Breite und Höhe des Bildes sind als Konstantendefinitionen + (\lstinline{#define}) in der Datei enthalten. + \end{itemize} + Sie können eine XBM-Datei sowohl mit einem Texteditor + als auch mit vielen Grafikprogrammen öffnen und bearbeiten. + + Beispiel (\gitfile{hp}{20171204}{aufgabe-1.xbm}):\hfill + \makebox(0,0)[tr]{\framebox{\includegraphics[scale=3]{aufgabe-1.png}}} + \begin{lstlisting} + #define aufgabe_1_width 14 + #define aufgabe_1_height 14 + static unsigned char aufgabe_1_bits[] = { + 0x00, 0x00, 0xf0, 0x03, 0x08, 0x04, 0x04, 0x08, 0x02, 0x10, 0x32, 0x13, + 0x22, 0x12, 0x02, 0x10, 0x0a, 0x14, 0x12, 0x12, 0xe4, 0x09, 0x08, 0x04, + 0xf0, 0x03, 0x00, 0x00 }; + \end{lstlisting} + Ein C-Programm, das eine XBM-Grafik nutzen will, + kann die \file{.xbm}-Datei mit \lstinline{#include "..."} direkt einbinden. + + Schreiben Sie ein Programm, das die XBM-Datei als ASCII-Grafik ausgibt, z.\,B.: + \begin{lstlisting}[style=terminal,lineskip=-4pt] + + ****** + * * + * * + * * + * ** ** * + * * * * + * * + * * * * + * * * * + * **** * + * * + ****** + ¡ ¿ + \end{lstlisting} + \points{8} + + (Hinweis für die Klausur: Abgabe auf Datenträger ist erlaubt und erwünscht, + aber nicht zwingend.) + + \clearpage + \exercise{LED-Blinkmuster} + + Wir betrachten das folgende Programm für einen ATmega32-Mikro-Controller + (Datei: \gitfile{hp}{20180108}{aufgabe-2.c}). + + \begin{minipage}[t]{7cm} + \begin{lstlisting}[gobble=6] + #include <stdint.h> + #include <avr/io.h> + #include <avr/interrupt.h> + + uint8_t counter = 1; + uint8_t leds = 0; + + ISR (TIMER0_COMP_vect) + { + if (counter == 0) + { + leds = (leds + 1) % 8; + PORTC = leds << 4; + } + counter++; + } + \end{lstlisting} + \end{minipage}\hfill\begin{minipage}[t]{8.5cm} + \begin{lstlisting}[gobble=6] + void init (void) + { + cli (); + TCCR0 = (1 << CS01) | (1 << CS00); + TIMSK = 1 << OCIE0; + sei (); + DDRC = 0x70; + } + + int main (void) + { + init (); + while (1) + ; /* do nothing */ + return 0; + } + \end{lstlisting} + \end{minipage} + + An die Bits Nr.\ 4, 5 und 6 des Output-Ports C des Mikro-Controllers sind LEDs angeschlossen.\\ + Sobald das Programm läuft, blinken diese in charakteristischer Weise: + \begin{quote} + \newcommand{\tdn}[1]{\raisebox{-2pt}{#1}} + \begin{tabular}{|c|c|c|c|}\hline + \tdn{Phase} & \tdn{LED oben (rot)} & \tdn{LED Mitte (gelb)} & \tdn{LED unten (grün)} \\[2pt]\hline + 1 & aus & aus & an \\\hline + 2 & aus & an & aus \\\hline + 3 & aus & an & an \\\hline + 4 & an & aus & aus \\\hline + 5 & an & aus & an \\\hline + 6 & an & an & aus \\\hline + 7 & an & an & an \\\hline + 8 & aus & aus & aus \\\hline + \end{tabular} + \end{quote} + % 8000000 / 64 = 125000 + % 8000000 / 64 / 256 = 488.28125 + % 8000000 / 64 / 256 / 256 = 1.9073486328125 + Jede Phase dauert etwas länger als eine halbe Sekunde. + Nach 8 Phasen wiederholt sich das Schema. + + Erklären Sie das Verhalten des Programms anhand des Quelltextes: + \vspace{-\medskipamount} + \begin{itemize}\itemsep0pt + \item[(a)] + Wieso macht das Programm überhaupt etwas, + wenn doch das Hauptprogramm nach dem Initialisieren lediglich eine Endlosschleife ausführt, + in der \emph{nichts} passiert? + \points{1} + \item[(b)] + Wieso wird die Zeile \lstinline|PORTC = leds << 4;| überhaupt aufgerufen, + wenn dies doch nur unter der Bedingung \lstinline|counter == 0| passiert, + wobei die Variable \lstinline|counter| auf 1 initialisiert, + fortwährend erhöht und nirgendwo zurückgesetzt wird? + \points{2} + \item[(c)] + Wie kommt das oben beschriebene Blinkmuster zustande? + \points{2} + \item[(d)] + Wieso dauert eine Phase ungefähr eine halbe Sekunde? + \points{2} + \item[(e)] + Was bedeutet "`\lstinline|ISR (TIMER0_COMP_vect)|"'? + \points{1} + \end{itemize} + + Hinweis: + \vspace{-\medskipamount} + \begin{itemize}\itemsep0pt + \item + Die Funktion \lstinline|init()| sorgt dafür, daß der Timer-Interrupt Nr.\ 0 des Mikro-Controllers + etwa 488mal pro Sekunde aufgerufen wird. + Außerdem % schaltet sie eventuell an Port B angeschlossene weitere LEDs aus und + initialisiert sie die benötigten Bits an Port C als Output-Ports. + Sie selbst brauchen die Funktion \lstinline|init()| nicht weiter zu erklären. +% \item +% Während der Übung können Sie sich das Verhalten des Programms +% an einem RP6-Roboter vorführen lassen. + \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}{20171127}{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}{20161127}{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}