新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 如何使用STM32的PVD對電源的電壓進(jìn)行監(jiān)控

        如何使用STM32的PVD對電源的電壓進(jìn)行監(jiān)控

        作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
        用戶在使用STM32時,可以利用其內(nèi)部的PVD對VDD的電壓進(jìn)行監(jiān)控,通過電源控制寄存器(PWR_CR)中的PLS[2:0]位來設(shè)定監(jiān)控的電壓值。
        PLS[2:0]位用于選擇PVD監(jiān)控電源的電壓閥值:
        000:2.2V
        001:2.3V
        010:2.4V
        011:2.5V
        100:2.6V
        101:2.7V
        110:2.8V
        111:2.9V
        在電源控制/狀態(tài)寄存器(PWR_CSR)中的PVDO標(biāo)志用來表明VDD是高于還是低于PVD設(shè)定的電壓閥值。該事件連接到外部中斷的第16線,如果該中斷在外部中斷寄存器中被使能的,該事件就會產(chǎn)生中斷。當(dāng)VDD下降到PVD閥值以下和(或)當(dāng)VDD上升到PVD閥值之上時,根據(jù)外部中斷第16 線的上升/下降邊沿觸發(fā)設(shè)置,就會產(chǎn)生PVD中斷。這一特性可用于發(fā)現(xiàn)電壓出現(xiàn)異常時,執(zhí)行緊急關(guān)閉任務(wù)。
        下面是用于測試PVD的代碼:
        主程序的代碼:
        /* Includes ------------------------------------------------------------------*/
        #include "stm32f10x_lib.h"
        /* Private typedef -----------------------------------------------------------*/
        typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
        /* Private define ------------------------------------------------------------*/
        /* Private macro -------------------------------------------------------------*/
        /* Private variables ---------------------------------------------------------*/
        ErrorStatus HSEStartUpStatus;
        /* Private function prototypes -----------------------------------------------*/
        void RCC_Configuration(void);
        void GPIO_Configuration(void);
        void EXTI_Configuration(void);
        void NVIC_Configuration(void);
        /* Private functions ---------------------------------------------------------*/
        STM32 中文應(yīng)用文檔
        /*****************************************************************************
        * Function Name : main
        * Description : Main program
        * Input : None
        * Output : None
        * Return : None
        ******************************************************************************/
        int main(void)
        {
        RCC_Configuration(); /* System Clocks Configuration */
        GPIO_Configuration(); /* Configure the GPIO ports */
        NVIC_Configuration(); /* NVIC configuration */
        EXTI_Configuration();
        PWR_PVDLevelConfig(PWR_PVDLevel_2V8);
        PWR_PVDCmd(ENABLE);
        while(1) {}
        }
        /*******************************************************************
        * Function Name : RCC_Configuration
        * Description : Configures the different system clocks.
        * Input : None
        * Output : None
        * Return : None
        ********************************************************************/
        void RCC_Configuration(void)
        {
        RCC_DeInit(); // RCC system reset(for debug purpose)
        RCC_HSEConfig(RCC_HSE_ON); // Enable HSE
        HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready
        if(HSEStartUpStatus == SUCCESS) {
        RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
        RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK
        RCC_PCLK1Config(RCC_HCLK_Div1); // PCLK1 = HCLK/2
        FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable Prefetch Buffer
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz
        RCC_PLLCmd(ENABLE); // Enable PLL
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} // Wait till PLL is ready
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock source
        while(RCC_GetSYSCLKSource() != 0x08) {} // Wait till PLL is used as system clock source
        }
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
        }
        STM32 中文應(yīng)用文檔
        /****************************************************************
        * Function Name : GPIO_Configuration
        * Description : Configures the different GPIO ports.
        * Input : None
        * Output : None
        * Return : None
        ****************************************************************/
        void GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIO B
        }
        /*******************************************************************
        * Function Name : EXTI_Configuration
        * Description : Configures .
        * Input : None
        * Output : None
        * Return : None
        ********************************************************************/
        void EXTI_Configuration(void)
        {
        EXTI_InitTypeDef EXTI_InitStructure;
        EXTI_DeInit();
        EXTI_StructInit(&EXTI_InitStructure);
        EXTI_InitStructure.EXTI_Line = EXTI_Line16;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitStructure); // Configure EXTI Line16 to generate an interrupt
        }
        /***************************************************************************
        * Function Name : NVIC_Configuration
        * Description : Configures Vector Table base location.
        * Input : None
        * Output : None
        * Return : None
        **************************************************************************/
        void NVIC_Configuration(void)
        {
        NVIC_InitTypeDef NVIC_InitStructure;
        #ifdef VECT_TAB_RAM
        /* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else /* VECT_TAB_FLASH */
        /* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif
        STM32 中文應(yīng)用文檔
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // Configure one bit for preemption priority
        NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQChannel;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure); // Enable the PVD Interrupt
        }
        中斷程序:
        /**************************************************************************
        * Function Name : PVD_IRQHandler
        * Description : This function handles PVD interrupt request.
        * Input : None
        * Output : None
        * Return : None
        ***************************************************************************/
        void PVD_IRQHandler(void)
        {
        if (PWR_GetFlagStatus(PWR_FLAG_PVDO))
        GPIO_WriteBit(GPIOB, 1 << 5, Bit_SET);
        else
        GPIO_WriteBit(GPIOB, 1 << 5, Bit_RESET);
        }
        注:在void EXTI_Configuration(void)中,對于EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; 中的初始化值,根據(jù)你的需要進(jìn)行修改,具體細(xì)節(jié)如下:
        EXTI_Trigger_Rising --- 表示電壓從高電壓下降到低于設(shè)定的電壓閥值產(chǎn)生中斷;
        EXTI_Trigger_Falling --- 表示電壓從低電壓上升到高于設(shè)定的電壓閥值產(chǎn)生中斷;
        EXTI_Trigger_Rising_Falling --- 表示電壓從高電壓下降到低于設(shè)定的電壓閥值、或從低電壓上升到高于設(shè)定的電壓閥值產(chǎn)生中斷。


        關(guān)鍵詞: STM32PVD監(jiān)

        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 浏阳市| 武定县| 开远市| 景东| 武强县| 山东| 云浮市| 沙湾县| 鹤山市| 广河县| 新巴尔虎左旗| 阜新| 天气| 射阳县| 万安县| 泗洪县| 景洪市| 石棉县| 遂平县| 平果县| 仙桃市| 宿松县| 苏尼特左旗| 新民市| 乌审旗| 清镇市| 响水县| 渭南市| 永修县| 安康市| 吴川市| 乌兰察布市| 彭阳县| 万安县| 当雄县| 偏关县| 舒兰市| 湖南省| 威远县| 镇巴县| 乌苏市|