gauravkothari23
Advanced Member level 2
Hello All.
I am trying to Interface RTC DS1307 with NUC029LAN Controller and display the date and time on LCD.
but the code is not working with NUC029LAN controller.
The same code i tried using 89S52 controller, but it works perfectly. (Code attached)
i have also tried increasing the Delay but still it does not work.
also tried to change the Pull Up resistor to 1K, 4K7, 10K.
Can anybody please let me know, where the issue is,
I am trying to Interface RTC DS1307 with NUC029LAN Controller and display the date and time on LCD.
but the code is not working with NUC029LAN controller.
The same code i tried using 89S52 controller, but it works perfectly. (Code attached)
i have also tried increasing the Delay but still it does not work.
also tried to change the Pull Up resistor to 1K, 4K7, 10K.
Can anybody please let me know, where the issue is,
C:
#include <stdio.h>
#include "NUC029xAN.h"
#define SCL P36 // DS1307 SCL
#define SDA P37 // DS1307 SDA
#define BITT6 0x40 //12 hour format set bit
#define BITT5 0x20
#define DS1307_ID 0xD0 // DS1307 ID
#define SEC_ADDRESS 0x00 // Address to access Ds1307 SEC register
#define DATE_ADDRESS 0x04 // Address to access Ds1307 DATE register
#define CONTROL 0x07 // Address to access Ds1307 CONTROL register
unsigned char sec,min,hour,day,month,year,AM,PM;
int32_t main(void)
{
unsigned int i;
while(1)
{
msdelay(20);
DS1307_Init();
msdelay(20);
LCD_init();
lcdclear();
lcdrow2();
LCD_puts(" TESTING RTC ");
DS1307_SetDate(0x24,0x04,0x21);
DS1307_SetTime(0x10,0x10,0x10);
lcdclear();
for(i=0;i<10;i++)
{
i=0;
DS1307_GetDate(&day,&month,&year);
DS1307_GetTime(&sec,&min,&hour);
lcdrow2();
send2lcd(day);
LCD_puts("-");
send2lcd(month);
LCD_puts("-");
send2lcd(year);
LCD_puts(" ");
if(hour&BITT5)
{
AM=0;
}
else
{
AM=1;
}
send2lcd(0x1F&hour);
LCD_puts("-");
send2lcd(min);
LCD_puts("-");
send2lcd(sec);
}
}
} // MAIN ENDS HERE
void msdelay(unsigned int value)
{
unsigned int i,j;
for(i=0;i<value;i++)
for(j=0;j<2000;j++);
}
// DS-1307 CODE START HERE
void I2C_Clock(void)
{
delay_us(1);
SCL = 1;
delay_us(1);
SCL = 0;
}
void I2C_Start()
{
SCL = 0; // Pull SCL low
SDA = 1; // Pull SDA High
delay_us(1);
SCL = 1; //Pull SCL high
delay_us(1);
SDA = 0; //Now Pull SDA LOW, to generate the Start Condition
delay_us(1);
SCL = 0; //Finally Clear the SCL to complete the cycle
}
void I2C_Stop(void)
{
SCL = 0; // Pull SCL low
delay_us(1);
SDA = 0; // Pull SDA low
delay_us(1);
SCL = 1; // Pull SCL High
delay_us(1);
SDA = 1; // Now Pull SDA High, to generate the Stop Condition
}
void I2C_Write(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++) // loop 8 times to send 1-byte of data
{
SDA = dat & 0x80; // Send Bit by Bit on SDA line
I2C_Clock(); // Generate Clock at SCL
dat = dat<<1;
}
SDA = 1; // Set SDA at last
}
unsigned char I2C_Read(void)
{
unsigned char i,
dat=0x00;
SDA=1; //Make SDA as I/P
for(i=0;i<8;i++) // loop 8times to read 1-byte of data
{
delay_us(1);
SCL = 1; // Pull SCL High
delay_us(1);
dat = dat<<1; //dat is Shifted each time and
dat = dat | SDA; //ORed with the received bit to pack into byte
SCL = 0; // Clear SCL to complete the Clock
}
return dat; // Finally return the received Byte*
}
void I2C_Ack()
{
SDA = 0; //Pull SDA low to indicate Positive ACK
I2C_Clock(); //Generate the Clock
SDA = 1; // Pull SDA back to High(IDLE state)
}
void I2C_NoAck()
{
SDA = 1; //Pull SDA high to indicate Negative/NO ACK
I2C_Clock(); // Generate the Clock
SCL = 1; // Set SCL */
}
void delay_us(unsigned int us_count)
{
while(us_count!=0)
{
us_count--;
}
}
void DS1307_Init()
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // Connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(CONTROL); // Select the Ds1307 ControlRegister to configure Ds1307
DS1307_Write(0x00); // Write 0x00 to Control register to disable SQW-Out
I2C_Stop(); // Stop I2C communication after initilizing DS1307
}
void DS1307_Write(unsigned char dat)
{
I2C_Write(dat); // Connect to DS1307 by sending its ID on I2c Bus
I2C_Clock();
}
unsigned char DS1307_Read()
{
unsigned char dat;
dat = I2C_Read(); // Connect to DS1307 by sending its ID on I2c Bus
return(dat);
}
void DS1307_SetTime(unsigned char ss, unsigned char mm, unsigned char hh)
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(SEC_ADDRESS); // Select the SEC RAM address
DS1307_Write(ss); // Write sec on RAM address 00H
DS1307_Write(mm); // Write min on RAM address 01H
if(AM==1)
DS1307_Write(BIT6|hh); // Write hour on RAM address 02H
else
DS1307_Write(BIT5|BIT6|hh);
I2C_Stop(); // Stop I2C communication after Setting the Time
}
void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy)
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
DS1307_Write(dd); // Write date on RAM address 04H
DS1307_Write(mm); // Write month on RAM address 05H
DS1307_Write(yy); // Write year on RAM address 06h
I2C_Stop(); // Stop I2C communication after Setting the Date
}
void DS1307_GetTime(unsigned char *s_ptr,unsigned char *m_ptr,unsigned char *h_ptr)
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(SEC_ADDRESS); // Request Sec RAM address at 00H
I2C_Stop(); // Stop I2C communication after selecting Sec Register
I2C_Start(); // Start I2C communication
DS1307_Write(0xD1); // connect to DS1307( under Read mode) //by sending its ID on I2c Bus
*s_ptr = DS1307_Read();
I2C_Ack(); // read second and return Positive ACK
*m_ptr = DS1307_Read();
I2C_Ack(); // read minute and return Positive ACK
*h_ptr = DS1307_Read();
I2C_NoAck(); // read hour and return Negative/No ACK
I2C_Stop(); // Stop I2C communication after reading the Time
}
void DS1307_GetDate(unsigned char *d_ptr,unsigned char *m_ptr,unsigned char *y_ptr)
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H
I2C_Stop(); // Stop I2C communication after selecting DAY Register
I2C_Start(); // Start I2C communication
DS1307_Write(0xD1); // connect to DS1307( under Read mode) // by sending its ID on I2c Bus
*d_ptr = DS1307_Read();
I2C_Ack(); // read Day and return Positive ACK
*m_ptr = DS1307_Read();
I2C_Ack(); // read Month and return Positive ACK
*y_ptr = DS1307_Read();
I2C_NoAck(); // read Year and return Negative/No ACK
I2C_Stop(); // Stop I2C communication after reading the Time
}
// LCD CODE START HERE
void LCD_delay(unsigned char ms)
{
unsigned char n;
unsigned int i;
for (n=0; n<ms; n++)
{
for (i=0; i<LCD_DELAY; i++); /* For 1 ms */
}
}
void LCD_enable()
{
LCD_en = 0; /* Clear bit*/
LCD_delay(5);
LCD_en = 1; /* Set bit */
}
void LCD_command(unsigned char command)
{
unsigned long temp;
temp = P1->PIN;
LCD_rs = 0; /* Clear bit */
P1->DOUT = (temp & 0xFFFFFFF0)|((command>>4) & 0xFFFFFF0F);
LCD_enable();
temp = P1->PIN;
P1->DOUT = (temp & 0xFFFFFFF0)|(command & 0xFFFFFF0F);
LCD_enable();
LCD_delay(5);
}
void LCD_putc(unsigned char ascii)
{
unsigned long temp;
temp = P1->PIN;
LCD_rs = 1;
P1->DOUT = (temp & 0xF0)|((ascii>>4) & 0x0F);
LCD_enable();
temp = P1->PIN;
P1->DOUT = (temp & 0xF0)|(ascii & 0x0F);
LCD_enable();
LCD_delay(5);
}
void LCD_puts(unsigned char *lcd_string)
{
while (*lcd_string)
{
LCD_putc(*lcd_string++);
}
}
void LCD_init() // INTERFACE THE LCD IN 4 BIT MODE
{
LCD_en=1; /* Set bit */
LCD_rs=0; /* Clear bit */
LCD_command(0x33);
LCD_command(0x32);
LCD_command(0x28);
LCD_command(0x0C);
LCD_command(0x06);
LCD_command(0x01); /* Clear */
msdelay(400);
}
void send2lcd(unsigned char value)
{
unsigned char buf = 0;
buf = value & 0xF0; /* Filter for high byte */
buf = (buf>>4)|(0x30); /* Convert to ascii code */
LCD_putc(buf); /* Show on LCD */
buf = value & 0x0F; /* Filter for low byte */
buf = buf | 0x30; /* Convert to ascii code */
LCD_putc(buf); /* Show on LCD */
}
Last edited: