我的STM32F103C8T6微控制器有问题。我正在使用(作为练习)外部中断来打开/关闭led,方法是按下一个外部开关,然后将其连接到PC13。我正在使用StdPeriph库。
当芯片编程后,什么都不会发生。相反,当我使用调试器(在Coocox中调试)时,芯片工作正常。我不知道问题出在哪里。
你能帮帮我吗?
这是我的密码。

#include<stm32f10x.h>
#include<stm32f10x_rcc.h>
#include<stm32f10x_gpio.h>

#include<stm32f10x_exti.h>
#include<misc.h>

typedef enum{
    on,
    off
}state;
state led=on;

int main(void){

    // enable clocks
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

    // uncomment to disable/remap JTAG pins
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

    // configure PC13 as input
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
    GPIO_Init(GPIOC,&GPIO_InitStructure);

    // configure PB8 as led output
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);

    // connect PC13 to EXTI controller
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13);

    // enable and configure EXTI controller
    EXTI_InitTypeDef EXTI_InitStructure;
    EXTI_InitStructure.EXTI_Line=EXTI_Line13;
    EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd=ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    // enable IRQ
    NVIC_EnableIRQ(EXTI15_10_IRQn);
    NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);

    // Configure NVIC
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
    NVIC_Init(&NVIC_InitStructure);

    // switch on led
    GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);

    while(1);

    return 0;
}

void EXTI15_10_IRQHandler(void){

    // clear pending bit
    if(EXTI_GetITStatus(EXTI_Line13)!=RESET){
        EXTI_ClearITPendingBit(EXTI_Line13);
    }

    if(led==off){
        GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);
        led=on;
    }else{
        GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET);
        led=off;
    }
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t * file,uint32_t line){
    /* Infinite loop */
    while (1);
}
#endif

最佳答案

我也有这个问题。我用的是STM32F030。
我的问题是没有启用SYSCFG时钟,它是RCC APB2ENR寄存器的位0。我猜这个设置是在debug中启用的,这样软件就可以调试了?否则时钟将被禁用!
我最后通过查看STM32F1参考手册发现了这一点,该手册略为全面。

10-02 02:01
查看更多