Sign in Join
Wiki copytemporary

AVR Embedded Tutorial - I²C External-Clock/de: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: AVR Embedded Tutorial - I²C External-Clock/de

OldNewLine
1+# I²C Externe Uhr DS3231
2+
3+## Vorwort
4+
5+Dieser Code ist für einen **Atmega328/Arduino** mit **16MHz** und einem Uhrenbaustein **DS3231**.
6+
7+Die folgende Funktionen, sind in folgenden Tutorials beschrieben:
8+
9+- [[AVR Embedded Tutorial - Delays/de|Delay]]
10+- [[AVR Embedded Tutorial - UART/de|UART, Serielle Schnittstelle]]
11+- [[AVR Embedded Tutorial - I²C, TWI/de|I²C-Bus ansteuern]] oder [[AVR Embedded Tutorial - Software I2C, TWI/de]]
12+
13+## Beschreibung
14+
15+Dieses Beispiel zeigt, wie man den Uhrenbaustein DS3231 anspricht, welcher am **I²C-Bus** hängt.
16+
17+**PulUp**-Widerstände sind **nötig**, der DS3231 enthält **keine**.
18+
19+## Funktionen für die DS3231 Ansteuerung.
20+
21+### Konstanten
22+
23+``` pascal
24+const
25+ I2CAddr = $68; // Defaultadresse des DS3231.
26+ TWI_Write = 0;
27+ TWI_Read = 1;
28+```
29+
30+Record für die Zeitspeicherung.
31+
32+``` pascal
33+type
34+ TDate = record
35+ year: uint16;
36+ month, day, hour, minute, second, dayOfTheWeek: uint8;
37+ end;
38+var
39+ Date: TDate;
40+```
41+
42+### Funktionen
43+
44+Umwandlung von BCD --\> BIN, BIN --\> BCD.
45+
46+Dies wird benötigt, das der im BCD-Format angesprochen wird.
47+
48+``` pascal
49+ function bcd2bin(val: uint8): uint8;
50+ begin
51+ Result := val - 6 * (val shr 4);
52+ end;
53+
54+ function bin2bcd(val: uint8): uint8;
55+ begin
56+ Result := val + 6 * (val div 10);
57+ end;
58+```
59+
60+Baustein mit einer Zeit beschreiben. Dabei sieht man, das man das Format umwandeln muss.
61+
62+``` pascal
63+ procedure WriteDS3231(addr: UInt16);
64+ begin
65+ TWIStart((addr shl 1) or TWI_Write);
66+ TWIWrite(0);
67+ TWIWrite(bin2bcd(Date.second));
68+ TWIWrite(bin2bcd(Date.minute));
69+ TWIWrite(bin2bcd(Date.hour));
70+ TWIWrite(bin2bcd(0));
71+ TWIWrite(bin2bcd(Date.day));
72+ TWIWrite(bin2bcd(Date.month));
73+ TWIWrite(bin2bcd(Date.year - 2000));
74+ TWIStop;
75+ end;
76+```
77+
78+Die Zeit aus dem Baustein auslesen.
79+
80+``` pascal
81+ procedure ReadDS3231(addr: UInt16);
82+ begin
83+ TWIStart((addr shl 1) or TWI_Write);
84+ TWIWrite(0);
85+ TWIStop;
86+
87+ TWIStart((addr shl 1) or TWI_Read);
88+ Date.second := bcd2bin(TWIReadACK_Error and $7F);
89+ Date.minute := bcd2bin(TWIReadACK_Error);
90+ Date.hour := bcd2bin(TWIReadACK_Error);
91+
92+ TWIReadACK_Error;
93+
94+ Date.day := bcd2bin(TWIReadACK_Error);
95+ Date.month := bcd2bin(TWIReadACK_Error);
96+ Date.year := bcd2bin(TWIReadNACK_Error) + 2000;
97+
98+ TWIStop;
99+ end;
100+```
101+
102+## Beispiel
103+
104+### Variablen
105+
106+``` pascal
107+var
108+ s: ShortString;
109+```
110+
111+### Hauptprogramm
112+
113+Die Zeit muss man nur in den DS3231 schreiben, wen man die Uhr richten will.
114+
115+``` pascal
116+begin
117+ UARTInit; // UART inizialisieren
118+ TWIInit; // I²C inizialisieren
119+ asm Sei end;
120+
121+ // Eine Zeit in den DS3231 schreiben.
122+ Date.second := 33;
123+ Date.minute := 22;
124+ Date.hour := 11;
125+ WriteDS3231(I2CAddr);
126+
127+ // Zeit auslesen und über UART ausgeben.
128+ repeat
129+ ReadDS3231(I2CAddr);
130+
131+ str(Date.second: 6, s);
132+ UARTSendString('Sec: ' + s);
133+ str(Date.minute: 6, s);
134+ UARTSendString(' Minute: ' + s);
135+ str(Date.hour: 6, s);
136+ UARTSendString(' Stunden: ' + s);
137+
138+ UARTSendString(#13#10);
139+ until 1 = 2;
140+end.
141+```
142+
143+## Siehe auch
144+
145+- Übersichtseite [[AVR Embedded Tutorial/de|AVR Embedded Tutorial]]
146+
147+Autor: Mathias
148+
149+---
150+*From the Free Pascal wiki, page "AVR Embedded Tutorial - I²C External-Clock/de", as it stood on 2020-03-08 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
151+Category: AVR/de
152+Category: Arduino/de
153+Category: Deutsch
154+Category: Embedded/de
155+Category: Tutorials/de

Back to history