brainwagon "There is much pleasure in useless knowledge." — Bertrand Russell

23Feb/114

Arduino + DS1307 Real Time Clock

Okay, this is no big deal: lots of people have done it before. But while I was watching TV, I soldered some header pins to the DS1307 based real time clock board I got from sparkfun.com , and coded up this simple program to read the time from it. I'd never really used the Wire library, but it's pretty straightforward. I plugged the board into a breadboard, jumpered it into the Arduino, and with a bit of tweaking, it works.


#include <Wire.h>
#define DS1307_I2C_ADDRESS (0x68)

byte h, htmp, m, s ;
byte dow, dom ;
byte mn, yr ;

byte
BCDToDEC(byte val)
{
  return (val >> 4) * 10 + (val & 0xf) ;
}

void
doDate()
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS) ;
  Wire.send(0x0) ;
  Wire.endTransmission() ;
  
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7) ;
  
  s   = BCDToDEC(Wire.receive() & 0x7f) ;
  m   = BCDToDEC(Wire.receive()) ;
  htmp  = Wire.receive() ;
  dow = BCDToDEC(Wire.receive()) ;
  dom = BCDToDEC(Wire.receive()) ;
  mn  = BCDToDEC(Wire.receive()) ;
  yr  = BCDToDEC(Wire.receive()) ;
  
  if (htmp & (1<<6)) {
      /* 12 hour mode... */
      h = BCDToDEC(htmp&0x1F) ;
  } else {
      /* 24 hour mode... */
      h = BCDToDEC(htmp&0x3F) ;
  }
  
  if (h < 10) Serial.print("0") ;
  Serial.print(h, DEC) ;
  
  Serial.print(":") ;
  if (m < 10)
    Serial.print("0") ;
  Serial.print(m, DEC) ;
  Serial.print(":") ;
  if (s < 10)
    Serial.print("0") ;
  Serial.print(s, DEC) ;
    
  if (htmp & (1<<6)) {
       if (htmp & (1<<5)) 
         Serial.print("PM") ;
       else
         Serial.print("AM") ;
  } 
  Serial.print(" ") ;
    
  Serial.print(mn, DEC) ;
  Serial.print("/") ;
  Serial.print(dom, DEC) ;
  Serial.print("/") ;
  Serial.print(yr, DEC) ;
  Serial.print("\n") ;
}
  
void
setup()
{
  Wire.begin() ;
  Serial.begin(9600) ;
}

void 
loop()
{
  doDate() ;
  delay(1000) ;
}

Nothing earth shattering here. Eventually, I want my beacon controller to have a real time clock, and this seemed like a good way to go. Having a reasonably accurate real time clock means that I can make my beacon send WSPR messages, which start at the beginning of even numbered minutes on the hour. Baby steps.

23Feb/111

PCB Order via DorkbotPDX

A while ago, I remember hearing that DorkbotPDX had some deal where you could submit PCB designs and have them ganged with other designs and manufactured for a pittance (or at least, a small number of pittances) but I hadn't bothered to chase down the details. Recently, I've thought of making a small board with an Atmel AVR, a RTC and maybe a DTC to use in (among other things) my beacon controller so I looked it up. Pretty neat: three copies of any board you submit with a cost of $5 per square inch. Thus, if you have a 2"x1" board, you get three copies for $10. They can also do 4 layer boards for $10 a square inch. Nifty.

PCB Order | DorkbotPDX.

Filed under: electronics 1 Comment