NRF24LE1

nrf24le1串口操作

NRF24LE1串口
nrf24le1配有一个与标准8051串口操作相同的串行接口。串行接口0(serial 0)两根串行端口信号线RxD和TxD在设备引脚UART、RxD上和UART/TxD。
串行接口(串口)时钟来源于MCU时钟:ckCpu
nrf24le1_clock_system.png

串口特征:

  • 同步模式,固定波特率
    8位UART模式,可变波特率
    9位UART模式,可变波特率
    9位UART模式,固定波特率
    专用波特率发生器
    注:不建议使用定时器1溢出作为波特率发生器。
    串口结构图如下所示:
    nrf24le1_serial_port_block_diagram.png

串口配置寄存器
nrf24le1_seria_s0con.jpg

常用波特率一览表:
nrf24le1_serial_baud_table.png

波特率通常单位是bit/s,也就是二进制位/秒。因为一个字节时8个bit,二一个字节通常可以表达一个ASCII码,比如一个英文字母。所以9600的波特率信道,理论上每秒可以传输9600/10 = 960byte。一秒钟传输960个字节。每个周期1000/9600≈0.1ms=100us。


#include "reg24le1.h" 
#include "string.h"


/*
================================================================================
UART_Init:
    Initialize the UART
input     : BaudIndex, the index of the  baudrate of the UART, 0-7, 1200-115200
Output    : None
================================================================================
*/
code unsigned int UART_BAUD_TBL[7] = { 607, 816, 920, 972, 998, 1011, 1015 };
//0----6,,,--> 1200, 2400, 4800, 9600, 19200, 38400, 57600
void UART_Init( unsigned char BaudIndex )
{
    IEN0 &= ~( 1<<4 );                  //Disable UART0 interrupt while initializeing
    S0CON = ( 1<<6 ) | ( 1<<4 );            //MODE1, Enable receive
    ADCON = 0x80;                   //Enable baudrate generator
    PCON |= 0x80;                   //mode1
    S0RELL = UART_BAUD_TBL[BaudIndex];
    S0RELH = UART_BAUD_TBL[BaudIndex] >> 8;
    IEN0 |= ( 1<<4 );                   //Enable interrupt
    P0DIR |= ( 1<<4 );                      //P0.4, RXD, set as input
    P0DIR &= ~( 1<<3 );                         //P0.3, TXD, set as output
}
unsigned char TX_OK = 0;
static void UART0_ISR( void ) interrupt INTERRUPT_UART0
{
    if( S0CON & ( 1<<1 ) )
    {
        //TX ok
        S0CON &= ~( 1<<1 );
        TX_OK = 1;
    }
    else if( S0CON & ( 1<<0 ) )
    {
        //RX ok        
        S0CON &= ~( 1<<0 );
        P0DIR &= ~( 1<<2 );         //Set P0.2 as output
        P0 ^= ( 1<<2 );             //Turn on the LED
        S0BUF = S0BUF;
    }
}


void UART0_SendBuff( unsigned char *buff, unsigned char size )
{  
    while( size -- )
    {
        TX_OK = 0;
        S0BUF = *buff ++;
        while( TX_OK == 0 );
    }
    TX_OK = 0;
}


void UART0_SendStr( char* str )
{
    UART0_SendBuff( ( unsigned char* ) str, strlen( str ) );
}


void main( void )
{
    CLKCTRL = ( CLKCTRL | 0x80 ) & ~0x08;


    UART_Init( 3 );    //baudrate = 9600
    EA = 1;
    UART0_SendStr( "nRF24LE1 UART Test Start!\r\n" );
    while( 1 );
}

回复

This is just a placeholder img.