本帖最后由 肥羊 于 2018-7-6 14:07 编辑
我打算用PVD实现掉电保存数据,然后就遇到一些奇怪的问题,读写flash是没有问题的。但是就是有些情况会保存不了数据,有些情况保存的了数据。
1.在调试的时候把调试JTAG拔掉,然后就可以保存数据了,然后以后都能保存数据
2.如果先断电在拔掉JTAG就会保存不了数据,以后都不能保存数据 |
void PWR_PVD_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//使能PWR和BKP外设时钟
NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn; //使能PVD所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
EXTI_StructInit(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line16; //PVD连接到中断线16上
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //使用中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //电压低于阀值时产生中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断线
EXTI_Init(&EXTI_InitStructure); //初始
EXTI_ClearITPendingBit(EXTI_Line16);//清中断
PWR_PVDLevelConfig(PWR_PVDLevel_2V9);//设定监控阀值
PWR_PVDCmd(ENABLE);//使能PVD
}
void PVD_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line16);//清中断
WriteAccountFlash();
}
//线擦除
void Init_FlashP(u32 address)
{
Erase_Flash(address);
}
//保存账目
void WriteAccountFlash(void)
{
u16 buf[50];
memcpy(&buf[0],&CreanZoren,4);
memcpy(&buf[2],&GameRunSt.Address,4);
STMFLASH_Write(ACCTION_ADDR,buf,50);
}
//读账目
void ReadAccountFlash(void)
{
u16 buf[50];
STMFLASH_Read(ACCTION_ADDR,buf,50);
memcpy(&CreanZoren, &buf[0],4);
memcpy(&GameRunSt.Address, &buf[2],4);
Init_FlashP(ACCTION_ADDR);
}
void CreanZore(void)
{
ReadAccountFlash();
if(CreanZoren!=CREANFALG)
{
CreanZoren=CREANFALG;
GameRunSt.Address=36;
WriteAccountFlash();
}
}
|