Toll-Free Customer Support 24/7

#include "reg51.h"
#include <intrins.h>
#include <stdio.h>

#define LCD_Port P2

sbit rs=P2^0; /* Register select pin */
sbit en=P2^1; /* Enable pin */

//*****************************************

void delay12(); //Delay for LCD
void start_bit(); //Condition for start bit to the I2C
void stop_bit(); //Condition for stop bit to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
void rtc_init();
unsigned char data_rd_display(); //
void display(unsigned char); //To read the data from EEPROM and display on LCD

//*****************************************

sbit sda=P3^3; //SDA line
sbit scl=P3^2;

void start_bit(); //Condition for start bit to the I2C
void stop_bit(); //Condition for stop bit to the I2C
void slave_add(unsigned char); //To send bit by bit data to the slave
void rtc_init();

/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/

void delay12()
{
int i,j;
for(i=0;i<1;i++)
for(j=0;j<112;j++);
}

void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<112;j++);
}
void LCD_Command (char cmnd) /* LCD16x2 command funtion */
{
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0);/* Send upper nibble */
rs=0; /* Command reg. */
en=1;
delay(1);
en=0;
delay(2);

LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}

void LCD_Char (char char_data) /* LCD data write function */
{
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0);/* Send upper nibble */
rs=1; /*Data reg.*/
en=1;
delay(1);
en=0;
delay(2);

LCD_Port = (LCD_Port & 0x0F) | (char_data << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);

}

void LCD_String (char *str) /* Send string to LCD function */
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]); /* Call LCD data write */
}
}

void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD function */
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str); /* Call LCD string function */
}

void LCD_Init (void) /* LCD Initialize function */
{
delay(20); /* LCD Power ON Initialization time >15ms */
LCD_Command (0x02); /* 4bit mode */
LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit mode */
LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}

void main()

{
unsigned char lcd_cmd[]={0x38,0x0f,0x01,0x06,0X80}; //Commands for the LCD
unsigned char i,ch;
unsigned char mes_1[]="TIME ";
unsigned char mes_2[]="DATE ";

//*****************************************************

LCD_Init();

//*****************************************************

rtc_init();

//*****************************************************
delay12();

while(1)
{
LCD_Command(0x80);

i=0;
while(mes_1[i])
{
LCD_Char(mes_1[i]);
i++;
}
start_bit();
delay12();
slave_add(0xd0);
delay12();
slave_add(0x02);
delay12();
start_bit();
delay12();
slave_add(0xd1);
delay12();
ch=data_rd_display();
delay12();
stop_bit();
delay12();
display(ch);
delay12();
delay12();
delay12();

LCD_Char(':');

start_bit();
delay12();
slave_add(0xd0);
delay12();
slave_add(0x01);
delay12();
start_bit();
delay12();
slave_add(0xd1);
delay12();
ch=data_rd_display();
delay12();
stop_bit();
delay12();
display(ch);
delay12();
delay12();
delay12();

LCD_Char(':');

start_bit();
slave_add(0xd0);
slave_add(0x00);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();

//***********************************

LCD_Command(0xc0);

i=0;
while(mes_2[i])
{
LCD_Char(mes_2[i]);
i++;
}
start_bit();
slave_add(0xd0);
slave_add(0x04);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();

LCD_Char('-');

start_bit();
slave_add(0xd0);
slave_add(0x05);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();

LCD_Char('-');

start_bit();
slave_add(0xd0);
slave_add(0x06);
start_bit();
slave_add(0xd1);
ch=data_rd_display();
stop_bit();
display(ch);
delay12();
delay12();



}
}

//***********************************************
void start_bit()
{
sda=1;
scl=1; //Start bit condition
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}

//*****************************************************

void stop_bit() //Stop bit condition
{
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}

//******************************************************

void slave_add(unsigned char add)
{
unsigned char t,i;
t=add;

for(i=0;i<8;i++)
{
t=(t&0x80);
if(t!=0)
sda=1;
else
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
t=add<<1;
add=t;
}
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
}

//*******************************************************

unsigned char data_rd_display()
{
unsigned char i,x=0;
unsigned char y;
y=0;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
_nop_();
_nop_();
_nop_();
scl=1;
if(sda==1)
x=x|0x01;
y=x;
x=x<<1;
scl=0;
}

sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
return(y);
}

//*******************************************************


void rtc_init()
{
unsigned char a[]={0x00,0x00,0x01,0x50,0x02,0x06,0x03,0x02,0x04,0x11,0x05,0x12,0x06,0x12};
unsigned char x=0;

start_bit();
slave_add(0xd0);
slave_add(a[0]);
slave_add(a[1]);
stop_bit();
start_bit();
slave_add(0xd0);
slave_add(a[2]);
slave_add(a[3]);
stop_bit();

start_bit();
slave_add(0xd0);
slave_add(a[4]);
slave_add(a[5]);
stop_bit();

start_bit();
slave_add(0xd0);
slave_add(a[8]);
slave_add(a[9]);
stop_bit();

start_bit();
slave_add(0xd0);
slave_add(a[10]);
slave_add(a[11]);
stop_bit();

start_bit();
slave_add(0xd0);
slave_add(a[12]);
slave_add(a[13]);
stop_bit();

}

void display(unsigned char a1)
{
unsigned char b1;
b1=(a1 & 0x0f0);
b1=(b1>>4)+0x30;
LCD_Char(b1);
b1=(a1 & 0x0f);
b1=b1+0x30;
LCD_Char(b1);
}

Back to top