Sign in Join
Wiki copytemporary

Revision 1 of 1, by Trev on 3-8-2020. The page as it is now.

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

I²C Externe Uhr DS3231

Vorwort

Dieser Code ist für einen Atmega328/Arduino mit 16MHz und einem Uhrenbaustein DS3231.

Die folgende Funktionen, sind in folgenden Tutorials beschrieben:

Beschreibung

Dieses Beispiel zeigt, wie man den Uhrenbaustein DS3231 anspricht, welcher am I²C-Bus hängt.

PulUp-Widerstände sind nötig, der DS3231 enthält keine.

Funktionen für die DS3231 Ansteuerung.

Konstanten

pascal
const
  I2CAddr   = $68; // Defaultadresse des DS3231.
  TWI_Write = 0;
  TWI_Read  = 1;

Record für die Zeitspeicherung.

pascal
type
  TDate = record
    year: uint16;
    month, day, hour, minute, second, dayOfTheWeek: uint8;
  end;
var
  Date: TDate;

Funktionen

Umwandlung von BCD --> BIN, BIN --> BCD.

Dies wird benötigt, das der im BCD-Format angesprochen wird.

pascal
  function bcd2bin(val: uint8): uint8;
  begin
    Result := val - 6 * (val shr 4);
  end;

  function bin2bcd(val: uint8): uint8;
  begin
    Result := val + 6 * (val div 10);
  end;

Baustein mit einer Zeit beschreiben. Dabei sieht man, das man das Format umwandeln muss.

pascal
  procedure WriteDS3231(addr: UInt16);
  begin
    TWIStart((addr shl 1) or TWI_Write);
    TWIWrite(0);
    TWIWrite(bin2bcd(Date.second));
    TWIWrite(bin2bcd(Date.minute));
    TWIWrite(bin2bcd(Date.hour));
    TWIWrite(bin2bcd(0));
    TWIWrite(bin2bcd(Date.day));
    TWIWrite(bin2bcd(Date.month));
    TWIWrite(bin2bcd(Date.year - 2000));
    TWIStop;
  end;

Die Zeit aus dem Baustein auslesen.

pascal
  procedure ReadDS3231(addr: UInt16);
  begin
    TWIStart((addr shl 1) or TWI_Write);
    TWIWrite(0);
    TWIStop;

    TWIStart((addr shl 1) or TWI_Read);
    Date.second := bcd2bin(TWIReadACK_Error and $7F);
    Date.minute := bcd2bin(TWIReadACK_Error);
    Date.hour   := bcd2bin(TWIReadACK_Error);

    TWIReadACK_Error;

    Date.day   := bcd2bin(TWIReadACK_Error);
    Date.month := bcd2bin(TWIReadACK_Error);
    Date.year  := bcd2bin(TWIReadNACK_Error) + 2000;

    TWIStop;
  end;

Beispiel

Variablen

pascal
var
  s: ShortString;

Hauptprogramm

Die Zeit muss man nur in den DS3231 schreiben, wen man die Uhr richten will.

pascal
begin
  UARTInit;     // UART inizialisieren
  TWIInit;      // I²C  inizialisieren
  asm Sei END;

  // Eine Zeit in den DS3231 schreiben.
  Date.second := 33;
  Date.minute := 22;
  Date.hour   := 11;
  WriteDS3231(I2CAddr);

  // Zeit auslesen und über UART ausgeben.
  repeat
    ReadDS3231(I2CAddr);

    str(Date.second: 6, s);
    UARTSendString('Sec: ' + s);
    str(Date.minute: 6, s);
    UARTSendString(' Minute: ' + s);
    str(Date.hour: 6, s);
    UARTSendString(' Stunden: ' + s);

    UARTSendString(#13#10);
  until 1 = 2;
end.

Siehe auch

Autor: Mathias


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.