stm32 BKP寄存器操作[操作寄存器+库函数]
#include <stm32f10x_lib.h>
#include “system.h”
#include “usart.h”
#include “bkp.h”#define LED1 PAout(4)
#define LED2 PAout(5)
void Gpio_Init(void);
int main(void)
{
u16 data,i=10;Rcc_Init(9); //系统时钟设置 Usart1_Init(72,9600); Bkp_Init(); Tamper_Init(); Nvic_Init(0,0,TAMPER_IRQChannel,0); //设置中断 Gpio_Init(); while(i){ Write_Bkp(i,i); data = Read_Bkp(i); printf("\n DR%u = 0x%04X\n",i,data); delay(30000); //延时30ms i--; } while(1);
}
void Gpio_Init(void)
{
RCC->APB2ENR|=1<<2; //使能PORTA时钟GPIOA->CRL&=0x0000FFFF; // PA0~3设置为浮空输入,PA4~7设置为推挽输出 GPIOA->CRL|=0x33334444; //USART1 串口I/O设置 GPIOA -> CRH&=0xFFFFF00F; //设置USART1 的Tx(PA.9)为第二功能推挽,50MHz;Rx(PA.10)为浮空输入 GPIOA -> CRH|=0x000008B0;
}
#include “stm32f10x_it.h”#include “system.h”
#include “stdio.h”
#define LED1 PAout(4)
#define LED2 PAout(5)
#define LED3 PAout(6)
#define LED4 PAout(7)
extern u16 Read_Bkp(u8 reg);
void TAMPER_IRQHandler(void)
{
u16 i=10,data;LED4 =1 ; printf("\r\n A Tamper is coming .\r\n"); while(i){ data = Read_Bkp(i); printf("\n DR%u = 0x%04X\n",i,data); delay(30000); //延时30ms i--; } BKP->CSR |= 3<<0; //清除事件中断标志位
}
#include <stm32f10x_lib.h>#include “bkp.h”
void Bkp_Init(void)
{
RCC->APB1RSTR |= 1<<27; //复位BKP寄存器
RCC->APB1RSTR &= ~(1<<27);RCC->APB1ENR|=1<<28; //使能电源时钟 RCC->APB1ENR|=1<<27; //使能BKP时钟
}
/**
后备寄存器写入操作 reg:寄存器编号
data:要写入的数值
**/
void Write_Bkp(u8 reg,u16 data)
{PWR->CR|=1<<8; //取消备份区写保护
switch(reg)
{case 1: BKP->DR1=data; break; case 2: BKP->DR2=data; break; case 3: BKP->DR3=data; break; case 4: BKP->DR4=data; break; case 5: BKP->DR5=data; break; case 6: BKP->DR6=data; break; case 7: BKP->DR7=data; break; case 8: BKP->DR8=data; break; case 9: BKP->DR9=data; break; case 10: BKP->DR10=data; break;
}
}
u16 Read_Bkp(u8 reg)
{
u16 data;
switch(reg)
{
case 1:
data = BKP->DR1;
break;
case 2:
data = BKP->DR2;
break;
case 3:
data = BKP->DR3;
break;
case 4:
data = BKP->DR4;
break;
case 5:
data = BKP->DR5;
break;
case 6:
data = BKP->DR6;
break;
case 7:
data = BKP->DR7;
break;
case 8:
data = BKP->DR8;
break;
case 9:
data = BKP->DR9;
break;
case 10:
data = BKP->DR10;
break;
}
return data;
}
//开启入侵检测,检测引脚为GPIOC13 但是不用打开其时钟和设置引脚模式
void Tamper_Init()
{
BKP->CSR |= 3<<0; //清除事件中断标志位
BKP->CR |= 1<<1; //设定为入侵电平为低电平
BKP->CSR |= 1<<2; //允许入侵中断
BKP->CR |= 1<<0; //开启入侵检测
}
#include <stm32f10x_lib.h>void Bkp_Init(void);
void Write_Bkp(u8 reg,u16 data);
u16 Read_Bkp(u8 reg);
void Tamper_Init(void);
#include “stm32f10x.h”#include “stdio.h”
#define PRINTF_ON 1
#define CHECK_CODE 0xAE86
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void BKP_Configuration(void);void PrintBKP(void);
void WriteBKP(u16 Data,u8 DRNumber);
u8 CheckBKP(void);int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART_Configuration();
BKP_Configuration();if(CheckBKP()) { printf("\r\n The datas are as their initial status. \r\n"); WriteBKP(0xA522,2); PrintBKP(); }else{ printf("\r\n The datas have been changed . \r\n"); WriteBKP(0xA53C,1); PrintBKP(); } while(1);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 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; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void BKP_Configuration(void)
{
PWR_BackupAccessCmd(ENABLE);
BKP_ClearFlag();
BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);
BKP_ITConfig(ENABLE);
BKP_TamperPinCmd(ENABLE);
}void RCC_Configuration(void)
{
/ 定义枚举类型变量 HSEStartUpStatus /
ErrorStatus HSEStartUpStatus;/* 复位系统时钟设置*/ RCC_DeInit(); /* 开启HSE*/ RCC_HSEConfig(RCC_HSE_ON); /* 等待HSE起振并稳定*/ HSEStartUpStatus = RCC_WaitForHSEStartUp(); /* 判断HSE起是否振成功,是则进入if()内部 */ if(HSEStartUpStatus == SUCCESS) { /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */ RCC_PCLK2Config(RCC_HCLK_Div1); /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* 设置FLASH延时周期数为2 */ FLASH_SetLatency(FLASH_Latency_2); /* 使能FLASH预取缓存 */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* 使能PLL */ RCC_PLLCmd(ENABLE); /* 等待PLL输出稳定 */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); /* 选择SYSCLK时钟源为PLL */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* 等待PLL成为SYSCLK时钟源 */ while(RCC_GetSYSCLKSource() != 0x08); } /* 打开APB2总线上的GPIOA时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1 , &USART_ClockInitStructure); 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_Cmd(USART1,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;NVIC_InitStructure.NVIC_IRQChannel = TAMPER_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
void WriteBKP(u16 Data,u8 DRNumber) // 还可加入一些加密算法;DRNumber (1-9)
{
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);
}u8 CheckBKP(void)
{
if( BKP_ReadBackupRegister(BKP_DR10) == 0xAE86 ) // 如果此位数据丢失,则BPK数据丢失
return 1;
else
return 0;
}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));}
#if PRINTF_ON
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8) ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
return ch;
}#endif
#include “stm32f10x_it.h”#include “stdio.h”
extern void PrintBKP(void);
void TAMPER_IRQHandler(void)
{
printf(“\r\n A Tamper is coming .\r\n”);
PrintBKP();
BKP_ClearITPendingBit();
BKP_ClearFlag();}