engineerfaisalnazir
Newbie level 1
Help required,
I can write data to the DS1307 chip but can not read the data.
Please if anyone can help me in this regards.
Microcontroller is in master mode, I2c has been designed for 100Khz, I have connected pullup resistors with the SDA and SCl lines, 32Khz Crystal
Backup power by lithium battery(3.3v)
Thank You.
Best Regards,
//************************************************************************************//
//************************************************************************************//
// Project: //
// Header: 8722 Project //
// PIC used: PIC18F8722 //
// Start Date: 1st Dec 2009
// Function for I2C //
//************************************************************************************//
//************************************************************************************//
//** HEADER FILES ********************************************************************/
#include <p18F8722.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <usart.h>
#include <timers.h>
#include <delays.h>
#include <i2c.h>
//** CONFIGURATION BITS **************************************************************/
#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON
//** FUNCTION PROTOTYPES *************************************************************/
void Init_Ports(void);
void Configure_I2C2(void);
void Day_Decision (void);
//** DEFINITIONS *********************************************************************/
#define CLEAR 0
#define SET 1
#define BRGValue 0x63
//** GLOBALS *************************************************************************/
unsigned char Second =5, Minutes=20, Hours=9, Days=3, Date=12, Month=06, Year=2009;
unsigned char Day,Sec,Min,Hr,Dy,Dat,Mon,Ye;
unsigned char ControlByte = 0xD0; //Control is always D0 for reads (& D1 for writes)
unsigned char Address; //High Order Address Byte
unsigned char Data; //Data Byte
unsigned char Length; //Length of bytes to read
//** MAIN CODE ***********************************************************************/
void main(void)
{
Init_Ports();
Configure_I2C2 ();
EEByteWrite2( ControlByte, 0x07 , 0x00); // control byte 0x00;
EEByteWrite2( ControlByte, 0x00 , (( Second/10)<<4) + ( Second%10) );// Decimal to BCD
EEByteWrite2( ControlByte, 0x01 , ((Minutes/10)<<4) + (Minutes%10) );
EEByteWrite2( ControlByte, 0x02 , ((Hours/10)<<4) + (Hours%10) );
EEByteWrite2( ControlByte, 0x03 , ((Days/10)<<4) + (Days%10) );
EEByteWrite2( ControlByte, 0x04 , ((Date/10)<<4) + (Date%10) );
EEByteWrite2( ControlByte, 0x05 , ((Month/10)<<4) + (Month%10) );
EEByteWrite2( ControlByte, 0x06 , ((Year/10)<<4) + (Year%10) );
Sec = EERandomRead2( 0xD1, 0x00 ); // BCD format
Second = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Min = EERandomRead2( 0xD1, 0x01 );
Minutes = ((Sec>>4)*10)+Sec%16;
Hr = EERandomRead2( 0xD1, 0x02 ); // BCD format
Hours = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Dy = EERandomRead2( 0xD1, 0x03 );
Days= ((Sec>>4)*10)+Sec%16;
Dat= EERandomRead2( 0xD1, 0x04 );
Date = ((Sec>>4)*10)+Sec%16;
Mon = EERandomRead2( 0xD1, 0x05 ); // BCD format
Month = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Ye= EERandomRead2( 0xD1, 0x06 );
Year = ((Sec>>4)*10)+Sec%16;
Day_Decision ();
while(SET)
{
Nop();
}
}
//*************************************************************************************
void Init_Ports(void)
{
PORTD = CLEAR;
LATD = 0b01100000;
TRISD = 0b01100000; // intialized RD6(SCL2) AND RD5(SDA2).
}
//*************************************************************************************
void Configure_I2C2 (void)
{
SSP2STAT = 0x80; //Disable SMBus &
SSP2CON1 = 0x28; //Enable MSSP Master
SSP2CON2 = 0x00; //Clear MSSP Conrol Bits
SSP2ADD = 0x63;
}
//************************************************************************************
void Day_Decision (void)
{
if (Days ==1)
{printf(" Monday: ");}
if (Days ==2)
{printf("Tuesday");}
if (Days==3)
{printf("Wenesday");}
if (Days==4)
{printf("Thursday");}
if (Days==5)
{printf("Friday");}
if (Days==6)
{printf("Saturday");}
if (Days==7)
{printf("Sunday");}
}
I can write data to the DS1307 chip but can not read the data.
Please if anyone can help me in this regards.
Microcontroller is in master mode, I2c has been designed for 100Khz, I have connected pullup resistors with the SDA and SCl lines, 32Khz Crystal
Backup power by lithium battery(3.3v)
Thank You.
Best Regards,
//************************************************************************************//
//************************************************************************************//
// Project: //
// Header: 8722 Project //
// PIC used: PIC18F8722 //
// Start Date: 1st Dec 2009
// Function for I2C //
//************************************************************************************//
//************************************************************************************//
//** HEADER FILES ********************************************************************/
#include <p18F8722.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <usart.h>
#include <timers.h>
#include <delays.h>
#include <i2c.h>
//** CONFIGURATION BITS **************************************************************/
#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON
//** FUNCTION PROTOTYPES *************************************************************/
void Init_Ports(void);
void Configure_I2C2(void);
void Day_Decision (void);
//** DEFINITIONS *********************************************************************/
#define CLEAR 0
#define SET 1
#define BRGValue 0x63
//** GLOBALS *************************************************************************/
unsigned char Second =5, Minutes=20, Hours=9, Days=3, Date=12, Month=06, Year=2009;
unsigned char Day,Sec,Min,Hr,Dy,Dat,Mon,Ye;
unsigned char ControlByte = 0xD0; //Control is always D0 for reads (& D1 for writes)
unsigned char Address; //High Order Address Byte
unsigned char Data; //Data Byte
unsigned char Length; //Length of bytes to read
//** MAIN CODE ***********************************************************************/
void main(void)
{
Init_Ports();
Configure_I2C2 ();
EEByteWrite2( ControlByte, 0x07 , 0x00); // control byte 0x00;
EEByteWrite2( ControlByte, 0x00 , (( Second/10)<<4) + ( Second%10) );// Decimal to BCD
EEByteWrite2( ControlByte, 0x01 , ((Minutes/10)<<4) + (Minutes%10) );
EEByteWrite2( ControlByte, 0x02 , ((Hours/10)<<4) + (Hours%10) );
EEByteWrite2( ControlByte, 0x03 , ((Days/10)<<4) + (Days%10) );
EEByteWrite2( ControlByte, 0x04 , ((Date/10)<<4) + (Date%10) );
EEByteWrite2( ControlByte, 0x05 , ((Month/10)<<4) + (Month%10) );
EEByteWrite2( ControlByte, 0x06 , ((Year/10)<<4) + (Year%10) );
Sec = EERandomRead2( 0xD1, 0x00 ); // BCD format
Second = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Min = EERandomRead2( 0xD1, 0x01 );
Minutes = ((Sec>>4)*10)+Sec%16;
Hr = EERandomRead2( 0xD1, 0x02 ); // BCD format
Hours = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Dy = EERandomRead2( 0xD1, 0x03 );
Days= ((Sec>>4)*10)+Sec%16;
Dat= EERandomRead2( 0xD1, 0x04 );
Date = ((Sec>>4)*10)+Sec%16;
Mon = EERandomRead2( 0xD1, 0x05 ); // BCD format
Month = ((Sec>>4)*10)+Sec%16; // BCD to Decimal
Ye= EERandomRead2( 0xD1, 0x06 );
Year = ((Sec>>4)*10)+Sec%16;
Day_Decision ();
while(SET)
{
Nop();
}
}
//*************************************************************************************
void Init_Ports(void)
{
PORTD = CLEAR;
LATD = 0b01100000;
TRISD = 0b01100000; // intialized RD6(SCL2) AND RD5(SDA2).
}
//*************************************************************************************
void Configure_I2C2 (void)
{
SSP2STAT = 0x80; //Disable SMBus &
SSP2CON1 = 0x28; //Enable MSSP Master
SSP2CON2 = 0x00; //Clear MSSP Conrol Bits
SSP2ADD = 0x63;
}
//************************************************************************************
void Day_Decision (void)
{
if (Days ==1)
{printf(" Monday: ");}
if (Days ==2)
{printf("Tuesday");}
if (Days==3)
{printf("Wenesday");}
if (Days==4)
{printf("Thursday");}
if (Days==5)
{printf("Friday");}
if (Days==6)
{printf("Saturday");}
if (Days==7)
{printf("Sunday");}
}
Code:
\[\]