#include "stm32f10x.h" #include "stm32f10x_exti.h" #include "misc.h"
void Delay(__IO uint32_t nCount) { for(; nCount != 0; nCount--); }
void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus;
//使能外部晶振 RCC_HSEConfig(RCC_HSE_ON); //等待外部晶振稳定 HSEStartUpStatus = RCC_WaitForHSEStartUp(); //如果外部晶振启动成功,则进行下一步操作 if(HSEStartUpStatus==SUCCESS) { //设置HCLK(AHB时钟)=SYSCLK RCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) = HCLK/2 RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) = HCLK RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4); //启动PLL RCC_PLLCmd(ENABLE); //等待PLL稳定 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //系统时钟SYSCLK来自PLL输出 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //切换时钟后等待系统时钟稳定 while(RCC_GetSYSCLKSource()!=0x08); }
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |RCC_APB2Periph_AFIO , ENABLE); //给GPIOA和重映射提供时钟,注意:一定要设置RCC_APB2Periph_AFIO
}
void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;
/************GPIO输入设置*****************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ; GPIO_Init(GPIOE, &GPIO_InitStructure); /************GPIO输出设置*****************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; GPIO_Init(GPIOB, &GPIO_InitStructure); }
/***中断引脚设置***/ void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2); //将PE2连接到外部中断通道3 EXTI_ClearITPendingBit(EXTI_Line2);
EXTI_InitStructure.EXTI_Line = EXTI_Line2; //外部中断通道3 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能 EXTI_Init(&EXTI_InitStructure); }
/****嵌套中断优先级设置******/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//先占优先级 1 位从优先级 3位 /* Enable the EXTI2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //EXTI2_IRQn换成EXTI2_IRQChannel就编译失败什么原因啊? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
int main(void) { RCC_Configuration(); GPIO_Config(); NVIC_Configuration(); EXTI_Configuration(); while (1) { /*====LED89-ON=======*/ GPIO_ResetBits(GPIOB , GPIO_Pin_8); GPIO_ResetBits(GPIOB , GPIO_Pin_9); Delay(0xfffff); Delay(0xfffff); Delay(0x5ffff); /*====LED89-ON=======*/ GPIO_SetBits(GPIOB , GPIO_Pin_8); GPIO_SetBits(GPIOB , GPIO_Pin_9); Delay(0xfffff); Delay(0xfffff); Delay(0x5ffff);
} }
void EXTI2_IRQHandler(void) //中断程序为什么这么写啊? { if ( EXTI_GetITStatus(EXTI_Line2) != SET ) { GPIO_ResetBits(GPIOB , GPIO_Pin_9); GPIO_SetBits(GPIOB , GPIO_Pin_8); EXTI_ClearITPendingBit(EXTI_Line2); EXTI_ClearFlag(EXTI_Line2); //清除中断标志 } }
程序有问题不啊?我编译没有问题,可是中断按键就是不行!大虾们看下呢?HELP !!
板上资源:LED1接GPIOB8,LED2接GPIOB9,KEY接GPIOE2谢谢啦!
[ 本帖最后由 anghost 于 2010-9-29 15:57 编辑 ]
|