STM32的GPIO中斷例子
前言:目前開發(fā)一個(gè)測距儀數(shù)據(jù)匯總顯示儀表,從17年2月開始學(xué)習(xí)硬件,本家是軟件,1個(gè)月畫原理圖,半個(gè)月畫PCB板,中間花了2個(gè)月時(shí)間用JAVA開發(fā)中級版藍(lán)牙APP,板子焊好后開始學(xué)習(xí)STM32各個(gè)功能,點(diǎn)亮LED燈,花了半個(gè)月顯示LCD彩屏,模擬SPI,USART通訊,按鍵,這次是中斷NVIC。
說明:我寫的不是為了教大家學(xué)會中斷,我是從http://www.stmcu.org/module/forum/thread-603652-1-1.html例子中學(xué)會的,大家可以去那里學(xué),以下只是我把我的DEMO貼出來,防忘,本人菜鳥,只會實(shí)現(xiàn)不會理解,有什么錯(cuò)誤也可以指出。實(shí)現(xiàn)兩個(gè)按鈕,按一個(gè)蜂鳴器一直響,按另一個(gè)關(guān)閉
正文:
---------------------------------------------RCC時(shí)鐘省略---------------------------------------------
---------------------------------------------GPIO配置---------------------------------------------
//兩個(gè)按鍵,用的PB4和PB8,50Mhz,下拉輸入,在.h文件里,不拿出來了
GPIO_InitTypeDef PortDef;
//KEY_OFF PB8
PortDef.GPIO_Pin=KEY_OFF_GPIO_PIN;
PortDef.GPIO_Speed=KEY_OFF_GPIO_SPEED;
PortDef.GPIO_Mode=KEY_OFF_GPIO_MODE;
GPIO_Init(KEY_OFF_GPIO_PORT,&PortDef);
//KEY_LEFT PB4
PortDef.GPIO_Pin=KEY_LEFT_GPIO_PIN;
PortDef.GPIO_Speed=KEY_LEFT_GPIO_SPEED;
PortDef.GPIO_Mode=KEY_LEFT_GPIO_MODE;
GPIO_Init(KEY_LEFT_GPIO_PORT,&PortDef);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);
KEY_LEFT_H;//先拉高按鍵
KEY_OFF_H;
---------------------------------------------NVIC配置---------------------------------------------
//中斷2,通道4和8,8是5到9共用,具體不懂
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; //td4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //td8
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //qz1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //xy1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
---------------------------------------------EXTI配置---------------------------------------------
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_ClearITPendingBit(EXTI_Line4);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line8 ;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
----------------------------------------EXTI4_IRQHandler配置----------------------------------------
//讓2個(gè)按鍵按了具體做什么
void EXTI4_IRQHandler(void)
{
TIM_Cmd(TIM2,ENABLE);//PWM蜂鳴器開
EXTI_ClearITPendingBit(EXTI_Line4);//清空中斷標(biāo)志位,防止持續(xù)進(jìn)入中斷
}
void EXTI9_5_IRQHandler(void)
{
TIM_Cmd(TIM2,DISABLE); //PWM蜂鳴器關(guān)
EXTI_ClearITPendingBit(EXTI_Line8);//清空中斷標(biāo)志位,防止持續(xù)進(jìn)入中斷
---------------------------------------main函數(shù)---------------------------------------------
以上所有的初始化
while(1);
結(jié)束語:
1.兩個(gè)按鍵方法EXTI4_IRQHandler,壓根沒調(diào)用,也會執(zhí)行,很神奇
2.別人都說要把按鍵方法放在stm32f10x_it.c里頭,發(fā)現(xiàn)不放也沒事,省的還要加很多的.h
3.這是GPIO的中斷,就是說GPIO和中斷配好之后,兩者關(guān)聯(lián)一下就ok了
4.據(jù)說還有別的中斷,沒用過
5.據(jù)說PA1只能用1通道,PB1也是用1通道
6.據(jù)說中斷1到4獨(dú)立,5到9共用一個(gè)什么函數(shù)
本人小白,剛接觸stm32,不喜歡看文檔,結(jié)束語只是一些自己沒用心證實(shí)的猜想