想实现在断电前写flash,但是读取出来的结果是乱码,看下有没有大佬看出是什么问题代码如下:
/***********************************
* 文件名:main.c
* 作者:Kyle
* 日期:20170818
* 描述:任务2测试
* 版本:1.0
* 版本信息:测试flash保存,测试断电状态
* 芯片类型:stm32f103ze
* 库版本:V3.5.0
***********************************/
#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
#include "stmflash.h"
//要写入到STM32 FLASH的字符串数组
const u8 TEXT_Buffer[]={"2"};
#define SIZE sizeof(TEXT_Buffer) //数组长度
#define FLASH_SAVE_ADDR 0X08011000 //设置FLASH 保存地址(必须为偶数,且其值要大于本代码所占用FLASH的大小+0X08000000)
void PWR_PVD_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR时钟
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); //初始
PWR_PVDLevelConfig(PWR_PVDLevel_2V8);//设定监控阀值
PWR_PVDCmd(ENABLE);//使能PVD
}
void PVD_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line16);//清中断
if(PWR_GetFlagStatus(PWR_FLAG_PVDO))
{
STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)TEXT_Buffer,SIZE);
}
}
int main(void)
{
//初始化
u8 datatemp[SIZE];
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
PWR_PVD_Init();
//STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)TEXT_Buffer,SIZE);
//读flash数据
printf("
start
");
while(1)
{
//printf("
读取信息:%d
",BKP_ReadBackupRegister(BKP_DR9));
STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)datatemp,SIZE);
printf("
读取信息:%s
",datatemp);
delay_ms(1000);
}
return 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
/***********************************
* 文件名:main.c
* 作者:Kyle
* 日期:20170818
* 描述:任务2测试
* 版本:1.0
* 版本信息:测试flash保存,测试断电状态
* 芯片类型:stm32f103rb
* 库版本:V3.5.0
***********************************/
#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
#include "stmflash.h"
//要写入到STM32 FLASH的字符串数组
const u8 TEXT_Buffer[]={"cccc"};
#define SIZE sizeof(TEXT_Buffer) //数组长度
#define FLASH_SAVE_ADDR 0x08001800 //设置FLASH 保存地址(必须为偶数,且其值要大于本代码所占用FLASH的大小+0X08000000)
//u16 num = 35;
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;/*FLASH擦除完成标志*/
void PWR_PVD_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR时钟
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); //初始
PWR_PVDLevelConfig(PWR_PVDLevel_2V9);//设定监控阀值
PWR_PVDCmd(ENABLE);//使能PVD
}
void PVD_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line16);//清中断
if(PWR_GetFlagStatus(PWR_FLAG_PVDO))
{
/*
//使能BKP并保存
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//使能PWR和BKP外设时钟
PWR_BackupAccessCmd(ENABLE);//使能后备寄存器访问
BKP_WriteBackupRegister(BKP_DR9,num);
*/
//写flash
//STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)TEXT_Buffer,SIZE);
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
STMFLASH_Write_NoCheck(FLASH_SAVE_ADDR,(u16*)TEXT_Buffer,SIZE);
FLASH_Lock();
}
}
int main(void)
{
//初始化
u8 datatemp[SIZE];
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
PWR_PVD_Init();
//STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)TEXT_Buffer,SIZE);
//读flash数据
printf(" start ");
while(1)
{
//printf(" 读取BKP信息:%d ",BKP_ReadBackupRegister(BKP_DR9));
STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)datatemp,SIZE);
printf(" 读取FLASH信息:%s ",datatemp);
//擦除
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
FLASHStatus = FLASH_ErasePage((uint32_t)0x08001800);
FLASH_Lock();
delay_ms(1000);
}
return 0;
}
运行结果如下:
[2017-08-21 05:04:22.262]
start[2017-08-21 05:04:22.280]
[2017-08-21 05:04:22.280]
读取FLASH信息:bbbb[2017-08-21 05:04:22.280]
[2017-08-21 05:04:31.262]
读取FLASH信息:??????????[2017-08-21 05:04:31.276]
[2017-08-21 05:04:40.140]
start[2017-08-21 05:04:40.169]
[2017-08-21 05:04:40.169]
读取FLASH信息:cccc[2017-08-21 05:04:40.169]
[2017-08-21 05:04:49.140]
读取FLASH信息:??????????[2017-08-21 05:04:49.155]
一周热门 更多>