Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ds1338.cpp
00001 #include "mbed.h" 00002 #include "ds1338.h" 00003 00004 /* 00005 * Constructor, initialize the ds1338 on i2c interface. 00006 * @param sda : sda i2c pin (PinName) 00007 * @param scl : scl i2c pin (PinName) 00008 */ 00009 DS1338::DS1338(PinName sda, PinName scl): _i2c(sda, scl) { 00010 _i2c.frequency(400000); 00011 buffer[0]=0x07; 00012 _i2c.write(DS1338_ADR,buffer,1,true); 00013 _i2c.read(DS1338_ADR,buffer,1); 00014 if (buffer[0]&0x20){ 00015 buffer[0]=0x07; 00016 buffer[1]=0xB0;//do not erase error condition ! 00017 }else{ 00018 buffer[0]=0x07; 00019 buffer[1]=0x90; 00020 } 00021 _i2c.write(DS1338_ADR,buffer,1+1); 00022 } 00023 /** 00024 * read bytes from nvram (55 bytes available) 00025 * @param adr the start address (starts at 0) 00026 * @param count number of byte to read 00027 * @param data where to put the bytes 00028 * @return the byte 00029 */ 00030 void DS1338::read(unsigned char adr,unsigned char count,char * data) { 00031 if (count>DS1338_BUFFER_SIZE) { 00032 count=DS1338_BUFFER_SIZE; 00033 } 00034 buffer[0]=9+adr; 00035 _i2c.write(DS1338_ADR,&(buffer[0]),1,true); 00036 _i2c.read(DS1338_ADR,data,count); 00037 } 00038 /** 00039 * write bytes to nvram (55 bytes available) 00040 * @param adr the start address (starts at 0) 00041 * @param count number of byte to write 00042 * @param data to be written 00043 * @return the byte 00044 */ 00045 void DS1338::write(unsigned char adr,unsigned char count,char * data) { 00046 if (count>DS1338_BUFFER_SIZE-1) { 00047 count=DS1338_BUFFER_SIZE-1; 00048 } 00049 buffer[0]=9+adr; 00050 for (unsigned char i=0; i<count; i++) { 00051 buffer[1+i]=*(data+i); 00052 } 00053 _i2c.write(DS1338_ADR,buffer,1+count); 00054 } 00055 /** 00056 * read the current time 00057 * @param x the time; 00058 */ 00059 void DS1338::readTime(tm * time) { 00060 buffer[0]=0; 00061 _i2c.write(DS1338_ADR,&(buffer[0]),1,true); 00062 _i2c.read(DS1338_ADR,buffer,9); 00063 time->tm_sec=((buffer[0]>>4)&0x07)*10+(buffer[0]&0x0F); 00064 time->tm_min=((buffer[1]>>4)&0x07)*10+(buffer[1]&0x0F); 00065 time->tm_hour=((buffer[2]>>4)&0x03)*10+(buffer[2]&0x0F); 00066 time->tm_mday=((buffer[4]>>4)&0x03)*10+(buffer[4]&0x0F); 00067 time->tm_mon=((buffer[5]>>4)&0x01)*10+(buffer[5]&0x0F)-1; 00068 time->tm_year=((buffer[6]>>4)&0x0F)*10+(buffer[6]&0x0F); 00069 if (time->tm_year<70){ 00070 time->tm_year+=100; 00071 } 00072 time->tm_wday=buffer[3]&0x07; 00073 time->tm_yday=0; 00074 time->tm_isdst=0; 00075 if (time->tm_sec>=60|| 00076 time->tm_min>=60|| 00077 time->tm_hour>=24|| 00078 time->tm_mday>31 || time->tm_mday<1|| 00079 time->tm_mon>11|| 00080 time->tm_wday>=7|| 00081 buffer[0x08]!=0xCA||//checksum 00082 (buffer[0x07]&0x20)!=0){//error condition 00083 time->tm_sec=0; 00084 time->tm_min=0; 00085 time->tm_hour=0; 00086 time->tm_mday=1; 00087 time->tm_mon=0; 00088 time->tm_year=70; 00089 time->tm_wday=1; 00090 time->tm_yday=0; 00091 time->tm_isdst=0; 00092 writeTime(time); 00093 } 00094 } 00095 /** 00096 * write the current time 00097 * @param time the time; 00098 */ 00099 void DS1338::writeTime(tm * time) { 00100 buffer[0]=0; 00101 buffer[1]=(((time->tm_sec/10)<<4)+(time->tm_sec%10))&0x7F;//clock not halted 00102 buffer[2]=(((time->tm_min/10)<<4)+(time->tm_min%10))&0x7F; 00103 buffer[3]=(((time->tm_hour/10)<<4)+(time->tm_hour%10))&0x3F;//mode 24h 00104 buffer[4]=(time->tm_wday)&0x03; 00105 buffer[5]=(((time->tm_mday/10)<<4)+(time->tm_mday%10))&0x3F; 00106 buffer[6]=((((time->tm_mon+1)/10)<<4)+((time->tm_mon+1)%10))&0x1F; 00107 buffer[7]=((((time->tm_year%100)/10)<<4)+(time->tm_year%10))&0xFF; 00108 buffer[8]=0x90;//1Hz, erase error cond 00109 buffer[9]=0xCA; 00110 _i2c.write(DS1338_ADR,buffer,1+9); 00111 }
Generated on Mon Jul 18 2022 14:59:31 by
1.7.2