直接上程序,一下子说不清楚。
u8 UART1_TX_ON;
u16 TxBuffer1_e;
u16 TxBuffer1_h;
u16 RxBuffer1_e;
u16 RxBuffer1_h;
@near u8 TxBuffer1[256];//发送fifo
@near u8 RxBuffer1[256]];//接收fifo
串口初始化:
void Uart1Init(u32 baute)
{
UART1_DeInit();
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1,ENABLE);//
UART1_Cmd(ENABLE);
enableInterrupts(); //开总中断
UART1_Init(baute, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE,
UART1_MODE_TXRX_ENABLE);
UART1_ITConfig(UART1_IT_TXE, DISABLE);//关串口开始发送中断
UART1_ITConfig(UART1_IT_TC, DISABLE);//关串口发送结束中断
/* Enable UART1 Receive and UART3 Transmit interrupt */
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
UART1_ITConfig(UART1_IT_TXE, ENABLE);
UART1_TX_ON = FALSE;
TxBuffer1_e = 0;
TxBuffer1_h = 0;
RxBuffer1_e = 0;
RxBuffer1_h = 0;
}
void add_data_to_uart1buff(u8 data)
{
TxBuffer1[TxBuffer1_h] = data;
TxBuffer1_h++;
if(TxBuffer1_h >= 256)
{
TxBuffer1_h = 0;
}
}
把PC串口发过来的数据返回给PC
void Deal_Uart1_Rx(void)
{
if(RxBuffer1_e != RxBuffer1_h)
{
add_data_to_uart1buff(RxBuffer1[RxBuffer1_e]);
RxBuffer1_e++;
if(RxBuffer1_e >= 256)
{
RxBuffer1_e = 0;
}
// add_data_to_uart1buff(0x55);
}
}
中断函数处理
接收
void UART1_RX_IRQHandler(void) interrupt 18
{
if(UART1_GetITStatus(UART1_IT_RXNE)==SET)
{
RxBuffer1[RxBuffer1_h] = UART1_ReceiveData8();
RxBuffer1_h++;
if(RxBuffer1_h>=256)RxBuffer1_h=0;
UART1_ClearITPendingBit(UART1_IT_RXNE);
}
}
发送
void UART1_TX_IRQHandler(void) interrupt 17
{
if(UART1_GetITStatus(UART1_IT_TXE) != RESET)
{
if(TxBuffer1_e != TxBuffer1_h)
{
/* Write one byte to the transmit data register */
UART1_SendData8(TxBuffer1[TxBuffer1_e]);
TxBuffer1_e++;
if(TxBuffer1_e >= 256)
{
TxBuffer1_e = 0;
}
}
else
{
/* Disable the USART1 Transmit interrupt */
UART1_ITConfig(UART1_IT_TXE, DISABLE);
UART1_TX_ON = FALSE;
}
UART1_ClearITPendingBit(UART1_IT_TXE);
}
}
现象就是 在void Deal_Uart1_Rx(void)中,如果把
add_str_to_uart1buff((u8 *)"B");屏蔽掉的话,发现只要从PC串口发送几次数据过来,尤其是当时间间隔很短的时候不停的发,程序就挂了,但是当我加上这个函数,发现程序正常,PC每隔5ms发几个字符过来,都正常。甚是不解。还请各位大虾帮忙看看。先谢谢了。 |