NVIC例程程序 /* Includes------------------------------------------------------------------*/ #include "stm32f10x_lib.h" NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; bool PreemptionOccured = FALSE; u8 PreemptionPriorityValue = 0; ErrorStatus HSEStartUpStatus; /* Private function prototypes-----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void EXTI_Configuration(void); void Delay(vu32 nCount); /* Private functions---------------------------------------------------------*/ int main(void) { #ifdef DEBUG debug(); #endif /*Configure the system clocks */ RCC_Configuration(); /*Configure GPIOs */ GPIO_Configuration(); /*Configures the EXTI Lines */ EXTI_Configuration(); #ifdef VECT_TAB_RAM /* Set the Vector Table base location at0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /*Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //先占优先级分组为1 /*Enable the EXTI0 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;// 外部中断线0中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =PreemptionPriorityValue; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; NVIC_Init(&NVIC_InitStructure); /*Configure the SysTick Handler Priority: Preemption priority and subpriority */ NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick,!PreemptionPriorityValue, 0); while (1) { if(PreemptionOccured != FALSE) { GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 -GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6))); Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_7))); Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_8, (BitAction)(1 -GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_8))); Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_9, (BitAction)(1 -GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9))); Delay(0x5FFFF); } } //GPIO_WriteBit(GPIO_TypeDef* GPIOx, u16GPIO_Pin, BitAction BitVal)设置或者清除指定的数据端口位 } void RCC_Configuration(void) { /*RCC system reset(for debug purpose) */ RCC_DeInit(); /*Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /*Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); //FLASH_Latency用来设置FLASH存储器延时时钟周期数2 /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能或者失能预取指缓存 /* PLLCLK = 8MHz * 9 = 72 MHz */ //PLL的输入时钟 = HSE时钟频率, RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }//当设置完成时,RCC_FLAG_PLLRDY返回SET(1) /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /*Enable GPIOA, GPIOB, GPIOC and AFIO Clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |RCC_APB2Periph_AFIO, ENABLE); } void GPIO_Configuration(void) { /*Configure PC6, PC7, PC8 and PC9 as output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); /*Configure GPIOA Pin0 as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /*Configure GPIOB Pin9 as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); } void EXTI_Configuration(void) { /*Connect EXTI Line0 to PA.00 */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); /*Configure EXTI Line0 to generate an interrupt on falling edge */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /*Connect EXTI Line9 to PB.09 */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9); /*Configure EXTI Line9 to generate an interrupt on falling edge */ EXTI_InitStructure.EXTI_Line = EXTI_Line9; EXTI_Init(&EXTI_InitStructure); } void Delay(vu32 nCount) { for(; nCount != 0; nCount--); } #ifdef DEBUG void assert_failed(u8* file, u32 line) { /*User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n",file, line) */ /*Infinite loop */ while (1) { } } #endif
|