Go Down

Topic: DS1307 adrress register help ! (Read 4843 times) previous topic - next topic

CrossRoads

Post the code you are running, lets see if we can get some other software help here.
I only ever wrote the one sketch, was my first code ever for getting data back over I2C also.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

It´s the same code that you gave me, I wanna see what it does to put in order my ideas
I am Learning, please be Patient !

CrossRoads

halejandro,
Do you have 10K pullup resistors on the SCL/SDA lines? And they are connected to the SCL/SDA lines on the microprocessor?
It should be connected like this (but without C12, that was for experimenting)

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

#18
Aug 09, 2011, 04:38 am Last Edit: Aug 09, 2011, 04:46 am by halejandro Reason: 1
Yes sir,  it's connected like that, Arduino pin 4, 5 !
I am Learning, please be Patient !

CrossRoads

Pullup resisters too? They are needed.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

I am Learning, please be Patient !

halejandro

Code: [Select]
// Only to request data
#include <Wire.h>

byte sec_address = 0x00;
byte seconds;
byte min_address = 0x01;
byte minutes;
byte hrs_address = 0x02;
byte hours;
byte day_address = 0x03;
byte day_week;
byte date_address = 0x04;
byte date;
byte mon_address = 0x05;
byte months;
byte yrs_address = 0x06;
byte years;
byte square_address = 0x07;


int  register_address = 0x68;

void setup(){
  Wire.begin();
  Serial.begin(9600);
}

void loop(){
  Wire.beginTransmission(register_address);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(register_address, 7);
  seconds = Wire.receive();
  minutes = Wire.receive();
  hours = Wire.receive();
  day_week = Wire.receive();
  date = Wire.receive();
  months = Wire.receive();
  years = Wire.receive();
 
  //TIME
 
  if ((hrs_address & 0x0F) < 10) Serial.print("0"); // Bitwise 0x02 & 0x0F resulting the bit that controls the units
  if (hrs_address < 0x23) hours = 0;
  Serial.print(hours,HEX);   
  Serial.print(":");
  if ((min_address & 0x0F) < 10) Serial.print("0");
  if (min_address < 0x59) minutes = 0;
  Serial.print(minutes, HEX);
  Serial.print(":");
  if ((sec_address & 0x0F) < 10) Serial.print("0");
  if (sec_address < 0x59) seconds = 0;
  Serial.print(seconds, HEX);
  Serial.print(" - ");
 
  //DATE
 
  if ((date_address & 0x0F) < 10) Serial.print("0");
  if (date_address < 0x31) date = 0;
  Serial.print(date,HEX);   
  Serial.print("/");
  if ((mon_address & 0x0F) < 10) Serial.print("0");
  if (mon_address < 0x12) months = 0;
  Serial.print(months, HEX);
  Serial.print("/");
  Serial.print("20");
  if ((yrs_address & 0x0F) < 10) Serial.print("0");
  if (yrs_address < 0x99) years = 0;
  Serial.println(years, HEX);
 
  delay(1000);
 
}


Finally I could show the format that I want to ! but this just request data from DS1307 !


I am Learning, please be Patient !

CrossRoads

Okay, what might be happening is that the recieved data is coming in as characters, so if you sent F049,
it would show up as
'F', '0', '4', '9'.
So you can test for 'F' no problem,
you can do the switch:case using '0', '1', '2', no problem.
The '2' and '7' you need to put back into 0x27 to send to the DS1307.
If you look here
http://www.asciitable.com/
You can see 0-9 in the Chr column, correspond to Hex 30-39.
So you should be able to take the 3rd & 4th characters, subtract 30, and have the real value needed.

3rdchar = 3drchar-0x30; // or -48
4thchar - 4thchar-0x30; // or -49

then
databyte_to_write = (3rdchar<<4 + 4thchar); // should result in 0x27

3rdchar gets shifted left 4 bits, so 0x02 becomes 0x20, and then add 7.

Let me know if that works out.  Try adding serialprint(incomingByte, HEX); and see if what you send in from the IDE monitor agrees with the table.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

#23
Aug 09, 2011, 10:23 am Last Edit: Aug 09, 2011, 11:07 am by halejandro Reason: 1
Sorry, but what are the 3rd & 4th characters? should I change something in the sketch?
I am Learning, please be Patient !

CrossRoads

"should I change something in the sketch? "

That's what I am suggesting.
Treat the data coming from the serial monitor as ASCII characters, so instead of hex bytes coming over, such as
F0 00 04
instead you may be getting
'F' (or 0x whatever F  is in the ASCII table)
'0' (or 0x30)
'0'
'0'
'0'
'4' (or 0x34)
which you can use as is for some things, but you need to convert to hex for others

But like I said, add a line to repeat the characters back
Serial.print(receivedByte, HEX);
and confirm that.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Okay, Yesterday I didn't see the screen shot, my moderator settings are fixed now.

That looks like the right kind of output when the device powers up.
You can change the Screen Settings so the text displayed wider & easier to see.

These is how I setup & run the test with RS232 Monitor

Settings for RTC_I2C_test:
a.   Setup Serial1 : RS232MON @ 57600, COM8, connected to DB9, R232 to USB adapter.
   Format: 2 bytes, HEX.

b.   Clock not set, time not running. Updates every 5 seconds:
   Hr 0 Mins 0 Secs 80 Day 1 Date 1 Month 1 Year 200 Square Wave 3

c.   Set Secs: enter F0 33 (high nibble 0-5 and nibble 0-9). High nibble bit 3 (indicates time stopped, hence 80 after initial power)

                      [You should see the time, date, etc change after every command.]

d.   Set Mins: F1 25 (high nibble 0-5 and low nibble 0-9)

e.   Set Hrs: F2 11 (high nibble 0-1 and low nibble 0-9)

f.   Set Day (of week): F3 05 (high nibble 0, low nibble 1-7)

g.   Set Date (of month): F4 09 (high nibble 0-3, low nibble 0-9)

h.   Set Month: F5 08 (high nibble 0-1, low nibble 0-9)

i.   Set Year: F6 11 (high nibble 09, low nibble 0-9)

j.   Set Square Wave: F7 10, test with LED. (high nibble 1  = square wave on, low nibble = 0 for 1 Hz)

k.   Set RAM: F8 (08-3F) (00-FF) (no code written for read back yet)
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

I´m introducing the commands but nothing change, SORRY but why does it have to set the RAM?
I am Learning, please be Patient !

CrossRoads

Ok, I tried duplicating the test, see what was going on.
I think I only told you 1/2 the settings.
See the Left side - Screen Settings seem to work at 1 or 2 bytes, needs to be text mode.

On the Send side, I've been using 2 byte and entering commands as shown - in this, a fresh power up, no battery back up, command used was F004 - sets the seconds to 4, and sets bit 7 to 0 so timekeeping is enabled.
Looks I could use a linefeed in there where too.

I remembered why I used Serial1 - my UBS port shows up as COM13, so I was using that for uploading sketches, and using Serial1 to see test results.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

halejandro

#28
Aug 10, 2011, 08:01 am Last Edit: Aug 10, 2011, 08:10 am by halejandro Reason: 1
Yeah, your code it`s so cool :D! now I can see the time and date !!!! Let me see if I understood the last explanation that you gave me, I have to try to do something similar but, with the exeption that I should use the serial monitor from Arduino IDE, inserting ASCII characters and take it to HEX!
I am Learning, please be Patient !

CrossRoads

Yes, let me know how it goes. The idea behind it is straightforward.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up