Real Time Clock ds1302 library

Files at this revision

API Documentation at this revision

Comitter:
gcibeira
Date:
Fri Dec 03 18:01:36 2010 +0000
Commit message:

Changed in this revision

ds1302.cpp Show annotated file Show diff for this revision Revisions of this file
ds1302.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r be8d71b29b37 ds1302.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ds1302.cpp	Fri Dec 03 18:01:36 2010 +0000
@@ -0,0 +1,128 @@
+/*
+  Copyright (c) 2010 Cibeira Gerardo
+*/
+
+#include "ds1302.h"
+#include "mbed.h"
+
+//constructor
+ds1302::ds1302(PinName pinSCLK,PinName pinIO,PinName pinRST) : SCLK(pinSCLK),IO(pinIO),RST(pinRST) {}
+
+//methods
+void ds1302::write_byte(BYTE cmd)
+{
+    BYTE i;
+    IO.output();
+    for(i=0;i<=7;i++)
+    {
+        IO = (cmd >> i) & 0x01;
+        wait_us(1);
+        SCLK=1;
+        wait_us(1);
+        SCLK=0;
+    }
+    IO.input();
+}
+
+void ds1302::write(BYTE cmd, BYTE data)
+{
+    RST=1;
+    wait_us(1);
+    write_byte(cmd);
+    write_byte(data);
+    RST=0;
+}
+
+BYTE ds1302::read(BYTE cmd)
+{
+    BYTE i,data=0;
+    
+    RST=1;
+    write_byte(cmd);
+    
+    IO.input();
+    wait_us(1);
+    
+    for(i=0;i<=7;i++)
+    {
+       data += IO<<i;
+       SCLK=1;
+       wait_us(1);
+       SCLK=0;
+       wait_us(1);
+    }
+    RST=0;
+    
+    return(data);
+}
+
+void ds1302::init()
+{
+    BYTE x;
+    RST=0;
+    wait_us(2);
+    SCLK=0;
+    write(0x8e,0);
+    write(0x90,0xa4);
+    x=read(0x81);
+    if((x & 0x80)!=0)
+        write(0x80,0);
+}
+
+BYTE ds1302::get_bcd(BYTE data)
+{
+    BYTE nibh=0;
+    BYTE nibl=0;
+    
+    nibh = data/10;
+    nibl = data-(nibh*10);
+    
+    return( (nibh<<4) | nibl);
+}
+
+BYTE ds1302::rm_bcd(BYTE data)
+{
+    BYTE i,aux=0;
+    
+    i = data;
+    aux = (i>>4)*10;
+    aux += (i & 0x0F);
+    
+    return aux;
+}
+
+void ds1302::set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min)
+{
+    write(0x86,get_bcd(day));
+    write(0x88,get_bcd(mth));
+    write(0x8c,get_bcd(year));
+    write(0x8a,get_bcd(dow));
+    write(0x84,get_bcd(hr));
+    write(0x82,get_bcd(min));
+    write(0x80,get_bcd(0));
+}
+
+void ds1302::get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
+{
+    day = rm_bcd(read(0x87));
+    mth = rm_bcd(read(0x89));
+    year = rm_bcd(read(0x8d));
+    dow = rm_bcd(read(0x8b));
+}
+
+void ds1302::get_time(BYTE &hr, BYTE &min, BYTE &sec)
+{
+    hr = rm_bcd(read(0x85));
+    min = rm_bcd(read(0x83));
+    sec = rm_bcd(read(0x81));
+}
+
+void ds1302::write_nvr(BYTE address, BYTE data)
+{
+    write(address|0xc0,data);
+}
+
+BYTE ds1302::read_nvr(BYTE address)
+{
+     return(read(address|0xc1));
+}
\ No newline at end of file
diff -r 000000000000 -r be8d71b29b37 ds1302.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ds1302.h	Fri Dec 03 18:01:36 2010 +0000
@@ -0,0 +1,38 @@
+/*
+  Copyright (c) 2010 Cibeira Gerardo
+*/
+
+#ifndef MBED_DS1302_H
+#define MBED_DS1302_H
+
+#include "mbed.h"
+#define BYTE unsigned char
+
+class ds1302
+{
+public:
+    //constructor
+    ds1302(PinName pinSCLK,PinName pinIO,PinName pinRST);
+    
+    //methods
+    void write_byte(BYTE cmd);
+    void write(BYTE cmd, BYTE data);
+    BYTE read(BYTE cmd);
+    void init();
+    BYTE get_bcd(BYTE data);
+    BYTE rm_bcd(BYTE data);
+    void set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min);
+    void get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow);
+    void get_time(BYTE &hr, BYTE &min, BYTE &sec);
+    void write_nvr(BYTE address, BYTE data);
+    BYTE read_nvr(BYTE address);
+    
+    
+private:
+    //data
+    DigitalOut SCLK;
+    DigitalInOut IO;
+    DigitalOut RST;
+};
+
+#endif
\ No newline at end of file