主函数代码:
#include "stm32f10x.h"
#define LED1_ON GPIO_ResetBits(GPIOE, GPIO_Pin_2)
#define LED1_OFF GPIO_SetBits(GPIOE, GPIO_Pin_2)
#define LED2_ON GPIO_ResetBits(GPIOE, GPIO_Pin_3)
#define LED2_OFF GPIO_SetBits(GPIOE, GPIO_Pin_3)
#define LED3_ON GPIO_ResetBits(GPIOE, GPIO_Pin_4)
#define LED3_OFF GPIO_SetBits(GPIOE, GPIO_Pin_4)
#define LED4_ON GPIO_ResetBits(GPIOE, GPIO_Pin_5)
#define LED4_OFF GPIO_SetBits(GPIOE, GPIO_Pin_5)
u16 Count=0;
RCC_ClocksTypeDef RCC_ClockFreq;
void Delay(u16 speed)
{
u16 i;
while(speed!=0)
{
speed--;
for(i=0;i<400;i++);
}
}
int main(void)
{ u16 speed=2000;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO, ENABLE);
// PE2,3,4,5输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOE, &GPIO_InitStructure);
//PC13上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// 连接IO口到中断线
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource13);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
while (1)
{ switch(Count)
{
case 0:
{
LED1_ON;
Delay(speed);
LED1_OFF;
LED2_ON;
Delay(speed);
LED2_OFF;
LED3_ON;
Delay(speed);
LED3_OFF;
LED4_ON;
Delay(speed);
LED4_OFF;
Delay(speed);
break;
}
case 1:
{ LED1_ON;
LED2_OFF;
LED3_OFF;
LED4_OFF;
break;
}
case 2:
{ LED1_ON;
LED2_ON;
LED3_OFF;
LED4_OFF;
break;
}
case 3:
{ LED1_ON;
LED2_ON;
LED3_ON;
LED4_OFF;
break;
}
case 4:
{ LED1_ON;
LED2_ON;
LED3_ON;
LED4_ON;
break;
}
case 5:
{ LED1_OFF;
LED2_ON;
LED3_ON;
LED4_ON;
break;
}
case 6:
{ LED1_OFF;
LED2_OFF;
LED3_ON;
LED4_ON;
break;
}
case 7:
{ LED1_OFF;
LED2_OFF;
LED3_OFF;
LED4_ON;
break;
}
}
}
}
EXTI函数代码
/*******************************************************************************
* Function Name : EXTI15_10_IRQHandler
* Description : This function handles External lines 15 to 10 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line13) != RESET)
{ EXTI_ClearITPendingBit(EXTI_Line13);
Count++;
if(Count==8)Count=0;
Delay(100);
}
}