/*
STOP状态下通过串口唤醒MCU
唤醒机制:在MCU进入STOP状态后,不能直接通过UART等外设唤醒,
在MCU进入STOP前将RX脚设为EXTI模式,并使能对应的中断。
*/
/***************************************************************************************
** 函数名称: main
** 功能描述: 起始入口
**--------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
int main(void)
{
System_Init();
PWR_DeInit();
UART_Init();
Global_Variables_Init();
while(1)
{
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
}
}
/***************************************************************************************
** 函数名称: EXTIX_Init
** 功能描述: 把RX改为EXTI,允许中断
** 参 数: None
** 返 回 值: None
** 作 者: WEN(wenshijian4@163.com,QQ214490357)
** 日 期: 2015年09月17日
**--------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11);
EXTI_InitStructure.EXTI_Line=EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : EXTI15_10_IRQHandler
* Description : 位于stm32f10x_it.c
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI15_10_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line11);
NVIC_GenerateSystemReset();
}
/***************************************************************************************
** 函数名称: PWR_EnterSTOPMode
** 功能描述: 调整寄存器,进入STOP模式
** 参 数:
** 返 回 值: None
** 作 者: WEN(wenshijian4@163.com,QQ214490357)
** 日 期: 2015年09月17日
**--------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
void PWR_EnterSTOPMode(u32 PWR_Regulator, u8 PWR_STOPEntry)
{
u32 tmpreg = 0;
EXTIX_Init();
/* Select the regulator state in STOP mode ---------------------------------*/
tmpreg = PWR->CR;
/* Clear PDDS and LPDS bits */
tmpreg &= CR_DS_Mask;
/* Set LPDS bit according to PWR_Regulator value */
tmpreg |= PWR_Regulator;
/* Store the new value */
PWR->CR = tmpreg;
/* Set SLEEPDEEP bit of Cortex System Control Register */
*(vu32 *) SCB_SysCtrl |= SysCtrl_SLEEPDEEP_Set;
/* Select STOP mode entry --------------------------------------------------*/
if(PWR_STOPEntry == PWR_STOPEntry_WFI)
{
/* Request Wait For Interrupt */
__WFI();
}
else
{
/* Request Wait For Event */
__WFE();
}
}
/***************************************************************************************
** 函数名称: UART_Init
** 功能描述: 串口初始化
** 参 数: None
** 返 回 值: None
** 作 者: WEN(wenshijian4@163.com,QQ214490357)
** 日 期: 2015年09月17日
**--------------------------------------------------------------------------------------
** 修 改 人:
** 日 期:
**--------------------------------------------------------------------------------------
****************************************************************************************/
void UART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable USART3 clock */
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
// GPIO Settings for USART3
// PB10: TXD_ROOM
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// PB11: RXD_ROOM
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate =9600;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_2;
USART_InitStructure.USART_Parity = USART_Parity_Even;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
UartRxON();
// In initial state, waiting for indoor data...
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
UART_State=UART_RX; //0; // 0: received state 1: transmit state
/* Enable the USART3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
}
上一篇:STM32在外部32Mhz晶振下的时钟配置方法
下一篇:stm32l476 内部flash HAL库操作方法
推荐阅读最新更新时间:2024-03-16 16:11