STM32PVD断电保存,进不了中断,搞了3天搞不出来,各种原因查,各种网上资料查,对比,每天弄到凌晨2-3点就是搞不出来,只能求助大神了!
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}//printf重定向
主函数:
int main(void)
{
RCC_Configuration(); //RCC时钟初始化
RCC_APB1PeriphClockCmd( RCC_APB1Periph_PWR, ENABLE);//打开电源时钟配置
PWR_PVDCmd(ENABLE);//使能PVD
PWR_PVDLevelConfig(PWR_PVDLevel_2V5);//电压阈值设定,这里也试过2V9
EXTI_Configuration();//中断初始化
GPIO_Configuration();//端口初始化
USART_Configuration();//串口初始化
NVIC_Configuration();//中断优先级初始化
cnt1=(*(__IO uint32_t*)(FLASH_ADR));//读cnt1的值
while(1)
{
printf("当前流量 cnt1=%d
",cnt1);//打印cnt1的值到电脑屏幕上
delay_ms(1000);
cnt1++;//cnt1数值加1
}
}
void GPIO_Configuration(void)//端口引脚配置
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void EXTI_Configuration(void)//PVD中断配置
{
EXTI_InitTypeDef EXTI_InitStructure;
PWR_ClearFlag(PWR_FLAG_PVDO);
EXTI_ClearITPendingBit(EXTI_Line16);
EXTI_InitStructure.EXTI_Line = EXTI_Line16;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void USART_Configuration(void)//串口配置
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void NVIC_Configuration(void)//中断优先级配置
{
NVIC_InitTypeDef NVIC_InitStructure;
//串口中断优先级配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//PVD中断优先级配置
NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断函数:
void PVD_IRQHandler(void)//PVD中断函数
{
EXTI_ClearITPendingBit(EXTI_Line16);//清中断16的标志
if(PWR_GetFlagStatus(PWR_FLAG_PVDO))//查电源标志
{
PWR_ClearFlag(PWR_FLAG_PVDO);//清电源标志
FLASH_Unlock();//解锁
FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|
FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);//清除标志位
FLASH_ErasePage(FLASH_0x0807F800);//要擦出页的起始地址
FLASH_ProgramWord(FLASH_ADR,cnt1);//写数据
FLASH_Lock();//锁定
}
}
主要实现的目的,是断电后,进入PVD中断,把CNT1的值存入stm32的flash中,然后电源恢复的时候,把flash中存的变量cnt1 读回来打印输出到电脑屏上。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>