#include <htc.h>
#define _XTAL_FREQ 4000000 // 4 MHz clock
__CONFIG(0X3F39);
#define LCD_EN RB3
#define LCD_RS RB5
#define LCD_RW RB4
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
unsigned char date [5] = {'D','a','t','e',':'};
unsigned char time [5] = {'T','i','m','e',':'};
unsigned char I2CData[8] = {0x00 , 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00};
unsigned char tmp;
void I2CInit(void){
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */
}
void I2CStart(){
SEN = 1; /* Start condition enabled */
while(SEN); /* automatically cleared by hardware */
/* wait for start condition to finish */
}
void I2CStop(){
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
void I2CRestart(){
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
}
void I2CAck(){
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak(){
ACKDT = 1; /* Acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */
}
void I2CSend(unsigned char dat){
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
unsigned char I2CRead(void){
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
I2CWait(); /* wait to check any pending transfer */
return temp; /* Return the read data from bus */
}
void lcddata(unsigned char value)
{
LCD_DATA = value;
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void lcdcmd(unsigned char value)
{
LCD_DATA = value;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
}
void bcdToascii(unsigned char value){
tmp = value;
tmp = tmp & 0xf0;
tmp = tmp >> 4;
tmp = tmp | 0x30;
lcddata(tmp);
tmp = value;
tmp = tmp & 0x0f;
tmp = tmp | 0x30;
lcddata(tmp);
}
//-------------------------------------
void display (){
lcdcmd(0x80);
for (char i=0;i<5;i++)
{
lcddata(time[i]);
}
bcdToascii(I2CData[3]);
lcddata('-');
bcdToascii(I2CData[2]);
lcddata('-');
bcdToascii(I2CData[1]);
lcdcmd(0xc0);
for (char j=0;j<5;j++)
{
lcddata(date[j]);
}
bcdToascii(I2CData[5]);
lcddata('-');
bcdToascii(I2CData[6]);
lcddata('-');
bcdToascii(I2CData[0]);
}
//-------------------------------------
void lcd_init(){
TRISD = 0;
TRISB = 0;
LCD_EN =0;
__delay_ms(175);
lcdcmd(0x38);
__delay_ms(175);
lcdcmd(0x0e);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);
}
//-------------------------------------
void main ()
{
lcd_init();
while(1){
i2c();
display();
}
}
i2c(){
I2CInit(); /* Initialize I2C Port */
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck();
I2CSend(0x00); /* Send subaddress 0x00, we are writing to this location */
I2CAck();
I2CRestart(); /* send slave address with read bit set */
I2CSend(0xD1); /* Loop to read 8 bytes from I2C slave */
I2CData[0]=I2CRead();
I2CAck();
I2CData[1]=I2CRead();
I2CAck();
I2CData[2]=I2CRead();
I2CAck();
I2CData[3]=I2CRead();
I2CAck();
I2CData[4]=I2CRead();
I2CAck();
I2CData[5]=I2CRead();
I2CAck();
I2CData[6]=I2CRead();
//I2CNak();
I2CStop(); /* Send stop */
/* end of program */
}
Vbat
Backup Supply Input for Any Standard 3V Lithium Cell or Other Energy Source. Battery
voltage must be held between the minimum and maximum limits for proper operation. Diodes in series
between the battery and the VBAT pin may prevent proper operation. If a backup supply is not
required, VBAT must be grounded. The nominal power-fail trip point (VPF) voltage at which access to
the RTC and user RAM is denied is set by the internal circuitry as 1.25 x VBAT nominal. A lithium
battery with 48mAh or greater will back up the DS1307 for more than 10 years in the absence of power at +25°C.
You can debug project throw USART.
Dear Alex
Thanks for reply
I think you mean write the value hex 10 to the address of 07 of RTC
Am I correct
View attachment 76314
Please help
i2c(){
I2CInit(); /* Initialize I2C Port */
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck();
I2CSend(0x07);
I2CAck();
I2CSend(0x10);
//I2CStop(); /* Send subaddress 0x00, we are writing to this location */
I2CAck();
I2CRestart(); /* send slave address with read bit set */
I2CSend(0xD1); /* Loop to read 8 bytes from I2C slave */
//I2CAck();
I2CData[0]=I2CRead();
I2CAck();
I2CData[1]=I2CRead();
I2CAck();
I2CData[2]=I2CRead();
I2CAck();
I2CData[3]=I2CRead();
I2CAck();
I2CData[4]=I2CRead();
I2CAck();
I2CData[5]=I2CRead();
I2CAck();
I2CData[6]=I2CRead();
I2CNak();
I2CStop(); /* Send stop */
/* end of program */
}
Dear papunbig
Thanks for reply
can we read what is the available data from TRC without write to it?
I think RTC starts to increment second when we power up it
Please advice
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck();
I2CSend(0x07);
I2CAck();
I2CSend(0x10);
//I2CStop(); /* Send subaddress 0x00, we are writing to this location */
I2CAck();
I2CRestart(); /* send
I2CInit(); /* Initialize I2C Port */
I2CStart(); /* Send Start condition */
I2CSend(0xD0); /* Send DS1307 slave address with write operation */
I2CAck();
I2CSend(0x00);
I2CAck();
[COLOR="#FF0000"]I2CSend(0x00); //clock halt (CH) bit[/COLOR]
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?