完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/* ******************************************************************* * file:ds1302_spi.c * * func:the driver of eeprom ds1302 * * time:2017.03.04 * * auth:lzx ******************************************************************* */ /*file*/ #include "config.h" #include #include "GPIO.h" #include "delay.h" #include "ds1302.h" /*flag*/ #define DS1302_SCK P44 #define DS1302_IO P20 #define DS1302_RST P21 #define DS1302_DELAY10US() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_() #define DS1302_READREGISTER_SECOND 0x81 #define DS1302_READREGISTER_MINUTE 0x83 #define DS1302_READREGISTER_HOUR 0x85 #define DS1302_READREGISTER_DAY 0x87 #define DS1302_READREGISTER_MONTH 0x89 #define DS1302_READREGISTER_WEEKDAY 0x8B #define DS1302_READREGISTER_YEAR 0X8D /*variable*/ struct calendar ds1302_calendar; unsigned char ds1302_calendar_char[]={'0','1','2','3','4','5','6','7','8','9'}; unsigned char *week[8]={"","星期一","星期二","星期三xfd","星期四","星期五","星期六","星期日"}; /* *********************************************** * 名称:ds1302_io_init(void) * * 功能:将P43口设置为输出 * * 参数: 无 * * 返回: void *********************************************** */ void ds1302_io_init(void) { GPIO_InitTypeDef GPIO_InitStructure; //结构定义 GPIO_InitStructure.Pin = GPIO_Pin_0|GPIO_Pin_1; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作 GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP GPIO_Inilize(GPIO_P2,&GPIO_InitStructure); //初始化 GPIO_InitStructure.Pin = GPIO_Pin_4; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作 GPIO_InitStructure.Mode = GPIO_PullUp; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP GPIO_Inilize(GPIO_P4,&GPIO_InitStructure); //初始化 } /* ************************************* * name:void ds1302_spi_initial() * * func:ds1302_spi_initial * * time:2017.03.04 * * auth:lzx ************************************* */ void ds1302_spi_initial(void) { DS1302_RST=0; DS1302_SCK=0; delay_ms(1); DS1302_RST=1; } /* ************************************* * name:void ds1302_spi_write(dat) * * func:ds1302_spi_write * * time:2017.03.04 * * auth:lzx ************************************* */ void ds1302_spi_write(unsigned char dat) { unsigned char i; for(i=0;i<8;i++) { DS1302_SCK=0; DS1302_DELAY10US(); if(dat&(0x01< { DS1302_IO=1; } else { DS1302_IO=0; } DS1302_DELAY10US(); DS1302_SCK=1; DS1302_DELAY10US(); DS1302_SCK=0; DS1302_DELAY10US(); } } /* ************************************* * name:uc ds1302_spi_read(void) * * func:ds1302_spi_read * * time:2017.03.04 * * auth:lzx ************************************* */ unsigned char ds1302_spi_read(void) { unsigned char i,temp=0x00; for(i=0;i<8;i++) { if(DS1302_IO) { temp=temp|(0x01< } DS1302_SCK=1; DS1302_DELAY10US(); DS1302_SCK=0; DS1302_DELAY10US(); } return temp; } /* *********************************************** * 名称:ds1302_write(address,dat) * * 功能:修正时间 * * 参数: address为修正的时间地址,例如年 * dat为数据,例如将月修改成6月,则为6 * * 返回: void *********************************************** */ void ds1302_write(unsigned char address,unsigned char dat) { ds1302_spi_initial(); ds1302_spi_write(address); ds1302_spi_write(dat); } /* *********************************************** * 名称:ds1302_initial(year,month,date,hour,minute,second,week) * * 功能:初始化时间 * * 参数:年月日时分秒星期 * * 返回: void *********************************************** */ void ds1302_initial(unsigned char year,unsigned char month,unsigned char date,unsigned char hour,unsigned char minute,unsigned char second,unsigned char week) { ds1302_write(0x8e,0x00); ds1302_write(DS1302_WRITEREGISTER_HOUR,hour); ds1302_write(DS1302_WRITEREGISTER_MINUTE,minute); ds1302_write(DS1302_WRITEREGISTER_SECOND,second); ds1302_write(DS1302_WRITEREGISTER_YEAR,year); ds1302_write(DS1302_WRITEREGISTER_MONTH,month); ds1302_write(DS1302_WRITEREGISTER_DAY,date); ds1302_write(DS1302_WRITEREGISTER_WEEKDAY,week); ds1302_write(0x8e,0x80); } /* ************************************* * name:uc ds1302_read(address) * * func:ds1302_read(address) * * time:2017.03.04 * * auth:lzx ************************************* */ unsigned char ds1302_read(unsigned char address) { unsigned char temp; ds1302_spi_initial(); ds1302_spi_write(address); temp=ds1302_spi_read(); return temp; } /* ************************************* * name:void ds1302_calendar(void) * * func:ds1302_calendar hex to char[] * * time:2017.03.05 * * auth:lzx ************************************* */ void ds1302_calendar_HexToString(void) { unsigned char second_ge=ds1302_read(DS1302_READREGISTER_SECOND)%16; //second unsigned char second_shi=ds1302_read(DS1302_READREGISTER_SECOND)/16; unsigned char minute_ge=ds1302_read(DS1302_READREGISTER_MINUTE)%16; //minute unsigned char minute_shi=ds1302_read(DS1302_READREGISTER_MINUTE)/16; unsigned char hour_ge=ds1302_read(DS1302_READREGISTER_HOUR)%16; //hour unsigned char hour_shi=ds1302_read(DS1302_READREGISTER_HOUR)/16; unsigned char day_ge=ds1302_read(DS1302_READREGISTER_DAY)%16; //day unsigned char day_shi=ds1302_read(DS1302_READREGISTER_DAY)/16; unsigned char month_ge=ds1302_read(DS1302_READREGISTER_MONTH)%16; //month unsigned char month_shi=ds1302_read(DS1302_READREGISTER_MONTH)/16; unsigned char weekday_ge=ds1302_read(DS1302_READREGISTER_WEEKDAY)%16; //weekday unsigned char year_ge=ds1302_read(DS1302_READREGISTER_YEAR)%16; //year unsigned char year_shi=ds1302_read(DS1302_READREGISTER_YEAR)/16; ds1302_calendar.clock[0]=ds1302_calendar_char[hour_shi]; //clock ds1302_calendar.clock[1]=ds1302_calendar_char[hour_ge]; ds1302_calendar.clock[2]=':'; ds1302_calendar.clock[3]=ds1302_calendar_char[minute_shi]; ds1302_calendar.clock[4]=ds1302_calendar_char[minute_ge]; ds1302_calendar.clock[5]=':'; ds1302_calendar.clock[6]=ds1302_calendar_char[second_shi]; ds1302_calendar.clock[7]=ds1302_calendar_char[second_ge]; ds1302_calendar.clock[8]=' |