本帖最后由 swillow 于 2015-5-20 14:13 编辑
做一个串口通信的项目,根据从机发回来的数据校验位是1来判断是最后一个数据,程序如下
实际测试时经常出现没有接到到最后一个数据的情况,但示波器跟踪波形是正确的,也就是说,经常会出现校验位跟实际不符的情况,
不知道问题出在哪了,哪位高手可指点一二
void Uart1Init(void)
{
uint16_t usFdiv;
LPC_IOCON->P0_15 &= 0xfffffff8;
LPC_IOCON->P0_15 |= (1 << 0);
LPC_IOCON->P0_16 &= 0xffffffe0;
LPC_IOCON->P0_16 |= (1 << 0); //U1_RXD
LPC_SC->PCONP |= (1<<4); /* 开启串口1功能模块 */
LPC_UART1->LCR |= (1<<LCR_DLAB); /* 允许设置波特率 */
//usFdiv = SystemFrequency/4/16/ UART_BPS; /* 设置波特率 */
usFdiv = 60000000/16/9600;
//usFdiv =279;
//usFdiv=391;
LPC_UART1->DLM = usFdiv >>8;
LPC_UART1->DLL = usFdiv & 0xff;
LPC_UART1->LCR &=(~(1<<LCR_DLAB)); //Lock the Bandrate set
//LPC_UART1->FDR = ((5<<4)+2); //Fractional Divider Register
LPC_UART1->LCR |= (3<<0)|(1<<3)|(3<<4); /*8 Bit data,Parity Enable,Space Check*/
//bit[1:0] Word Length select
//Bit[2] Stop Bit Select
//Bit[3] Parity Enable
//Bit[5:4]Parity Select
//Bit[7] DLAB
LPC_UART1->FCR = 0x07; /* FIFO Enable */
//LPC_UART1->TER |=(1<<7);
}
uint8_t Uart1Rec(uint8_t *dat,uint8_t *mode)
{
uint8_t bit;
uint32_t tick=DATA_RECEIVE_MAX_TIME;
//ParitySet(MARK);
bit=LPC_UART1->LSR;
while ((bit & 0x01) == 0) /* 等待接收标志置位 */
{
tick--;
if(0x00==tick)
{
return 0;
}
bit=LPC_UART1->LSR;
}
//bit=LPC_UART1->LSR &(1<<2);
*dat= LPC_UART1->RBR; /* 读取数据 */
//*mode=LPC_UART1->LSR & (1<<2); /*LSR[2] Parity Error 0,no Error,1,Error*/
if((0x03<<4)==(LPC_UART1->LCR & (0x03<<4))) //Space Parity check
{
if((1<<2)==(bit&(1<<2))) //Parity Error
*mode=1;
else
*mode=0;
//Uart0Send(0x11);
}
else //Mark Parity Check
{
if((1<<2)==(bit&(1<<2))) //Parity Error
*mode=0;
else
*mode=1;
}
return 1;
} |