查看: 3248|回复: 1

[项目] 按键的外部中断

[复制链接]
  • TA的每日心情
    奋斗
    2023-12-3 18:51
  • 签到天数: 772 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-7-8 05:57:06 | 显示全部楼层 |阅读模式
    分享到:
    固件库有按键中断的例程序,我打开看了下,怎么会是PF7,我看了下小红板的管脚分配如下:
    1507082.JPG
    是每PC12,于是我就把工程里自带的模板考到子目录里:

    然后设置如下:在C/C++把编译路径换到我现在的路径下边:
    1507083.JPG

    然后再把包括的文件统统移除,并按现在的路径加载一下,这样就相当于自己的模板了。

    再然后查一下总线分配看看PC12分配在哪个总线上,看了下周PF7在一个总线,那就改吧!
    1507081.JPG

    改完的程序如下:
    中断程序:
    1. void EXTI4_15_IRQHandler(void)
    2. {
    3.     if(EXTI_GetIntBitState(EXTI_LINE12) != RESET)
    4.     {
    5.         GPIO_WriteBit(GPIOC, GPIO_PIN_10,
    6.                      (BitState)((1-GPIO_ReadOutputBit(GPIOC, GPIO_PIN_10))));
    7.         
    8.                 EXTI_ClearIntBitState(EXTI_LINE12);
    9.     }
    10. }
    复制代码
    主程序:
    1. #include "gd32f1x0.h"

    2. /* Private variables ---------------------------------------------------------*/
    3. GPIO_InitPara GPIO_InitStructure;
    4. EXTI_InitPara EXTI_InitStructure;
    5. NVIC_InitPara NVIC_InitStructure;

    6. /* Private function prototypes -----------------------------------------------*/
    7. void NVIC_Config(void);
    8. void EXTI_Config(void);

    9. /* Private functions ---------------------------------------------------------*/
    10. /**
    11.   * @brief  Main program.
    12.   * @param  None
    13.   * @retval None
    14.   */
    15. int main(void)
    16. {
    17.     RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOC, ENABLE);
    18.    
    19.     /* Configure the LED1 GPIO */
    20.     GPIO_InitStructure.GPIO_Pin = GPIO_PIN_10;
    21.     GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;
    22.     GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;
    23.     GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
    24.     GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
    25.     GPIO_Init( GPIOC, &GPIO_InitStructure );
    26.     /* Turn on LED1 */
    27.     GPIO_SetBits(GPIOC, GPIO_PIN_10);
    28.    
    29.     /* Configure the EXTI line12 */
    30.     EXTI_DeInit(&EXTI_InitStructure);
    31.     EXTI_Config();
    32.    
    33.     while(1)
    34.     {
    35.     }                                    
    36. }

    37. /**
    38.   * @brief  Configure NVIC and Vector Table base location.
    39.   * @param  None
    40.   * @retval None
    41.   */
    42. void NVIC_Config(void)
    43. {
    44.     /* 1 bits for pre-emption priority and 3 bits for subpriority */
    45.     NVIC_PRIGroup_Enable(NVIC_PRIGROUP_1);

    46.     /* Enable and set EXTI4_15 Interrupt to the highest priority */
    47.     NVIC_InitStructure.NVIC_IRQ = EXTI4_15_IRQn;
    48.     NVIC_InitStructure.NVIC_IRQPreemptPriority = 0;
    49.     NVIC_InitStructure.NVIC_IRQSubPriority = 0;
    50.     NVIC_InitStructure.NVIC_IRQEnable = ENABLE;
    51.     NVIC_Init(&NVIC_InitStructure);
    52. }

    53. /**
    54.   * @brief  Configure the selected EXTI line.
    55.   * @param  None
    56.   * @retval None
    57.   */
    58. void EXTI_Config(void)
    59. {
    60.     /* Enable GPIOF clock */
    61.     RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOC, ENABLE);
    62.    
    63.     NVIC_Config();

    64.     GPIO_InitStructure.GPIO_Pin = GPIO_PIN_12;
    65.     GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN;
    66.     GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
    67.     GPIO_Init(GPIOC, &GPIO_InitStructure);

    68.     /* Enable SYSCFG clock */
    69.     RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_CFG, ENABLE);
    70.    
    71.         /* Connect EXTI12 Line to PF12 pin */
    72.     SYSCFG_EXTILine_Config(EXTI_SOURCE_GPIOC, EXTI_SOURCE_PIN12);

    73.     /* Configure EXTI7 line */
    74.     EXTI_InitStructure.EXTI_LINE = EXTI_LINE12;
    75.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    76.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    77.     EXTI_InitStructure.EXTI_LINEEnable = ENABLE;
    78.     EXTI_Init(&EXTI_InitStructure);
    79. }

    80. /******************* (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
    复制代码
    改完后运行,则按一下LED0取反一下。程序成功。


    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2020-4-19 12:39
  • 签到天数: 128 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-7-8 07:38:26 | 显示全部楼层
    楼主好早0.0
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 11:35 , Processed in 0.133198 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.