DS1302.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #include "Data_deal.h"
  2. #include "DS1302.h"
  3. uint8_t time_buf[7] = {0x20,0x20,0x05,0x29,0x17,0x20,0x14}; //初始时间2020年5月29号17点41分14秒 星期五
  4. //DS1302初始化
  5. void ds1302_Init(void)
  6. {
  7. GPIO_InitTypeDef GPIO_InitStruct = {0};
  8. __HAL_RCC_GPIOB_CLK_ENABLE();
  9. __HAL_RCC_GPIOE_CLK_ENABLE();
  10. GPIO_InitStruct.Pin = DS1302_RST;
  11. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  12. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  13. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  14. GPIO_InitStruct.Pin = DS1302_DATA;
  15. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  16. // GPIO_InitStruct.Pull = GPIO_NOPULL;
  17. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  18. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  19. GPIO_InitStruct.Pin = DS1302_SCK;
  20. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  21. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  22. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  23. HAL_GPIO_WritePin(GPIOE, DS1302_DATA,GPIO_PIN_SET); //拉高
  24. HAL_GPIO_WritePin(GPIOE, DS1302_RST,GPIO_PIN_SET); //置低
  25. HAL_GPIO_WritePin(GPIOB, DS1302_SCK,GPIO_PIN_RESET); //置低
  26. }
  27. void DS1302_IO_IN(void)
  28. {
  29. GPIO_InitTypeDef GPIO_InitStruct = {0};
  30. __HAL_RCC_GPIOE_CLK_ENABLE();
  31. GPIO_InitStruct.Pin = DS1302_DATA;
  32. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  33. GPIO_InitStruct.Pull = GPIO_NOPULL;
  34. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  35. }
  36. void DS1302_IO_OUT(void)
  37. {
  38. GPIO_InitTypeDef GPIO_InitStruct = {0};
  39. __HAL_RCC_GPIOE_CLK_ENABLE();
  40. GPIO_InitStruct.Pin = DS1302_DATA;
  41. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  42. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  43. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  44. }
  45. void delay_us(uint32_t us)
  46. {
  47. uint16_t i = 0;
  48. i = us*216;
  49. while (i--)
  50. {
  51. __NOP();
  52. }
  53. }
  54. //void delay_us(uint32_t us)
  55. //{
  56. // uint32_t delay = (HAL_RCC_GetHCLKFreq() / 6000000 * us);
  57. // while (delay--)
  58. // {
  59. // ;
  60. // }
  61. //}
  62. //向DS1302写入一个字节数据
  63. void ds1302_Write_Byte(uint8_t addr, uint8_t data)
  64. {
  65. uint8_t i;
  66. DS1302_RST_0; //停止DS1302总线
  67. delay_us(10);
  68. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  69. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  70. DS1302_RST_1; //启动DS1302总线
  71. addr = addr & 0xFE; //最低位置零,写数据
  72. DS1302_IO_OUT();
  73. for(i = 0; i < 8; i ++) //写地址
  74. {
  75. if (addr & 0x01)
  76. DS1302_DATA_OUT_1;
  77. else
  78. DS1302_DATA_OUT_0;
  79. DS1302_SCK_1; //产生时钟
  80. delay_us(10);
  81. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  82. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  83. DS1302_SCK_0;
  84. addr = addr>>1;
  85. }
  86. for (i = 0; i < 8; i ++) //写数据
  87. {
  88. if(data & 0x01)
  89. DS1302_DATA_OUT_1;
  90. else
  91. DS1302_DATA_OUT_0;
  92. DS1302_SCK_1; //产生时钟
  93. delay_us(10);
  94. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  95. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  96. DS1302_SCK_0;
  97. data = data>>1;
  98. }
  99. DS1302_RST_0; //停止DS1302总线
  100. }
  101. //从DS1302读出一个字节数据
  102. uint8_t ds1302_Read_Byte(uint8_t addr)
  103. {
  104. uint8_t i,temp;
  105. DS1302_RST_0; //停止DS1302总线
  106. delay_us(10);
  107. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  108. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  109. DS1302_RST_1; //启动DS1302总线
  110. addr = addr | 0x01; //最低位置高,读数据
  111. DS1302_IO_OUT();
  112. for(i = 0; i < 8; i ++) //写地址
  113. {
  114. if (addr & 0x01)
  115. DS1302_DATA_OUT_1;
  116. else
  117. DS1302_DATA_OUT_0;
  118. DS1302_SCK_1; //产生时钟
  119. delay_us(10);
  120. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  121. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  122. DS1302_SCK_0;
  123. addr = addr>>1;
  124. }
  125. DS1302_IO_IN();
  126. for (i = 0; i < 8; i ++) //读数据
  127. {
  128. temp = temp >> 1;
  129. if(DS1302_DATA_IN())
  130. temp |= 0x80;
  131. else
  132. temp &= 0x7F;
  133. DS1302_SCK_1; //产生时钟
  134. delay_us(10);
  135. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  136. __NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
  137. DS1302_SCK_0;
  138. }
  139. DS1302_RST_0; //停止DS1302总线
  140. return temp;
  141. }
  142. //向DS1302写入时间数据
  143. void ds1302_Write_Time(uint8_t *Time_buf)
  144. {
  145. ds1302_Write_Byte(ds1302_control_add, 0x00); //关闭写保护
  146. ds1302_Write_Byte(ds1302_sec_add, 0x80); //暂停时钟
  147. //DS1302_Write_Byte(ds1302_charger_add, 0xA9); //涓流充电
  148. ds1302_Write_Byte(ds1302_year_add,HEX2BCD(Time_buf[1])); //年
  149. ds1302_Write_Byte(ds1302_month_add,HEX2BCD(Time_buf[2])); //月
  150. ds1302_Write_Byte(ds1302_date_add,HEX2BCD(Time_buf[3])); //日
  151. ds1302_Write_Byte(ds1302_hr_add,HEX2BCD(Time_buf[4])); //时
  152. ds1302_Write_Byte(ds1302_min_add,HEX2BCD(Time_buf[5])); //分
  153. ds1302_Write_Byte(ds1302_sec_add,HEX2BCD(Time_buf[6])); //秒
  154. //ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
  155. ds1302_Write_Byte(ds1302_control_add,0x80); //打开写保护
  156. }
  157. //从DS302读出时钟数据
  158. void ds1302_Read_Time(void)
  159. {
  160. time_buf[1] = ds1302_Read_Byte(ds1302_year_add); //年
  161. time_buf[2] = ds1302_Read_Byte(ds1302_month_add); //月
  162. time_buf[3] = ds1302_Read_Byte(ds1302_date_add); //日
  163. time_buf[4] = ds1302_Read_Byte(ds1302_hr_add); //时
  164. time_buf[5] = ds1302_Read_Byte(ds1302_min_add); //分
  165. time_buf[6] = (ds1302_Read_Byte(ds1302_sec_add))&0x7f; //秒,屏蔽秒的第7位,避免超出59
  166. //time_buf[7] = ds1302_Read_Byte(ds1302_day_add); //周
  167. //Can_Send_Hu_Msg(0xb1,time_buf,8,0x1b,0x00000012);
  168. }
  169. void ds1302_SynTime(SDateTime * datetime)
  170. {
  171. ds1302_Read_Time();
  172. datetime->year = BCD2HEX(time_buf[1]);
  173. datetime->month = BCD2HEX(time_buf[2]);
  174. datetime->day = BCD2HEX(time_buf[3]);
  175. datetime->hour = BCD2HEX(time_buf[4]);
  176. datetime->min = BCD2HEX(time_buf[5]);
  177. datetime->sec = BCD2HEX(time_buf[6]);
  178. return;
  179. }
  180. //DS1302向上层返回时间数据
  181. void ds1302_Get_Time(uint8_t *time)
  182. {
  183. ds1302_Read_Time();
  184. time[0]=(time_buf[0]>>4); //年
  185. time[1]=(time_buf[0]&0x0f);
  186. time[2]=(time_buf[1]>>4);
  187. time[3]=(time_buf[1]&0x0f);
  188. time[4]=(time_buf[2]>>4); //月
  189. time[5]=(time_buf[2]&0x0f);
  190. time[6]=(time_buf[3]>>4); //日
  191. time[7]=(time_buf[3]&0x0f);
  192. //time[8]=(time_buf[7]&0x07); //星期
  193. time[9]=(time_buf[4]>>4); //时
  194. time[10]=(time_buf[4]&0x0f);
  195. time[11]=(time_buf[5]>>4); //分
  196. time[12]=(time_buf[5]&0x0f);
  197. time[13]=(time_buf[6]>>4); //秒
  198. time[14]=(time_buf[6]&0x0f);
  199. }
  200. //////////////////////////////////////--以下李伟添加 李伟修改 20210613
  201. //ds1302------stm32f746vgt6
  202. //pin5 rst--pin98--Pe1
  203. //pin6 io pin97--PE0
  204. //pin7 sck pin96--PB9
  205. /*
  206. 1、 初始化IO口。
  207. void GPIO_Config(void)
  208. {
  209. GPIO_InitTypeDef GPIO_InitStructure;
  210. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE);
  211. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14;
  212. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  213. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  214. GPIO_Init(GPIOD, &GPIO_InitStructure);
  215. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE);
  216. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  217. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  218. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  219. GPIO_Init(GPIOD, &GPIO_InitStructure);
  220. }
  221. 2、 ds1302_init(); // 调用ds1302初始化程序
  222. 3、 get_result() ; //定时读取ds1302时间 这个函数每隔一段时间调用一次。比如50ms调用一次。
  223. */
  224. #define RST_1 HAL_GPIO_WritePin(GPIOE, DS1302_RST, GPIO_PIN_SET) //GPIO_SetBits(GPIOE, GPIO_PIN_1);
  225. #define RST_0 HAL_GPIO_WritePin(GPIOE, DS1302_RST, GPIO_PIN_RESET) //GPIO_ResetBits(GPIOE, GPIO_PIN_1);
  226. #define IO_1 HAL_GPIO_WritePin(GPIOE, DS1302_DATA, GPIO_PIN_SET) //GPIO_SetBits(GPIOD, GPIO_Pin_13);
  227. #define IO_0 HAL_GPIO_WritePin(GPIOE, DS1302_DATA, GPIO_PIN_RESET) //GPIO_ResetBits(GPIOD, GPIO_Pin_13);
  228. #define SCLK_1 HAL_GPIO_WritePin(GPIOB, DS1302_SCK, GPIO_PIN_SET) //GPIO_SetBits(GPIOB, GPIO_Pin_9);
  229. #define SCLK_0 HAL_GPIO_WritePin(GPIOB, DS1302_SCK, GPIO_PIN_RESET) //DS1302_SCK_0 //GPIO_ResetBits(GPIOB, GPIO_Pin_9);
  230. /*定义待设置的时间: 秒、 分、 时、 日、 月、 星期、年、控制字*/
  231. volatile char time_tab[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  232. char tableDS1302[7];
  233. char date_1302[6];
  234. char time_1302[6];
  235. unsigned int year;
  236. unsigned char month;
  237. unsigned char date;
  238. unsigned char hour;
  239. unsigned char min;
  240. unsigned char sec;
  241. /********************************************************************
  242. 函 数 ds1302_initV2()DS1302初始化函数
  243. 功 能:DS1302初始化
  244. 入口参数:无
  245. 返 回 值:无
  246. ***********************************************************************/
  247. void ds1302_initV2(void)
  248. {
  249. SCLK_0;
  250. RST_0;
  251. RST_1;
  252. ds1302_writeV2(0x8e);
  253. ds1302_writeV2(0);
  254. RST_0;
  255. }
  256. /********************************************************************
  257. 函 数 名:ds1302_write()写一个字节数据函数
  258. 功 能:DS1302中写入一个字节
  259. 入口参数:time_tx 要写入的一个字节
  260. 返 回 值:无
  261. ***********************************************************************/
  262. void ds1302_writeV2(unsigned char time_tx)
  263. {
  264. int j;
  265. Set_IO_OUT();
  266. for(j=0;j<8;j++)
  267. {
  268. IO_0;
  269. SCLK_0;
  270. if((time_tx&0x01)!=0) IO_1;
  271. time_tx=time_tx>>1;
  272. SCLK_1;
  273. }
  274. SCLK_0;
  275. }
  276. /********************************************************************
  277. 函 数 名:ds1302_read()读一个字节函数
  278. 功 能:从DS1302读一个字节
  279. 入口参数:无
  280. 返 回 值:unsigned char :读取的数据
  281. ***********************************************************************/
  282. unsigned char ds1302_readV2(void)
  283. {
  284. int j;
  285. unsigned char time_rx=0;
  286. Set_IO_INPUT();
  287. for(j=0;j<8;j++)
  288. {
  289. SCLK_0;
  290. time_rx=time_rx>>1;
  291. //if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13)==1) time_rx|=0x80;//把接收到的数据放到接收寄存器的最高位
  292. if (DS1302_DATA_IN()==1) time_rx|=0x80;//把接收到的数据放到接收寄存器的最高位
  293. SCLK_1;
  294. }
  295. SCLK_0;
  296. return(time_rx);
  297. }
  298. /********************************************************************
  299. 函 数 名:set_time()设置时间函数
  300. 功 能:设置初始时间
  301. 说 明:使用了多字节写方式
  302. 入口参数:无
  303. 返 回 值:无
  304. ***********************************************************************/
  305. void set_time(void)
  306. {
  307. int i;
  308. RST_1;
  309. ds1302_writeV2(0xbe);
  310. for(i=0;i<8;i++)
  311. {
  312. ds1302_writeV2(time_tab[i]);
  313. }
  314. RST_0;
  315. }
  316. /********************************************************************
  317. 函 数 名:get_all()读取全部时间信息函数
  318. 功 能:读取DS1302内部的全部时间信息
  319. 入口参数:无
  320. 返 回 值:无。
  321. ***********************************************************************/
  322. void DS1302_get_all(void)
  323. {
  324. int i;
  325. RST_1;
  326. ds1302_writeV2(0xbf);
  327. for(i=0;i<7;i++)
  328. {
  329. tableDS1302[i]=ds1302_readV2();
  330. }
  331. RST_0;
  332. }
  333. /********************************************************************
  334. 函 数 名:get_time()读取时间函数
  335. 功 能:读取DS1302当前时间
  336. 入口参数:无
  337. 返 回 值:无。
  338. ***********************************************************************/
  339. void get_timeV2(void)
  340. {
  341. DS1302_get_all();
  342. time_1302[0]=tableDS1302[0]&0x0F;//求秒的个位
  343. time_1302[1]=tableDS1302[0]&0x70; //求秒的十位
  344. time_1302[1]>>=4;//右移4位
  345. time_1302[2]=tableDS1302[1]&0x0F; //求分的个位
  346. time_1302[3]=tableDS1302[1]&0x70; //求分的十位
  347. time_1302[3]>>=4;
  348. time_1302[4]=tableDS1302[2]&0x0F; //求时的个位
  349. time_1302[5]=tableDS1302[2]&0x70; //求时的十位
  350. time_1302[5]>>=4;
  351. }
  352. /********************************************************************
  353. 函 数 名:get_date()读取日期函数
  354. 功 能:读取DS1302当前日期
  355. 入口参数:无
  356. 返 回 值:无。
  357. ***********************************************************************/
  358. void get_date(void)
  359. {
  360. DS1302_get_all();
  361. date_1302[0]=tableDS1302[3]&0x0F;//求日的个位
  362. date_1302[1]=tableDS1302[3]&0x30; //求日的十位
  363. date_1302[1]>>=4;//右移4位
  364. date_1302[2]=tableDS1302[4]&0x0F; //求月的个位
  365. date_1302[3]=tableDS1302[4]&0x10; //求月的十位
  366. date_1302[3]>>=4;
  367. date_1302[4]=tableDS1302[6]&0x0F; //求年的个位
  368. date_1302[5]=tableDS1302[6]&0xF0; //求年的十位
  369. date_1302[5]>>=4;
  370. }
  371. void get_result(void)
  372. {
  373. int i;
  374. int j;
  375. get_date();
  376. i=date_1302[5];
  377. j=date_1302[4];
  378. year=i*10+j;
  379. i=date_1302[3];
  380. j=date_1302[2];
  381. month=i*10+j;
  382. i=date_1302[1];
  383. j=date_1302[0];
  384. date=i*10+j;
  385. get_timeV2();
  386. i=time_1302[5];
  387. j=time_1302[4];
  388. hour=i*10+j;
  389. i=time_1302[3];
  390. j=time_1302[2];
  391. min=i*10+j;
  392. i=time_1302[1];
  393. j=time_1302[0];
  394. sec=i*10+j;
  395. }
  396. void Set_IO_OUT(void)
  397. {
  398. GPIO_InitTypeDef GPIO_InitStructure; /*定义一个GPIO_InitTypeDef类型的结构体*/
  399. __HAL_RCC_GPIOE_CLK_ENABLE();
  400. GPIO_InitStructure.Pin = DS1302_DATA;
  401. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; // /设置引脚模式为输入
  402. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
  403. //GPIO_Init(GPIOE, &GPIO_InitStructure);
  404. HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
  405. }
  406. void Set_IO_INPUT(void)
  407. {
  408. GPIO_InitTypeDef GPIO_InitStructure; /*定义一个GPIO_InitTypeDef类型的结构体*/
  409. __HAL_RCC_GPIOE_CLK_ENABLE();
  410. GPIO_InitStructure.Pin = DS1302_DATA;
  411. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;//GPIO_MODE_INPUT; // /设置引脚模式为输入
  412. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_MEDIUM;
  413. //GPIO_Init(GPIOE, &GPIO_InitStructure);
  414. HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
  415. }