文档库 最新最全的文档下载
当前位置:文档库 › STM32外部中断

STM32外部中断

//main.c 作者:周明龙 2010年10月18日
//上电点亮LED3.4,保持,当按下UP键,则进入中断函数EXTI9_5_IRQHandler();然后点亮,熄灭LED1.2各四次
//之后,退出中断函数.
// PC6~PC9 对应LED1~LED4
//exti_8IRQn 对应外部输入EXTI8


#include "stm32f10x.h"
//#include "Clock.h"//时钟函数是正确的,因为主函数都能正常执行,中断服务函数也能正确执行,就是执行的时间不对
ErrorStatus HSEStartUpStatus;

void RCC_Configuration(void); //时钟配置
void NVIC_Configuration(void);//中断向量表和优先级设定
void GPIO_Configuartion(void);//端口模式配置
void Exti_Configuartion(void);//外部中断模式配置
void Delay(vu32 nCount);//延时函数

int main(void)
{
RCC_Configuration(); //配置时钟
NVIC_Configuration();
GPIO_Configuartion();
Exti_Configuartion();

GPIO_SetBits(GPIOC, GPIO_Pin_8 );//熄灭led
GPIO_SetBits(GPIOC, GPIO_Pin_9);//熄灭led

// Delay(0xffffff);//Dealy_Systick(1000);
// GPIO_ResetBits(GPIOC, GPIO_Pin_9 );//点亮led
// GPIO_SetBits(GPIOC, GPIO_Pin_8);//熄灭led
while (1)
{
// Delay(0xffffff);
// GPIO_ResetBits(GPIOC, GPIO_Pin_8 );//点亮led
// GPIO_SetBits(GPIOC, GPIO_Pin_9);//熄灭led
//
// Delay(0xffffff);//Dealy_Systick(1000);
// GPIO_ResetBits(GPIOC, GPIO_Pin_9 );//点亮led
// GPIO_SetBits(GPIOC, GPIO_Pin_8);//熄灭led
}
}

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//中断向量存储在FlASh区
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//选择中断优先级分组1
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;//外部中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();

/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //x

if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);

/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */
RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}

/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock

source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}

}

void GPIO_Configuartion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED控制端口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开用到的gpiod时钟,驱动LED1~LED4

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_6|GPIO_Pin_7;//选择要配置的端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//配置为推免输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//转换速度
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化gpio
GPIO_ResetBits(GPIOC, GPIO_Pin_8 | GPIO_Pin_9|GPIO_Pin_6 | GPIO_Pin_7); //先复位
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开用到的gpiod时钟,驱动LED1~LED4

//中断控制端口 PD8 最为外部中断输入
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);//打开用到的gpiob时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//选择要配置的端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//配置为浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//转换速度
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化gpio
}


void Exti_Configuartion(void)
{
EXTI_InitTypeDef EXTI_InitStructure; //PD8 为外部输入 EXTI8
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource8);
EXTI_InitStructure.EXTI_Line=EXTI_Line8;//将PB6配置为EXTI Line6
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
//EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_Line8); //软件产生一个中断
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~stm3210X_it.c~~~~~~~~~~~~~~~~~~~~~~~

extern void Delay(vu32 nCount);//延时函数声明,这没问题

void EXTI9_5_IRQHandler(void)
{
int i;

// EXTI_ClearITpendingBit(EXTI_Line8);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);//点亮led
GPIO_ResetBits(GPIOC, GPIO_Pin_9);//点亮led
if(EXTI_GetITStatus(EXTI_Line8)!=RESET)
{
for(i=0;i<4;i++)
{
GPIO_SetBits(GPIOC, GPIO_Pin_6 );//点亮led
GPIO_SetBits(GPIOC, GPIO_Pin_7);//点亮led
Delay(0xffffff);
Delay(0xffffff);
GPIO_ResetBits(GPIOC, GPIO_Pin_6 );//点亮led
GPIO_ResetBits(GPIOC, GPIO_Pin_7);//点亮led
Delay(0xffffff);
Delay(0xFffffF);

// Delay(0xffffff);//Dealy_Systick(1000);
// GPIO_Re

setBits(GPIOC, GPIO_Pin_6 );//点亮led
// GPIO_SetBits(GPIOC, GPIO_Pin_7);//熄灭led
// EXTI_ClearITPendingBit(EXTI_Line8);
}
}
EXTI_ClearFlag(EXTI_Line8);

}

相关文档