Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing RTC with 8952 code problem

Status
Not open for further replies.

southafrikanse

Junior Member level 1
Joined
Jun 12, 2007
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Amora, Portugal
Activity points
1,418
ic 8952

Hello

I'm using the DS12887A RTC to interface with my microcontroller AT89S8252.

What do I need, code speaking, to interface it with my µC?

Do I have to inicialize it? How can I send and wright on the RTC using C?

This is urgent and any help is welcomed.
 

at89s8253.h

This should be a good starting point:



As far as the initialization is concerned, you have to turn the oscillator on .. see attached picture ..

Regards,
IanP
 

1307 time delay i rtc

Hi

Good choice, A nice RTC

I think i had found some full code in C for 12887 in 8052.com

Please search with ds12887 in 8052.com

Regards
Nandhu
 

const unsigned char * daystr[7] = {{sun},

Refer www.Microchipc.com For sample coding of interfacing the DS1307 to Controller.

Make RTC board with crystal near to that IC . which gives Higher accuracy .
 

brief in of ic 8952

Here is what you want

**broken link removed**

Regards
Nandhu
 

89s52 rtc12887 interfacing

masud58 said:
ds12887 may be obsulate. try another like 1307.

hi masud..

i am using RTC DS1307 with 89S52 and need help..

can you please help me..

i have posted in forum. please check this link::


thanks to you..
 

if else for sbuf mcs-51 pdf

Dear sir,
Need a code in c for interfacing 1288 with 8051.Looking for a simple code,easy to understand.Any changes in the code given in mazdi book.Please help if possible.Thankyou
 

how to code 8952 ic

Here are some bits of code pulled from a larger program. Possibly there may be some useful bits. Some of it is not relevant, so just study the clocks/I2C bits etc.

#pragma SMALL // Small memory model

// Global Includes
#include <AT89S8253.h>
#include <stdio.h>
#include <stdlib.h>
#include "delay.h"
#include "lcd.h"

#define ResetWD() WDTCON = WDTCON | 0x02

//Bit Outputs
#define DoutEn P3_2 // Active High
#define DinEn P3_3 // Active Low
#define BackLight P2_6
#define ExtSound P2_5
#define IntSound P2_4
#define Spare2 P2_1
#define PrintOut P2_0

// Bit Inputs Active Low
#define IP1 P3_5
#define IP2 P3_6
#define IP3 P3_7

#define SDA P0_1 // connect to SDA pin (Data)
#define SCL P0_0 // connect to SCL pin (Clock)
#define Set P0_2 // Set Clock
#define Up P0_3 // Increment
#define Down P0_4 // Decrement

#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01

const unsigned char * DayStr[7] = {{"Sun"},
{"Mon"},
{"Tue"},
{"Wed"},
{"Thu"},
{"Fri"},
{"Sat"}};


const unsigned char * MonthStr[12] ={{"Jan"},
{"Feb"},
{"Mar"},
{"Apr"},
{"May"},
{"Jun"},
{"Jul"},
{"Aug"},
{"Sep"},
{"Oct"},
{"Nov"},
{"Dec"}};


// Variables (Global)
char buf[16]; // LCD Display Buffer
unsigned char Data, i, p, Minute=30, Hour=12, Day=3, Date=15, Month=6, Year=9;
unsigned char LcdMessage;
unsigned char idata RTC_ARR[7]; // Buffer for second,minute,.....,year
unsigned char idata Store[82];//,RxBuf[16];
bit Cancel, Flag, PrintFlag, Bleep;

//////////////////////////////////////////////////////////////////////
//Prototypes
void serial_init(void);
void ReadRTC(unsigned char * buff);
void WriteRTC(unsigned char * buff);
char * Int2Day(unsigned char day);
char * Int2Month(unsigned char month);
void Convert(void);
void WriteTime(void);
void GetTime (void);
void SetTime (void);
void TimeDelay (void);
void DisplayIt (void);

////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------
// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;

}

//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
SDA = 1;
SCL = 1;
DelayMs(1);DelayMs(1);
SDA = 0;
DelayMs(1);DelayMs(1);
SCL = 0;
DelayMs(1);DelayMs(1);
}

//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
SDA = 0;
DelayMs(1);DelayMs(1);
SCL = 1;
DelayMs(1);DelayMs(1);
SDA = 1;
}

//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{
unsigned char i;
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}

SCL = 1;
DelayMs(1);DelayMs(1);
SCL = 0;

}

//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{

unsigned char Data=0,i;

SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
DelayMs(1);
}

if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK

DelayMs(1);DelayMs(1);
SCL = 1;
DelayMs(1);DelayMs(1);
SCL = 0;

return Data;
}

//-------------------------------
// Read RTC (all real time)
//-------------------------------
void ReadRTC(unsigned char * buff)
{


Start();
WriteI2C(0xD0);
WriteI2C(0x00);

Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
Stop();
}

//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{

Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
Stop();
}

//-------------------------------
// Convert date (BCD) to string of Day
// 1=Sanday
// 2=Monday
// And so on
//-------------------------------
char * Int2Day(unsigned char day)
{
return DayStr[day-1];
}

//-------------------------------
// Convert month (BCD) to string of Month
// 0x01=January
// 0x02=February
// ...........
// 0x12 = December
// And so on
//-------------------------------
char * Int2Month(unsigned char month)
{
return MonthStr[BCD2HEX(month)-1];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Convert(void)
{
ResetWD();
if (Minute >49 && Minute < 60)Minute = Minute + 30;
if (Minute >39 && Minute < 50)Minute = Minute + 24;
if (Minute >29 && Minute < 40)Minute = Minute + 18;
if (Minute >19 && Minute < 30)Minute = Minute + 12;
if (Minute >9 && Minute < 20)Minute = Minute + 6;

if (Hour >19 && Hour < 24)Hour = Hour + 12;
if (Hour >9 && Hour < 20)Hour = Hour + 6;

if (Date >29) Date = Date + 18;
if (Date >19 && Date < 30)Date = Date + 12;
if (Date >9 && Date < 20)Date = Date + 6;

if (Month >9 && Month < 13)Month = Month + 6;

if (Year >49 && Year < 60)Year = Year + 30;
if (Year >39 && Year < 50)Year = Year + 24;
if (Year >29 && Year <40) Year = Year + 18;
if (Year >19 && Year < 30)Year = Year + 12;
if (Year >9 && Year < 20)Year = Year + 6;
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void GetTime(void)
{
ReadRTC(&RTC_ARR[0]);

if (PrintFlag == 0)
{
ResetWD();
if (RI) {Data = SBUF; RI=0;}
printf("%s - ",Int2Day(RTC_ARR[3]));
if (RI) {Data = SBUF; RI=0;}
printf("%02bX/%s/20%02bX - ",RTC_ARR[4],Int2Month(RTC_ARR[5]),RTC_ARR[6]);
if (RI) {Data = SBUF; RI=0;}
printf("%02bX:%02bX:%02bX\r\n",RTC_ARR[2],RTC_ARR[1],RTC_ARR[0]);
if (RI) {Data = SBUF; RI=0;}
ResetWD();
printf ("\r\n");
ResetWD();
}

if (PrintFlag == 1)
{
lcd_goto(0x00);
ResetWD();
if (RI) {Data = SBUF; RI=0;}
sprintf (buf,"%02bX/%02bX/%02bX %02bX:%02bX",RTC_ARR[4],RTC_ARR[5],RTC_ARR[6],RTC_ARR[2],RTC_ARR[1]);
if (RI) {Data = SBUF; RI=0;}
lcd_puts(buf);
ResetWD();

}
}


////////////////////////////////////////////////////////////////////////////////
//-----------------------------------
// Setup time and enable oscillator
//-----------------------------------
void SetTime(void)
{
PrintFlag=1;
lcd_goto(0x00);
lcd_puts(" Minute 0-59 ");
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Minute++; while (Up==0) {ResetWD();};
if (Down == 0) Minute--; while(Down==0){ResetWD();};
if (Minute <1)Minute=59;
if (Minute >59)Minute=1;
lcd_puts(" ");
sprintf(buf,"Minute: %bu",Minute);
lcd_puts(buf);
if (Set == 0) break;
}

lcd_goto(0x00);
lcd_puts(" Hour 0-24 ");
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Hour++; while (Up==0) {ResetWD();};
if (Down == 0) Hour--; while(Down==0){ResetWD();};
if (Hour <1)Hour=23;
if (Hour >23)Hour=1;
lcd_puts(" ");
sprintf(buf,"Hour: %bu",Hour);
lcd_puts(buf);
if (Set == 0) break;
}

lcd_goto(0x00);
lcd_puts(" Day 1-7 Sun=1 ");
DelayMs(255);DelayMs(255);DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Day++; while (Up==0) {ResetWD();};
if (Down == 0) Day--; while(Down==0){ResetWD();};
sprintf(buf,"Day: %bu",Day);
if (Day<1) Day=7;
if (Day>7) Day=1;
lcd_puts(" ");
lcd_goto(0x00);
lcd_puts(buf);
if (Set == 0) break;
}

lcd_goto(0x00);
lcd_puts(" Date 1-31 ");
DelayMs(255);DelayMs(255);DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Date++; while(Up==0) {ResetWD();};
if (Down == 0) Date--; while(Down==0){ResetWD();};
if (Date <1)Date=31;
if (Date >31)Date=1;
lcd_puts(" ");
sprintf(buf,"Date: %bu",Date);
lcd_puts(buf);
if (Set == 0) break;
}

lcd_goto(0x00);
lcd_puts(" Month 1-12 ");
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Month++; while (Up==0) {ResetWD();};
if (Down == 0) Month--; while(Down==0){ResetWD();};
if (Month <1)Month=12;
if (Month >12)Month=1;
lcd_puts(" ");
sprintf(buf,"Month: %bu",Month);
lcd_puts(buf);
if (Set == 0) break;
}

lcd_goto(0x00);
lcd_puts(" Year ");
DelayMs(255);DelayMs(255);DelayMs(255);DelayMs(255);
while (Set == 0){ResetWD();}

while (Set == 1)
{
ResetWD();
if (Up == 0) Year++; while (Up==0) {ResetWD();};
if (Down == 0) Year--; while(Down==0){ResetWD();};
if (Year <1)Year=59;
if (Year >59)Year=1;
lcd_puts(" ");
sprintf(buf,"Year: %bu",Year);
lcd_puts(buf);
if (Set == 0) break;
}

Convert();
WriteTime();
lcd_goto(0x00);
lcd_puts(" Data Recorded ");
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
PrintFlag = 1; GetTime();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
DelayMs(255);DelayMs(255);
ResetWD();
PrintFlag = 0;
}

///////////////////////////////////////////////////////////////////////////
void WriteTime(void)
{
ResetWD();
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = Minute;
RTC_ARR[2] = Hour; // 24-hour mode(bit 6=0)
RTC_ARR[3] = Day; // Day = 1 or Sunday
RTC_ARR[4] = Date;
RTC_ARR[5] = Month;
RTC_ARR[6] = Year;
WriteRTC(&RTC_ARR[0]); // Set RTC
}
/////////////////END OF FUNCTIONS////////////////////////////////////////////

Added after 43 seconds:

Sorry, forgot to say, it is for DS1307
 

printf(lcd_put

sir this is the code for interfacing 8052 with 12887 in c whic is not working can any1 help?
#include <reg51.h>
#include <absacc.h>
unsigned char hr,min,sec;
void bcdconv(unsigned char mybyte);
void serial(unsigned char x);
sbit RS = P2^2;
sbit DS= P3^7;
sbit RW1= P3^6;
void print(unsigned char *ch);
sbit RW = P2^3;
sbit E = P2^4;
void put (unsigned char ch,bit rs);
void clear(void);
void delay(unsigned int);
void display();
void initlcd(void);
void print(unsigned char *ch);
void main(void)
{
delay(200);
initlcd();
XBYTE[10]=0x20;
XBYTE[11]=0x83;
XBYTE[0]= 0x55;
XBYTE[2]=0x58;
XBYTE[4]=0x36;
XBYTE[7]=0x19;
XBYTE[8]=0x10;
XBYTE[9]=0x04;
XBYTE[11]=0x03;

TMOD=0X20;
TH1=0XFD;
SCON=0x50;
TR1=1;
//display();
while(1)
{
serial("RTC");
hr=XBYTE[4];
bcdconv(hr);
serial(':');
min=XBYTE[2];
bcdconv(min);
serial(':');
sec=XBYTE[0];
bcdconv(sec);
serial(0x0D);
serial(0x0A);
display();
} }
//display();

void bcdconv(unsigned char mybyte)
{
unsigned char x,y;
x=mybyte&0XF0;
x=x|0x30;
y=mybyte&0XF0;
y=y>>4;
y=y|0x30;
serial(y);
serial(x);
}
void serial(unsigned char x)
{
SBUF=x;
while(TI==0);
TI=0;
}
void put (unsigned char ch,bit rs)
{
E=1;
RW=0;
P0=ch;
RS=rs;
delay(1);
E=0;
delay(1);
RW=1;
return;
}
void clear(void)
{
put(0x01,0);
delay(100);
return;
}

void delay(unsigned int t)
{
unsigned int k,l;
for(l=0;l<t;l++)
for(k=0;k<975;k++);
return;
}
void initlcd(void)
{
delay(250);
put(0x38,0);
delay(250);
put(0x0C,0);
delay(250);
put(0x0E,0);
delay(250);
put(0x10,0);
delay(250);
put(0x01,0);
delay(250);
put(0x06,0);
//delay(250);
//put(0x0C,0);
print("RTC");
clear();
delay(100);
return;
}
void display()
{
print("hour");
put(hr,1);
put(SBUF,1);
delay(100);
//clear();
put(0xC0,0);
print("min");
put(min,1);
delay(100);
//clear();
put(0x94,0);
print("sec");
put(sec,1);
delay(100);
//clear();
}
void print(unsigned char *ch)
{
while(*ch)
{
put(*ch,1);
//put(0x10,0);
ch++;
}
return;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top