I am trying to write code for DS1307. I just want to write the first five registers of DS1307
Here is code
I am stuck with the following function. When I send the device address. After that, I need to check ACK/NAK . after that I am thinking to use a loop to send value in five registers
Here is code
C:
#include <xc.h>
// Configuration word for PIC16F877A
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);
#define _XTAL_FREQ 20000000
// Define i2c pins
#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 100 // kbps
#define Address 0xD0
#define WordAddress 0x00
// Function Purpose: Configure I2C module
void InitI2C(void)
{
SDA_DIR = 1; // Make SDA and
SCK_DIR = 1; // SCK pins input
SSPADD = ((_XTAL_FREQ/4000)/I2C_SPEED) - 1;
SSPSTAT = 0x80; // Slew Rate control is disabled
SSPCON = 0x28; // Select and enable I2C in master mode
}
// Function Purpose: I2C_Start sends start bit sequence
void I2C_Start(void)
{
SEN = 1; // Send start bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}
//Function : I2C_Stop sends stop bit sequence
void I2C_Stop(void)
{
PEN = 1; // Send stop bit
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
}
// Function Purpose: I2C_Write_Byte transfers one byte
__bit I2C_Write_Byte(unsigned char Byte)
{
SSPBUF = Byte; // Send Byte value
while(!SSPIF); // Wait for it to complete
SSPIF = 0; // Clear the flag bit
return ACKSTAT; // Return ACK/NACK from slave
}
void Write_Byte_To_DS1307_RTC(unsigned char address, unsigned char wordAddress, unsigned char *DataByte, unsigned char size)
{
I2C_Start(); // Start i2c communication
I2C_Write_Byte(address); // Send i2c address of DS1307 with write command
I2C_Stop(); // Stop i2c communication
}
void main(void)
{
unsigned char Size = 6;
unsigned char DataByte[ ] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
InitI2C();
Write_Byte_To_DS1307_RTC( Address, WordAddress, DataByte, Size);
}
I am stuck with the following function. When I send the device address. After that, I need to check ACK/NAK . after that I am thinking to use a loop to send value in five registers
C:
void Write_Byte_To_DS1307_RTC(unsigned char address, unsigned char wordAddress, unsigned char *DataByte, unsigned char size)
{
I2C_Start(); // Start i2c communication
I2C_Write_Byte(address); // Send i2c address of DS1307 with write command
I2C_Stop(); // Stop i2c communication
}
Last edited: