*******************************/
#include<stm32f10x.h>
#include"led.h"
#include"delay.h"
char flag;
/**********************
PG8 :user
**********************/
void buttoninit()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
//GPIO_InitStruct.GPIO_Speed= GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(GPIOG,&GPIO_InitStruct);
}
void button_exit_init()
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOG,GPIO_PinSource8);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn ; //外部中断线9-5
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main()
{
buttoninit();
ledinit();
NVIC_Configuration();
button_exit_init();
while(1)
{
if(flag)
{
GPIO_WriteBit( GPIOF, GPIO_Pin_6, Bit_SET);
delay_ms(100);
GPIO_WriteBit( GPIOF, GPIO_Pin_6, Bit_RESET);
GPIO_WriteBit( GPIOF, GPIO_Pin_7, Bit_SET);
delay_ms(100);
GPIO_WriteBit( GPIOF, GPIO_Pin_7, Bit_RESET);
}
else
{
GPIO_WriteBit( GPIOF, GPIO_Pin_8, Bit_SET);
delay_ms(100);
GPIO_WriteBit( GPIOF, GPIO_Pin_8, Bit_RESET);
GPIO_WriteBit( GPIOF, GPIO_Pin_9, Bit_SET);
delay_ms(100);
GPIO_WriteBit( GPIOF, GPIO_Pin_9, Bit_RESET);
}
}
}
中断函数
void EXTI9_5_IRQn_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line8)!=RESET)
{
flag=!flag;
}
EXTI_ClearFlag(EXTI_Line8);
}
void ledinit()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_Init(GPIOF,&GPIO_InitStruct);
GPIO_Write(GPIOF,0x0000);
}
void delay_ms(u16 time)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
//SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick_Config(72000);
nTime =time;
while(nTime);
}
void SysTick_Handler(void)
{
nTime--;
}
程序进中断就卡死了,很奇怪,求解
led都正常,一按按键程序似乎停住了。
|