找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3022|回复: 1
打印 上一主题 下一主题
收起左侧

NRF51822的一个串口通信例程源码

[复制链接]
跳转到指定楼层
楼主
ID:161493 发表于 2018-10-15 17:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
NRF51822的一个串口通信例程

单片机源程序如下:
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */

  12. #include <stdint.h>

  13. #include "nrf.h"
  14. #include "simple_uart.h"
  15. #include "nrf_delay.h"
  16. #include "nrf_gpio.h"

  17. uint8_t simple_uart_get(void)
  18. {
  19.   while (NRF_UART0->EVENTS_RXDRDY != 1)
  20.   {
  21.     // Wait for RXD data to be received
  22.   }
  23.   
  24.   NRF_UART0->EVENTS_RXDRDY = 0;
  25.   return (uint8_t)NRF_UART0->RXD;
  26. }

  27. bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data)
  28. {
  29.   bool ret = true;
  30.   
  31.   while (NRF_UART0->EVENTS_RXDRDY != 1)
  32.   {
  33.     if (timeout_ms-- >= 0)
  34.     {
  35.       // wait in 1ms chunk before checking for status
  36.       nrf_delay_us(1000);
  37.     }
  38.     else
  39.     {
  40.       ret = false;
  41.       break;
  42.     }
  43.   }  // Wait for RXD data to be received

  44.   if (timeout_ms >= 0)
  45.   {
  46.     // clear the event and set rx_data with received byte
  47.       NRF_UART0->EVENTS_RXDRDY = 0;
  48.       *rx_data = (uint8_t)NRF_UART0->RXD;
  49.   }

  50.   return ret;
  51. }

  52. void simple_uart_put(uint8_t cr)
  53. {
  54.   NRF_UART0->TXD = (uint8_t)cr;

  55.   while (NRF_UART0->EVENTS_TXDRDY!=1)
  56.   {
  57.     // Wait for TXD data to be sent
  58.   }

  59.   NRF_UART0->EVENTS_TXDRDY=0;
  60. }

  61. void simple_uart_putstring(const uint8_t *str)
  62. {
  63.   uint_fast8_t i = 0;
  64.   uint8_t ch = str[i++];
  65.   while (ch != '\0')
  66.   {
  67.     simple_uart_put(ch);
  68.     ch = str[i++];
  69.   }
  70. }

  71. void simple_uart_config(  uint8_t rts_pin_number,
  72.                           uint8_t txd_pin_number,
  73.                           uint8_t cts_pin_number,
  74.                           uint8_t rxd_pin_number,
  75.                           bool    hwfc)
  76. {
  77. /** @snippet [Configure UART RX and TX pin] */
  78.   nrf_gpio_cfg_output(txd_pin_number);
  79.   nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL);  

  80.   NRF_UART0->PSELTXD = txd_pin_number;
  81.   NRF_UART0->PSELRXD = rxd_pin_number;
  82. /** @snippet [Configure UART RX and TX pin] */
  83.   if (hwfc)
  84.   {
  85.     nrf_gpio_cfg_output(rts_pin_number);
  86.     nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL);
  87.     NRF_UART0->PSELCTS = cts_pin_number;
  88.     NRF_UART0->PSELRTS = rts_pin_number;
  89.     NRF_UART0->CONFIG  = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
  90.   }

  91.   NRF_UART0->BAUDRATE         = (UART_BAUDRATE_BAUDRATE_Baud115200 << UART_BAUDRATE_BAUDRATE_Pos);
  92.   NRF_UART0->ENABLE           = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
  93.   NRF_UART0->TASKS_STARTTX    = 1;
  94.   NRF_UART0->TASKS_STARTRX    = 1;
  95.   NRF_UART0->EVENTS_RXDRDY    = 0;
  96. }



  97. /**********************************************
  98. * 函数功能:串口处理
  99. * 入口参数:无
  100. * 返回值:无
  101. **********************************************/
  102. void Usart_Pro(void)
  103. {
  104.         simple_uart_putstring("123456\r\n");
  105. }


复制代码

所有资料51hei提供下载:
NRF51822_USART.7z (321.13 KB, 下载次数: 39)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:442911 发表于 2018-12-27 22:02 | 只看该作者
good job
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表