标签:
C:\Users\LiTao\Desktop\儿童智能硬件资料\nrf51822\nRF51822EK_TM配套资料\EK_TM测试程序
串口测试
1 #include <stdint.h> 2 3 #include "nrf.h" 4 #include "simple_uart.h" 5 #include "nrf_delay.h" 6 #include "nrf_gpio.h" 7 8 uint8_t simple_uart_get(void) 9 { 10 while (NRF_UART0->EVENTS_RXDRDY != 1) 11 { 12 // Wait for RXD data to be received 13 } 14 15 NRF_UART0->EVENTS_RXDRDY = 0; 16 return (uint8_t)NRF_UART0->RXD; 17 }//等待直到可以直接RXDready,然后将该标志位设置为0,并把收到的数据返回(数据在RXD中) 18 19 bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data) 20 { 21 bool ret = true; 22 23 while (NRF_UART0->EVENTS_RXDRDY != 1) 24 { 25 if (timeout_ms-- >= 0) 26 { 27 // wait in 1ms chunk before checking for status 28 nrf_delay_us(1000); 29 } 30 else 31 { 32 ret = false; 33 break; 34 } 35 } // Wait for RXD data to be received 36 37 if (timeout_ms >= 0) 38 { 39 // clear the event and set rx_data with received byte 40 NRF_UART0->EVENTS_RXDRDY = 0; 41 *rx_data = (uint8_t)NRF_UART0->RXD; 42 } 43 44 return ret; 45 }//等一定时间然后接收 46 47 void simple_uart_put(uint8_t cr) 48 { 49 NRF_UART0->TXD = (uint8_t)cr; 50 51 while (NRF_UART0->EVENTS_TXDRDY != 1) 52 { 53 // Wait for TXD data to be sent 54 } 55 56 NRF_UART0->EVENTS_TXDRDY = 0; 57 } 58 59 void simple_uart_putstring(const uint8_t *str) 60 { 61 uint_fast8_t i = 0; 62 uint8_t ch = str[i++]; 63 while (ch != ‘\0‘) 64 { 65 simple_uart_put(ch); 66 ch = str[i++]; 67 } 68 } 69 70 void simple_uart_config( uint8_t rts_pin_number, 71 uint8_t txd_pin_number, 72 uint8_t cts_pin_number, 73 uint8_t rxd_pin_number, 74 bool hwfc) 75 { 76 nrf_gpio_cfg_output(txd_pin_number); 77 nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL); 78 79 NRF_UART0->PSELTXD = txd_pin_number; 80 NRF_UART0->PSELRXD = rxd_pin_number; 81 82 if (hwfc) 83 { 84 nrf_gpio_cfg_output(rts_pin_number); 85 nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL); 86 NRF_UART0->PSELCTS = cts_pin_number; 87 NRF_UART0->PSELRTS = rts_pin_number; 88 NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); 89 } 90 91 NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud38400 << 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 }
链接:http://pan.baidu.com/s/1i31tNBR
标签:
原文地址:http://www.cnblogs.com/happyhappy/p/4741252.html