您的位置 首页 设计

STM32 外部中止的运用

例程名称:外部中断实验作者:tkzsld(sky)实验硬件:火牛开发板硬件连接:指示灯连接PD9按钮连接PA8功能描述:本例程实现外部中断

例程称号:外部中止实验

者: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_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);
//等候外部晶振安稳
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);

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/sheji/258976.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部