1
STM32ÖжϷÖ×é
STM32 µÄÿһ¸öGPIO¶¼ÄÜÅäÖóÉÒ»¸öÍⲿÖжϴ¥·¢Ô´£¬ÕâµãÒ²ÊÇ STM32 µÄÇ¿´óÖ®´¦¡£STM32 ͨ¹ý¸ù¾ÝÒý½ÅµÄÐòºÅ²»Í¬½«ÖÚ¶àÖжϴ¥·¢Ô´·Ö³É²»Í¬µÄ×飬±ÈÈ磺PA0£¬PB0£¬PC0£¬PD0£¬PE0£¬PF0£¬PG0ΪµÚÒ»×飬ÄÇôÒÀ´ËÀàÍÆ£¬ÎÒÃÇÄܵóöÒ»¹²ÓÐ16 ×飬STM32 ¹æ¶¨£¬Ã¿Ò»×éÖÐͬʱֻÄÜÓÐÒ»¸öÖжϴ¥·¢Ô´¹¤×÷£¬ÄÇô£¬×î¶à¹¤×÷µÄÒ²¾ÍÊÇ16¸öÍⲿÖжϡ£STM32F103 µÄÖжϿØÖÆÆ÷Ö§³Ö 19 ¸öÍⲿÖжÏ/ʼþÇëÇó¡£Ã¿¸öÖжÏÉèÓÐ״̬λ£¬Ã¿¸öÖжÏ/ʼþ¶¼ÓжÀÁ¢µÄ´¥·¢ºÍÆÁ±ÎÉèÖá£STM32F103 µÄ19 ¸öÍⲿÖжÏΪ£º
Ïß 0~15£º¶ÔÓ¦Íⲿ IO ¿ÚµÄÊäÈëÖжϡ£
Ïß 16£ºÁ¬½Óµ½ PVD Êä³ö¡£
Ïß 17£ºÁ¬½Óµ½ RTC ÄÖÖÓʼþ¡£
Ïß 18£ºÁ¬½Óµ½ USB »½ÐÑʼþ¡£
2
ÍⲿÖжϵÄÅäÖùý³Ì
1.ÅäÖô¥·¢Ô´GPIO¿Ú
ÒòΪGPIO¿Ú×÷Ϊ´¥·¢Ô´Ê¹Óã¬ËùÒÔ½«GPIO¿ÚÅäÖóÉÊäÈëģʽ£¬´¥·¢Ä£Ê½ÓÐÒÔϼ¸ÖÖ£º
a.GPIO_Mode_AIN £¬Ä£ÄâÊäÈ루ADCÄ£ÄâÊäÈ룬»òÕߵ͹¦ºÄÏÂÊ¡µç£©
b.GPIO_Mode_IN_FLOATING £¬¸¡¿ÕÊäÈë
c.GPIO_Mode_IPD £¬´øÏÂÀÊäÈë
d.GPIO_Mode_IPU £¬´øÉÏÀÊäÈë¡¡
GPIO_InitTypeDef GPIO_InitStructure;//¶¨Òå½á¹¹Ìå
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//ʹÄÜʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//Ñ¡ÔñIO¿Ú PE2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOE, &GPIO_InitStructure);//ʹÓýṹÌåÐÅÏ¢½øÐгõʼ»¯IO¿Ú
2.ʹÄÜAFIO¸´ÓÃʱÖÓ¹¦ÄÜ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);¡¡
3.½«GPIO¿ÚÓëÖжÏÏßÓ³ÉäÆðÀ´
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
4.ÖжÏÏßÉϽøÐÐÖжϳõʼ»¯
EXTI_InitTypeDef EXTI_InitStructure;//¶¨Òå³õʼ»¯½á¹¹Ìå
EXTI_InitStructure.EXTI_Line=EXTI_Line2; //ÖжÏÏߵıêºÅ È¡Öµ·¶Î§ÎªEXTI_Line0~EXTI_Line15
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//ÖжÏģʽ£¬¿ÉѡֵΪÖÐ¶Ï EXTI_Mode_Interrupt ºÍʼþ EXTI_Mode_Event¡£
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//´¥·¢·½Ê½£¬¿ÉÒÔÊÇϽµÑØ´¥·¢ EXTI_Trigger_Falling£¬ÉÏÉýÑØ´¥·¢ EXTI_Trigger_Rising£¬»òÕßÈÎÒâµçƽ£¨ÉÏÉýÑغÍϽµÑØ£©´¥·¢EXTI_Trigger_Rising_Falling
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);//¸ù¾Ý½á¹¹ÌåÐÅÏ¢½øÐгõʼ»¯
5.ÖжÏÓÅÏȼ¶ÅäÖÃ
NVIC_InitTypeDef NVIC_InitStructure;//¶¨Òå½á¹¹Ìå
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //ʹÄÜÍⲿÖжÏËùÔÚµÄͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //ÇÀÕ¼ÓÅÏȼ¶ 2£¬
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //×ÓÓÅÏȼ¶ 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜÍⲿÖжÏͨµÀ
NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý½á¹¹ÌåÐÅÏ¢½øÐÐÓÅÏȼ¶³õʼ»¯
6.ÍⲿÖжϷþÎñº¯ÊýµÄ±àд
ÍⲿÖжϺ¯Êý·Ö±ðΪ£º
EXPORT EXTI0_IRQHandler
EXPORT EXTI1_IRQHandler
EXPOR T EXTI2_IRQHandler
EXPORT EXTI3_IRQHandler
EXPORT EXTI4_IRQHandler
EXPORT EXTI9_5_IRQHandler
EXPORT EXTI15_10_IRQHandler
ÖжÏÏß 0-4 ÿ¸öÖжÏÏ߶ÔÓ¦Ò»¸öÖжϺ¯Êý£¬ÖжÏÏß 5-9 ¹²ÓÃÖжϺ¯Êý £¬EXTI9_5_IRQHandler£¬ÖжÏÏß 10-15 ¹²ÓÃÖжϺ¯Êý EXTI15_10_IRQHandler¡£¡¡
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2)!=RESET)//ÅжÏij¸öÏßÉϵÄÖжÏÊÇ·ñ·¢Éú
{
¡¡ÖжÏÂß¼¡
¡¡EXTI_ClearITPendingBit(EXTI_Line2); //Çå³ý LINE ÉϵÄÖжϱê־λ
}
}
ʹÓð´¼ü½øÐÐÍⲿÖжϵÄʱºò£¬Ò»°ã¶¼ÐèÒª½øÐа´¼üÑÓʱÏû¶¶ÒÔ¼°ËÉÊÖ¼ì²âµÄÏà¹Ø´¦Àí£¬ÖжϺ¯Êý¿ÉÒԲο´ÒÔÏ´úÂ룺
void EXTI2_IRQHandler(void)
{
¡¡delay_ms(10);//ÑÓʱÏû¶¶
¡¡if(KEY2==0) //°´¼üÕæµÄ±»°´ÏÂ
¡¡¡¡{
¡¡¡¡¡¡LED0=!LED0;
¡¡¡¡}
¡¡while(KEY2!=0);//µÈ´ýËÉÊÖ
¡¡EXTI_ClearITPendingBit(EXTI_Line2); //Çå³þÖжϱê־λ
}
µ±È»£¬Èç¹ûÄãµÄ°´¼üÊÇÔÊÐí³¤°´¹¦Äܵģ¬ÄÇô¾Í½øÐбðµÄÂß¼²Ù×÷£¬ÕâÀï²»×÷Ñо¿¡£