Last active
February 17, 2018 14:06
-
-
Save s-kostyuk/a782bd52f09fa4d70e4cd3d75bf3f2e7 to your computer and use it in GitHub Desktop.
Demonstration of transreceiving with nRF24L01+ modules, AT89S52 and Arduino UNO. More info & video: https://www.youtube.com/watch?v=Uc9rZH_F2NI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /********************************************************************* | |
| ** Device: nRF24L01 ** | |
| ** File: 89LV51.c ** | |
| ** ** | |
| ** Created by ElecFreaks Robi.W /10 June 2011 ** | |
| ** ** | |
| ** Description: ** | |
| ** This file is a sample code for your reference. ** | |
| ** ** | |
| ** ** | |
| ** This demo code is free software; you can redistribute it and/or ** | |
| ** modify it under the terms of the GNU Lesser General Public ** | |
| ** License as published by the Free Software Foundation; either ** | |
| ** version 2.1 of the License, or (at your option) ** | |
| ** any later version. ** | |
| ** ** | |
| ** Copyright (C) 2011 ElecFreaks Corp. ** | |
| ** ** | |
| ** ** | |
| ** http://www.elecfreaks.com ** | |
| *********************************************************************/ | |
| /********************************************************************* | |
| ** Device: nRF24L01 ** | |
| ** File: 89LV51.c ** ** | |
| ** ** | |
| ** Rewritten by Sergey Kostyuk /17 February 2018 ** | |
| ** ** | |
| ** Changelog: ** | |
| ** - removed a lot of functions; ** | |
| ** - disabled UART; ** | |
| ** - kept original functions: ** | |
| ** * init_io ** | |
| ** * delay_ms ** | |
| ** * SPI_RW ** | |
| ** * SPI_RW_Reg ** | |
| ** * SPI_Read ** | |
| ** * SPI_Read_Buf ** | |
| ** * SPI_Write_Buf ** | |
| ** - added new functions: ** | |
| ** * startWrite ** | |
| ** * blink ** | |
| ** * stopWrite ** | |
| ** * diagnostic (print all regs from 0x00 to 0x16 including) ** | |
| ** * configure ** | |
| ** - new functionality: ** | |
| ** 1) set everything up ** | |
| ** 2) send sample data from TX_buf ** | |
| ** 3) print STATUS register on P0 leds ** | |
| ** 4) wait for 500 ms and start from point 2 ** | |
| ** ** | |
| ** ** | |
| ** https://github.com/s-kostyuk/ ** | |
| *********************************************************************/ | |
| #include <reg51.h> | |
| //#include <intrins.h> | |
| #include "api.h" | |
| /* | |
| *This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTYT; | |
| * | |
| *uart:9600BPS | |
| */ | |
| /***************************************************/ | |
| #define uchar unsigned char | |
| #define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width | |
| #define TX_PLOAD_WIDTH 3 // 3 bytes TX payload | |
| //uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address | |
| uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0xc2,0xc2,0xc2,0xc2,0xc2}; // Define a static TX address | |
| uchar rx_buf[TX_PLOAD_WIDTH]; | |
| uchar tx_buf[TX_PLOAD_WIDTH] = {0x02,0x02,0x02}; | |
| uchar flag; | |
| uchar old_state; | |
| uchar i = 0; | |
| /**************************************************/ | |
| sbit CE = P2^0; | |
| sbit CSN= P2^1; | |
| sbit SCK= P2^2; | |
| sbit MOSI= P2^3; | |
| sbit MISO= P2^4; | |
| sbit IRQ = P2^5; | |
| /**************************************************/ | |
| uchar bdata sta; | |
| sbit RX_DR =sta^6; | |
| sbit TX_DS =sta^5; | |
| sbit MAX_RT =sta^4; | |
| /**************************************************/ | |
| #define PWR_UP 0x02 | |
| #define PRIM_RX 0x01 | |
| #define WRITE_REG_CMD 0x20 | |
| /**************************************************/ | |
| /************************************************** | |
| Function: init_io(); | |
| Description: | |
| flash led one time,chip enable(ready to TX or RX Mode), | |
| Spi disable,Spi clock line init high | |
| /**************************************************/ | |
| #define KEY 0xaa | |
| void init_io(void) | |
| { | |
| P0=KEY; // led light | |
| CE=0; // chip enable | |
| CSN=1; // Spi disable | |
| SCK=0; // Spi clock line init high | |
| P0=0xff; // led close | |
| } | |
| /**************************************************/ | |
| void delay_ms(unsigned int x) | |
| { | |
| unsigned int i,j; | |
| i=0; | |
| for(i=0;i<x;i++) | |
| { | |
| j=108; | |
| ; | |
| while(j--); | |
| } | |
| } | |
| /**************************************************/ | |
| /************************************************** | |
| Function: SPI_RW(); | |
| Description: | |
| Writes one byte to nRF24L01, and return the byte read | |
| from nRF24L01 during write, according to SPI protocol | |
| /**************************************************/ | |
| uchar SPI_RW(uchar byte) | |
| { | |
| uchar bit_ctr; | |
| for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit | |
| { | |
| MOSI = (byte & 0x80); // output 'byte', MSB to MOSI | |
| byte = (byte << 1); // shift next bit into MSB.. | |
| SCK = 1; // Set SCK high.. | |
| byte |= MISO; // capture current MISO bit | |
| SCK = 0; // ..then set SCK low again | |
| } | |
| return(byte); // return read byte | |
| } | |
| /**************************************************/ | |
| /************************************************** | |
| Function: SPI_RW_Reg(); | |
| Description: | |
| Writes value 'value' to register 'reg' | |
| /**************************************************/ | |
| uchar SPI_RW_Reg(BYTE reg, BYTE value) | |
| { | |
| uchar status; | |
| CSN = 0; // CSN low, init SPI transaction | |
| status = SPI_RW(reg); // select register | |
| SPI_RW(value); // ..and write value to it.. | |
| CSN = 1; // CSN high again | |
| return(status); // return nRF24L01 status byte | |
| } | |
| /**************************************************/ | |
| /************************************************** | |
| Function: SPI_Read(); | |
| Description: | |
| Read one byte from nRF24L01 register, 'reg' | |
| /**************************************************/ | |
| BYTE SPI_Read(BYTE reg) | |
| { | |
| BYTE reg_val; | |
| CSN = 0; // CSN low, initialize SPI communication... | |
| SPI_RW(reg); // Select register to read from.. | |
| reg_val = SPI_RW(0); // ..then read registervalue | |
| CSN = 1; // CSN high, terminate SPI communication | |
| return(reg_val); // return register value | |
| } | |
| /**************************************************/ | |
| /************************************************** | |
| Function: SPI_Read_Buf(); | |
| Description: | |
| Reads 'bytes' #of bytes from register 'reg' | |
| Typically used to read RX payload, Rx/Tx address | |
| /**************************************************/ | |
| uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes) | |
| { | |
| uchar status,byte_ctr; | |
| CSN = 0; // Set CSN low, init SPI tranaction | |
| status = SPI_RW(reg); // Select register to write to and read status byte | |
| for(byte_ctr=0;byte_ctr<bytes;byte_ctr++) | |
| pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01 | |
| CSN = 1; // Set CSN high again | |
| return(status); // return nRF24L01 status byte | |
| } | |
| /**************************************************/ | |
| /************************************************** | |
| Function: SPI_Write_Buf(); | |
| Description: | |
| Writes contents of buffer '*pBuf' to nRF24L01 | |
| Typically used to write TX payload, Rx/Tx address | |
| /**************************************************/ | |
| uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes) | |
| { | |
| uchar status,byte_ctr; | |
| CSN = 0; // Set CSN low, init SPI tranaction | |
| status = SPI_RW(reg); // Select register to write to and read status byte | |
| for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf) | |
| SPI_RW(*pBuf++); | |
| CSN = 1; // Set CSN high again | |
| return(status); // return nRF24L01 status byte | |
| } | |
| /**************************************************/ | |
| void startWrite() { | |
| // Transmitter power-up | |
| uchar config = SPI_Read(0x00); | |
| // write to CONFIG = 0b0010 0000 = 0x20 | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x00, ( config | PWR_UP & ~PRIM_RX )); | |
| config = SPI_Read(0x00); | |
| P3 = config; | |
| delay_ms(150); | |
| // Send the payload | |
| // write_payload = 0b1010 0000 = 0xA0 | |
| SPI_Write_Buf( 0xA0, tx_buf, TX_PLOAD_WIDTH); | |
| P3 = 0xff; | |
| delay_ms(3000); | |
| P3 = SPI_Read(0x17); | |
| delay_ms(3000); | |
| P3 = 0xff; | |
| delay_ms(150); | |
| // Allons! | |
| CE = 1; | |
| delay_ms(15); | |
| CE = 0; | |
| P3 = 0xff; | |
| delay_ms(3000); | |
| P3 = SPI_Read(0x17); | |
| delay_ms(3000); | |
| P3 = 0xff; | |
| } | |
| void blink() { | |
| for (i = 0; i < 4; i++) { | |
| P3 = 0x00; | |
| delay_ms(250); | |
| P3 = 0xFF; | |
| delay_ms(250); | |
| } | |
| } | |
| void stopWrite() { | |
| uchar config = SPI_Read(0x00); | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x00, ( config & ~PWR_UP )); | |
| //P3 = 0; | |
| //delay_ms(3000); | |
| //blink(); | |
| //P3 = config; | |
| //delay_ms(3000); | |
| //P3 = SPI_Read(0x00); | |
| //delay_ms(3000); | |
| //P3 = SPI_Read(0x08); | |
| //delay_ms(3000); | |
| } | |
| void diagnostic() { | |
| blink(); | |
| for (i = 0; i < 0x17; i++) { | |
| P3 = SPI_Read(i); | |
| delay_ms(1000); | |
| } | |
| blink(); | |
| } | |
| void configure() { | |
| //diagnostic(); | |
| //while(1); | |
| //setup_retr addr = 0x04; write_setup_retr = 0x24 | |
| // ARD = 0b0101 = 0x5 | |
| // ARC = 0b1111 = 0xF | |
| // content = 0x4F | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x04, 0x5F); | |
| //pa_level | |
| // RF_SETUP_ADDR = 0x06 | |
| // RF_PWR_MAX = 0b0000 0110 = 0x06 | |
| // DEFAULT = 0x06 | |
| //data_rate | |
| // RF_SETUP_ADDR = 0x06 | |
| // RF_DR_LOW = 0b0010 0000 = 0x20 | |
| // RF_DR_HIGH = 0b0000 1000 = 0x80 | |
| // 1MBPS -> RF_DR_LOW = 0; RF_DR_LOW = 0; | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x06, 0x06); | |
| //crc_lengh | |
| // CONFIG_ADDR = 0x00; | |
| // CRC_16_BITS = 0b0000 0100 = 0x04 | |
| // EN_CRC = 0b0000 1000 = 0x08 | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x00, 0x04 | 0x08); | |
| //DYNPD | |
| // DYNPD_ADDR = 0x1C; | |
| // DISABLE_ALL = 0x00; // DEFAULT | |
| // FEATURE_ADDR = 0x1D; | |
| // EN_DPL = 0b0000 0100 = 0x04 // DEFAULT == 0 | |
| // DO NOTHING | |
| //STATUS | |
| // STATUS_ADDR = 0x07; | |
| // RX_DR = 0b0100 0000 = 0x40; // clear ready RX FIFO interrupt | |
| // TX_DS = 0b0010 0000 = 0x20; // clear sent TX FIFO interrupt | |
| // MAX_RT = 0b0000 1000 = 0x08; // clear max num of TX retransmits bit | |
| // DO NOTHING | |
| //CHANNEL | |
| // RF_CH_ADDR = 0x05; | |
| // value = 10; | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x05, 10); | |
| // RX_PW_P0 = 0x11; | |
| // value = n_of_bytes in payload; | |
| SPI_RW_Reg(WRITE_REG_CMD | 0x11, TX_PLOAD_WIDTH); | |
| //flush_rx = 0b1110 0001 = 0xe1 | |
| //flush_tx = 0b1110 0010 = 0xe2 | |
| // | |
| SPI_RW_Reg(0xE1, 0); | |
| SPI_RW_Reg(0xE2, 0); | |
| //RX_ADDR_P0 == 0x0A | |
| //TX_ADDR == 0x10 | |
| SPI_Write_Buf(WRITE_REG_CMD | 0x0A, TX_ADDRESS, TX_ADR_WIDTH); | |
| SPI_Write_Buf(WRITE_REG_CMD | 0x10, TX_ADDRESS, TX_ADR_WIDTH); | |
| //delay_ms(1000); | |
| //diagnostic(); | |
| //delay_ms(1000); | |
| } | |
| /************************************************** | |
| Function: main(); | |
| Description: | |
| control all subprogrammes; | |
| /**************************************************/ | |
| void main(void) | |
| { | |
| init_io(); // Initialize IO port | |
| configure(); | |
| delay_ms(1000); | |
| diagnostic(); | |
| delay_ms(1000); | |
| while(1) | |
| { | |
| startWrite(); | |
| stopWrite(); | |
| delay_ms(500); | |
| } | |
| } | |
| /**************************************************/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <SPI.h> | |
| #include <printf.h> | |
| #include <RF24_config.h> | |
| #include <nRF24L01.h> | |
| #include <RF24.h> | |
| #define SERIAL_DEBUG | |
| //#define IF_SERIAL_DEBUG(x) ({x;}) | |
| int msg[3]; | |
| RF24 radio(7,8); | |
| const uint64_t pipe = 0xc2c2c2c2c2LL; | |
| //const uint64_t pipe = 0xe7e7e7e7e7LL; | |
| //const uint64_t pipe = 0x3443101001LL; | |
| //const uint64_t pipe = 0x0110104334LL; | |
| //0x34,0x43,0x10,0x10,0x01 | |
| // | |
| void setup(void){ | |
| Serial.begin(115200); | |
| printf_begin(); | |
| radio.begin(); | |
| radio.setChannel(10); | |
| radio.setPayloadSize(3); | |
| radio.setCRCLength(RF24_CRC_16); | |
| radio.setDataRate(RF24_1MBPS); | |
| radio.setPALevel(RF24_PA_MAX); | |
| radio.openReadingPipe(1,pipe); | |
| radio.startListening(); | |
| radio.printDetails(); | |
| } | |
| void loop(void) { | |
| if (radio.available()) { | |
| radio.read(msg, 3); | |
| Serial.println(msg[0]); | |
| } | |
| else { | |
| Serial.println("No radio available"); | |
| //radio.printDetails(); | |
| } | |
| delay(1000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment