DS1307 RTC with Arduino UNO

Status
Not open for further replies.

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
817
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
6,493
Please let me know I found a code for RTC DS1307 I want to understand some coding in it.
What are they doing bitwise operation here
Serial.print((month & 0x10)>>4); //month & 0x10 why 0x10 is used here
Serial.print(month & 0x0F);
Serial.print((date & 0x70)>>4); //date & 0x70 why 0x70 is used here
Serial.print(date & 0x0F);


Code:
if(Wire.available() == 7)       // slave may send less than requested
  {
    byte seconds = Wire.read();    // receive a byte as character
    byte minutes = Wire.read();    // receive a byte as character
    byte hours   = Wire.read();    // receive a byte as character
    byte day     = Wire.read();    // receive a byte as character
    byte date    = Wire.read();    // receive a byte as character
    byte month   = Wire.read();    // receive a byte as character
    byte year    = Wire.read();    // receive a byte as character

    // Print date to serial port
    Serial.print("Date: ");
    Serial.print((month & 0x10)>>4); 
    Serial.print(month & 0x0F);
    Serial.print("/");
    Serial.print((date & 0x70)>>4);
    Serial.print(date & 0x0F);
    Serial.print("/");
    Serial.print((year & 0x70)>>4);
    Serial.print(year & 0x0F);

    // Print time to serial port
    Serial.print(" Time: ");
    Serial.print(hours);
    Serial.print(":");
    Serial.print((minutes & 0x70)>>4);
    Serial.print(minutes & 0x0F);
    Serial.print(":"); 
    Serial.print((seconds & 0x70)>>4);
    Serial.print(seconds & 0x0F);
    Serial.println("");
    }
 

Hi,

The time/date is BCD coded.
example:
0x12 (hex)= month december = 18 (decimal) should be displayd as "12" (two ASCII letters)
thus the "1" of 0x12 needs to be shifted right. It is masked with & 0x10 to focus on the "1". the result is 0x10.
then it is shifted 4 bits left (>>4) to become 0x01 =1 = "1"

Klaus
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…