總瀏覽量

2012年7月18日 星期三

Clock = Arduino / Motoduino + RTC (DS1307) + SPI LED (7-Segment)

利用RTC module和LED七段顯示器做出一個簡單的時鐘, SPI介面可以省去許多arduino的IO pin.



DIY材料:
1.Arduino
2.RTC (DS1307) module
3.SPI LED module(7-segment)
4.Inteface Shield (option)
5.9V battery

Arduino sketch:

#include <stdio.h>
#include <string.h>
#include <DS1302.h>

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 3;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 9;
byte Tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
byte Dot = 0x7f;
boolean DotOn;
/* Set the appropriate digital I/O pin connections */
uint8_t CE_PIN   = 5;  //RST
uint8_t IO_PIN   = 6;
uint8_t SCLK_PIN = 7;

/* Create buffers */
char buf[50];
char day[10];

/* Create a DS1302 object */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);

void clearAll()
{
  for(int i=0; i<8; i++)
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0xff);
    digitalWrite(latchPin, HIGH);
    delay(10);
  }
}

void displayTime(byte h1, byte h2, byte m1, byte m2, byte s1, byte s2)
{
  if (DotOn)
  {
  digitalWrite(latchPin, LOW);
  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, s2);
  shiftOut(dataPin, clockPin, MSBFIRST, s1);
  shiftOut(dataPin, clockPin, MSBFIRST, Dot);
  shiftOut(dataPin, clockPin, MSBFIRST, m2);
  shiftOut(dataPin, clockPin, MSBFIRST, m1);
  shiftOut(dataPin, clockPin, MSBFIRST, Dot);
  shiftOut(dataPin, clockPin, MSBFIRST, h2);
  shiftOut(dataPin, clockPin, MSBFIRST, h1);
    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
  DotOn = false;
  }
  else
  {
  digitalWrite(latchPin, LOW);
  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, s2);
  shiftOut(dataPin, clockPin, MSBFIRST, s1);
  shiftOut(dataPin, clockPin, MSBFIRST, 0xff);
  shiftOut(dataPin, clockPin, MSBFIRST, m2);
  shiftOut(dataPin, clockPin, MSBFIRST, m1);
  shiftOut(dataPin, clockPin, MSBFIRST, 0xff);
  shiftOut(dataPin, clockPin, MSBFIRST, h2);
  shiftOut(dataPin, clockPin, MSBFIRST, h1);
    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
   DotOn = true;
  }
}

void print_time()
{
  /* Get the current time and date from the chip */
  Time t = rtc.time();

  /* Name the day of the week */
  memset(day, 0, sizeof(day));  /* clear day buffer */
  int Hour1 = t.hr/10;
  int Hour2 = t.hr%10;
  int min1 = t.min/10;
  int min2 = t.min%10;
  int sec1 = t.sec/10;
  int sec2 = t.sec%10;
 
  byte   h1 = Tab[Hour1];
  byte   h2 = Tab[Hour2];
  byte   m1 = Tab[min1];
  byte   m2 = Tab[min2];
  byte   s1 = Tab[sec1];
  byte   s2 = Tab[sec2];

    displayTime(h1, h2, m1, m2, s1, s2);
}


void setup()
{
  Serial.begin(57600);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);

  rtc.write_protect(false);
  rtc.halt(false);

  /* Make a new time object to set the date and time */
  Time t(2012, 7, 11, 21, 16, 37, 3);

  /* Set the time and date on the chip */
  rtc.time(t);
    clearAll();
}


/* Loop and print the time every second */
void loop()
{
  print_time();
  delay(1000);
}


Video: http://www.youtube.com/watch?v=RAb9WjOctdU

My Blog:  http://sinocgtchen.blogspot.com
More videos: http://www.youtube.com/user/sinocgtchen
Motoduino information: http://motoduino.com
My Email: sinocgtchen@gmail.com

沒有留言:

張貼留言