1、NRF51822的UART通信特点:
(1)全双工通信;
(2)自动数据流控制;
(3)奇偶校验,并且自动发送校验位
(4)32个GPIO口都可以设为RX 和TX ,这为PCB板布线带来了极大的方便。
2、接线:
RX(NRF51822 - Pin 9 )—— MAX232 -TX
RX(NRF51822 - Pin 10 )—— MAX232 -RX
这里暂时不用到RTS 和CTS 请求发送和清除发送 功能位。
3、应用驱动程序
/*获取PC发送的字符*/
uint8_t simple_uart_get(void)
{
while (NRF_UART0->EVENTS_RXDRDY != 1) //µÈ´ý½ÓÊÕ×¼±¸Î»Îª1
{
// Wait for RXD data to be received
}
NRF_UART0->EVENTS_RXDRDY = 0;
return (uint8_t)NRF_UART0->RXD;
}
/* 接收数据的最大允许等待时间 */
bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data)
{
bool ret = true;
while (NRF_UART0->EVENTS_RXDRDY != 1)
{
if (timeout_ms-- >= 0)
{
// wait in 1ms chunk before checking for status
nrf_delay_us(1000);
}
else
{
ret = false;
break;
}
} // Wait for RXD data to be received
if (timeout_ms >= 0)
{
// clear the event and set rx_data with received byte
NRF_UART0->EVENTS_RXDRDY = 0;
*rx_data = (uint8_t)NRF_UART0->RXD;
}
return ret;
}
/* 向PC发送一个字符*/
void simple_uart_put(uint8_t cr)
{
NRF_UART0->TXD = (uint8_t)cr;
while (NRF_UART0->EVENTS_TXDRDY!=1)
{
// Wait for TXD data to be sent
}
NRF_UART0->EVENTS_TXDRDY=0;
}
/* 向PC发送字符串 */
void simple_uart_putstring(const uint8_t *str)
{
uint_fast8_t i = 0;
uint8_t ch = str[i++];
while (ch != '\0')
{
simple_uart_put(ch);
ch = str[i++];
}
}
/* UART 通信引脚、波特率、使能等设定*/
void simple_uart_config( uint8_t txd_pin_number,
uint8_t rxd_pin_number)
{
nrf_gpio_cfg_output(txd_pin_number);
nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL);
NRF_UART0->PSELTXD = txd_pin_number;
NRF_UART0->PSELRXD = rxd_pin_number;
NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud38400 << UART_BAUDRATE_BAUDRATE_Pos);
NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
NRF_UART0->TASKS_STARTTX = 1;
NRF_UART0->TASKS_STARTRX = 1;
NRF_UART0->EVENTS_RXDRDY = 0;
}
用户377235 2015-5-21 11:07
用户377235 2014-8-13 14:45
用户402158 2014-8-8 12:00
用户403664 2014-2-25 16:05