Jump to content


Photo

PIC18F4520 + NRF24L01


  • Please log in to reply
No replies to this topic

#1 OFFLINE   bufferzone

bufferzone

    Nou venit

  • Membri
  • Pip
  • 20 posts

Posted 20 December 2016 - 08:35 AM

Salut,

Incerc sa transmit date de la Pic la un Raspberry Pi 3 si nu reusesc sub nici o forma. Va rog spuneti-mi daca codul sursa pentru pic este ok:

/**************************************************************************************************
* nRF24L01+ connections
**************************************************************************************************/
sfr sbit RF_IRQ_TRIS at TRISB7_bit;
sfr sbit RF_CS_TRIS  at TRISE0_bit;
sfr sbit RF_CE_TRIS  at TRISE1_bit;
sfr sbit SCK_TRIS    at TRISC3_bit;
sfr sbit SDI_TRIS    at TRISC4_bit;
sfr sbit SDO_TRIS    at TRISC5_bit;

sfr sbit IRQ_pin     at RB7_bit;
sfr sbit SS_pin      at RE0_bit;
sfr sbit CE_pin      at RE1_bit;
sfr sbit SCK_pin     at RC3_bit;
sfr sbit MISO_pin    at RC4_bit; //SDI Lectura Entrada
sfr sbit MOSI_pin    at RC5_bit; //SDO Escritura Salida
sbit Led at RC1_bit;

#define R_REGISTER_cmd                0x00
#define W_REGISTER_cmd                0x20
#define R_RX_PL_WID_cmd               0x60
#define R_RX_PAYLOAD_cmd              0x61
#define W_TX_PAYLOAD_cmd              0xA0
#define W_ACK_PAYLOAD_cmd             0xA8
#define W_TX_PAYLOAD_NO_ACK_cmd       0xB0
#define FLUSH_TX_cmd                  0xE1
#define FLUSH_RX_cmd                  0xE2
#define REUSE_TX_PL_cmd                      0xE3
#define NOP_cmd                       0xFF

#define CONFIG_reg                         0x00
#define EN_AA_reg                     0x01
#define EN_RXADDR_reg                 0x02
#define SETUP_AW_reg                      0x03
#define SETUP_RETR_reg                0x04
#define RF_CH_reg                      0x05
#define RF_SETUP_reg                  0x06
#define STATUS_reg                    0x07
#define OBSERVE_TX_reg                0x08
#define RPD_reg                       0x09
#define RX_ADDR_P0_reg                0x0A
#define RX_ADDR_P1_reg                0x0B
#define RX_ADDR_P2_reg                0x0C
#define RX_ADDR_P3_reg                0x0D
#define RX_ADDR_P4_reg                0x0E
#define RX_ADDR_P5_reg                0x0F
#define TX_ADDR_reg                   0x10
#define RX_PW_P0_reg                  0x11
#define RX_PW_P1_reg                  0x12
#define RX_PW_P2_reg                  0x13
#define RX_PW_P3_reg                  0x14
#define RX_PW_P4_reg                  0x15
#define RX_PW_P5_reg                  0x16
#define FIFO_STATUS_reg               0x17
#define DYNPD_reg                     0x1C
#define FEATURE_reg                   0x1D


unsigned char x = 0;


unsigned char nRF24L01_read()
{
  unsigned char s = 0;
  unsigned char msg = 0;

  for(s = 0; s < 8; s++)
  {
      msg <<= 1;
      SCK_pin = 1;
      delay_us(8);
      if(MISO_pin != 0)
      {
        msg |= 1;
      }
      SCK_pin = 0;
      delay_us(8);
  }

  return msg;
}


void nRF24L01_write(unsigned char d)
{
  unsigned char s = 0;

  for(s = 0; s < 8; s++)
  {
    if((d & 0x80) != 0)
    {
      MOSI_pin = 1;
    }
    else
    {
      MOSI_pin = 0;
    }
    d <<= 1;
    SCK_pin = 1;
    delay_us(8);
    SCK_pin = 0;
    delay_us(8);
  }
}


void register_write(unsigned char reg, unsigned char value)
{
  SS_pin = 0;
  nRF24L01_write((reg | W_REGISTER_cmd));
  nRF24L01_write(value);
  SS_pin = 1;
  delay_us(8);
}


void write_command(unsigned char cmd)
{
  SS_pin = 0;
  nRF24L01_write(cmd);
  SS_pin = 1;
  delay_us(8);
}


unsigned char register_read(unsigned char reg)
{
  unsigned char value = 0;

  SS_pin = 0;
  nRF24L01_write((reg | R_REGISTER_cmd));
  value = nRF24L01_read();
  SS_pin = 1;
  delay_us(8);

  return value;
}


void set_TX_RX_address(unsigned char *addr, unsigned char bytes, unsigned char reg)
{
  unsigned char n = 0;

  SS_pin = 0;
  nRF24L01_write((reg | W_REGISTER_cmd));
  for(n = 0; n < bytes; n++)
  {
    nRF24L01_write(addr[n]);
  }
  SS_pin = 1;
  delay_us(8);
}


void flush_TX_RX()
{
  register_write(STATUS_reg, 0x70);
  write_command(FLUSH_TX_cmd);
  write_command(FLUSH_RX_cmd);
}


void send_data(unsigned char bytes, unsigned char *value)
{
  unsigned char s = 0;

  flush_TX_RX();
  register_write(CONFIG_reg, 0x3A);

  SS_pin = 0;
  nRF24L01_write(W_TX_PAYLOAD_cmd);
  for(s = 0; s < bytes; s++)
  {
    nRF24L01_write(value[s]);
  }
  SS_pin = 1;
  delay_us(8);

  CE_pin = 1;
  delay_us(60);
  CE_pin = 0;

  register_write(CONFIG_reg, 0x38); //En modo RX se quita
}

void receive_data(unsigned char bytes, unsigned char *value)
{
  unsigned char s = 0;

  SS_pin = 0;
  nRF24L01_write(R_RX_PAYLOAD_cmd);
  for (s = 0; s < bytes; s++)
  {
    value[s] = nRF24L01_read();
  }
  SS_pin = 1;
  delay_us(8);
}


void nrF24L01_init_TX()
{
  unsigned char address[5] = {0x99, 0x99, 0x99, 0x99, 0x99};

  CE_pin = 0;

  register_write(SETUP_RETR_reg, 0x00);
  register_write(SETUP_AW_reg, 0x03);
  register_write(RF_SETUP_reg, 0x0E);
  register_write(RF_CH_reg, 0x09);
  register_write(EN_AA_reg, 0x00);
  register_write(CONFIG_reg, 0x38);
  set_TX_RX_address(address, 5, TX_ADDR_reg);
  set_TX_RX_address(address, 5, RX_ADDR_P0_reg);
  flush_TX_RX();

  CE_pin = 1;
}

void nrF24L01_init_RX()
{
  unsigned char address[5] = {0x99, 0x99, 0x99, 0x99, 0x99};

  CE_pin = 0;

  register_write(CONFIG_reg, 0x38);
  register_write(SETUP_RETR_reg, 0x00);
  register_write(SETUP_AW_reg, 0x03);
  register_write(RF_SETUP_reg, 0x0E);
  register_write(RF_CH_reg, 0x09);
  register_write(EN_AA_reg, 0x00);
  register_write(RX_PW_P0_reg, 0x01);
  register_write(CONFIG_reg, 0x3B);
  set_TX_RX_address(address, 5, TX_ADDR_reg);
  set_TX_RX_address(address, 5, RX_ADDR_P0_reg);
  flush_TX_RX();

  CE_pin = 1;
}

unsigned char get_Status_Reg()
{
  return register_read(STATUS_reg);
}


  unsigned char val = 0;

void main(){

delay_ms(1000);
//Enntradas con 1 y salidas 0
 TRISA     =  0x00;
 TRISB     =  0x00;
 TRISC     =  0x00;
 ADCON1 = 0x0F;
 PORTB = 0X00;
 PORTC = 0x00;

 RF_IRQ_TRIS = 1;
 RF_CS_TRIS = 0;
 RF_CE_TRIS = 0;
 SCK_TRIS = 0;
 SDI_TRIS = 1;  //MISO
 SDO_TRIS = 0;  //MOSI

 CE_pin = 0;
 SS_pin = 0;
 SCK_pin = 0;
 MOSI_pin = 0;

 delay_ms(1000);

 nrF24L01_init_TX();

 delay_ms(900);


 while(1){
  send_data(1, &x);
  porta = x;
  x++;
  delay_ms(1000);
  Led = 1;
  if (x == 6){
   flush_TX_RX();
   nrF24L01_init_RX();
   delay_ms(900);
   porta = 0;
   x = 0;
   while(1){
    if(get_Status_Reg() == 0x40)
    {
      receive_data(1, &x);
      porta = x;
    }
   }
  }
 }
}

Iar acesta este codul sursa pentru Raspberry Pi:

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0x99, 0x99, 0x99, 0x99, 0x99], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x05)

radio.setDataRate(NRF24.BR_2MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openReadingPipe(0, pipes[1])
radio.printDetails()

radio.startListening()

while(1):
    ackPL = [1]
    while not radio.available(0):
        time.sleep(1 / 100)
    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received: {}".format(receivedMessage))

    print("Translating the receivedMessage into unicode characters")
    string = ""
    for n in receivedMessage:
        # Decode into standard unicode set
        if (n &gt;= 32 and n &lt;= 126):
            string += chr(n)
    print(string)
    radio.writeAckPayload(1, ackPL, len(ackPL))
    print("Loaded payload reply of {}".format(ackPL))

Multumesc mult pentru ajutor!



Cu_reclama

Cu_reclama
  • Membri




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users