初级会员

- 积分
- 152
- 金钱
- 152
- 注册时间
- 2012-8-28
- 在线时间
- 2 小时
|

楼主 |
发表于 2013-8-19 17:31:14
|
显示全部楼层
加延时了也不行。。
函数如下
/*****************************************************************************
  rototype : MApi_Uart2Via485RecHandler
Description: uart2 via rs485 receive tx board rs232 data .
Input : void
Output : None
Return Value: void
Calls :
Called By :
History :
1.Date : 2013/8/10
Author : Corey.Chen
Modification: Created function
*****************************************************************************/
void MApi_Uart2Via485RecHandler(void)
{
byte rec_buf;
byte i, j;
while(1)
{
/* See if any byte in the fifo, if have, get the first byte, else
return */
//DIS_INT;
//DIS_USART1_RX_INT();
if (uart_rx_fifo.cnt == 0)
{
//EN_INT;
//EN_USART1_RX_INT();
break;
}
rec_buf = uart_rx_fifo.buf[uart_rx_fifo.out_pos];
//PRINT_DATA("rec_buf is 0x%x..", rec_buf);
EN_INT();
#if 0
USART_SendData(USART2, rec_buf);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART2, USART_FLAG_TXE);
#endif
uart_rx_fifo.out_pos++;
uart_rx_fifo.out_pos = uart_rx_fifo.out_pos % UART1_FIFO_SIZE;
uart_rx_fifo.cnt--;
/// EN_INT;
/* Have data received, do something */
switch (uart_poll_state)
{
case US_P_IDLE:
if (rec_buf == U2_PAK_HEAD_1)
uart_poll_state = US_P_GET_HD1;
break;
case US_P_GET_HD1:
if (rec_buf == U2_PAK_HEAD_2)
{
uart_poll_state = US_P_REC;
buf_i = 0;
g_revComp = 0;
}
else
uart_poll_state = US_P_IDLE;
break;
case US_P_REC:
uart_rx_buf[buf_i++] = rec_buf;
#if 0
USART_SendData(USART2, rec_buf);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART2, USART_FLAG_TXE);
#endif
if (buf_i >= UART1_FIFO_DATA_SIZE)
uart_poll_state = US_P_WAIT_END;
break;
case US_P_WAIT_END:
if (rec_buf == U2_PAK_END)
{
/* get a good end byte of packet, a complete pkt is
received , we can do something with this packet */
do_uart_pkt();
g_revComp = 1;
//EN_USART1_RX_INT();
#if 0
USART_SendData(USART2, rec_buf);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART2, USART_FLAG_TXE);
#endif
uart_poll_state = US_P_IDLE;
GPIO_CPLBits(GPIOB, GPIO_Pin_1);
}
else
{
/* get a bad end byte of packet, this packe is illegal, so
we need find out that if there is another packet header in
the buffer received, if have ,do another received process
with the data follow the header in the uart_rx_buf[], which has
received in last packet */
for (i = 0; i < UART1_FIFO_DATA_SIZE - 1; i++)
{
if ( (uart_rx_buf == U2_PAK_HEAD_1) &&
(uart_rx_buf[i + 1] == U2_PAK_HEAD_2) )
{
/* get another pkt header in the buf */
i = i + 2;
for (j = 0; j < (UART1_FIFO_DATA_SIZE - i); j++)
uart_rx_buf[j] = uart_rx_buf[i++];
uart_poll_state = US_P_REC;
buf_i = j;
break;
}
}
/* have found the pkt header */
if (uart_poll_state == US_P_REC) break;
/* if the last bit in the buf is U_PAK_HEAD_1 */
if (uart_rx_buf[UART1_FIFO_DATA_SIZE - 1] == U2_PAK_HEAD_1)
uart_poll_state = US_P_GET_HD1;
else uart_poll_state = US_P_IDLE;
}
break;
default:
uart_poll_state = US_P_IDLE;
}
}
} |
|