The DS1307 Real Time Clock uses I2c communication lines to connect with the microcontroller.
I2C uses two lines commonly known as Serial Data/Address or SDA and Serial Clock Line or SCL. The two lines SDA and SCL are standardised and they are implemented using either an open collector or open drain configuration. What this means is that you need to pull these lines UP to VCC. For complete information on how the i2C is implemented in ATmega328PB, you need to go through the section of the datasheet called TWI or Two-Wire Serial Interface.
To start I2C in ATmega328PB, first the SCL frequency needs to set which must be under 100KHz .
To set the SCL frequency you set two registers TWBR0 and TWSR0.
TWSR0 has two bit 0 and bit 1; which sets the prescaler for the clock to the TWI.
Then TWBR0 needs to be set which can anything from 0 to 255.
THen you need to write the I2C functions for start, repeated start, data trasmission and recepetion and stop.
/*
* main.c
*
* Created: 8/20/2022 2:08:09 PM
* Author: abhay
*/
#define F_CPU 16000000
#include <xc.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "util/delay.h"
#include "uart.h"
#define Device_Write_address 0xD0 /* Define RTC DS1307 slave address for write operation */
#define Device_Read_address 0xD1 /* Make LSB bit high of slave address for read operation */
#define TimeFormat12 0x40 /* Define 12 hour format */
#define AMPM 0x20
int second,minute,hour,day,date,month,year;
void TWI_init_master(void) // Function to initialize master
{
TWBR0=127; // Bit rate
TWSR0= (1<<TWPS1)|(1<<TWPS0); // Setting prescalar bits
// SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
}
uint8_t I2C_Start(char write_address); /* I2C start function */
uint8_t I2C_Repeated_Start(char read_address); /* I2C repeated start function */
void I2C_Stop(); /* I2C stop function */
void I2C_Start_Wait(char write_address); /* I2C start wait function */
uint8_t I2C_Write(char data); /* I2C write function */
int I2C_Read_Ack(); /* I2C read ack function */
int I2C_Read_Nack(); /* I2C read nack function */
void RTC_Read_Clock(char read_clock_address)
{
I2C_Start(Device_Write_address); /* Start I2C communication with RTC */
I2C_Write(read_clock_address); /* Write address to read */
I2C_Repeated_Start(Device_Read_address); /* Repeated start with device read address */
second = I2C_Read_Ack(); /* Read second */
minute = I2C_Read_Ack(); /* Read minute */
hour = I2C_Read_Nack(); /* Read hour with Nack */
I2C_Stop(); /* Stop i2C communication */
}
void RTC_Read_Calendar(char read_calendar_address)
{
I2C_Start(Device_Write_address);
I2C_Write(read_calendar_address);
I2C_Repeated_Start(Device_Read_address);
day = I2C_Read_Ack(); /* Read day */
date = I2C_Read_Ack(); /* Read date */
month = I2C_Read_Ack(); /* Read month */
year = I2C_Read_Nack(); /* Read the year with Nack */
I2C_Stop(); /* Stop i2C communication */
}
int main(void)
{
char buffer[20];
const char* days[7]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
UART_Init();
TWI_init_master();
sei();
I2C_Start(Device_Write_address); /* Start I2C communication with RTC */
I2C_Write(0); /* Write address to read */
I2C_Write(0x00); //sec
I2C_Write(0x00); //min /* Write address to read */
I2C_Write(0x17); //hour
I2C_Write(0x03); //tuesday
I2C_Write(0x23); //day
I2C_Write(0x09); //month
I2C_Write(0x21); //year
I2C_Stop(); /* Stop i2C communication */
while(1)
{
//TODO:: Please write your application code
RTC_Read_Clock(0);
//UART_Transmit(second);
sprintf(buffer, "\n%02x:%02x:%02x ", (hour & 0b00011111), minute, second);
UART_SendString(buffer);
RTC_Read_Calendar(3);
sprintf(buffer, "%02x/%02x/%02x %s", date, month, year,days[day-1]);
UART_SendString(buffer);
_delay_ms(1000);
}
}
uint8_t I2C_Start(char write_address) /* I2C start function */
{
uint8_t status; /* Declare variable */
TWCR0 = (1<<TWSTA)|(1<<TWEN)|(1<<TWINT); /* Enable TWI, generate start condition and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (start condition) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status != 0x08) /* Check weather start condition transmitted successfully or not? */
return 0; /* If not then return 0 to indicate start condition fail */
TWDR0 = write_address; /* If yes then write SLA+W in TWI data register */
TWCR0 = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status == 0x18) /* Check weather SLA+W transmitted & ack received or not? */
return 1; /* If yes then return 1 to indicate ack received i.e. ready to accept data byte */
if (status == 0x20) /* Check weather SLA+W transmitted & nack received or not? */
return 2; /* If yes then return 2 to indicate nack received i.e. device is busy */
else
return 3; /* Else return 3 to indicate SLA+W failed */
}
uint8_t I2C_Repeated_Start(char read_address) /* I2C repeated start function */
{
uint8_t status; /* Declare variable */
TWCR0 = (1<<TWSTA)|(1<<TWEN)|(1<<TWINT); /* Enable TWI, generate start condition and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (start condition) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status != 0x10) /* Check weather repeated start condition transmitted successfully or not? */
return 0; /* If no then return 0 to indicate repeated start condition fail */
TWDR0 = read_address; /* If yes then write SLA+R in TWI data register */
TWCR0 = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status == 0x40) /* Check weather SLA+R transmitted & ack received or not? */
return 1; /* If yes then return 1 to indicate ack received */
if (status == 0x20) /* Check weather SLA+R transmitted & nack received or not? */
return 2; /* If yes then return 2 to indicate nack received i.e. device is busy */
else
return 3; /* Else return 3 to indicate SLA+W failed */
}
void I2C_Stop() /* I2C stop function */
{
TWCR0=(1<<TWSTO)|(1<<TWINT)|(1<<TWEN); /* Enable TWI, generate stop condition and clear interrupt flag */
while(TWCR0 & (1<<TWSTO)); /* Wait until stop condition execution */
}
void I2C_Start_Wait(char write_address) /* I2C start wait function */
{
uint8_t status; /* Declare variable */
while (1)
{
TWCR0 = (1<<TWSTA)|(1<<TWEN)|(1<<TWINT); /* Enable TWI, generate start condition and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (start condition) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status != 0x08) /* Check weather start condition transmitted successfully or not? */
continue; /* If no then continue with start loop again */
TWDR0 = write_address; /* If yes then write SLA+W in TWI data register */
TWCR0 = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status != 0x18 ) /* Check weather SLA+W transmitted & ack received or not? */
{
I2C_Stop(); /* If not then generate stop condition */
continue; /* continue with start loop again */
}
break; /* If yes then break loop */
}
}
uint8_t I2C_Write(char data) /* I2C write function */
{
uint8_t status; /* Declare variable */
TWDR0 = data; /* Copy data in TWI data register */
TWCR0 = (1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (Write operation) */
status = TWSR0 & 0xF8; /* Read TWI status register with masking lower three bits */
if (status == 0x28) /* Check weather data transmitted & ack received or not? */
return 0; /* If yes then return 0 to indicate ack received */
if (status == 0x30) /* Check weather data transmitted & nack received or not? */
return 1; /* If yes then return 1 to indicate nack received */
else
return 2; /* Else return 2 to indicate data transmission failed */
}
int I2C_Read_Ack() /* I2C read ack function */
{
TWCR0=(1<<TWEN)|(1<<TWINT)|(1<<TWEA); /* Enable TWI, generation of ack and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (read operation) */
return TWDR0; /* Return received data */
}
int I2C_Read_Nack() /* I2C read nack function */
{
TWCR0=(1<<TWEN)|(1<<TWINT); /* Enable TWI and clear interrupt flag */
while (!(TWCR0 & (1<<TWINT))); /* Wait until TWI finish its current job (read operation) */
return TWDR0; /* Return received data */
}
Leave a Reply