[mw_shl_code=c,true]
[mw_shl_code=c,true]void USART_Config(USART_TypeDef* USARTx,u32 baud){
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate =baud; //波特率38400bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
USART_InitStructure.USART_Parity = USART_Parity_No; //无校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USARTx, &USART_InitStructure);
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //使能接收中断
/* 使能串口 */
USART_Cmd(USARTx, ENABLE);
}[/mw_shl_code]
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //设置串口2中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_IRQHandler(void)
{
OS_CPU_SR cpu_sr;
USART_TypeDef *usart;
CPU_INT08U rx_data;
OS_ENTER_CRITICAL(); //
OSIntNesting++; //
OS_EXIT_CRITICAL(); //
usart = USART2;
if (USART_GetFlagStatus(usart, USART_FLAG_ORE) != RESET)//
{
USART_ReceiveData(usart);
}
DBG(USART1,"* USART2_IRQHandler :\r\n");
if(USART_GetITStatus(usart, USART_IT_RXNE) != RESET) //
{
rx_data = USART_ReceiveData(usart) & 0xFF; /* Read one byte from the receive data register */
SmartHomeUart2RxBuf[SmartHomeUart2RxCnt++] =rx_data; //
if(SmartHomeUart2RxBuf[SmartHomeUart2RxCnt-1]==0xfe)
{
SmartHomeUart2RxBuf[0]=0xfe; SmartHomeUart2RxCnt=1;
}
else if((SmartHomeUart2RxBuf[0]==0xfe)&&(SmartHomeUart2RxBuf[SmartHomeUart2RxCnt-1]==0xfd))
{
SmartHomeUart2RxCnt=0;
if(SmartHomeUart2RxBuf[1]==1)
SmartHomeZigbeeInspection=1;
else if(SmartHomeUart2RxBuf[1]==2)
SmartHomeZigbeeInspection=3;//1
}
USART_ClearITPendingBit(usart, USART_IT_RXNE);
}
if (USART_GetITStatus(usart, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(usart, USART_IT_TXE); /* Clear the USART1 transmit interrupt */
}
OSIntExit();
}[/mw_shl_code]