0
\$\begingroup\$

I'm trying to use the nRF24L01 with a pic18f2550 using microc compiler, I started from an example code provided by Nordic for 18F45K22 (which can be found here example code).

I've changed pin definitions accordingly,but I don't understand what "ANSELA =0 " mean ?

Here is my code for now :

char InData[25]; // incoming data from RF
char testRcv;
char PayLoadBuff[25];
char Code_[32];

//sbit Irq_pin   at PORTB.B0; sfr;
sbit Irq_pin   at PORTB.B2; sfr;//use interrupt 2
sbit Mosi_pin  at LATC.B7;  sfr;//same
sbit Ce_pin    at LATA.B4;  sfr;//same
//sbit Sclk_pin  at LATA.B3;  sfr;
sbit Sclk_pin  at LATB.B1;  sfr;
//sbit Csn_pin   at LATA.B2;  sfr;
sbit Csn_pin   at LATA.B5;  sfr;
//sbit Miso_pin  at PORTC.B6; sfr;
sbit Miso_pin  at PORTB.B0; sfr;

//sbit Irq_tris  at TRISB.B0; sfr;
sbit Irq_tris  at TRISB.B2; sfr;//use interrupt 2
sbit Mosi_tris at TRISC.B7; sfr;//same
sbit Ce_tris   at TRISA.B4; sfr;//same
//sbit Sclk_tris at TRISA.B3; sfr;
sbit Sclk_tris at TRISB.B1; sfr;
//sbit Csn_tris  at TRISA.B2; sfr;
sbit Csn_tris  at TRISA.B5; sfr;
//sbit Miso_tris at TRISC.B6; sfr;
sbit Miso_tris at TRISB.B0; sfr;

void interrupt(){
  // in case you use interrupts, here is the moment when Rx has got the data from Tx
  //**************** Rx interrupt ***************
  //if (INTCON.INT0IF == 1){
  if (INTCON.INT2IF == 1){

      //INTCON.INT2IF = 0; // clear interrupt flag on RB0 (IRQ pin)
      INTCON.INT2IF = 0; // clear interrupt flag on RB2 (IRQ pin)
  }
  //*************************************************
}

void PreparePayload(){
char i;
  // prepare payload to be sent together with the ACK (see datasheet for NRF24L01+)
  for (i = 0; i < 20; i++)
    PayLoadBuff[i] = i;
  NRF24L01P_Write_Ack_Payload(PayLoadBuff, 0); // store payload to transmit buffer
}

void Init(){
  ANSELA = 0;           // all pins digital
  ANSELB = 0;
  ANSELC = 0;
  ANSELD = 0;

  //LATD   = 1;
  //TRISD  = 0;           // we will use PORTD for indicators
  TRISB.B3 = 0;           //B3 will be used as indicator
  LATB.B3  = 1;
  TRISB.B4 = 0;           //B4 will be used as indicator
  LATB.B4  = 1;

  INTCON = 0;           // no interrupts

  //INTCON.INT0IE   = 1;   // allow interrupt on INTO (IRQ_PIN)
  INTCON.INT2IE   = 1;   // allow interrupt on INT2 (IRQ_PIN)
  //INTCON2.INTEDG0 = 0;  // falling edge on INT0 causes the interrupt
  INTCON2.INTEDG2 = 0;  // falling edge on INT causes the interrupt

  RBPU_bit = 0;         // enable weak pull up on PORTB

  memset(&InData, 0, 21);// clear the array

  // address code, must be the same on Rx and Tx side
  Code_[10] = 0xAA;
  Code_[11] = 0xAA;
  Code_[12] = 0xAA;
  Code_[13] = 0xAA;
  Code_[14] = 0xAA;

  INTCON.GIE = 1;      // enable all interrupts, if you want them
}

char DetectTx(){
char passcnt;
char result;

  passcnt = 0;
  result  = 0;
  NRF24L01P_PowerUp();                  // start the NRF chip
  NRF24L01P_Rf_Channel = 20;           // set the radio channel, from 0..124
  NRF24L01P_init(5);                    // initialize RF module with the highest power
  NRF24L01P_Setup_Receiver(Code_);      // configure receiver
  memset(&InData, 0, 21);
  testRcv = NRF24L01P_Receive(InData); // procitaj ako je ista je stiglo na RF- brisi buffer
  PreparePayload();
  NRF24L01P_ClearIRQ();

  while (1){
      passcnt = 0;                     // try 50 times before giving up
      while (passcnt < 50){
          if (NRF24L01P_Has_Data()){ // is there any data received?
              testRcv = NRF24L01P_Receive(InData); // read the data and clear Rx buffer
              PreparePayload();         // prepare payload for the next session
              passcnt = 0;
              LATB.B4 = ~PORTB.B4;                // show activity when data arrives
              result = 1;
          }
        passcnt++;
        Delay_100ms();
      }

      NRF24L01P_PowerDown();            // shut down NRF24L01+
      Delay_100ms();
      NRF24L01P_Rf_Channel = 20;       // working channel
      NRF24L01P_Init(5);                // Initialize RF module
      NRF24L01P_Setup_Receiver(Code_);  // Reinitialize receiver
      memset(&InData, 0, 21);           // Clear inoming buffer
      LATB.B3 = PORTB.B3 ^ 1;
      PreparePayload();                 // prepare payload for the next session
      if (testRcv){
          // the data has been received
          result = 1;
      }
  }
}

void main(){
  Init();

  while (1){
      if (DetectTx()){
          // Tx is here, the data is received, it is stored in InData
          memset(&InData, 0, 21); // clear indata
      }
  }
}

Also, if you notice another error, I'll appreciate your advice

Edit:

Okey, after a bit of research I've found that ANSELx registers are for analog input control, defining if the input should be considered as analog if the bit is set in the corresponding ANSELx register, but still I don't know the equivalent in 18f2550 which doesn't have the ANSELx registers

Here is a diagram showing IO port operation on P18F45K22 IO port operation

\$\endgroup\$
  • 1
    \$\begingroup\$ Look for a definition in the header file \$\endgroup\$ – Voltage Spike Apr 21 '17 at 19:46
  • \$\begingroup\$ @laptop2d am using microc, that's why there is no include library for the standard libraries. Any ideas where I might find library definitions for pins-functions ? \$\endgroup\$ – Ernest Dujo Apr 21 '17 at 19:52
  • \$\begingroup\$ Okey, after a bit of research I've found that ANSELx registers are for analog input control, defining if the input should be considered as analog if the bit is set in the corresponding ANSELx register, but still I don't know the equivalent in 18f2550 which doesn't have the ANSELx registers \$\endgroup\$ – Ernest Dujo Apr 21 '17 at 20:29
  • \$\begingroup\$ Don't worry about equivalent ANSELx registers, worry about how you set the analog inputs to digital. But, first look at the h file and figure out where all the definitions for your chip are loaded. \$\endgroup\$ – Scott Seidman Apr 21 '17 at 20:32
  • 1
    \$\begingroup\$ Since you seem to be using only Port B, you should look at ADCON1 register to set analog pins to digital and also the PBADEN config bit. I dont know about MikroC but XC8 comes with a hardware SPI library that's easy to work with. Personally, I'd prefer to port the relevant parts of the Arduino tmrh20 library rather than this mess. \$\endgroup\$ – TisteAndii Apr 21 '17 at 23:23
1
\$\begingroup\$

ANSELA is used to change a port/pin to analogue if you wish to use the ADC. Be aware that not all ports have ADC's and they will not be the same from one PIC to another. Capabilities of each pin for PORTA for examples is listed in the table on page 112 of the datasheet. You will only find ANSELx on the pins/ports that have them available.

It would probably be better to to rewrite the code to avoid mistakes and problems.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.