×
单片机 > 单片机程序设计 > 详情

STM32 ISL1208编程

发布时间:2020-09-02 发布时间:
|
[html] view plaincopy
 
  1. //ISL1208.h  
  2. #ifndef _ISL1208_H_  
  3. #define _ISL1208_H_  
  4. #include "stm32f10x.h"  
  5.   
  6. typedef struct My_Time  
  7. {  
  8.     u8 _year;  
  9.     u8 _month;  
  10.     u8 _day;  
  11.     u8 _hour;  
  12.     u8 _min;  
  13.     u8 _sec;  
  14.     u8 _week;  
  15. } MY_TIME,*pMY_TIME;  
  16.   
  17. extern MY_TIME sMyTime;  
  18. void ISL128Init(void);  
  19. void SetTime(pMY_TIME _mytime);  
  20. void GetTime(pMY_TIME _mytime);  
  21. void Auto_Time_Set(void);  
  22. #endif //_ISL1208_H_  

 

 

[html] view plaincopy
 
  1. //ISL128.C  
  2. #include "isl1208.h"  
  3. #include "i2c_ee.h"  
  4.   
  5. const u8 *COMPILED_DATE=__DATE__;//获得编译日期  
  6. const u8 *COMPILED_TIME=__TIME__;//获得编译时间  
  7. const u8 Month_Tab[12][3]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};   
  8. u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表   
  9.   
  10. MY_TIME sMyTime={0x13,0x04,0x13,0x14,0x10,0x05,0x03};  
  11. static int IntToBCD(int i);//十进制转BCD  
  12. static int BCDToInt(int bcd); //BCD转十进制  
  13. static u8 RTC_Get_Week(u16 year,u8 month,u8 day);  
  14.   
  15. int IntToBCD(int i) //十进制转BCD  
  16. {  
  17.     return (((i / 10) <
  18. }  
  19.   
  20. int BCDToInt(int bcd) //BCD转十进制  
  21. {  
  22.     return (0xff & (bcd >> 4)) * 10 + (0xf & bcd);  
  23. }  
  24.   
  25. //比较两个字符串指定长度的内容是否相等  
  26. //参数:s1,s2要比较的两个字符串;len,比较长度  
  27. //返回值:1,相等;0,不相等  
  28. u8 my_strcmp(u8*s1,u8*s2,u8 len)  
  29. {  
  30.     u8 i;  
  31.     for(i=0;i
  32.     return 1;        
  33. }  
  34.   
  35. void ISL128Init()  
  36. {  
  37.     u8 SR_REG_DATA=0x91;  
  38.     u8 INT_REG_DATA=0xca;  
  39.     EEPROM_ADDRESS=ISL1208_ADDRESS;  
  40.     I2C_EE_ByteWrite(&SR_REG_DATA, 0x07);  
  41.     I2C_EE_ByteWrite(&INT_REG_DATA, 0x08);  
  42. }  
  43. void SetTime(pMY_TIME _mytime)  
  44. {  
  45.     EEPROM_ADDRESS=ISL1208_ADDRESS;  
  46.     I2C_EE_ByteWrite(&_mytime->_sec,  0x00);  
  47.     I2C_EE_ByteWrite(&_mytime->_min,  0x01);  
  48.     I2C_EE_ByteWrite(&_mytime->_hour, 0x02);  
  49.     I2C_EE_ByteWrite(&_mytime->_day,  0x03);  
  50.     I2C_EE_ByteWrite(&_mytime->_month,0x04);  
  51.     I2C_EE_ByteWrite(&_mytime->_year, 0x05);  
  52.     I2C_EE_ByteWrite(&_mytime->_week, 0x06);  
  53. }  
  54. void GetTime(pMY_TIME _mytime)  
  55. {  
  56.     EEPROM_ADDRESS=ISL1208_ADDRESS;  
  57.     I2C_EE_BufferRead(&_mytime->_sec,  0x00,1);  
  58.     I2C_EE_BufferRead(&_mytime->_min,  0x01,1);  
  59.     I2C_EE_BufferRead(&_mytime->_hour, 0x02,1);  
  60.     I2C_EE_BufferRead(&_mytime->_day,  0x03,1);  
  61.     I2C_EE_BufferRead(&_mytime->_month,0x04,1);  
  62.     I2C_EE_BufferRead(&_mytime->_year, 0x05,1);  
  63.     I2C_EE_BufferRead(&_mytime->_week, 0x06,1);  
  64.       
  65.     _mytime->_sec=BCDToInt(_mytime->_sec);  
  66.     _mytime->_min=BCDToInt(_mytime->_min);  
  67.     _mytime->_hour=BCDToInt(_mytime->_hour);  
  68.     _mytime->_day=BCDToInt(_mytime->_day);  
  69.     _mytime->_month=BCDToInt(_mytime->_month);  
  70.     _mytime->_year=BCDToInt(_mytime->_year);  
  71.     _mytime->_week=BCDToInt(_mytime->_week);  
  72. }  
  73.   
  74. void Auto_Time_Set()  
  75. {  
  76.     u8 temp[3];  
  77.     u8 i;  
  78.     u8 mon,date,week;  
  79.     u16 year;  
  80.     u8 sec,min,hour;  
  81.     for(i=0;i<3;i++)temp[i]=COMPILED_DATE[i];     
  82.     for(i=0;i<12;i++)if(my_strcmp((u8*)Month_Tab[i],temp,3))break;     
  83.     mon=i+1;//得到月份  
  84.     if(COMPILED_DATE[4]==' ')date=COMPILED_DATE[5]-'0';   
  85.     else date=10*(COMPILED_DATE[4]-'0')+COMPILED_DATE[5]-'0';    
  86.     year=10*(COMPILED_DATE[9]-'0')+COMPILED_DATE[10]-'0';        
  87.     hour=10*(COMPILED_TIME[0]-'0')+COMPILED_TIME[1]-'0';    
  88.     min=10*(COMPILED_TIME[3]-'0')+COMPILED_TIME[4]-'0';    
  89.     sec=10*(COMPILED_TIME[6]-'0')+COMPILED_TIME[7]-'0';    
  90.     week=RTC_Get_Week(year+2000,mon,date);  
  91.     sMyTime._day=IntToBCD(date);  
  92.     sMyTime._hour=IntToBCD(hour);  
  93.     sMyTime._min=IntToBCD(min);  
  94.     sMyTime._month=IntToBCD(mon);  
  95.     sMyTime._sec=IntToBCD(sec);  
  96.     sMyTime._year=IntToBCD(year);  
  97.     sMyTime._week=IntToBCD(week);  
  98.   
  99.     SetTime(&sMyTime);  
  100. }   
  101.   
  102.   
  103. u8 RTC_Get_Week(u16 year,u8 month,u8 day)  
  104. {     
  105.     u16 temp2;  
  106.     u8 yearH,yearL;  
  107.       
  108.     yearH=year/100; yearL=year%100;   
  109.     // 如果为21世纪,年份数加100    
  110.     if (yearH>19)yearL+=100;  
  111.     // 所过闰年数只算1900年之后的    
  112.     temp2=yearL+yearL/4;  
  113.     temp2=temp2%7;   
  114.     temp2=temp2+day+table_week[month-1];  
  115.     if (yearL%4==0&&month<3)temp2--;  
  116.     return(temp2%7);  
  117. }   

关键字:STM32  ISL1208编程 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
C51 特殊功能寄存器SFR的名称和地址