中级会员
 
- 积分
- 389
- 金钱
- 389
- 注册时间
- 2019-4-25
- 在线时间
- 74 小时
|
5金钱
main函数下
case 1: //处理接收
printf("0x%02X\r,0x%02X\n",RxBuffer[0],RxBuffer[1]); //--返回接受到的
if (RxBuffer[1] == 0x01){
OpenTime();
}else if (RxBuffer[1] == 0x02){
CloseTime();
}
USART设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART1_InitStructure.USART_BaudRate = 115200;
USART1_InitStructure.USART_WordLength = USART_WordLength_8b;
USART1_InitStructure.USART_StopBits = USART_StopBits_1;
USART1_InitStructure.USART_Parity = USART_Parity_No;
USART1_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART1_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USARTy */
USART_Init(USART1, &USART1_InitStructure);
/* Enable USARTy Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1,ENABLE);
中断函数
void USART1_IRQHandler(void) //向上
{
int index = 0;
//int sum = 0;
if (USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET) //换成USART_IT_RXNE 也不行
{
Rxc = USART_ReceiveData(USART1);
RxBuffer[index] = Rxc;
index++;
// sum = sum + RxBuffer[k];
// k++;
}
if (RxBuffer[0] == 0xcc)
{
Flag = 1;
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //
}
使用串口助手发送16进制数时,主函数printf返回的字符串时0XCC 0X000X01 0X000X01 0X000X01 ......然后一直都这样,
我只发送一次的0xcc 0x01,为什么会多次进入接受中断,还是说我的RxBuffer没处理好?需要清0?但我是定义 RxBuffer[5]= {0};,有大佬帮一下吗?
|
|