浏览代码

RTC 功能调试完成, 第一版程序发布

guoqiang 1 年之前
父节点
当前提交
d7c158477e
共有 16 个文件被更改,包括 628 次插入6467 次删除
  1. 0 137
      Device/Ds1302.c
  2. 228 0
      Device/Rtcx.c
  3. 11 10
      Device/Ds1302.h
  4. 5 5
      Device/gpio.c
  5. 1 1
      Device/watchdog.h
  6. 231 294
      Project/AirControlValve.uvguix.孙凯
  7. 8 8
      Project/AirControlValve.uvoptx
  8. 7 7
      Project/AirControlValve.uvprojx
  9. 90 5968
      Project/JLinkLog.txt
  10. 3 3
      User/main.c
  11. 4 4
      User/main_task.c
  12. 1 1
      User/pid.c
  13. 6 6
      User/process.c
  14. 22 14
      User/protocol.c
  15. 8 8
      User/storage.c
  16. 3 1
      User/storage.h

+ 0 - 137
Device/Ds1302.c

@@ -1,137 +0,0 @@
-#include "Ds1302.h"
-#include "string.h"
-#include "gpio.h"
-#include "stdio.h"
-#include "ac780x_rtc.h"
-#include "ac780x.h"
-
-
-
-DateTime g_dateTime;
-
-/**
- * RTCCallBackFunc
- *
- * @param[in]  wparam: reserved
- * @param[in]  lparam: reserved
- * @return 0: success, other: error value
- *
- * @brief  中断回调函数
- * 改变LED输出,使得LED闪烁
- */
-void RTCCallBackFunc(void *device, uint32_t wpara, uint32_t lpara)
-{
-	
-	if (wpara & RTC_SC_RPIF_Msk) //预分频中断
-	{
-	
-	}
-	if (wpara & RTC_SC_RTIF_Msk) //RTC溢出中断
-	{
-		//printf("g_timer0Cnt:%d \r\n", g_timer0Cnt);
-		DS1302_UpdateTime();
-	}	
-	
-}
-
-/**
- * RTC_Timeout1s
- *
- * @param[in]  none
- * @return   none
- *
- * @brief  配置RTC计时1S,时钟源为Bus clock
- */
-void RTC_Timeout1s(void)
-{
-    uint32_t tmpMod = 239999;
-    uint32_t tmpPrescalerValue = 99;             //(99+1)*(239999+1)/24000000 = 1s
-    RTC_ConfigType RTCConfig;
-	
-		memset(&RTCConfig,0,sizeof(RTCConfig));
-
-    RTCConfig.clockSource = RTC_CLOCK_APB;                         //时钟源选择:0:Bus clk (demo板上总线时钟为24M);
-	                                                   //1: LSI 32KHz; 2: External OSC; 3:Internal 8M;
-    RTCConfig.periodValue = tmpMod;
-		RTCConfig.psrInterruptEn = DISABLE;
-    RTCConfig.rtcInterruptEn = ENABLE;
-    RTCConfig.psrValue = tmpPrescalerValue;      //time = (pre+1)*(mod+1)/clk
-    RTCConfig.rtcOutEn = DISABLE;
-    RTCConfig.callBack = RTCCallBackFunc;
-    RTC_Init(&RTCConfig);                        //配置RTC
-
-
-}
-
-
-
-
-void DS1302_Init(void)
-{
-
-	g_dateTime.year = 24;
-	g_dateTime.month = 7;
-	g_dateTime.day = 5;
-	g_dateTime.hour = 15;
-	g_dateTime.minute = 0;
-	g_dateTime.second = 0;
-	
-	RTC_Timeout1s();
-	
-
-}
-
-uint8_t Ds1302_ReadReg(uint8_t _address)
-{
-
-	
-	return 0;
-}
-
-
-void DS1302_SetTime(DateTime* pDateTime)
-{
-
-}
-
-
-void DS1302_UpdateTime()
-{
-	g_dateTime.second++;
-	if(g_dateTime.second >= 60){
-		g_dateTime.second=0;
-		g_dateTime.minute++;
-		if(g_dateTime.minute >= 60){
-			g_dateTime.minute=0;
-			g_dateTime.hour++;
-			if(g_dateTime.hour>= 24){
-					g_dateTime.hour=0;
-					g_dateTime.day++;
-			}
-		}
-	}
-	
-	//if(g_dateTime.day>= 3){
-	//
-	//}
-
-
-}
-
-
-void DS1302_PrintDateTime()
-{
-
-	
-		printf("DS1302 DateTime:[%d][%d][%d] \r\n", g_dateTime.hour, g_dateTime.minute, g_dateTime.second);
-
-}
-
-
-void DS1302_DeInit(void)
-{
-
-}
-
-
-

+ 228 - 0
Device/Rtcx.c

@@ -0,0 +1,228 @@
+#include "Rtcx.h"
+#include "string.h"
+#include "gpio.h"
+#include "stdio.h"
+#include "ac780x.h"
+
+DateTime g_dateTime;
+//DateTime tmpdateTime;
+
+#define ISL1208_IIC_ADDR		(0x6F)
+
+
+#define ISL1208_SEC_Reg 		0x00
+#define ISL1208_MIN_Reg 		0x01
+#define ISL1208_HOUR_Reg 	0x02
+#define ISL1208_DATE_Reg 	0x03
+#define ISL1208_MONTH_Reg 0x04
+#define ISL1208_YEAR_Reg 	0x05
+#define ISL1208_WEEK_Reg 	0x06
+#define ISL1208_CFG_Reg 		0x07
+
+#define FLAG_Reg 					(0x12)
+#define FLAG_VAL 	  			(0xAA)
+
+
+
+static uint8_t bin2bcd(uint8_t bin)
+{
+	return (((bin/10) << 4) + bin%10);
+}
+
+static uint8_t bcd2bin(uint8_t bcd)
+{
+	return ((bcd/16)*10 + bcd%16);
+}
+
+
+/**
+* @prototype I2C_ParaInit(void)
+*
+* @param[in] void
+* @return	 void
+*
+* @brief  	 初始化I2C.
+*/
+void I2C_ParaInit(void)
+{
+	I2C_ConfigType i2cConfig;
+	
+	/*初始化引脚功能为I2C.*/
+	//GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
+	//GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);
+	
+	/*清零结构体变量.*/
+	memset(&i2cConfig, 0x00, sizeof(i2cConfig));
+	
+	/*无论是主机还是从机模式都需要配置的参数.*/
+	i2cConfig.mode				= I2C_MASTER;/*设置主从机模式.*/
+	i2cConfig.extAddrEn			= DISABLE;/*10bit扩展地址禁能.*/
+	i2cConfig.dmaRxEn			= DISABLE;/*设置DMA接收数据.*/
+	i2cConfig.dmaTxEn			= DISABLE;/*设置DMA发送数据.*/
+	i2cConfig.interruptEn		= DISABLE;/*I2C中断,BND/SAMF/ARBLOST.*/
+	i2cConfig.nackInterruptEn	= DISABLE;/*NACK中断.*/
+	i2cConfig.ssInterruptEn		= DISABLE;/*总线start或stop中断.*/
+	i2cConfig.i2cEn				= ENABLE;/*使能模块.*/
+	i2cConfig.callBack			= NULL;/*中断回调函数.*/
+	
+	/*主机模式需要配置的参数,配置成从机模式可忽略.*/
+	i2cConfig.masterConfig.sampleCnt = 9;/*设置波特率为100Kbps,bandrate=(24M/(10*12*2))=100Kbps.*/
+	i2cConfig.masterConfig.stepCnt	 = 11;
+	i2cConfig.masterConfig.ARBEn 	 = ENABLE;/*设置主机仲裁功能.*/
+	i2cConfig.masterConfig.SYNCEn	 = ENABLE;/*设置主机SCL同步功能.*/
+	
+	/*从机模式需要配置的参数,配置成主机模式可忽略.*/
+	
+	//i2cConfig.slaveConfig.slaveAddr		 = AT24C02_DEV_ADDR;/*从机地址.*/
+	//i2cConfig.slaveConfig.slaveRangeAddr = 0;/*从机范围地址.*/
+	//i2cConfig.slaveConfig.glitchFilterCnt= 0;/*毛刺过滤.*/
+	//i2cConfig.slaveConfig.stretchEn		 = DISABLE;/*从机SCL延伸功能.*/
+	//i2cConfig.slaveConfig.rangeAddrEn	 = DISABLE;/*禁能范围地址.*/
+	//i2cConfig.slaveConfig.monitorEn		 = DISABLE;/*禁能从机监测功能.*/
+	//i2cConfig.slaveConfig.generalCallEn	 = DISABLE;/*从机SCL广播地址.*/
+	//i2cConfig.slaveConfig.wakeupEn		 = DISABLE;/*唤醒功能,仅从机时有效.*/
+	//i2cConfig.slaveConfig.rxOFInterruptEn	= DISABLE;/*接收缓存溢出中断.*/
+	//i2cConfig.slaveConfig.txUFInterruptEn	= DISABLE;/*发送缓存溢出中断.*/
+	
+	
+	I2C_Init(I2C1, &i2cConfig);
+	
+}
+
+
+
+static uint8_t isl1208_readbyte(uint8_t addr)
+{
+	uint8_t data;
+	
+	I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
+	I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, &data, 1);//读取数据
+	
+	return data;
+}
+
+
+static void isl1208_writebyte(uint8_t addr, uint8_t data)
+{
+	uint8_t buffer[2];
+	buffer[0] = addr;
+	buffer[1] = data;
+	
+	I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 2, ENABLE);//发送读取地址
+}
+
+
+void RTCx_SetTime(DateTime* pDateTime)
+{
+
+		uint8_t addr = 0x00;
+		uint8_t buffer[8];
+	
+		pDateTime->year-=2000;		//年默认2000年开始
+		if(pDateTime->year > 100) pDateTime->year = 0;
+		pDateTime->week -= 1;
+	
+		//cfg = ht1382_readbyte(HT1382_CFG_Reg);
+		buffer[0] = addr;
+	
+		buffer[1] = bin2bcd(pDateTime->second);
+		buffer[2] = bin2bcd(pDateTime->minute);
+		buffer[3] = bin2bcd(pDateTime->hour);
+		buffer[3] |= 0x80;  //时   24小时模式
+		buffer[4] = bin2bcd(pDateTime->day);
+		buffer[5] = bin2bcd(pDateTime->month);
+		buffer[6] = bin2bcd(pDateTime->year);
+		buffer[7] = bin2bcd(pDateTime->week);
+		
+		isl1208_writebyte(ISL1208_CFG_Reg,0x90); //写控制寄存器、 开启写使能、暂停时钟晶振、状态自动复位
+		
+		I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, buffer, 8, ENABLE);//发送读取地址
+		
+		printf("RTC_SetTime end \r\n");
+	
+}
+
+static void RTCx_GetTime(void)
+{
+	
+	uint8_t addr = 0x00;
+	uint8_t data[7];
+	
+	I2C_MasterTransmitPoll(I2C1, ISL1208_IIC_ADDR, &addr, 1, DISABLE);//发送读取地址
+	I2C_MasterReceivePoll(I2C1, ISL1208_IIC_ADDR, data, 7);//读取数据
+	
+	data[2] = 0x7F&data[2];
+	
+
+	//BCD码转十进制
+	g_dateTime.second  = bcd2bin(data[0]);
+	g_dateTime.minute  = bcd2bin(data[1]);
+	g_dateTime.hour = bcd2bin(data[2]);
+	g_dateTime.day  = bcd2bin(data[3]);
+	g_dateTime.month  = bcd2bin(data[4]);
+	g_dateTime.year = bcd2bin(data[5])+2000;
+	g_dateTime.week = bcd2bin(data[6])+1;	
+	
+}
+
+
+void RTCx_UpdateTime(void)
+{
+	RTCx_GetTime();
+	
+}
+
+
+void RTCx_PrintDateTime(void)
+{
+	RTCx_GetTime();
+	printf("DateTime:[%d][%d][%d] [%d][%d][%d] week[%d] \r\n",g_dateTime.year, g_dateTime.month, g_dateTime.day, g_dateTime.hour, g_dateTime.minute, g_dateTime.second, g_dateTime.week);
+
+}
+
+
+/*
+*********************************************************************************************************
+*	函 数 名: void DS1307_Init_Time(void)
+*	功能说明: 第一次上电时,需要初始化时间,初始化一次后就不必重复初始化
+*	形    参:无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void RTCx_Init(void)
+{
+	
+	I2C_ParaInit();
+
+	if(FLAG_VAL != isl1208_readbyte(FLAG_Reg))//如果未初始化,执行初始化时间
+	{
+			g_dateTime.year = 2024;
+			g_dateTime.month = 7;
+			g_dateTime.day = 12;
+			g_dateTime.hour = 11;
+			g_dateTime.minute = 56;
+			g_dateTime.second = 20;
+			g_dateTime.week = 5;
+		
+			RTCx_SetTime(&g_dateTime);
+		
+			isl1208_writebyte(FLAG_Reg, FLAG_VAL);
+		
+	}	else{
+		
+			RTCx_PrintDateTime();
+		
+	}
+	
+	
+}
+
+
+void RTCx_DeInit(void)
+{
+
+	
+}
+
+
+

+ 11 - 10
Device/Ds1302.h

@@ -34,34 +34,35 @@
 
 /*************<start>******************/
 
-#ifndef DS1302_H__
-#define DS1302_H__
+#ifndef RTCX_H__
+#define RTCX_H__
 
 #if 1
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#include "ac780x_spi.h"
+#include "ac780x_i2c.h"
 
 typedef struct _datetime
 {
-    uint8_t year;
+    uint16_t year;
     uint8_t month;
     uint8_t day;
     uint8_t hour;
     uint8_t minute;
     uint8_t second;
+		uint8_t week;
 }DateTime;
 
 extern DateTime g_dateTime;
 
 
-void DS1302_Init(void);
-void DS1302_SetTime(DateTime* pDateTime);
-void DS1302_UpdateTime();
-void DS1302_PrintDateTime();
-void DS1302_DeInit(void);
+void RTCx_Init(void);
+void RTCx_SetTime(DateTime* pDateTime);
+void RTCx_UpdateTime(void);
+void RTCx_PrintDateTime(void);
+void RTCx_DeInit(void);
 
 
 #ifdef __cplusplus
@@ -71,4 +72,4 @@ void DS1302_DeInit(void);
 
 #endif 
 
-#endif //DS1302_H__
+#endif //RTCX_H__

+ 5 - 5
Device/gpio.c

@@ -117,12 +117,12 @@ void Gpio_Init(void)
 		GPIO_SetFunc(GPIOA, GPIO_PIN7, GPIO_FUN3);//MOSI
 		GPIO_SetFunc(GPIOA, GPIO_PIN8, GPIO_FUN3);//CS
 		
-		/*³õʼ»¯RTC  ds1302*/
-		//GPIO_SetFunc(GPIOB, GPIO_PIN13, GPIO_FUN3);//I2C1_SCL
-		//GPIO_SetFunc(GPIOC, GPIO_PIN4, GPIO_FUN3);//I2C1_sda
+		/*³õʼ»¯RTC  ds1307*/
+		GPIO_SetFunc(GPIOB, GPIO_PIN13, GPIO_FUN3);//I2C1_SCL
+		GPIO_SetFunc(GPIOC, GPIO_PIN4, GPIO_FUN3);//I2C1_sda
 		
-		GPIO_SetFunc(GPIOB, GPIO_PIN6, GPIO_FUN0);
-		GPIO_SetDir(GPIOB, GPIO_PIN6, GPIO_OUT);
+		//GPIO_SetFunc(GPIOB, GPIO_PIN6, GPIO_FUN0);
+		//GPIO_SetDir(GPIOB, GPIO_PIN6, GPIO_OUT);
 }
 
 

+ 1 - 1
Device/watchdog.h

@@ -40,7 +40,7 @@ extern "C" {
 #include "ac780x.h"
 
 
-//#define WATCHDOG_ENABLE   (1)  //
+#define WATCHDOG_ENABLE   (1)  //
 #define WATCHDOG_TIMEOUT  2  // 2s 
 
 

文件差异内容过多而无法显示
+ 231 - 294
Project/AirControlValve.uvguix.孙凯


+ 8 - 8
Project/AirControlValve.uvoptx

@@ -155,7 +155,7 @@
       <DebugFlag>
         <trace>0</trace>
         <periodic>1</periodic>
-        <aLwin>1</aLwin>
+        <aLwin>0</aLwin>
         <aCover>0</aCover>
         <aSer1>0</aSer1>
         <aSer2>0</aSer2>
@@ -687,24 +687,24 @@
     <File>
       <GroupNumber>3</GroupNumber>
       <FileNumber>40</FileNumber>
-      <FileType>5</FileType>
+      <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Device\Ds1302.h</PathWithFileName>
-      <FilenameWithoutPath>Ds1302.h</FilenameWithoutPath>
+      <PathWithFileName>..\Device\Rtcx.c</PathWithFileName>
+      <FilenameWithoutPath>Rtcx.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
       <FileNumber>41</FileNumber>
-      <FileType>1</FileType>
+      <FileType>5</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Device\Ds1302.c</PathWithFileName>
-      <FilenameWithoutPath>Ds1302.c</FilenameWithoutPath>
+      <PathWithFileName>..\Device\Rtcx.h</PathWithFileName>
+      <FilenameWithoutPath>Rtcx.h</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -728,7 +728,7 @@
 
   <Group>
     <GroupName>::Device</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>1</RteFlg>

+ 7 - 7
Project/AirControlValve.uvprojx

@@ -49,7 +49,7 @@
             <InvalidFlash>1</InvalidFlash>
           </TargetStatus>
           <OutputDirectory>.\Objects\</OutputDirectory>
-          <OutputName>ElectricalValve_20240626</OutputName>
+          <OutputName>ElectricalValve_20240803</OutputName>
           <CreateExecutable>1</CreateExecutable>
           <CreateLib>0</CreateLib>
           <CreateHexFile>1</CreateHexFile>
@@ -589,14 +589,14 @@
               <FilePath>..\Device\Motor.c</FilePath>
             </File>
             <File>
-              <FileName>Ds1302.h</FileName>
-              <FileType>5</FileType>
-              <FilePath>..\Device\Ds1302.h</FilePath>
+              <FileName>Rtcx.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Device\Rtcx.c</FilePath>
             </File>
             <File>
-              <FileName>Ds1302.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Device\Ds1302.c</FilePath>
+              <FileName>Rtcx.h</FileName>
+              <FileType>5</FileType>
+              <FilePath>..\Device\Rtcx.h</FilePath>
             </File>
           </Files>
         </Group>

文件差异内容过多而无法显示
+ 90 - 5968
Project/JLinkLog.txt


+ 3 - 3
User/main.c

@@ -10,7 +10,7 @@
 #include "Motor.h"
 #include "W25Q64.h"
 #include "AngleSensor.h"
-#include "Ds1302.h"
+#include "Rtcx.h"
 #include "storage.h"
 
 
@@ -41,11 +41,11 @@ int main(void)
     Gpio_Init();
 	  IAP_Init();
 
-		DS1302_Init();
+		RTCx_Init();
 		AngleSensor_Init();
 		ADCSample_Init();
 		Motor_Init();
-		//W25Q64_Init();
+		W25Q64_Init();
 		Storage_Init();
 
 		Process_Init();

+ 4 - 4
User/main_task.c

@@ -121,7 +121,7 @@ void mb_03_handle(void)
 				}
 				break;
 			case Cmd_DateTime:
-				if(6 == read_len){
+				if(8 == read_len){
 					tx_offset += Read_DateTime(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
 				}else{
 					g_mb_ctx.exception_code = INVALID_DATA;
@@ -381,8 +381,8 @@ void mb_10_handle(void)
 					}
 					break;
 				case Cmd_DateTime:
-					if(6 == data_len){
-						ret_write |= Write_DateTime(g_mb_ctx.rxbuf+7, 6); 
+					if(8 == data_len){
+						ret_write |= Write_DateTime(g_mb_ctx.rxbuf+7, 8); 
 						if(ret_write&RET_DATAINVALID)
 							g_mb_ctx.exception_code = INVALID_DATA;
 					}else{
@@ -758,7 +758,7 @@ void Task_Handle(void)
 {
 	
 		Process_RunPrd();
-		//Process_Motor();
+
 		Process_Storage(); 
 
 		Uart0_RxDataHandle();

+ 1 - 1
User/pid.c

@@ -47,4 +47,4 @@ void PID_Calc(PID *pid, float reference, float feedback)
 		
 		
 }
- 
+

+ 6 - 6
User/process.c

@@ -8,7 +8,7 @@
 #include "AngleSensor.h"
 #include "pid.h"
 #include "cfg.h"
-#include "Ds1302.h"
+#include "Rtcx.h"
 #include "storage.h"
 
 uint8_t		g_devicebusy = 0;
@@ -38,7 +38,7 @@ uint8_t g_lockcount[STATUS_LOCKALL]={0};
 uint8_t g_covercount[STATUS_COVERALL]={0};
 
 static float s_angle = 0.0;
-static float tmp_angle = 0.0;
+//static float tmp_angle = 0.0;
 static uint8_t get_lockstatus();
 static PID g_pid = {0}; 
 static uint8_t tmp_speed;
@@ -66,7 +66,6 @@ static void update_runstate()
 	
 
 
-
 }
 
 void Process_Init(void)
@@ -93,8 +92,6 @@ void Process_Init(void)
 
 void Process_RunPrd(void)
 {
-	uint8_t i=0;
-	uint8_t _status = 0;
 	
 	/*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
 	if (g_blinkLedTime >= g_blinkLedTgtTime)
@@ -157,6 +154,9 @@ void Process_RunPrd(void)
 		
 		printf(" Battery Voltage:%f V \r\n", getBatteryVoltage());
 		
+		RTCx_PrintDateTime();
+		//AngleSensor_PrintInfo();
+		
 	
 	}
 	
@@ -420,7 +420,7 @@ void Process_Poweroff(void)
 					mdelay(500);
 					LDOEn_DISABLE;
 					//NVIC_SystemReset();
-					printf(" NVIC_SystemReset  \r\n");
+					//printf(" NVIC_SystemReset  \r\n");
 				
 			}
 		}

+ 22 - 14
User/protocol.c

@@ -4,7 +4,7 @@
 #include "uart.h"
 #include "Motor.h"
 #include "AngleSensor.h"
-#include "Ds1302.h"
+#include "Rtcx.h"
 #include "storage.h"
 
 
@@ -12,9 +12,9 @@ static DataItem tmp_record;
 static uint8_t tmp_i=0;
 	
 #ifdef IS_BOOTLOADER
-uint32_t Firmware_Version[4] = {1, 0, 0, 20240711};
+uint32_t Firmware_Version[4] = {1, 0, 0, 20240802};
 #else
-uint32_t Firmware_Version[4] = {1, 1, 1, 20240711};
+uint32_t Firmware_Version[4] = {1, 1, 1, 20240803};
 #endif
 
 
@@ -165,22 +165,24 @@ uint16_t Read_Angle(uint8_t *pBuf, uint16_t buf_len)
 uint16_t Read_DateTime(uint8_t *pBuf, uint16_t buf_len)
 {
 
-	if( buf_len < 6){
+	if( buf_len < 8){
 		return 0;
 	}
 	
-	DS1302_UpdateTime();
+	RTCx_UpdateTime();
 	
 	
-	pBuf[0] = g_dateTime.year;
+	pBuf[0] = g_dateTime.year-2000;
 	pBuf[1] = g_dateTime.month;
 	
 	pBuf[2] = g_dateTime.day;
 	pBuf[3] = g_dateTime.hour;
 	pBuf[4] = g_dateTime.minute;
 	pBuf[5] = g_dateTime.second;
+	pBuf[6] = g_dateTime.week;
+	pBuf[7] = 0x00;
 
-	return 6;
+	return 8;
 	
 }
 
@@ -358,14 +360,20 @@ uint8_t Write_ThresholdCalibration(uint8_t *pdata, uint8_t len)
 //±ê¶¨É豸ÈÕÆÚʱ¼ä 
 uint8_t Write_DateTime(uint8_t *pdata, uint8_t len)
 {
-	if(len == 6){
+	static DateTime dateTime;
+	
+	if(len == 8){
+		
+		dateTime.year = pdata[0];
+		dateTime.year += 2000;
+		dateTime.month = pdata[1];
+		dateTime.day = pdata[2];
+		dateTime.hour = pdata[3];
+		dateTime.minute = pdata[4];
+		dateTime.second = pdata[5];
+		dateTime.week   = pdata[6];
 		
-		g_dateTime.year = pdata[0];
-		g_dateTime.month = pdata[1];
-		g_dateTime.day = pdata[2];
-		g_dateTime.hour = pdata[3];
-		g_dateTime.minute = pdata[4];
-		g_dateTime.second = pdata[5];
+		RTCx_SetTime(&dateTime);
 		
 		return RET_OK;
 		

+ 8 - 8
User/storage.c

@@ -1,5 +1,5 @@
 #include "storage.h"
-#include "Ds1302.h"
+#include "Rtcx.h"
 
 #define INDEX_ADDR  (0x00)
 #define INDEX_MAGIC (0xAA55AA55)
@@ -54,7 +54,7 @@ void Storage_AddItem(uint8_t itemtype, uint8_t eventtype)
 		item.eventtype = eventtype;
 		
 		//timestamp 
-		item.timestamp[0] = g_dateTime.year;
+		item.timestamp[0] = g_dateTime.year-2000;
 		item.timestamp[1] = g_dateTime.month;
 		item.timestamp[2] = g_dateTime.day;
 		item.timestamp[3] = g_dateTime.hour;
@@ -77,7 +77,7 @@ void Storage_AddItem(uint8_t itemtype, uint8_t eventtype)
 		
 		W25Q64_WriteBytes(g_recordindex.next_addr, pItem, sizeof(DataItem));
 		g_recordindex.num++;
-		g_recordindex.next_addr += PAGE_SIZE;
+		g_recordindex.next_addr += ITEM_STORAGE_SPACE;
 		
 		//printf("Storage_AddItem: write ok \r\n");
 		
@@ -105,9 +105,9 @@ uint8_t Storage_GetItemFromOldest(DataItem* pitem)
 	}
 	
 	if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
-		item_addr = g_recordindex.next_addr - g_recordindex.num*PAGE_SIZE;
+		item_addr = g_recordindex.next_addr - g_recordindex.num*ITEM_STORAGE_SPACE;
 	}else{
-		item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-g_recordindex.num*PAGE_SIZE;
+		item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-g_recordindex.num*ITEM_STORAGE_SPACE;
 	}
 	
 	
@@ -130,9 +130,9 @@ uint8_t Storage_GetItemFromOldest2(uint8_t read_id, DataItem* pitem)
 	}
 	
 	if(g_recordindex.start_addr+g_recordindex.num*PAGE_SIZE <= g_recordindex.next_addr){
-		item_addr = g_recordindex.next_addr - (g_recordindex.num-read_id)*PAGE_SIZE;
+		item_addr = g_recordindex.next_addr - (g_recordindex.num-read_id)*ITEM_STORAGE_SPACE;
 	}else{
-		item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-(g_recordindex.num-read_id)*PAGE_SIZE;
+		item_addr = g_recordindex.next_addr+MAX_STORAGE_SPACE-(g_recordindex.num-read_id)*ITEM_STORAGE_SPACE;
 	}
 	
 	
@@ -203,7 +203,7 @@ void update_index(void)
 	//printf("update_index 222 \r\n");
 	W25Q64_WriteBytes(INDEX_ADDR, (uint8_t *)&g_recordindex, sizeof(record_index));
 	
-	printf("update_index 333 \r\n");
+	//printf("update_index 333 \r\n");
 
 }
 

+ 3 - 1
User/storage.h

@@ -47,7 +47,9 @@ extern "C" {
 #define MAX_STORAGE_SPACE   (0x100000)  //1MB 
 #define MAX_RECORD_NUM      (200)
 
-//#define RECORD_SIZE				(256)	/**/
+#define ITEM_STORAGE_SPACE	(256)	/* ±¶ÊýÒªÓësector´óС¶ÔÆë*/
+
+
 #define ITEM_MAGIC				(0xA5A5)
 #define ITEM_LOG					(0x01)
 #define ITEM_RECORD				(0x02)