低功耗蓝牙cc2541学习笔记之UART-2-驱动uart
在 CC2541 中,USART0 和 USART1 是串行通信接口,它们能够分别运行于异步 USART 模式或者同步 SPI 模式。两个 USART 的功能是一样的,可以通过设置在单独的 IO 引脚上。
查看 CC2541 的 datasheet 可知: UART0 对应的外部设备 IO 引脚关系为:
P0_2 ------ RX
P0_3 ------ TX
UART1 对应的外部设备 IO 引脚关系为:
P0_5 ------ RX P0_4 ------ Tx
USART 模式的操作具有下列特点:
1、8 位或者 9 位负载数据 2、奇校验、偶校验或者无奇偶校验 3、配置起始位和停止位电平 4、配置 LSB 或者 MSB 首先传送 5、独立收发中断 6、独立收发 DMA 触发
CC2541 配置串口的一般步骤: 1 、 配置 IO,使用外部设备功能。 2 、 配置相应串口的控制和状态寄存器。 3 、 配置串口工作的波特率。
The UART mode provides full-duplex asynchronous transfers,In the UART mode, the interface uses a two-wire or four-wire interface consisting of the pins RXD and TXD, and optionally RTS and CTS. (全双工。2线制:TXD,RXD。4线制:TXD,RXD,RTS,CTS)
17.1.1 UART Transmit A UART transmission is initiated when the USART receive/transmit data buffers, UxDBUF, are written. The byte is transmitted on the TXDx output pins. The UxDBUF registers are double-buffered. The UxCSR.ACTIVE bit goes high when the byte transmission starts and low when it ends. When the transmission ends, the UxCSR.TX_BYTE bit is set to 1. An interrupt request is generated when the UxDBUF register is ready to accept new transmit data. This happens immediately after the transmission has been started; hence, a new data byte value can be loaded into the data buffer while the byte is being transmitted.
大意:讲解uart 传输的原理:
UxDBUF(the USART receive/transmit data buffer)写值后,The byte就会从TXDX引脚输出,UxDBUF是个双缓冲。
if(the byte transmission starts)
{
UxCSR.ACTIVE = 1;
}
else if(the byte transmission ends)
{
UxCSR.ACTIVE = 0;
}
当传输结束时,UxCSR.TX_BYTE位设置为1.当UxDBUF寄存器准备好接受新的发送数据时,产生中断请求。 这在传输开始后立即发生; 因此,当发送字节时,可以将新的数据字节值加载到数据缓冲器中。
17.1.2 UART Receive Data reception on the UART is initiated when a 1 is written to the UxCSR.RE bit. The UART then searches for a valid start bit on the RXDx input pin and sets the UxCSR.ACTIVE bit high. When a valid start bit has been detected, the received byte is shifted into the receive register. The UxCSR.RX_BYTE bit is set and a receive interrupt is generated when the operation has completed. At the same time, UxCSR.ACTIVE goes low. The received data byte is available through the UxDBUF register. When UxDBUF is read, UxCSR.RX_BYTE is cleared by hardware.
大意:讲解uart 接受的原理:
当将1写入UxCSR.RE位时,UART上的数据接收发起。 UART然后在RXDx输入引脚上搜索有效的起始位,并将UxCSR.ACTIVE位设置为高电平。 当检测到有效的起始位时,接收到的字节被移入接收寄存器。 UxCSR.RX_BYTE位置1,当操作完成时产生接收中断。 同时,UxCSR.ACTIVE也置低。 接收的数据字节可通过UxDBUF寄存器使用。 当UxDBUF被读取时,UxCSR.RX_BYTE被硬件清零。
接受数据的时候,UxCSR.ACTIVE=1,接受完毕,UxCSR.ACTIVE=0。UxDBUF有接受到的值时,UxCSR.RX_BYTE=1,被读取后,UxCSR.RX_BYTE=0。
波特率设置:
根据ti提供给的文档说明,结合寄存器说明,程序就很好写了。
寄存器具体配置如下:
void InitUart(void)
{
PERCFG = 0x00; //外设控制寄存器 USART 0的IO位置:0为P0口位置1
P0SEL = 0x0c; //P0_2,P0_3用作串口(外设功能)
P2DIR &= ~0xC0; //P0优先作为UART0
U0CSR |= 0x80; //设置为UART方式
U0GCR |= 11;
U0BAUD |= 216; //波特率设为115200
UTX0IF = 0; //UART0 TX中断标志初始置位0
U0CSR |= 0x40; //允许接收
IEN0 |= 0x84; //开总中断允许接收中断
}
