void EXTIX_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG時(shí)鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //使能GPIOE時(shí)鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通輸入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4,5
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);//PE2 連接到中斷線2
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);//PE3 連接到中斷線3
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);//PE4 連接到中斷線4
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource5);//PE5 連接到中斷線5
/* 配置EXTI_Line2,3,4 */
EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line4 | EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中斷事件
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿觸發(fā)
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中斷線使能
EXTI_Init(&EXTI_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;//外部中斷2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//搶占優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//外部中斷3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;//搶占優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中斷4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//搶占優(yōu)先級(jí)1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中斷4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//搶占優(yōu)先級(jí)1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置系統(tǒng)中斷優(yōu)先級(jí)分組2
delay_init(100); //初始化延時(shí)函數(shù)
LED_Init(); //初始化LED
CS_Init();
SPI1_Init();
SPI2_Init();
EXTIX_Init();
while(1)
{
CS2=1;
if(Flage&0x02)
{
Flage=Flage&0xfd;
CS3=1;
}
if(Flage&0x04)
{
Flage=Flage&0xfb;
SPI1_WriteByte(0xaa);
SPI_Data[0]=SPI2_ReadByte();
SPI1_WriteByte(0xc3);
SPI_Data[1]=SPI2_ReadByte();
SPI1_WriteByte(0x55);
SPI_Data[2]=SPI2_ReadByte();
if((SPI_Data[0]==0xaa)&&(SPI_Data[1]==0xc3)&&(SPI_Data[2]==0x55))
{
BEEP=~BEEP;
LED0=~LED0;
LED1=~LED1;
delay_ms(500);
}
memset(SPI_Data,0,3);
}
}
}
評(píng)論
查看更多