while (1) { } }
void BusFault_Handler(void)
{/* Go to infinite loop when Bus Fault exception occurs */ while (1) { } }
void UsageFault_Handler(void)
{ /* Go to infinite loop when Usage Fault exception occurs */ while (1) {} }
void SVC_Handler(void){}
void DebugMon_Handler(void){} void PendSV_Handler(void){} void SysTick_Handler(void) {
TimingDelay_Decrement(); }
void DMA1_Channel1_IRQHandler (void){} void RTC_WKUP_IRQHandler(void){} void EXTI0_IRQHandler(void) { int i;
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
EXTI_GenerateSWInterrupt(EXTI_Line1); EXTI_ClearFlag(EXTI_Line0); }
void EXTI1_IRQHandler(void) {
int i;
GPIO_TOGGLE(LD_GPIO_PORT ,LD_BLUE_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_BLUE_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_BLUE_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_BLUE_GPIO_PIN ); for(i=0;i<500000;i++){}
EXTI_GenerateSWInterrupt(EXTI_Line2); EXTI_ClearFlag(EXTI_Line1); }
void EXTI2_IRQHandler(void) {
int i;
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){}
GPIO_TOGGLE(LD_GPIO_PORT ,LD_GREEN_GPIO_PIN ); for(i=0;i<500000;i++){} EXTI_ClearFlag(EXTI_Line2); }
/*void PPP_IRQHandler(void) { }*/ * @}
(配置的两个定时器通道的中断请求的时间间隔分别为1秒和2秒)红红每隔1秒闪烁1次,绿灯每隔2秒闪烁1次。 #include \ #include \
/* 自定义同义关键字 ---------------*//* 自定义参数宏 --------------------------*/
/* 自定义函数宏 ------------------------*//* 自定义变量 ------------------*/
uint16_t CCR1_Val =2500; /*初始化输出比较通道1计数周期变量*/ //uint16_t CCR1_Val =5000;
uint16_t CCR2_Val =1250; /**/ //uint16_t CCR2_Val =2500; /**/ //uint16_t CCR3_Val =10000; /**/ //uint16_t CCR4_Val =5000; /**/
/* 自定义函数声明 -------------------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void TIM_Configuration(void);
* 函数名 : main* 函数描述 : 主函数* 输入参数 : 无 int main(void)
{ /* 设置系统时钟 */ RCC_Configuration(); /* 设置NVIC */
NVIC_Configuration(); /* 设置GPIO端口 */
GPIO_Configuration(); /* 设置 TIM */
TIM_Configuration(); while (1); }
* 函数名 : RCC_Configuration* 函数描述 : 设置系统各部分时钟* 输入参数 : 无 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_1); /* 使能FLASH预取缓存 */
FLASH_PrefetchBufferCmd(ENABLE);
/* 选择锁相环(PLL)时钟源为HSE 2分频,倍频数为8,则PLL输出频率为 8MHz * 4 = 32MHz */
RCC_PLLConfig(RCC_PLLSource_HSE, RCC_PLLMul_8,RCC_PLLDiv_2); /* 使能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_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE); }
* 函数名 : GPIO_Configuration* 函数描述 : 设置各GPIO端口功能
void GPIO_Configuration(void)
{ /* 定义GPIO初始化结构体 GPIO_InitStructure */ GPIO_InitTypeDef GPIO_InitStructure;
/* 设置PA.0,PA.1,PA.2为浮空输入(EXTI Line0)*/ GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; GPIO_Init(GPIOB , &GPIO_InitStructure); }
* 函数名 : NVIC_Configuration* 函数描述 : 设置NVIC参数 void TIM_Configuration(void)
{ /*定襎IM_TimeBase初始化结构体?/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /*定义TIM_OCInit初始化结构体*/
TIM_OCInitTypeDef TIM_OCInitStructure;
/*技术重载值为65535,预分频值为16000,时钟分割0,向上计数模式*/ TIM_TimeBaseStructure.TIM_Period=65535; TIM_TimeBaseStructure.TIM_Prescaler= 0; TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); /*设置预分频值,立即装入*/
TIM_PrescalerConfig( TIM2,3199,TIM_PSCReloadMode_Immediate);
/*设置OC1,OC2通道,工作模式为计数器模式,使能比较匹配输出极性,时钟分割0,向上计数模式*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse=CCR1_Val; TIM_OC1Init(TIM2,&TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse=CCR2_Val; TIM_OC2Init(TIM2,&TIM_OCInitStructure); /*TIM_OCInitStructure.TIM_Pulse=CCR3_Val; TIM_OC3Init(TIM2,&TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse=CCR4_Val; TIM_OC4Init(TIM2,&TIM_OCInitStructure);*/ /*禁止预装载寄存器*/
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Disable); TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable); // TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Disable); // TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Disable); /*使能TIM中断*/
TIM_ITConfig(TIM2,TIM_IT_CC1 | TIM_IT_CC2,ENABLE ); /*启动TIM计数*/
TIM_Cmd(TIM2,ENABLE); }
* 函数名 : NVIC_Configuration* 函数描述 : 设置NVIC参数 void NVIC_Configuration(void) {
/* 定义NVIC初始化结构体 NVIC_InitStructure */ NVIC_InitTypeDef NVIC_InitStructure;
/* #ifdef...#else...#endif结构的作用是根据预编译条件决定中断向量表起始地址*/ #ifdef VECT_TAB_RAM
/* 中断向量表起始地址从 0x20000000 开始 */ NVIC_SetVectorTable(NVIC_VectTab_RAM , 0x0); #else /* VECT_TAB_FLASH */
/* 中断向量表起始地址从 0x80000000 开始 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH , 0x0); #endif
/* 选择NVIC优先级分组2 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* 使能EXIT 0通道 ,2级先占优先级 ,0级次占优先级 */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
#include \ #include \
extern uint16_t CCR1_Val; /*声明输出比较通道1计数周期变量*/ extern uint16_t CCR2_Val; extern uint16_t CCR3_Val; extern uint16_t CCR4_Val; void NMI_Handler(void) {}
void HardFault_Handler(void)
{ /* Go to infinite loop when Hard Fault exception occurs */ while (1) { } }
void MemManage_Handler(void)
{ /* Go to infinite loop when Memory Manage exception occurs */ while (1) { } }
void BusFault_Handler(void)