CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

M41T80 Real time clock bcd2bin bin2bcd

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

M41T80 Real time clock bcd2bin bin2bcd
PostPosted: Mon Aug 22, 2005 9:16 am     Reply with quote

Code:
////////////////////////////////////////////////////////////////////////
//                               M41T80.C                           ////
//                     Driver for Real Time Clock                   ////
//  init_rtc()
//  multi_read_rtc(int16 address,char *data,int16 size)
//  multi_write_rtc(int16 address,char *data,int16 size)
//  BYTE data=read_rtc(long int address)
//  write_rtc(long int address, BYTE data)
//  BOOLEAN rtc_ready()
////////////////////////////////////////////////////////////////////////
#ifndef m40t80_SDA
  #define m40t80_SDA  PIN_C4
  #define m40t80_SCL  PIN_C3
#endif

//#use I2C(master,sda=m40t80_SDA,scl=m40t80_SCL,FORCE_HW,RESTART_WDT)
#use I2C(master,sda=m40t80_SDA,scl=m40t80_SCL,FAST,FORCE_HW,RESTART_WDT)

#define bcd2bin(val) ((val)&15) + ((val)>>4)*10
#define bin2bcd(val) (((val)/10)<<4) + (val)%10

struct _time
{             //  BYTE
  int s:7;        //1. Seconds Register
  int st:1;       //1. Stop
  int m:7;        //2. Minutes Register};
  int j2:1;       // -- waisted bits --
  int h:6;        //3. Hours Register
  int CB:1;       //3. Century Register
  int CEB:1;      //3. Century Enable Register
};
struct _date
{
  int dow:8;      //4. Day of week Register
  int d:6;        //5. Day of month Register
  int j3:2;       // -- waisted bits --
  int M:5;        //6. Month Register (note that its UPPER case)
  int j4:3;       // -- waisted bits --
  int y:8;        //7. Year Register
};
struct _datetime
{
  int s:7;        //1. Seconds Register
  int st:1;       //1. Stop
  int m:7;        //2. Minutes Register};
  int j2:1;       // -- waisted bits --
  int h:6;        //3. Hours Register
  int CB:1;       //3. Century Register
  int CEB:1;      //3. Century Enable Register
  int dow:8;      //4. Day of week Register
  int d:6;        //5. Day of month Register
  int j3:2;       // -- waisted bits --
  int M:5;        //6. Month Register
  int j4:3;       // -- waisted bits --
  int y:8;        //7. Year Register
};

//========================Prototypes==============================//
void init_rtc();
BOOLEAN rtc_ready();
void write_rtc(long int address, BYTE data);
BYTE read_rtc(long int address);
BOOLEAN multi_read_rtc(int16 address,char *data,int16 size);
BOOLEAN multi_write_rtc(int16 address,char *data,int16 size);
//======================init_rtc===========================//
void init_rtc() {
  output_float(m40t80_SCL);
  output_float(m40t80_SDA);
}
//======================rtc_ready===========================//
BOOLEAN rtc_ready() {
  int1 ack;
  i2c_start();            // If the write command is acknowledged,
  ack = i2c_write(0xD0);  // then the device is ready.
  i2c_stop();
  return !ack;
}
//======================write_rtc===========================//
void write_rtc(long int address, BYTE data) {
  while(!rtc_ready());//loop here until device is ready
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))&0xFE);
  i2c_write(address);
  i2c_write(data);
  i2c_stop();
}
//======================read_rtc===========================//
BYTE read_rtc(long int address) {
  BYTE data;
  while(!rtc_ready());
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))&0xFE);
  i2c_write(address);
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))|1);
  data=i2c_read(0);
  i2c_stop();
  return(data);
}
//======================multi_write_rtc===========================//
BOOLEAN multi_write_rtc(int16 address,char *data,int16 size)
{
  while(!rtc_ready());
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))&0xFE);
  i2c_write(address);
  for(;size;data++,address++,size--)
  {
    i2c_write(*data);
  }
  i2c_stop();
  return(TRUE);
}
//======================multi_read_rtc===========================//
BOOLEAN multi_read_rtc(int16 address,char *data,int16 size)
{
  while(!rtc_ready());
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))&0xFE);
  i2c_write(address);
  i2c_start();
  i2c_write((0xD0|(BYTE)(address>>7))|1);
  for(;size;data++,size--)
  {
    *data=i2c_read(1);//The ack will put next data on bus
  }
  data++; //last read requires no ack
  *data=i2c_read(0);//last read requires no ack
  i2c_stop();
  return(TRUE);
}
//////struct short_date_time now;
////const char dow_text[8][4] =
////{
////  "---",// -0-
////  "Mon",// -1-
////  "Tue",// -2-
////  "Wed",// -3-
////  "Thr",// -4-
////  "Fri",// -5-
////  "Sat",// -6-
////  "Sun" // -7-
////};
//////Comment out the text if your not using it. This saves on ram
////const char month_text[13][4] =
////{
////  "---",// -0-
////  "Jan",// -1-
////  "Feb",// -2-
////  "Mar",// -3-
////  "Apr",// -4-
////  "May",// -5-
////  "Jun",// -6-
////  "Jul",// -7-
////  "Aug",// -8-
////  "Sep",// -9-
////  "Oct",// -10-
////  "Nov",// -11-
////  "Dec"// -12-
////};
//void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec) {
//struct _datetime now;
//  now.s=bin2bcd(sec);
//  now.m=bin2bcd(min);
//  now.h=bin2bcd(hr);
//  now.CB=0;
//  now.CEB=1;
//  now.dow=bin2bcd(dow);
//  now.d=bin2bcd(day);
//  now.M=bin2bcd(mth);
//  now.y=bin2bcd(year);
//  multi_write_rtc(1,&now,sizeof(now));//write the struc to addr=1
//}

//void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) {
//   day = read_rtc(0x87);
//   mth = read_rtc(0x89);
//   year = read_rtc(0x8d);
//   dow = read_rtc(0x8b);
//}
//
//
//void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) {
//   hr = read_rtc(0x85);
//   min = read_rtc(0x83);
//   sec = read_rtc(0x81);
//}

//int8 getDayOfWeek(int m, int d, int y)
//{
//  int8 csys=1;
//     // csys = 1 for Gregorian Calendar
//     if (m < 3)
//     {
//           m = m + 12;
//           y = y - 1;
//     }
//     return (d+(2 * m)+int(6*(m+1)/10)+y+int(y/4)-int(y/100)+int(y/400)+csys)%7;
//}



Code:

 multi_write_rtc(1,&TimeNow,sizeof(TimeNow));//write the struc to addr=1;not messing with seconds
multi_read_rtc(1,&TimeNow,sizeof(TimeNow));//reread clock

//  printf(glcd_putc,"   %s %s %02u ",dow_text[bcd2bin(current.dow)],month_text[bcd2bin(current.M)],bcd2bin(current.d));
//  printf(glcd_putc,"2%U",current.CB);//print 2000 or 2100 depending on century bit
//  printf(glcd_putc,"%02u",bcd2bin(current.y));
//  printf(glcd_putc," %02u:%02u:%02u   ",bcd2bin(current.h),bcd2bin(current.m),bcd2bin(current.s));
//  printf(glcd_putc,"---");
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group