ajit_nayak87
Member level 5
Deal all i am trying to communicate RTC MCP7940N device with PIC18F24k40. Intially i have tested my code with Arduino Uno board . these are waveform i got with arduino device.With Arduino device i could able to read /write date & time operation
Arduino code
PIC uses MCC generate library function. with below code i could able to display 20 & 10 on screen but sec parameter is not updating .Can someone suggest me what shall i send i2c Read/write to get response.i have tested similar way for other i2c device like ds1307/ds3231/pcf85163 without any change in library .
How to get RTC date and time with below code??
Microchip code
Arduino code
Code:
#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 = 0x01; // Register Address: Time Minute
const int REG_RTCHOUR = 0x02; // 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( 0); // SECONDS
timeStamp[1] = convertToBcd(48); // MINUTES
timeStamp[2] = convertToBcd(19); // 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(500);
}
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);
}
PIC uses MCC generate library function. with below code i could able to display 20 & 10 on screen but sec parameter is not updating .Can someone suggest me what shall i send i2c Read/write to get response.i have tested similar way for other i2c device like ds1307/ds3231/pcf85163 without any change in library .
How to get RTC date and time with below code??
Microchip code
Code:
#include "mcc_generated_files/mcc.h"
#include"mcc_generated_files/examples/i2c1_master_example.h"
unsigned int i=0;
unsigned int k=0;
unsigned int count;
unsigned int x;
#define LED RC7
#define CONTROLREG 0xFF
#define SDA RC4 // Data pin for i2c
#define SCK RC3 // Clock pin for i2c
#define SDA_DIR TRISC4 // Data pin direction
#define SCK_DIR TRISC3 // Clock pin direction
#define I2C_SPEED 150 // kbps
unsigned short int cnt, num,Dgt=0;;
unsigned short int temp1,temp2,temp3;
int sec;
int min;
int hour;
int date;
int month;
int year;
int day;
int temp=0;
int r_data;
#define Seg1 0x01
#define Seg2 0x02
#define Seg3 0x04
#define Seg4 0x08
#define Seg5 0x10
#define Seg6 0x20
unsigned char Flag_Update=0;
void Delay(unsigned int k) {
unsigned int j;
for(j=0; j<k; j++);
}
void SetSeg(unsigned short data, unsigned short segno)
{
switch(data) {
case 0:
PORTB = 0x3F;
break;
case 1:
PORTB = 0x06;
break;
case 2:
PORTB = 0x5B;
break;
case 3:
PORTB = 0x4F;
break;
case 4:
PORTB = 0x66;
break;
case 5:
PORTB = 0x6D;
break;
case 6:
PORTB = 0x7D;
break;
case 7:
PORTB = 0x07;
break;
case 8:
PORTB = 0x7F;
break;
case 9:
PORTB = 0x6F;
break;
default :
PORTB = 0X00;
break;
}
if(segno==1) {
PORTA = Seg4;
}
if(segno==2) {
PORTA = Seg3;
}
if(segno==3) {
PORTA = Seg2;
}
if(segno==4) {
PORTA = Seg1;
}
if(segno==5) {
PORTC=0X00;
PORTC = 0x40;//DP2 fourth Segment
// PORTCbits.RC5=1;
}
if(segno==6) {
PORTC=0X00;
PORTC= 0x20;//DP2 third Segment
//PORTCbits.RC6=1;
}
if(segno==7) {
PORTA=0X00;
PORTA = Seg5; //DP2 Second Segment
// PORTAbits.RA4=1;
}
if(segno==8) {
PORTA=0X00;
PORTA = Seg6; //DP2 First Segment
// PORTAbits.RA5=1;
}
}
unsigned int bcdtodecimal(unsigned int bcd) {
unsigned int decimal;
decimal = (((bcd & 0xF0) >> 4) * 10) + (bcd & 0x0F);
return decimal;
}
void ISR_Routine(void) {
if(PIR0bits.TMR0IF==1) {
PIR0bits.TMR0IF = 0;
count= count+1;
if(count>=10) {
Flag_Update=1;
count=0;
LED=!LED;
}
}
}
void main(void) {
// Initialize the device
SYSTEM_Initialize();
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
I2C1_Initialize();
I2C1_Write1ByteRegister(0x6F,0x00,0x10);//sec
I2C1_Write1ByteRegister(0x6F,0x01,0x20);//min
I2C1_Write1ByteRegister(0x6F,0x02,0x01);//Hour
I2C1_Write1ByteRegister(0x6F,0x03,0x01);
I2C1_Write1ByteRegister(0x6F,0x04,0x02);
I2C1_Write1ByteRegister(0x6F,0x05,0x03);
I2C1_Write1ByteRegister(0x6F,0x06,0x10);
I2C1_Write1ByteRegister(0x6F,0x07,0x48);
while (1)
{
sec=I2C1_Read1ByteRegister(0x6F,0x00);
min=I2C1_Read1ByteRegister(0x6F,0x01);
if(Flag_Update==1) {
SetSeg(min >> 4,4);
__delay_ms(5);
SetSeg(min & 0x0f,3);
__delay_ms(5);
SetSeg(sec >> 4,2);
__delay_ms(5);
SetSeg(sec & 0x0f,1);
__delay_ms(5);
Flag_Update = 0; //ready for next update
}
}
}