0% found this document useful (0 votes)
42 views

RTC - To Write Some Initial Value To The Clock and Read Back

This code writes an initial value of 0x00 to the seconds register of a real-time clock (RTC), then reads that value back and displays it. It first writes the RTC to the off state, writes the initial value, then writes the RTC to the on state. It continuously reads the seconds register value, displays it, and verifies it has incremented since the RTC is running.

Uploaded by

axeray
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

RTC - To Write Some Initial Value To The Clock and Read Back

This code writes an initial value of 0x00 to the seconds register of a real-time clock (RTC), then reads that value back and displays it. It first writes the RTC to the off state, writes the initial value, then writes the RTC to the on state. It continuously reads the seconds register value, displays it, and verifies it has incremented since the RTC is running.

Uploaded by

axeray
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

RTC – To write some initial value to the clock and read

back

Connections : Port 0 Upper is connected to SCL and SDA, Pins 1 and 0

respectively. Connectors K 21 and K 3 are to be connected

(Zone – F and Zone – G)

Source Code :

#include<reg52.h>

sbit buzz = P0^6;

sbit SCL = P0^5;

sbit SDA = P0^4;

#define RTC_WRITE 0xA0

#define RTC_READ 0xA1

#define CONTROL_REG 0x00

#define RTC_OFF 0x80

#define RTC_ON 0x00

#define SECONDS_REG 0x02

#define SECONDS_VAL 0x00

#define NO_ACK 0
unsigned int j;

void write_rtc(unsigned char loc, unsigned char dat);

unsigned char read_rtc(unsigned char loc);

void start(void);

void stop(void);

unsigned char read(bit ack);

bit write(unsigned char dat);

bit in;

void delay(unsigned int del) {

while(del--)

void start(void)

SDA = 1;

SCL = 1;

delay(100);

SDA = 0;

SCL = 0;

void stop(void)
{

SDA = 0;

SCL = 1;

delay(100);

SDA = 1;

SCL = 0;

bit write(unsigned char dat)

unsigned char mask;

bit ack = 0;

for(mask = 0x80; mask; mask >>= 1)

if(dat & mask)

SDA = 1;

else

SDA = 0;

SCL = 1;

SCL = 0;

SDA = 1;

SCL = 1;

ack = SDA;
SCL = 0;

return(ack);

unsigned char read(bit ack)

unsigned char temp = 0x00, g;

for(g = 0; g < 8; g++)

SCL = 1;

temp <<= 1;

if(SDA)

temp |= 0x01;

SCL = 0;

if(ack)

SDA = 0;

SCL = 1;

delay(100);

SCL = 0;

SDA = 1;

else
{

SDA = 1;

SCL = 1;

SCL = 0;

//P2 = 8;

return(temp);

void main (void) {

unsigned char val;

buzz = 1;

SCL = 0;

start();

if(!(write(RTC_WRITE))) {

if(!write(CONTROL_REG)) {

if(!write(RTC_OFF)) {

start();

if(!write(RTC_WRITE)) {

if(!write(SECONDS_REG)) {

if(!(write(SECONDS_VAL))) {
start();

if(!write(RTC_WRITE)) {

if(!write(CONTROL_REG))
{

if(!write(RTC_ON))

P1 = 0x00;

stop();

while(1) {

start();

if(!write(RTC_WRITE)) {

if(!write(SECONDS_REG)) {

start();

if(!write(RTC_READ)) {

val = read(NO_ACK);

P1 = 0x01;

}
}

stop();

P2 = val ;

Output : A value is written into location 0x02 of the RTC and is read back and

displayed on the BCD decoder based display. Since 00 is written into

the Control Register (0x00), the RTC is on. Therefore the value read

back and the value written in will differ by a small margin, as the clock

is running.

You might also like