Sie sind auf Seite 1von 9

IMPLEMENTATION OF REAL TIME CLOCK CONTROLLER USING

EMBEDDED MICROCONTROLLER

PROGRAM:
#include <REG51xD2.H>
#include <stdio.h>
#define CR 0x0d
void Read_RTC(void);
void Set_RTC(void);
void Backspace(void);
void space();
void delete(void);
void Disp_Month(void);
unsigned char Units_Tens(unsigned int);
unsigned char xdata porta _at_ 0xe000;
unsigned char xdata portb _at_ 0xe001;
unsigned char xdata portc _at_ 0xe002;
unsigned char xdata control _at_ 0xe003;
unsigned char xdata parameter[15] = {0x0,0x1,0x0,0x1,0x1,0x1,0x6,0x6,0x0,0x1,0x0,0x07,0x0};
unsigned char xdata week[15][15] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
/*Send the hexadecimal value to the UART*/
void sendhex(int hex)
{
if(hex > 9)
putchar('A' + (hex - 9));
else
putchar('0' + hex);
}
void sendstr (char *p) { /* Write string */
while (*p) {
putchar (*p++);
}
}
/*Function to clear the displayed results*/
void Backspace()
{
putchar(0x08);
}
/*Function to insert spaces between the display*/
void space()
{
putchar(0x20);
putchar(0x20);
putchar(0x20);
}
/*Function to club the units and tens of the value passed to it as parameter*/
unsigned char Units_Tens(unsigned int i)
{
unsigned char temp = 0,temp1 = 0;
temp = parameter[i] & 0x0f;
i ++;
temp1 = parameter[i] & 0x0f;
temp1 <<= 4;
temp1 |= temp;
return temp1;
}

/*Function to display the RTC values*/
void display()
{
unsigned int j = 0,temp = 0,temp1 = 0;
unsigned char c;
j = 0x04; /*Read the hour value*/
c = Units_Tens(j);
c &= 0x3f;
temp1 = c;
sendhex((c & 0xf0) >> 4); /*Display the hour value*/
sendhex(temp1 & 0x0f);
space();
j = 0x02; /*Read the minutes*/
c = Units_Tens(j);
temp1 = c;
sendhex((c & 0xf0) >> 4); /*Display the minutes*/
sendhex(temp1 & 0x0f);
space();
j = 0x00; /*Read the seconds value*/
c = Units_Tens(j);
temp1 = c;
sendhex((c & 0xf0) >> 4); /*Display the seconds*/
sendhex(temp1 & 0x0f);
space();
j = 0x5; /*Read the week*/
c = (parameter[j] & 0x08);
if(c != 0x00)
{
putchar('-'); /*send hyphens to display*/
putchar('-');
putchar('-');
space();
Disp_Month(); /*Display month of the year*/
}
else
{
temp = (parameter[j] & 0x04);
if(temp != 0x00) /*Check for the time*/
{ /*whether ii is set in AM or PM*/
sendstr("PM"); /*Send PM string to display*/
space();
Disp_Month();
}
else
{
sendstr("AM"); /*Send AM string to display*/
space();
Disp_Month();
}
}
}
/*Function to read the RTC parameters*/
void Read_RTC(void)
{
unsigned int i = 0,j = 0,index = 0, k = 0;
unsigned char data1 = 0;
unsigned char c = 0,temp = 0,temp1 = 0;
control = 0x90;
portc = 0x10; /*HOLD signal high*/
while(i != 0xff)
{
i ++;
}
portc = 0x30; /*READ signal high*/
while(j != 0xd)
{
index = (j & 0x0f);
portb = index; /*Set the register address*/
for(k = 0; k <= 1000; k ++);
data1 = porta & 0xff;
parameter[j] = data1 & 0x0f;
j ++;
}
portc = 0x00;
}
/*Function to clear the display and update the same with new values*/
void delete()
{
unsigned int i = 0;
for (i = 0; i <= 0x22; i ++)
{
Backspace();
}
}
void Disp_Month(void)
{
unsigned int j = 0, i = 0, a = 0;
unsigned char c = 0,temp = 0;
j = 0x6; /*Read the week*/
c = parameter[j];
j = c;
sendstr(week[j]); /*Display the week*/
putchar(0x20);
putchar(0x20);
j = 0x7; /*Read the date*/
c = Units_Tens(j);
temp = c;
sendhex((c & 0xf0) >> 4); /*Display the date*/
sendhex(temp & 0x0f);
putchar('-');
j = 0x09; /*Read the month of the year*/
c = Units_Tens(j);
temp = c;
sendhex((c & 0xf0) >> 4); /*Display the month*/
sendhex(temp & 0x0f);
putchar('-');
j = 0xb; /*Read the year value*/
c = Units_Tens(j);
temp = c;
sendhex((c & 0xf0) >> 4); /*Display the year*/
sendhex(temp & 0x0f);
for(a=0x00;a<=32000;a++); /* Delay */
delete(); /*Clear the display*/
}
/*Function to set RTC parameters*/
void Set_RTC(void)
{
unsigned int i = 0,j = 0,count = 0,k = 0;
portc = 0x10;
while(i != 0xff)
{
i ++;
}
i = 0;
while(count != 0x0d)
{
portb = count;
porta = parameter[j];
portc = 0x50;
for(k = 0; k <= 1000; k ++);
portc = 0x10;
count ++;
j ++;
}
portc = 0x00;
}
void main()
{
SCON = 0x52;
TMOD |= 0x20;
TH1 = 0xe6;
TR1 = 1;
control = 0x80;
Set_RTC();
getchar();
sendstr ("\n\nHRS MIN SEC A/P DAY DD-MM-YY ");
sendstr ("\n07 45 05 AM SUN 3- 03- 2013\n");
while(1)
{
Read_RTC(); /*Read RTC parameters*/
Read_RTC();
display(); /*Display the read RTC parameters*/
Read_RTC();
}
}













OUTPUT:

HRS MIN SEC A/P DAY DD-MM-YY
--- --- --- --- --- -------------
07 45 05 AM SUN 3- 03- 2013

Das könnte Ihnen auch gefallen