国民技术N32L40X进入Stop2

1.首先配置唤醒MCU的外部中断

void Exti_Pin_Config(void)
{
    EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;
	GPIO_InitType GPIO_InitStructure;
	GPIO_InitStruct(&GPIO_InitStructure);
	
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
	
	GPIO_InitStructure.Pin        = GPIO_PIN_0;
	GPIO_InitStructure.GPIO_Pull  = GPIO_Pull_Up;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
	GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.Pin        = GPIO_PIN_7;
	GPIO_InitStructure.GPIO_Pull  = GPIO_Pull_Up;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
	GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.Pin        = GPIO_PIN_8;
	GPIO_InitStructure.GPIO_Pull  = GPIO_Pull_Up;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
	GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.Pin        = GPIO_PIN_11;
	GPIO_InitStructure.GPIO_Pull    = GPIO_Pull_Up;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
	GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
	
	GPIO_InitStructure.Pin        = GPIO_PIN_13;
	GPIO_InitStructure.GPIO_Pull    = GPIO_Pull_Up;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
	GPIO_InitPeripheral(GPIOC, &GPIO_InitStructure);

	GPIO_ConfigEXTILine(GPIOA_PORT_SOURCE, GPIO_PIN_SOURCE0);
	GPIO_ConfigEXTILine(GPIOA_PORT_SOURCE, GPIO_PIN_SOURCE7);
	GPIO_ConfigEXTILine(GPIOA_PORT_SOURCE, GPIO_PIN_SOURCE8);
	GPIO_ConfigEXTILine(GPIOB_PORT_SOURCE, GPIO_PIN_SOURCE11);
	GPIO_ConfigEXTILine(GPIOC_PORT_SOURCE, GPIO_PIN_SOURCE13);

    EXTI_InitStructure.EXTI_Line = EXTI_LINE0 | EXTI_LINE7 | EXTI_LINE8 | EXTI_LINE11 | EXTI_LINE13;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel                   = EXTI0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x0D;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel                   = EXTI9_5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x0D;
	NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel                   = EXTI15_10_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x0D;
	NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	EXTI_ClrStatusFlag(EXTI_LINE0 | EXTI_LINE7 | EXTI_LINE8 | EXTI_LINE11 | EXTI_LINE13);
}

2.配置进入Stop2模式函数

void Stop2_Mode_Init(void)
{
	Led_Rest_Low;
	printf("Stop2_Mode_Init \r\n");
	Exti_Pin_Config();
	
	RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_PWR, ENABLE);
	PWR_EnterSTOP2Mode(PWR_STOPENTRY_WFI,PWR_CTRL3_RAM1RET);

	NVIC_SystemReset();
}

3.中断响应函数要记得写一下

void EXTI0_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_LINE0) != RESET)
    {
		EXTI_ClrITPendBit(EXTI_LINE0);
		//printf("EXTI_LINE0\r\n");
    }
}

void EXTI9_5_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_LINE7) != RESET)
    {
		EXTI_ClrITPendBit(EXTI_LINE7);
		//printf("EXTI_LINE7\r\n");
    }
    else if (EXTI_GetITStatus(EXTI_LINE8) != RESET)
    {
		EXTI_ClrITPendBit(EXTI_LINE8);
		//printf("EXTI_LINE8\r\n");
    }
}

void EXTI15_10_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_LINE11) != RESET)
    {
		EXTI_ClrITPendBit(EXTI_LINE11);
		//printf("EXTI_LINE11\r\n");
    }
	else if (EXTI_GetITStatus(EXTI_LINE13) != RESET)
	{
		EXTI_ClrITPendBit(EXTI_LINE13);
		//printf("EXTI_LINE13\r\n");
	}
}

你可能感兴趣的:(国民单片机开发,单片机,stm32,物联网)