STM32实现对BKP的读取和写入(纯库函数无寄存器版)

BKP.H

#ifndef  _BKP_H_
#define  _BKP_H_

#include "DataType.h"

void InitBKP(void);
void WriteBKP(u8 DRNumber, u16 Data);
void ReadBKP(u8 DRNumber, u16* Data);
void PrintBKP(void);
u8   CheckBKP(void);



#endif

 

BKP.C

#include "BKP.h"
#include 
#include   //或者到里加上
#include 
#include "stdio.h"

#define  CHECK_CODE  0xAE86


void InitBKP(void)
{
	PWR_BackupAccessCmd(ENABLE);
	BKP_ClearFlag();
	BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);
	BKP_ITConfig(ENABLE);
	BKP_TamperPinCmd(ENABLE);
}

void ReadBKP(u8 DRNumber, u16 Data)
{
	switch (DRNumber)
	{
	case 0x01: BKP_WriteBackupRegister(BKP_DR1, Data); break;
	case 0x02: BKP_WriteBackupRegister(BKP_DR2, Data); break;
	case 0x03: BKP_WriteBackupRegister(BKP_DR3, Data); break;
	case 0x04: BKP_WriteBackupRegister(BKP_DR4, Data); break;
	case 0x05: BKP_WriteBackupRegister(BKP_DR5, Data); break;
	case 0x06: BKP_WriteBackupRegister(BKP_DR6, Data); break;
	case 0x07: BKP_WriteBackupRegister(BKP_DR7, Data); break;
	case 0x08: BKP_WriteBackupRegister(BKP_DR8, Data); break;
	case 0x09: BKP_WriteBackupRegister(BKP_DR9, Data); break;
	default:   BKP_WriteBackupRegister(BKP_DR1, Data);
	}
	BKP_WriteBackupRegister(BKP_DR10, CHECK_CODE);
}


void ReadBKP(u8 DRNumber, u16 * Data)
{
	switch (DRNumber)
	{
		case 0x01: *Data = BKP_ReadBackupRegister(BKP_DR1); break;
		case 0x02: *Data = BKP_ReadBackupRegister(BKP_DR2); break;
		case 0x03: *Data = BKP_ReadBackupRegister(BKP_DR3); break;
		case 0x04: *Data = BKP_ReadBackupRegister(BKP_DR4); break;
		case 0x05: *Data = BKP_ReadBackupRegister(BKP_DR5); break;
		case 0x06: *Data = BKP_ReadBackupRegister(BKP_DR6); break;
		case 0x07: *Data = BKP_ReadBackupRegister(BKP_DR7); break;
		case 0x08: *Data = BKP_ReadBackupRegister(BKP_DR8); break;
		case 0x09: *Data = BKP_ReadBackupRegister(BKP_DR9); break;
		default:   *Data = BKP_ReadBackupRegister(BKP_DR1); break;
	}
	
}




void PrintBKP(void)
{
	printf("DR1 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR1));
	printf("DR2 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR2));
	printf("DR3 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR3));
	printf("DR4 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR4));
	printf("DR5 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR5));
	printf("DR6 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR6));
	printf("DR7 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR7));
	printf("DR8 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR8));
	printf("DR9 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR9));
	printf("DR10 = 0x%04X\t", BKP_ReadBackupRegister(BKP_DR10));
}

u8 CheckBKP(void)
{
	if (BKP_ReadBackupRegister(BKP_DR10) == 0xAE86)   // 如果此位数据丢失,则BPK数据丢失
		return 1;
	else
		return 0;
}

 

你可能感兴趣的:(电子)