close

接線方法:
    1. Arduino 的 SDA 接到 RTC 時鐘模組的 SDA
    2. Arduino 的 SCL 接到 RTC 時鐘模組的 SCL
    3. RTC時鐘模組 接上 3.3V 與 接地 0V

    到這裡基本上就完成了,因為我的模組預設 SCL 與 SDA 都也接上 10K 上拉電阻,所以不用再接電阻只要4條線就可以動了。如果大家發現板子不會動,記得把SCL 與 SDA 兩條線分別接上 10K 上拉電阻 至 3.3V,如圖:

image00

Arduino UNO 接線:

1311251615cdb8f69872bf5d81  


Arduino Mega 2560 實際接線:

DSC_0302  DSC_0303

DSC_0300

DSC_0301  


然而地址 0x68 是根據 DS1307Z 的 Data Sheet 第12頁下方
http://datasheets.maximintegrated.com/en/ds/DS1307...
他說 Slave Address (也就是 DS1307Z 的地址 ) 是 1101000
然而 1101000 為 2 進位地址,轉換為16進位為 0x68

  

 

// 開發板:Arduino MEGA 2560
// RTC 時鐘:DS1307Z
// 作者:黃彥霖


#include "Wire.h"

 

const byte DS1307_I2C_ADDRESS = 0x68; // DS1307 (I2C) 地址
const byte NubberOfFields = 7; // DS1307 (I2C) 資料範圍

int y; // 年
byte m, d, w, h, mi, s; // 月/日/週/時/分/秒

void setup() {
    Serial.begin(9600);
    Serial.println("-------------------");

    Wire.begin();
    setTime(13,4,26,5,14,53,0); // 設定時間:2013 年 4 月 26 日 星期五 14 點 53 分 0 秒
}

void loop() {
    getTime(); // 取得時間
    digitalClockDisplay(); // 顯示時間
    delay(1000);
}

// BCD 轉 DEC
byte bcdTodec(byte val){
    return ((val / 16 * 10) + (val % 16));
}

// DEC 轉 BCD
byte decToBcd(byte val){
    return ((val / 10 * 16) + (val % 10));
}

// 設定時間
void setTime(byte y, byte m, byte d, byte w, byte h, byte mi, byte s){
    Wire.beginTransmission(DS1307_I2C_ADDRESS);
    Wire.write(0);
    Wire.write(decToBcd(s));
    Wire.write(decToBcd(mi));
    Wire.write(decToBcd(h));
    Wire.write(decToBcd(w));
    Wire.write(decToBcd(d));
    Wire.write(decToBcd(m));
    Wire.write(decToBcd(y));
    Wire.endTransmission();
}

// 取得時間
void getTime(){
    Wire.beginTransmission(DS1307_I2C_ADDRESS);
    Wire.write(0);
    Wire.endTransmission();

    Wire.requestFrom(DS1307_I2C_ADDRESS, NubberOfFields);

    s = bcdTodec(Wire.read() & 0x7f);
    mi = bcdTodec(Wire.read());
    h = bcdTodec(Wire.read() & 0x7f);
    w = bcdTodec(Wire.read());
    d = bcdTodec(Wire.read());
    m = bcdTodec(Wire.read());
    y = bcdTodec(Wire.read()) + 2000;
}

// 顯示時間
void digitalClockDisplay(){
    Serial.print(y);
    Serial.print("/");
    Serial.print(m);
    Serial.print("/");
    Serial.print(d);
    Serial.print(" ( ");
    Serial.print(w);
    Serial.print(" ) ");
    Serial.print(h);
    Serial.print(":");
    Serial.print(mi);
    Serial.print(":");
    Serial.println(s);
}


執行結果:

Forbidden filename  

 

 

 

 

arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(9) 人氣()