本工程实现 低电压检测 (分为低电压中断 及低电压复位 )电压门限值可设,经过测试 低电压复位好用 但是低电压中断 寄存器配置应该没什么问题 但是总死在startup_NUC1xx.s里,如果用人弄通了 告诉我一声不胜感激
main函数- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: 欠压检测(本来打算用欠压检测中断的,但是老是死机老死在startup_NUC1xx.s中)
- 后来只能用欠压检测复位(其中欠压检测中断的代码 没删 有兴趣的可以研究一下
- 如果解决了问题 告诉我一声 在下不胜感激 )
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- Set_System(); //封装一些初始化模块
- while(1)
- {}
- }
hw_config函数- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- DrvGPIO_ClrBit(E_GPA,2); //LED1 点亮
- DrvGPIO_ClrBit(E_GPA,3); //LED2 点亮
- DrvGPIO_ClrBit(E_GPA,4); //LED3 点亮
- delay_ms(1000);
- DrvGPIO_SetBit(E_GPA,4); //LED3 熄灭
- delay_ms(1000);
- BODCR_Configuration(); //配置欠压检测控制寄存器BODCR
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);// 与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); // while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT ); //led端口设置为输出
- DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- }
- /*************************************************************************************
- ** Function name: BODCR_Configuration
- ** Descriptions: BODCR配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void BODCR_Configuration()
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SelectBODVolt(3); //设置欠压检测门限值为2.2V (3: 4.5V, 2: 3.8V, 1: 2.7V, 0: 2.2V)
- DrvSYS_DisableBODLowPowerMode(); //SYS->BODCR.BOD_LPM = 0;BOD工作于正常模式
- DrvSYS_DisableLowVoltReset(); //SYS->BODCR.LVR_EN = 0; 禁止低压复位功能
- DrvSYS_SetBODFunction (1, 1,BODCR_CALLBACK); // SYS->BODCR.BOD_EN = 1;使能欠压检测功能
- //SYS->BODCR.BOD_RSTEN = 0;使能欠压中断功能 SYS->BODCR.BOD_RSTEN = 1;使能欠压复位功能
- //备注:我尝试使用欠压中断功能 但是老是死在startup_NUC1xx.s中 不知为什么
- //BODCR_CALLBACK 设置欠压中断的回调函数
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: BODCR_CALLBACK
- ** Descriptions: 欠压中断的回调函数
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void BODCR_CALLBACK()
- {
- DrvGPIO_SetBit(E_GPA,2);
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
hw_config头文件- #ifndef __HW_CONFIG_H__
- #define __HW_CONFIG_H__
- void Set_System(void);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void BODCR_Configuration(void);
- void BODCR_CALLBACK(void);
- void delay_ms(uint32_t count);
- #endif
已测试 掉电复位好用
工程
|