AWOL:
(or whatever 83 converts to from bcd).
Woah!
Don't confuse hex with BCD or decimal.0b10000011 is 0x83 and BCD 83, but 131 decimal.
yes, and it is confusing for someone just learning (my lesson is getting an spi device to work from reading the datasheet and not using a library) but im careful with the numbers and constantly using the debug window to see whats coming out. also mike, im gonna post the full code, but at this point its just writing and reading a couple registers. the bcd conversion function is by someone else i cant take credit for that, and some of the code is by others also.
#include <SPI.h>
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
#define CONTROL_REG 0x0F
#define DS1305_ADDRESS_OFFSET 0x80
#define SECONDS_REG 0x00
#define MINUTES_REG 0x01
#define HOURS_REG 0x02
SPI.begin();
digitalWrite(SLAVESELECT,LOW);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
// initialize SPI:
Serial.begin(9600);
digitalWrite(SLAVESELECT, HIGH);
writeRegister(CONTROL_REG,0x07);
unsigned int r = readRegister(CONTROL_REG);
Serial.println("CONTROL = ");
Serial.println(r,BIN);
writeRegister(SECONDS_REG,0);
writeRegister(MINUTES_REG,decToBcd(17));
char hn = B00010001; //next few lines is me experimenting, this didnt work so needs to be rewritten
char mask = B01010001;
writeRegister(HOURS_REG,decToBcd(hn));
hn|=mask;
writeRegister(HOURS_REG,decToBcd(hn));
}
void loop(){
delay(1000);
byte seconds = bcdToDec(readRegister(SECONDS_REG));
byte minutes = bcdToDec(readRegister(MINUTES_REG));
byte hours = bcdToDec(readRegister(HOURS_REG));
Serial.print(hours,DEC);
Serial.print(":");
Serial.print(minutes,DEC);
Serial.print(":");
Serial.println(seconds,DEC);
}
unsigned int readRegister(byte thisRegister)
{
byte inByte = 0;
unsigned int result = 0;
digitalWrite(SLAVESELECT, HIGH);
SPI.transfer(thisRegister);
result = SPI.transfer(0x00);
digitalWrite(SLAVESELECT, LOW);
return(result);
}
void writeRegister(byte thisRegister, char thisValue)
{
digitalWrite(SLAVESELECT, HIGH);
SPI.transfer(thisRegister+DS1305_ADDRESS_OFFSET);
SPI.transfer(thisValue);
digitalWrite(SLAVESELECT, LOW);
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}