Go Down

Topic: DS1305 does not respond (Read 714 times) previous topic - next topic

antaris

Dear All.
I have a DS1305 connected to a Uno by SPI. The problem is that the DS1305 does not respond to any command given. I am running out of ideas to figure out the problem. Help is appreciated. Here is the code. Thanks.

#include <SPI.h>

#define DS1305_ADDRESS_OFFSET 80
#define SECONDS_REG 0x00
#define CONTROL_REG 0x0F

#include <SoftwareSerial.h>

#define rxPin 7
#define txPin 6
#define SSpin 10

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()
{
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);

  digitalWrite(SSpin, HIGH);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.setDataMode(SPI_MODE1);
  writeRegister(CONTROL_REG,0x00);
  unsigned int r = readRegister(CONTROL_REG);
  mySerial.print("CONTROL = ");
  mySerial.print(r,DEC);
  writeRegister(SECONDS_REG,0x10);
}

void loop()
{
  char s,min,h,d,m,y;
  delay(1000);

  unsigned int r = readRegister(CONTROL_REG);
  mySerial.print("CONTROL = ");
  mySerial.print(r,DEC);

  r = readRegister(SECONDS_REG);
  mySerial.print(":");
  mySerial.println(r,DEC);
 
  writeRegister(0x20,0x30);
  r = readRegister(0x20);
  mySerial.print("0x20 = ");
  mySerial.println(r,HEX);
}

unsigned int readRegister(byte thisRegister)
{
  byte inByte = 0;
  unsigned int result = 0;
 
  digitalWrite(SSpin, HIGH);
  SPI.transfer(thisRegister);
  result = SPI.transfer(0x00);
  digitalWrite(SSpin, LOW);
  return(result);
}

void writeRegister(byte thisRegister, char thisValue)
{
  digitalWrite(SSpin, HIGH);
  SPI.transfer(thisRegister+DS1305_ADDRESS_OFFSET);
  SPI.transfer(thisValue);
  digitalWrite(SSpin, LOW);
}

Nick Gammon

You take SS low to enable the chip, and SS high afterwards. You are doing it backwards.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

antaris

DS1305 datasheet says CE pin should be high during data transactions; that's what I am doing, not the opposite.
Thanks for your post.


Nick Gammon

Oh I see, oops sorry.

Well I still see two problems.

Code: [Select]
digitalWrite(SSpin, HIGH);
SPI.begin();


First, this is setting SS high before initializing SPI. This won't matter too much in itself, since SPI.begin sets it high anyway. However of greater concern is that the clock polarity is determined when SS goes active, so it may be the wrong way around (as it is active immediately).

Second, since SS is high initially the first attempt to access it, namely:

Code: [Select]
writeRegister(CONTROL_REG,0x00);


will not cause a SS low-to-high transition (it already is high). The spec says "The address byte is always the first byte entered after CE is driven high.". Well if it isn't driven high, the address byte won't be read.

I would do something like this:

Code: [Select]
  SPI.begin();
  digitalWrite(SSpin, LOW);
  SPI.setDataMode(SPI_MODE1);


This means that SS is now low, and you then change the data-mode to the correct value. After this, accessing the chip should start with low-to-high and work correctly.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

antaris

My app is finally up and running. How? Cannot really say how. You did touch a sensible point, tough. My pins should have been a bit messy for a while. Dear Nick, you have put me thinking more deeply about potential problems. I thank you for that.
For a while I had my DS1305 measuring time strangly. Why? I originally had the code line
#define DS1305_ADDRESS_OFFSET 80
which should be
#define DS1305_ADDRESS_OFFSET 0x80
Seely me. It took me some time to see that an x was missing.
Best regards to the forum.

Go Up