int i=1; uint8_t Pwd_ValidFlag = 0xA5; uint8_t Pwd_ValidFlagClear = 0x00; uint16_t Pwd_valid_address = ADDR_SETTING_LED_PWD_DATETIME+EEPROM_SETTING_LED_PWD_SIZE-7; for(i=1;i<=10;i++) { Setting_Led_Get_Pwd_LastValid(Pwd_valid_address, &Pwd_last_valid); if(i == 10) { Pwd_last_group = 0; Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME+(Pwd_last_group * 7),&Pwd_ValidFlag); break; } if (Pwd_last_valid == 0xA5) { Pwd_last_group = 10 - i; Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME+(Pwd_last_group * 7),&Pwd_ValidFlagClear); Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME+(Pwd_last_group * 7)+7,&Pwd_ValidFlag); break; } Pwd_valid_address = Pwd_valid_address - 7; } abTime[0] = g_stcPanel.stcRTCGet.Year; abTime[1] = g_stcPanel.stcRTCGet.Month; abTime[2] = g_stcPanel.stcRTCGet.Day; abTime[3] = g_stcPanel.stcRTCGet.Hour; abTime[4] = g_stcPanel.stcRTCGet.Minute; abTime[5] = g_stcPanel.stcRTCGet.Second; Setting_Led_Set_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME+(Pwd_last_group * 7)+7+1,abTime); Setting_Led_Get_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME+(Pwd_last_group * 7)+1,abTime2);要求7x10=70,留出10组,分别为0-9,每组7个字节,第1个字节存有效性标志0xA5,后6个字节存储日期时间,每次轮询0xA5标志,清零标志,在下一个组写入标志与日期时间,第十组写完后,下一次从第一组开始,请根据上面给出的部分代码进行完善和优化
时间: 2023-06-28 20:04:18 浏览: 184
M531X DM流程_v2.0.pdf
根据需求,需要对给出的部分代码进行完善和优化,以下是一个可能的实现方案:
1. 在代码开头,定义常量和变量,用于存储密码数据的地址、有效性标志和日期时间等信息:
```c
#define ADDR_SETTING_LED_PWD_DATETIME 0x8000 // 密码数据的存储地址,假设为0x8000
#define EEPROM_SETTING_LED_PWD_SIZE 70 // 密码数据总大小,假设为70字节
uint8_t pwd_valid_flag = 0xA5; // 密码有效性标志
uint8_t pwd_valid_flag_clear = 0x00; // 密码有效性标志清零值
uint8_t pwd_last_valid; // 上一个密码数据的有效性标志
uint8_t pwd_last_group = 0; // 上一个密码数据所在的组号,初始为0
uint16_t pwd_valid_address = ADDR_SETTING_LED_PWD_DATETIME + EEPROM_SETTING_LED_PWD_SIZE - 7; // 上一个密码数据的地址,初始为最后一个组的起始地址
uint8_t abTime[6]; // 当前时间,用于更新密码数据的日期时间信息
uint8_t abTime2[6]; // 读取到的密码数据的日期时间信息,用于检查写入是否成功
```
2. 在一个循环中轮询密码数据,找到上一个有效的密码数据的组号和地址,并清除其有效性标志:
```c
for (int i = 1; i <= 10; i++) {
Setting_Led_Get_Pwd_LastValid(pwd_valid_address, &pwd_last_valid);
if (i == 10) {
pwd_last_group = 0;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
break;
}
if (pwd_last_valid == pwd_valid_flag) {
pwd_last_group = 10 - i;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag_clear);
break;
}
pwd_valid_address -= 7;
}
```
3. 更新密码数据的日期时间信息,写入有效性标志和日期时间到下一个组:
```c
abTime[0] = g_stcPanel.stcRTCGet.Year;
abTime[1] = g_stcPanel.stcRTCGet.Month;
abTime[2] = g_stcPanel.stcRTCGet.Day;
abTime[3] = g_stcPanel.stcRTCGet.Hour;
abTime[4] = g_stcPanel.stcRTCGet.Minute;
abTime[5] = g_stcPanel.stcRTCGet.Second;
Setting_Led_Set_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7 + 1, abTime);
Setting_Led_Get_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 1, abTime2);
pwd_last_group = (pwd_last_group + 1) % 10; // 更新上一个密码数据所在的组号为下一个组
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
```
4. 重复上述过程,直到所有的密码数据都被写入:
```c
for (int i = 1; i <= 9; i++) {
pwd_valid_address -= 7;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7, &pwd_valid_flag_clear);
abTime[0] = 0;
abTime[1] = 0;
abTime[2] = 0;
abTime[3] = 0;
abTime[4] = 0;
abTime[5] = 0;
Setting_Led_Set_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7 + 1, abTime);
Setting_Led_Get_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 1, abTime2);
pwd_last_group = (pwd_last_group + 1) % 10;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
}
```
完整的代码如下所示:
```c
#define ADDR_SETTING_LED_PWD_DATETIME 0x8000 // 密码数据的存储地址,假设为0x8000
#define EEPROM_SETTING_LED_PWD_SIZE 70 // 密码数据总大小,假设为70字节
uint8_t pwd_valid_flag = 0xA5; // 密码有效性标志
uint8_t pwd_valid_flag_clear = 0x00; // 密码有效性标志清零值
uint8_t pwd_last_valid; // 上一个密码数据的有效性标志
uint8_t pwd_last_group = 0; // 上一个密码数据所在的组号,初始为0
uint16_t pwd_valid_address = ADDR_SETTING_LED_PWD_DATETIME + EEPROM_SETTING_LED_PWD_SIZE - 7; // 上一个密码数据的地址,初始为最后一个组的起始地址
uint8_t abTime[6]; // 当前时间,用于更新密码数据的日期时间信息
uint8_t abTime2[6]; // 读取到的密码数据的日期时间信息,用于检查写入是否成功
for (int i = 1; i <= 10; i++) {
Setting_Led_Get_Pwd_LastValid(pwd_valid_address, &pwd_last_valid);
if (i == 10) {
pwd_last_group = 0;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
break;
}
if (pwd_last_valid == pwd_valid_flag) {
pwd_last_group = 10 - i;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag_clear);
break;
}
pwd_valid_address -= 7;
}
abTime[0] = g_stcPanel.stcRTCGet.Year;
abTime[1] = g_stcPanel.stcRTCGet.Month;
abTime[2] = g_stcPanel.stcRTCGet.Day;
abTime[3] = g_stcPanel.stcRTCGet.Hour;
abTime[4] = g_stcPanel.stcRTCGet.Minute;
abTime[5] = g_stcPanel.stcRTCGet.Second;
Setting_Led_Set_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7 + 1, abTime);
Setting_Led_Get_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 1, abTime2);
pwd_last_group = (pwd_last_group + 1) % 10;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
for (int i = 1; i <= 9; i++) {
pwd_valid_address -= 7;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7, &pwd_valid_flag_clear);
abTime[0] = 0;
abTime[1] = 0;
abTime[2] = 0;
abTime[3] = 0;
abTime[4] = 0;
abTime[5] = 0;
Setting_Led_Set_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 7 + 1, abTime);
Setting_Led_Get_Pwd_DateTime(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7) + 1, abTime2);
pwd_last_group = (pwd_last_group + 1) % 10;
Setting_Led_Set_Pwd_Valid(ADDR_SETTING_LED_PWD_DATETIME + (pwd_last_group * 7), &pwd_valid_flag);
}
```
阅读全文