例程名稱:外部中斷實(shí)驗(yàn)作 者:tkzsld(sky)
實(shí)驗(yàn)硬件:火牛開發(fā)板
硬件連接:指示燈連接 PD9
按鈕連接 PA8
功能描述:本例程實(shí)現(xiàn)外部中斷的輸入試驗(yàn),
按鍵次數(shù) 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
********************************************************************/
本文引用地址:http://m.butianyuan.cn/article/201611/321208.htm#include <stm32f10x_lib.h> // 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);
//等待外部晶振穩(wěn)定
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//如果外部晶振啟動成功,則進(jìn)行下一步操作
if(HSEStartUpStatus==SUCCESS)
{
//設(shè)置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穩(wěn)定
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//系統(tǒng)時鐘SYSCLK來自PLL輸出
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//切換時鐘后等待系統(tǒng)時鐘穩(wěn)定
while(RCC_GetSYSCLKSource()!=0x08);
}
//給GPIOA和重映射提供時鐘,注意:一定要設(shè)置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); //搶占式優(yōu)先級別設(shè)置為無搶占優(yōu)先級
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; //指定中斷源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //指定響應(yīng)優(yōu)先級別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); //清除中斷標(biāo)志(必須)
// 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);
}
評論