Dear all
I am trying to interface Arduino Uno board with MCP7940-N RTC device . I have tested the code with DS1307 where i used to get proper SDA/SCL response using logical analyzer.
Here is my code tested
#include "I2C.h"
const int MCP7940_I2C = 0x6F; // I2C Address for the RTC
const int REG_RTCSEC = 0x00; // Register Address: Time Second
const int REG_RTCMIN = 0x42; // Register Address: Time Minute
const int REG_RTCHOUR = 0X03; // Register Address: Time Hour
const int REG_RTCWKDAY = 0x03; // Register Address: Date Day of Week
const int REG_RTCDATE = 0x04; // Register Address: Date Day
const int REG_RTCMTH = 0x05; // Register Address: Date Month
const int REG_RTCYEAR = 0x06; // Register Address: Date Year
byte timeStamp[7]; // Byte array holding a full time stamp.
// Array position is the same as the register address.
void setup()
{
Serial.begin (9600);
I2c.begin (); // Initialize the I2C library
I2c.pullup (0); // Disable the internal pullup resistors
I2c.setSpeed (0); // Enable 100kHz I2C Bus Speed
I2c.timeOut (250); // Set a 250ms timeout before the bus resets
// These are the values that will be written to the MCP7940
timeStamp[0] = convertToBcd( 05); // SECONDS
timeStamp[1] = convertToBcd(42); // MINUTES
timeStamp[2] = convertToBcd(15); // HOURS
timeStamp[3] = convertToBcd( 6); // DAY OF WEEK (arbitrary value 1 - 7)
timeStamp[4] = convertToBcd( 26); // DAY
timeStamp[5] = convertToBcd( 8); // MONTH
timeStamp[6] = convertToBcd( 20); // YEAR
// Write our time stamp to the time/date registers
I2c.write(MCP7940_I2C, REG_RTCSEC, timeStamp, 7);
// Initialize our chip with any further configuration data
init_MCP7940();
}
void loop()
{
I2c.read(MCP7940_I2C, REG_RTCSEC, 7, timeStamp);
Serial.print ("Current Time: ");
Serial.print (convertFromBcd(timeStamp[2] & 0x3F));
Serial.print (":");
if (convertFromBcd(timeStamp[1] & 0x7F) / 10 == 0) Serial.print ("0");
Serial.print (convertFromBcd(timeStamp[1] & 0x7F));
Serial.print (":");
if (convertFromBcd(timeStamp[0] & 0x7F) / 10 == 0) Serial.print ("0");
Serial.println (convertFromBcd(timeStamp[0] & 0x7F));
delay(1000);
}
void init_MCP7940() {
byte registerValue = 0x00; // Holds the received register value
byte twelveHour = 0x00; // 0 = 24 Hour Clock Mode / 1 = 12 Hour Clock Mode
byte startClock = 0x01; // 0 = Start Oscillator / 1 = Stop Oscillator
// Turn on/off: 12 hour vs. 24 hour clock
I2c.read (MCP7940_I2C, REG_RTCHOUR, 1);
registerValue = I2c.receive();
if (twelveHour == 0x00) I2c.write (MCP7940_I2C, REG_RTCHOUR, bitClear (registerValue, 6));
if (twelveHour == 0x01) I2c.write (MCP7940_I2C, REG_RTCHOUR, bitSet (registerValue, 6));
// Turn on/off: Oscillator (starts the clock)
I2c.read (MCP7940_I2C, REG_RTCSEC, 1);
registerValue = I2c.receive();
if (startClock == 0x00) I2c.write (MCP7940_I2C, REG_RTCSEC, bitClear (registerValue, 7));
if (startClock == 0x01) I2c.write (MCP7940_I2C, REG_RTCSEC, bitSet(registerValue, 7));
}
byte convertToBcd(byte byteDecimal) {
return (byteDecimal / 10) << 4 | (byteDecimal % 10);
}
byte convertFromBcd(byte byteBCD) {
byte byteMSB = 0;
byte byteLSB = 0;
byteMSB = (byteBCD & B11110000) >> 4;
byteLSB = (byteBCD & B00001111);
return ((byteMSB * 10) + byteLSB);
}
If check Both code . i could see Serial Monitor printing Data . But when i connect SDA/SCL line it wont respond to slave device.
I have attached waveform for reference.
Code 2:
#include <Wire.h>
#include <MCP794xx.h>
MCP794xx rtc1;
void setup() {
// Start Serial Communication
Serial.begin(9600);
// Run through setting the date and time.
rtc1.setHours12(5, true); // Set the time to 11 AM
//rtc1.setHours24(11); // Set the time to 1100 (24h format)
rtc1.setMinutes(13); // Set the minutes to 39
rtc1.setSeconds(45); // Set the second to 45
// You can also set the time in a single function call
rtc1.setTime12(5, true, 13, 45); // Set the time in 12h format
//rtc1.setTime24(11, 39, 45); // Set the time in 24h format
rtc1.setYear(20); // Set the year to 18
rtc1.setMonth(_AUG); // Set the Month
rtc1.setDate(28); // Set the date (0-31)
rtc1.setWeekday(_WED); // Set the day of the week
// You can also set the date in a single function call
rtc1.setCalendar(20, _AUG, 26); // Set the calendar, weekday must be set seperately
rtc1.setWeekday(_WED); // Set the weekday.
rtc1.start(); // Turn on the clock, begins tracking time
delay(1000);
// Read back the date and time.
Serial.print("Time: ");
Serial.print(rtc1.getHours()); // returns Hours in int format
Serial.print("h ");
Serial.print(rtc1.getMinutes()); // returns minutes in int format
Serial.print("m ");
Serial.print(rtc1.getSeconds()); // returns seconds in in format.
Serial.print(" s");
// The PM value of the MCP7940 class is updated every time the hours are read from the module.
// PM is TRUE if the time is in 12 hour format and in the after noon (i.e. 3:45 PM)
// Otherwise PM is false
if (rtc1.PM == true)
{
Serial.print(" PM\n");
} else {
Serial.print(" AM\n");
}
Serial.print("Weekday: ");
// Returns the Day of the Week value, these can be user defined but the library contains predefined values
Serial.print(rtc1.getWeekday());
Serial.print(" | Date: ");
Serial.print(rtc1.getDate()); // Returns the date of the month.
Serial.print(" | Month: ");
Serial.print(rtc1.getMonth()); // Returns the month
Serial.print(" | Year: ");
Serial.print(rtc1.getYear()); // Returns the last two digits of the year
Serial.print("\n");
delay(1000);
Serial.print("Weekday: ");
Serial.print(rtc1.getWeekday());
Serial.print(" | Date: ");
Serial.print(rtc1.getDate());
Serial.print(" | Month: ");
Serial.print(rtc1.getMonth());
Serial.print(" | Year: ");
Serial.print(rtc1.getYear());
Serial.print("\n");
// Set the Multifunction pin to output a square wave.
rtc1.setMFPinSquareWave(_32kHz);
delay(1000);
rtc1.setMFPinSquareWave(_8kHz);
delay(1000);
rtc1.setMFPinSquareWave(_4kHz);
delay(1000);
rtc1.setMFPinSquareWave(_1Hz);
delay(1000);
}
// the loop function runs over and over again until power down or reset
void loop() {
Serial.print("Time: ");
Serial.print(rtc1.getHours());
Serial.print("h ");
Serial.print(rtc1.getMinutes());
Serial.print("m ");
Serial.print(rtc1.getSeconds());
Serial.print(" s");
rtc1.PM ? Serial.print(" PM") : Serial.print(" AM");
Serial.print("\n");
rtc1.setMFPinSquareWave(_32kHz);
delay(1000);
rtc1.setMFPinSquareWave(_8kHz);
delay(1000);
rtc1.setMFPinSquareWave(_4kHz);
delay(1000);
rtc1.setMFPinSquareWave(_1Hz);
delay(1000);
}