在线时间49 小时
UID1860622
注册时间2016-10-24
NXP金币0
TA的每日心情 | 无聊 2024-9-24 10:29 |
---|
签到天数: 95 天 连续签到: 1 天 [LV.6]常住居民II
高级会员

- 积分
- 663
- 最后登录
- 2024-9-24
|
分享下串口的设置
#define IER_RBR 0x01
#define IER_THRE 0x02
#define IER_RLS 0x04
#define IIR_PEND 0x01
#define IIR_RLS 0x03
#define IIR_RDA 0x02
#define IIR_CTI 0x06
#define IIR_THRE 0x01
#define LSR_RDR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80
PINCON->PINSEL0 = 0x00000050; /* RxD0 is P0.3 and TxD0 is P0.2 */
UART0->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1/4 of the SystemFrequency. */
Fdiv = ( SystemFrequency/4/16 ) / baudrate ; /*baud rate, Fpclk: 18MHz */
UART0->DLM = Fdiv / 256;
UART0->DLL = Fdiv % 256;
UART0->LCR = 0x03; /* DLAB = 0 */
UART0->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
NVIC_EnableIRQ(UART0_IRQn);
UART0->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART0 interrupt */
return (TRUE);
上面主要是设置gpio 设置串口为 baudrate 波特率 8 no 1 模式 使能fifo 中断
下面看下中断处理函数
uint8_t IIRValue, LSRValue;
uint8_t Dummy = Dummy;
IIRValue = UART0->IIR;
IIRValue >>= 1; /* skip pending bit in IIR */
IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
if ( IIRValue == IIR_RLS ) /* Receive Line Status */
{
LSRValue = UART0->LSR;
/* Receive Line Status */
if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
{
//这里处理一些错误 打断的
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt */
UART0Status = LSRValue;
Dummy = UART0->RBR; /* Dummy read on RX to clear
interrupt, then bail out */
return;
}
//准备接收数据
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
UART0Buffer[UART0Count] = UART0->RBR;
UART0Count++;
if ( UART0Count == BUFSIZE )
{
UART0Count = 0; /* buffer overflow */
}
}
}
//接收数据
else if ( IIRValue == IIR_RDA ) /* Receive Data Available */
{
/* Receive Data Available */
UART0Buffer[UART0Count] = UART0->RBR;
UART0Count++;
if ( UART0Count == BUFSIZE )
{
UART0Count = 0; /* buffer overflow */
}
}
//超时
else if ( IIRValue == IIR_CTI ) /* Character timeout indicator */
{
/* Character Time-out indicator */
UART0Status |= 0x100; /* Bit 9 as the CTI error */
}
else if ( IIRValue == IIR_THRE ) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = UART0->LSR; /* Check status in the LSR to see if
valid data in U0THR or not */
if ( LSRValue & LSR_THRE )
{
UART0TxEmpty = 1;
}
else
{
UART0TxEmpty = 0;
}
}
主函数内
if(UART0Count != 0)//如果有数据进入
{
UART0->IER = IER_THRE | IER_RLS; /* Disable RBR */ //清除标志
UARTSend( 0, (uint8_t *)UART0Buffer, UART0Count );//发送接收到的数据
UART0Count = 0;
UART0->IER = IER_THRE | IER_RLS | IER_RBR; /* Re-enable RBR *///使能标志
}
|
|