例程名称:外部中断实验
作 者:tkzsld(sky)
实验硬件:火牛开发板
硬件连接:指示灯连接 PD9
按钮连接 PA8
功能描述:本例程实现外部中断的输入试验,
按键次数 1:PD8 PD9 ON 2:PD8 PD9 OFF
3:PD10 PD11 ON 4:PD10 PD11 OFF
5:PD8-11 ON 6:PD8-11 OFF
7: PB5 BEEP ON 8 :BEEP OFF
********************************************************************/
#include // STM32F10x Library Definitions
#include "STM32_Init.h" // STM32 Initialization
u8 flag=0;
void beep(void);
void ledblink(void);
void Delay(vu16 cnt) {
uint16 i,j;
for (i=0;i
{ for (j=0;j<1000;j++)
{ }
}
}
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);
}
//给GPIOA和重映射提供时钟,注意:一定要设置RCC_APB2Periph_AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_Line =EXTI_Line8;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //抢占式优先级别设置为无抢占优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; //指定中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道8
NVIC_Init(&NVIC_InitStructure);
// EXTI_GenerateSWInterrupt(EXTI_Line8);
}
int main (void)
{
RCC_Configuration(); //配置时钟
GPIO_Configuration(); //配置IO口
NVIC_Configuration(); //中断配置
EXTI_Configuration(); //外部中断配置
while(1)
{
switch(flag)
{
case 0: GPIO_WriteBit(GPIOD, GPIO_Pin_8|GPIO_Pin_9,(BitAction)1);break;
//Delay(1000);
case 1: GPIO_WriteBit(GPIOD, GPIO_Pin_9|GPIO_Pin_8,(BitAction)0);break;
//Delay(1000);
case 2: GPIO_WriteBit(GPIOD, GPIO_Pin_10|GPIO_Pin_11,(BitAction)1); break;
case 3: GPIO_WriteBit(GPIOD, GPIO_Pin_10|GPIO_Pin_11,(BitAction)0); break;
case 4: GPIO_WriteBit(GPIOD, GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,(BitAction)1); break;
case 5: GPIO_WriteBit(GPIOD, GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,(BitAction)0); break;
case 6: beep();break;
case 7: ledblink();break;
case 8: GPIO_WriteBit(GPIOB, GPIO_Pin_5,(BitAction)0);break;
default: flag=0;break;
}
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
Delay(100);
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
//添加中断处理程序
flag++;
EXTI_ClearFlag(EXTI_Line8); //清除中断标志(必须)
// EXTI_ClearITPendingBit(EXTI_Line8);
}
}
}
void ledblink()
{
u16 k;
for(k=0;k<100;k=k+5)
{
GPIO_WriteBit(GPIOD, GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,(BitAction)1);
Delay(1000);
GPIO_WriteBit(GPIOD, GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11,(BitAction)0);
Delay(1000);
}
}
void beep(void)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_5,(BitAction)1);
Delay(1000);
GPIO_WriteBit(GPIOB, GPIO_Pin_5,(BitAction)0);
}