您的位置 首页 新能源

STM32 ISL1208编程

[html]viewplaincopyISL1208hifndef_ISL1208_H_define_ISL1208_H_includestm32f10xhtypedefstructMy_Time{u

[html]view plaincopy

  1. //ISL1208.h
  2. #ifndef_ISL1208_H_
  3. #define_ISL1208_H_
  4. #include”stm32f10x.h”
  5. typedefstructMy_Time
  6. {
  7. u8_year;
  8. u8_month;
  9. u8_day;
  10. u8_hour;
  11. u8_min;
  12. u8_sec;
  13. u8_week;
  14. }MY_TIME,*pMY_TIME;
  15. externMY_TIMEsMyTime;
  16. voidISL128Init(void);
  17. voidSetTime(pMY_TIME_mytime);
  18. voidGetTime(pMY_TIME_mytime);
  19. voidAuto_Time_Set(void);
  20. #endif//_ISL1208_H_

[html]view plaincopy

  1. //ISL128.C
  2. #include”isl1208.h”
  3. #include”i2c_ee.h”
  4. constu8*COMPILED_DATE=__DATE__;//取得编译日期
  5. constu8*COMPILED_TIME=__TIME__;//取得编译时刻
  6. constu8Month_Tab[12][3]={“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”};
  7. u8consttable_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};//月批改数据表
  8. MY_TIMEsMyTime={0x13,0x04,0x13,0x14,0x10,0x05,0x03};
  9. staticintIntToBCD(inti);//十进制转BCD
  10. staticintBCDToInt(intbcd);//BCD转十进制
  11. staticu8RTC_Get_Week(u16year,u8month,u8day);
  12. intIntToBCD(inti)//十进制转BCD
  13. {
  14. return(((i/10)<<4)+((i%10)&0x0f));
  15. }
  16. intBCDToInt(intbcd)//BCD转十进制
  17. {
  18. return(0xff&(bcd>>4))*10+(0xf&bcd);
  19. }
  20. //比较两个字符串指定长度的内容是否持平
  21. //参数:s1,s2要比较的两个字符串;len,比较长度
  22. //返回值:1,持平;0,不持平
  23. u8my_strcmp(u8*s1,u8*s2,u8len)
  24. {
  25. u8i;
  26. for(i=0;i
  27. return1;
  28. }
  29. voidISL128Init()
  30. {
  31. u8SR_REG_DATA=0x91;
  32. u8INT_REG_DATA=0xca;
  33. EEPROM_ADDRESS=ISL1208_ADDRESS;
  34. I2C_EE_ByteWrite(&SR_REG_DATA,0x07);
  35. I2C_EE_ByteWrite(&INT_REG_DATA,0x08);
  36. }
  37. voidSetTime(pMY_TIME_mytime)
  38. {
  39. EEPROM_ADDRESS=ISL1208_ADDRESS;
  40. I2C_EE_ByteWrite(&_mytime->_sec,0x00);
  41. I2C_EE_ByteWrite(&_mytime->_min,0x01);
  42. I2C_EE_ByteWrite(&_mytime->_hour,0x02);
  43. I2C_EE_ByteWrite(&_mytime->_day,0x03);
  44. I2C_EE_ByteWrite(&_mytime->_month,0x04);
  45. I2C_EE_ByteWrite(&_mytime->_year,0x05);
  46. I2C_EE_ByteWrite(&_mytime->_week,0x06);
  47. }
  48. voidGetTime(pMY_TIME_mytime)
  49. {
  50. EEPROM_ADDRESS=ISL1208_ADDRESS;
  51. I2C_EE_BufferRead(&_mytime->_sec,0x00,1);
  52. I2C_EE_BufferRead(&_mytime->_min,0x01,1);
  53. I2C_EE_BufferRead(&_mytime->_hour,0x02,1);
  54. I2C_EE_BufferRead(&_mytime->_day,0x03,1);
  55. I2C_EE_BufferRead(&_mytime->_month,0x04,1);
  56. I2C_EE_BufferRead(&_mytime->_year,0x05,1);
  57. I2C_EE_BufferRead(&_mytime->_week,0x06,1);
  58. _mytime->_sec=BCDToInt(_mytime->_sec);
  59. _mytime->_min=BCDToInt(_mytime->_min);
  60. _mytime->_hour=BCDToInt(_mytime->_hour);
  61. _mytime->_day=BCDToInt(_mytime->_day);
  62. _mytime->_month=BCDToInt(_mytime->_month);
  63. _mytime->_year=BCDToInt(_mytime->_year);
  64. _mytime->_week=BCDToInt(_mytime->_week);
  65. }
  66. voidAuto_Time_Set()
  67. {
  68. u8temp[3];
  69. u8i;
  70. u8mon,date,week;
  71. u16year;
  72. u8sec,min,hour;
  73. for(i=0;i<3;i++)temp[i]=COMPILED_DATE[i];
  74. for(i=0;i<12;i++)if(my_strcmp((u8*)Month_Tab[i],temp,3))break;
  75. mon=i+1;//得到月份
  76. if(COMPILED_DATE[4]==)date=COMPILED_DATE[5]-0;
  77. elsedate=10*(COMPILED_DATE[4]-0)+COMPILED_DATE[5]-0;
  78. year=10*(COMPILED_DATE[9]-0)+COMPILED_DATE[10]-0;
  79. hour=10*(COMPILED_TIME[0]-0)+COMPILED_TIME[1]-0;
  80. min=10*(COMPILED_TIME[3]-0)+COMPILED_TIME[4]-0;
  81. sec=10*(COMPILED_TIME[6]-0)+COMPILED_TIME[7]-0;
  82. week=RTC_Get_Week(year+2000,mon,date);
  83. sMyTime._day=IntToBCD(date);
  84. sMyTime._hour=IntToBCD(hour);
  85. sMyTime._min=IntToBCD(min);
  86. sMyTime._month=IntToBCD(mon);
  87. sMyTime._sec=IntToBCD(sec);
  88. sMyTime._year=IntToBCD(year);
  89. sMyTime._week=IntToBCD(week);
  90. SetTime(&sMyTime);
  91. }
  92. u8RTC_Get_Week(u16year,u8month,u8day)
  93. {
  94. u16temp2;
  95. u8yearH,yearL;
  96. yearH=year/100;yearL=year%100;
  97. //假如为21世纪,年份数加100
  98. if(yearH>19)yearL+=100;
  99. //所过闰年数只算1900年之后的
  100. temp2=yearL+yearL/4;
  101. temp2=temp2%7;
  102. temp2=temp2+day+table_week[month-1];
  103. if(yearL%4==0&&month<3)temp2--;
  104. return(temp2%7);
  105. }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/xinnengyuan/261419.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部