标题:
NRF51822的一个串口通信例程源码
[打印本页]
作者:
q745904106
时间:
2018-10-15 17:36
标题:
NRF51822的一个串口通信例程源码
NRF51822的一个串口通信例程
单片机源程序如下:
/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
*/
#include <stdint.h>
#include "nrf.h"
#include "simple_uart.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
uint8_t simple_uart_get(void)
{
while (NRF_UART0->EVENTS_RXDRDY != 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;
}
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;
}
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++];
}
}
void simple_uart_config( uint8_t rts_pin_number,
uint8_t txd_pin_number,
uint8_t cts_pin_number,
uint8_t rxd_pin_number,
bool hwfc)
{
/** @snippet [Configure UART RX and TX pin] */
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;
/** @snippet [Configure UART RX and TX pin] */
if (hwfc)
{
nrf_gpio_cfg_output(rts_pin_number);
nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL);
NRF_UART0->PSELCTS = cts_pin_number;
NRF_UART0->PSELRTS = rts_pin_number;
NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
}
NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud115200 << 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;
}
/**********************************************
* 函数功能:串口处理
* 入口参数:无
* 返回值:无
**********************************************/
void Usart_Pro(void)
{
simple_uart_putstring("123456\r\n");
}
复制代码
所有资料51hei提供下载:
NRF51822_USART.7z
(321.13 KB, 下载次数: 39)
2018-10-15 17:35 上传
点击文件名下载附件
N51822_串口例程
下载积分: 黑币 -5
作者:
cnc202
时间:
2018-12-27 22:02
good job
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1