Toll-Free Customer Support 24/7

8051 Development Board

/*
 * Project name:
    8051 Development Board
 * Copyright
     (c) Researchdesignlab.com
 * Description:
    
 * Test configuration:
     MCU:             AT89S52
     Dev.Board:       8051
     Oscillator:      11.0592 MHz
     Software:        Keil uVision3

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

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

void lcd_cmd_wt(unsigned char);		//To write the commands to the LCD
void lcd_data_wt(unsigned char);	//To write the data to the LCD
void delay();						//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
void busy();

//*****************************************
#define	LCD_PORT P2
sbit sda=P1^1;						//SDA line
sbit scl=P1^0;						//SCL line
sbit rs=P3^5;
sbit en=P3^7;
sbit D7=P2^7;
sbit rw=P3^6;	

					

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

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

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

	for(i=0;i<5;i++)									//To give LCD commands
	{
		temp=lcd_cmd[i];
		lcd_cmd_wt(temp);
	}
		
//*****************************************************
	
	rtc_init();	

//*****************************************************		
	delay();
	
	while(1)
	{	
		lcd_cmd_wt(0x80);
		
		i=0;
		while(mes_1[i])
		{
			lcd_data_wt(mes_1[i]);
			i++;
		}
			start_bit();
			delay();
			slave_add(0xd0);
			delay();
			slave_add(0x02);
			delay();
			start_bit();
			delay();
			slave_add(0xd1);
			delay();
			ch=data_rd_display();
			delay();
			stop_bit();  
			delay();
			display(ch);
			delay();
			delay();
         	delay();

			lcd_data_wt(':');

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

			lcd_data_wt(':');

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

//***********************************	
			
			lcd_cmd_wt(0xc0);

			i=0;
			while(mes_2[i])
			{
				lcd_data_wt(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);
			delay();
			delay();
	
			lcd_data_wt('-');

			start_bit();
			slave_add(0xd0);
			slave_add(0x05);
			start_bit();
			slave_add(0xd1);
			ch=data_rd_display();
			stop_bit();  
			display(ch);
			delay();
			delay();
	
			lcd_data_wt('-');

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


		
		}
}

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

void lcd_cmd_wt(unsigned char val)						//LCD command
{
	busy();
	LCD_PORT=val;
	rs=0;
	rw=0;
	en=1;
	en=0;
}

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

void lcd_data_wt(unsigned char ch)						//LCD data
{
    busy();
	LCD_PORT = ch;
	rs=1;
	rw=0;
	en=1;
	en=0;
}

//*****************************************************
void busy()
{
	D7=1;
	rs=0;                                          
	rw=1;
	while(D7!=0)
	{
		en=0;
		en=1;
	}
}
//****************************************************
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 delay()
{
	unsigned char z;
	for(z=250;z>0;z--)
	;
}	


void rtc_init()
{
	unsigned char a[]={0x00,0x00,0x01,0x50,0x02,0x06,0x03,0x02,0x04,0x07,0x05,0x12,0x06,0x07};
	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();                                        int RTC date and time
	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_data_wt(b1);
	b1=(a1 & 0x0f);
	b1=b1+0x30;
	lcd_data_wt(b1);
}


				

Back to top