stm32串口中断接收的数据怎么都是0
发布网友
发布时间:2022-07-02 10:59
我来回答
共1个回答
热心网友
时间:2023-10-26 08:22
int UART_WriteBuf(u8 byCom, u8 *pBuf, u16 Len)
{
int i = 0;;
if(Len == 0)
return 0;
switch(byCom)
{
case 1 :
Uart_data.send_len = Len - 1;
Uart_data.j = 1;
memmove(Uart_data.send_buff,pBuf,Len);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
USART_SendData(USART1, *pBuf);
break;
case 2 :
Uart_data.send_len = Len - 1;
Uart_data.j = 1;
memmove(Uart_data.send_buff,pBuf,Len);
USART_ITConfig(USART2, USART_IT_TC, ENABLE);
USART_SendData(USART2, *pBuf);
break;
case 3 :
Uart_data.send_len = Len - 1;
Uart_data.j = 1;
memmove(Uart_data.send_buff,pBuf,Len);
USART_SendData(USART3, *pBuf);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
break;
}
return Len;
}
void USART3_IRQHandler(void)
{
int j = 0;
if(USART_GetITStatus(USART3,USART_IT_RXNE))
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
Uart_data.rec_buff[Uart_data.rec_len] = USART_ReceiveData(USART3);
Uart_data.rec_len ++;
if(Uart_data.rec_len >= UART_DATA_SIZE) //防止数组越界
Uart_data.rec_len = UART_DATA_SIZE - 1;
}
else if(USART_GetITStatus(USART3,USART_IT_TXE))
{
USART_ClearITPendingBit(USART3,USART_IT_TXE);
USART_ClearFlag(USART3,USART_FLAG_TC);
if(Uart_data.j <= Uart_data.send_len)
{
// printf("TC = %d,TXE = %d,FLAG_TC = %d\n\r",USART_GetITStatus(USART3,USART_IT_TC),USART_GetITStatus(USART3,USART_IT_TXE),USART_GetFlagStatus (USART3,USART_FLAG_TC));
USART_SendData(USART3,Uart_data.send_buff[Uart_data.j]);
while(j < 800)
j++;
// printf("interrupt:j = %d,len = %d,buff[%d] = %c,buff = %s\n\n\r",Uart_data.j,Uart_data.len,Uart_data.j,Uart_data.buff[Uart_data.j],Uart_data.buff);
}
else
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
Uart_data.j++;
}
}