STM32 ISL1208編程
- //ISL1208.h
- #ifndef_ISL1208_H_
- #define_ISL1208_H_
- #include"stm32f10x.h"
- typedefstructMy_Time
- {
- u8_year;
- u8_month;
- u8_day;
- u8_hour;
- u8_min;
- u8_sec;
- u8_week;
- }MY_TIME,*pMY_TIME;
- externMY_TIMEsMyTime;
- voidISL128Init(void);
- voidSetTime(pMY_TIME_mytime);
- voidGetTime(pMY_TIME_mytime);
- voidAuto_Time_Set(void);
- #endif//_ISL1208_H_
[html]view plaincopy
- //ISL128.C
- #include"isl1208.h"
- #include"i2c_ee.h"
- constu8*COMPILED_DATE=__DATE__;//獲得編譯日期
- constu8*COMPILED_TIME=__TIME__;//獲得編譯時(shí)間
- constu8Month_Tab[12][3]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
- u8consttable_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};//月修正數(shù)據(jù)表
- MY_TIMEsMyTime={0x13,0x04,0x13,0x14,0x10,0x05,0x03};
- staticintIntToBCD(inti);//十進(jìn)制轉(zhuǎn)BCD
- staticintBCDToInt(intbcd);//BCD轉(zhuǎn)十進(jìn)制
- staticu8RTC_Get_Week(u16year,u8month,u8day);
- intIntToBCD(inti)//十進(jìn)制轉(zhuǎn)BCD
- {
- return(((i/10)<<4)+((i%10)&0x0f));
- }
- intBCDToInt(intbcd)//BCD轉(zhuǎn)十進(jìn)制
- {
- return(0xff&(bcd>>4))*10+(0xf&bcd);
- }
- //比較兩個(gè)字符串指定長度的內(nèi)容是否相等
- //參數(shù):s1,s2要比較的兩個(gè)字符串;len,比較長度
- //返回值:1,相等;0,不相等
- u8my_strcmp(u8*s1,u8*s2,u8len)
- {
- u8i;
- for(i=0;i
- return1;
- }
- voidISL128Init()
- {
- u8SR_REG_DATA=0x91;
- u8INT_REG_DATA=0xca;
- EEPROM_ADDRESS=ISL1208_ADDRESS;
- I2C_EE_ByteWrite(&SR_REG_DATA,0x07);
- I2C_EE_ByteWrite(&INT_REG_DATA,0x08);
- }
- voidSetTime(pMY_TIME_mytime)
- {
- EEPROM_ADDRESS=ISL1208_ADDRESS;
- I2C_EE_ByteWrite(&_mytime->_sec,0x00);
- I2C_EE_ByteWrite(&_mytime->_min,0x01);
- I2C_EE_ByteWrite(&_mytime->_hour,0x02);
- I2C_EE_ByteWrite(&_mytime->_day,0x03);
- I2C_EE_ByteWrite(&_mytime->_month,0x04);
- I2C_EE_ByteWrite(&_mytime->_year,0x05);
- I2C_EE_ByteWrite(&_mytime->_week,0x06);
- }
- voidGetTime(pMY_TIME_mytime)
- {
- EEPROM_ADDRESS=ISL1208_ADDRESS;
- I2C_EE_BufferRead(&_mytime->_sec,0x00,1);
- I2C_EE_BufferRead(&_mytime->_min,0x01,1);
- I2C_EE_BufferRead(&_mytime->_hour,0x02,1);
- I2C_EE_BufferRead(&_mytime->_day,0x03,1);
- I2C_EE_BufferRead(&_mytime->_month,0x04,1);
- I2C_EE_BufferRead(&_mytime->_year,0x05,1);
- I2C_EE_BufferRead(&_mytime->_week,0x06,1);
- _mytime->_sec=BCDToInt(_mytime->_sec);
- _mytime->_min=BCDToInt(_mytime->_min);
- _mytime->_hour=BCDToInt(_mytime->_hour);
- _mytime->_day=BCDToInt(_mytime->_day);
- _mytime->_month=BCDToInt(_mytime->_month);
- _mytime->_year=BCDToInt(_mytime->_year);
- _mytime->_week=BCDToInt(_mytime->_week);
- }
- voidAuto_Time_Set()
- {
- u8temp[3];
- u8i;
- u8mon,date,week;
- u16year;
- u8sec,min,hour;
- for(i=0;i<3;i++)temp[i]=COMPILED_DATE[i];
- for(i=0;i<12;i++)if(my_strcmp((u8*)Month_Tab[i],temp,3))break;
- mon=i+1;//得到月份
- if(COMPILED_DATE[4]==)date=COMPILED_DATE[5]-0;
- elsedate=10*(COMPILED_DATE[4]-0)+COMPILED_DATE[5]-0;
- year=10*(COMPILED_DATE[9]-0)+COMPILED_DATE[10]-0;
- hour=10*(COMPILED_TIME[0]-0)+COMPILED_TIME[1]-0;
- min=10*(COMPILED_TIME[3]-0)+COMPILED_TIME[4]-0;
- sec=10*(COMPILED_TIME[6]-0)+COMPILED_TIME[7]-0;
- week=RTC_Get_Week(year+2000,mon,date);
- sMyTime._day=IntToBCD(date);
- sMyTime._hour=IntToBCD(hour);
- sMyTime._min=IntToBCD(min);
- sMyTime._month=IntToBCD(mon);
- sMyTime._sec=IntToBCD(sec);
- sMyTime._year=IntToBCD(year);
- sMyTime._week=IntToBCD(week);
- SetTime(&sMyTime);
- }
- u8RTC_Get_Week(u16year,u8month,u8day)
- {
- u16temp2;
- u8yearH,yearL;
- yearH=year/100;yearL=year%100;
- //如果為21世紀(jì),年份數(shù)加100
- if(yearH>19)yearL+=100;
- //所過閏年數(shù)只算1900年之后的
- temp2=yearL+yearL/4;
- temp2=temp2%7;
- temp2=temp2+day+table_week[month-1];
- if(yearL%4==0&&month<3)temp2--;
- return(temp2%7);
- }
評論