Pages: [1] 2   Go Down
Author Topic: Using the I2C PCF8563 Real Time Clock  (Read 4892 times)
0 Members and 1 Guest are viewing this topic.
Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm trying to use the PCF8563 RTC/Calendar/Alarm IC. It uses the I2C bus, an Interrupt for alarms and has a clock output. I know I2C works as I have other I2C devices plugged in (PCF8574 Bidi shift registers). I have hooked up the clock out to the external interrupt of the arduino and wanted to be able to read a steady 1Hz. I have a pull up resistor (1.5K) on the line as clock out is an open drain output. sadly that's a no joy.

Here is my code:
Code:
/* PCF8563 (I2C) Real Time Clock / Calendar / Alarm
 * ---------------
 *
 * (cleft) 2005 by Matthieu Lalonde
 * <http://smurfturf.net>
 * <mailto:mlalonde(at)smurfturf(dot)net>
 *
 */
#include <Wire.h>
#define BAUD_RATE 19200
#define statusLED 12
                  // Addr. |Mode (162 + Read)
#define RTCAddressW B10100010
#define RTCAddressR B10100011

int blinkStatus = LOW;

void setup(void) {
  // initialize inputs/outputs
  
  // start serial
  Serial.begin(BAUD_RATE);
  
  Wire.begin();
  Wire.beginTransmission(RTCAddressW);
  //Wire.send(0);
                        // Registers
  Wire.send(B00000000); // 00H  Test mode
  Wire.send(B00000000); // 01H  Alarm mode
  Wire.send(B00000000); // 02H  Seconds
  Wire.send(30);        // 03H  Minutes
  Wire.send(19);        // 04H  Hours
  Wire.send(11);        // 05H  Date
  Wire.send(2);         // 06H  Day of the week
  Wire.send(B00000000); // 07H  Century bit
  Wire.send(7);         // 08H  year
  Wire.send(B10000000); // 09H  Minute alarm
  Wire.send(B10000000); // 0AH  Hour alarm
  Wire.send(B10000000); // 0BH  Day alarm
  Wire.send(B10000000); // 0CH  Weekday alarm
  Wire.send(B10000011); // 0DH  CLKOUT control (1Hz)
  Wire.send(B00000000); // 0EH  Timer control
  Wire.send(B00000000); // 0FH  Timer
  
  Wire.endTransmission();

  reportAVRState(3,0);
  pinMode(statusLED, OUTPUT);
  attachInterrupt(1, clockIn, CHANGE);
}

void loop()
{
  digitalWrite(statusLED, blinkStatus);
  if (blinkStatus) {
    Serial.println("Blinked");
  }
}
void clockIn( void ) {
  blinkStatus != blinkStatus;
}

// Reports the state of the controller via pin 13's led and serial
void reportAVRState(int howManyTimes, int leaveOn) {
  int i;

  pinMode(statusLED, OUTPUT);

  for (i=0; i < howManyTimes; i++) {
    digitalWrite(statusLED, HIGH);
    delay(200);
    digitalWrite(statusLED, LOW);
    delay(200);  
  }

  if (leaveOn) {
    digitalWrite(statusLED, HIGH);
  }

  Serial.println();
  Serial.println("AVR Initialized");
}
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You might need to declare the blinkStatus variable as volatile, e.g.:

Code:
volatile int blinkStatus = LOW;

Otherwise, the compiler will sometimes assume that it can't be changed (since it doesn't see any calls to clockIn()) and optimize it out.

Let me know if that helps, and I'll add something to the attachInterrupt() documentation about it.
« Last Edit: January 12, 2007, 02:37:17 am by mellis » Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I tried it out. It compiles fine, but everything acts the same way. I, sadly, do not have a scope to check the frequency of the crystal or the clock out. But normally it would clock out at 1Hz, and that should be easy enough to see with a multimeter.... but it doesn't seem like anything happens on clock out. I'm starting to think I  got something wrong with the way the IC works. Although from what I see in the very well documented datasheet and app notes, it doesn't seem so. The crystal might also be the factor as I salvaged it from an old Nomad Jukebox. But it does say 32(R6E0) and looks like a clock crystal.

Anyone? Anyone? Buller?
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you ever find the solution to this?  I'm trying to figure this PCF8563 thing out :/
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, at the moment I am able to write to the RTC, and read all the registers. The R/(not)W bit doesn't seem to have an effect and the clock never changes when I read back the registers. Clkout still does not work. I'm definitely starting to blame the crystal. I which I knew a company that offers samples of Khz range xtals. I've only found Mhz and I don't want to order one or two of a 1$ part when shipping will cost me like 10$ or more. What's the difference from the 8562 to 8563?
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you can write and not read?  Weird.  Annoying.

I ended up ordering this board last night, looks pretty snazzy, and is a 2 wire interface.
http://www.sparkfun.com/commerce/product_info.php?products_id=99

I wish I could tell you the difference between those chips, but I'm a total n00b.
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I ended up ordering a new crystal at ecliptek.com, let's hope they ship fast ;P
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
shrz
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
did you ever find a solution on this issue?
I tried to use the ds1307, with the same results as you. i can write to the clock registers and read them, but nothing changes, the values wont change. The clock seems not to run.
The Post i've started: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1180908809/5#5


any news?
woo
« Last Edit: June 19, 2007, 04:37:24 am by woo_oow » Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just bought another crystal, and tried again. No go smiley-sad I really wished I knew what was the problem.
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so I'm really getting desperate... I can't figure out what the hell I'm doing wrong smiley-sad

Here's my latest code:
Code:
/* PROGRAM'S TITLE
* ---------------
      *
      * Comments
      *
      * (cleft) 2007 by Matthieu Lalonde
      * <http://smurfturf.net>
* <mailto:mlalonde(at)smurfturf(dot)net>
      *
*/
#include <Wire.h>

#define BAUD_RATE 19200
#define statusLED 12

// Addr. |Mode (162 + Read)
#define RTCAddressW 0xA2
#define RTCAddressR 0xA3

      volatile int blinkStatus = LOW;

void setup(void)
{
// initialize inputs/outputs

// start serial
      Serial.begin(BAUD_RATE);

      Wire.begin();

      Wire.beginTransmission(RTCAddressW);
      Wire.send(0);
      Wire.endTransmission();

// Registers

      Wire.beginTransmission(RTCAddressW);
      Wire.send(B00000000); // 00H      Test mode
      Wire.send(B00000000); // 01H      Alarm mode
      Wire.send(B00000001); // 02H      Seconds
      Wire.send(30);            // 03H      Minutes:p
      Wire.send(19);            // 04H      Hours
      Wire.send(11);            // 05H      Date
      Wire.send(2);                  // 06H      Day of the week
      Wire.send(B00000000); // 07H      Century bit 19XX = 1 | 20XX = 0
      Wire.send(7);                  // 08H      year
      Wire.send(B00000000); // 09H      Minute alarm
      Wire.send(B00000000); // 0AH      Hour alarm
      Wire.send(B10000000); // 0BH      Day alarm
      Wire.send(B10000000); // 0CH      Weekday alarm
      Wire.send(B10000011); // 0DH      CLKOUT control (00: 32.768Khz 11: 1Hz)
      Wire.send(B00000000); // 0EH      Timer control
      Wire.send(B00000000); // 0FH      Timer
      Wire.endTransmission();

      reportAVRState(3,0);
}

#define numberOfRegisters 16
char i = 0;
char c[numberOfRegisters];

void loop()
{
      Wire.beginTransmission(RTCAddressW);
      Wire.send(0);
      Wire.send(RTCAddressR);
        
      Wire.requestFrom(RTCAddressR, numberOfRegisters);
      
      i = 0;
      while(Wire.available())
      {
            c[i] = Wire.receive();
            i++;
      }
      Wire.endTransmission();
      
      for(i=0;i<numberOfRegisters;i++)
      {
            Serial.print(i, DEC);
            Serial.print(":\t");
            Serial.println(c[i], BIN);
      }
      
      Serial.println("Waiting a little");
      delay(2000);
}

// Reports the state of the controller via pin 13's led and serial
void reportAVRState(int howManyTimes, int leaveOn)
{
      int i;

      pinMode(statusLED, OUTPUT);

      for (i=0; i < howManyTimes; i++) {
            digitalWrite(statusLED, HIGH);
            delay(200);
            digitalWrite(statusLED, LOW);
            delay(200);      
      }

      if (leaveOn) {
            digitalWrite(statusLED, HIGH);
      }

      Serial.println();
      Serial.println("AVR Initialized");
}
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
shrz
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

check this post, the last post by libhart might help you. I didnt tried it yet, but i will soon, then I'm gonna post my results.

w  
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's an old thread. Nothing relevant in my case.
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope I'm not breaking any rules by dredging up this thread (:X) but I've had the same problems, and I think I've found the solution.

When using the Wire library, you only need to pass Wire.beginTransmission and Wire.requestfrom the 7-bit device address, less the read/write bit. Apparently, Wire takes care of the read/write bit for you, so if you pass it the full 8-bit address, your data is gonna get sent to the wrong place. This isn't mentioned anywhere in the documentation, and I had to find out about it from a blog. : |
Logged

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeap, as I found out later, that's totally right!
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you get the PCF8563 working? I am thinking of adding a Real time clock to my arduino project and so I was wondering if I should go with PCF8563. My project requires an RTC that gives me the current time... more or less accurately.

Are there other chips that I should think off?
Logged

Pages: [1] 2   Go Up
Jump to: