Hello,
I manage to get Brennen’s library working with 2 PIC18LF2550 and nRF24L01+ modules. The compiler is C18.
The first problem I am facing with C18 is that I must use software UART with SPI as HW UART uses one pin that IS used for spi:
http://circuits.datasheetdir.com/19/PIC … pinout.jpg
As you can see pin 18 is both for RX and SDO.
Here is the HW configuration
CSN → A5
CE → A2
IRQ → C2
SCK → B1
SDI → B0
SDO → C7
LED0 → A0
LED1 → A1
I changed RX and TX pin from readuart.asm, writeuart.asm and openuart.asm from C:\Program Files (x86)\Microchip\mplabc18\v3.41\src\pmc_common\SW_UART
like this :
SWTXD equ PORTC ; Transmit pin port and pin
SWTXDpin equ 6
TRIS_SWTXD equ TRISC ; Transmit pin tris and pin
SWRXD equ PORTC ; Receive pin port and pin
SWRXDpin equ 5
TRIS_SWRXD equ TRISC ; Receive pin tris and pin
so that My UART TX and RX will be on pin C6 and C5.
Now I am not sure about how to configure uart in my main file. I changed “#include <usart.h>” in “#include <sw_uart.h>” here it is :
#include <p18F2550.H>
//#include <STDLIB.H>
#include <stdio.h>
#include <sw_uart.h>
#include <spi.h>
#include "delays.h"
#include "nrf24l01.h"
void Initialize(void);
void InitializeIO(void);
void Initialize(void)
{
InitializeIO(); //set up IO (directions and functions)
// OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 51); //open UART with spbrg = 8Mhz/(16*9600)-1
OpenUART();
OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1
nrf24l01_initialize_debug(false, 1, false); //initialize the 24L01 to the debug configuration as TX, 1 data byte, and auto-ack disabled
}
//initialize IO pins
void InitializeIO(void)
{
ADCON1 = 0x7; //disable AD converter functionality on PORTA
TRISAbits.TRISA0 = 0; //make PORTA.0 an output to control LED
PORTAbits.RA0 = 1; //turn on LED0
TRISAbits.TRISA1 = 0; //make PORTA.1 an output to control LED
PORTAbits.RA1 = 1; //turn on LED1
TRISCbits.TRISC2 = 1; //make sure that PORTC.2 INPUT since it is IRQ pin
TRISAbits.TRISA2 = 0; //make sure that PORTA.2 OUTPUT since it is CE pin
TRISCbits.TRISC7 = 0; //make sure that PORTC.7 OUTPUT since it is SDO pin
TRISCbits.TRISC6 = 0; //make sure that PORTC.6 OUTPUT since it is TX pin UART
TRISCbits.TRISC5 = 1; //make sure that PORTC.5 INPUT since it is RX pin UART
TRISAbits.TRISA5 = 0; //make sure that PORT5.5 OUTPUT since it is CSN pin
PORTAbits.RA5 = 1; //set CSN bit
TRISBbits.TRISB1 = 0; //make sure that PORTB.1 OUTPUT since it is SCK pin
TRISBbits.TRISB0 = 1; //make sure that PORTB.0 INPUT since it is SDI pin
//TRISC = 0x91; //make CSN, CE, SCK, MOSI (SDO), and TX outputs
}
void main (void){
unsigned char data; //register to hold letter sent and received
unsigned int count; //counter for for loop
while(1){
//data = ReadUSART(); //get data from UART
data=ReadUART(); //for software UART
//delay_us(100);
nrf24l01_write_tx_payload(&data, 1, true); //transmit received char over RF
//wait until the packet has been sent or the maximum number of retries has been reached
while(!(nrf24l01_irq_pin_active() && nrf24l01_irq_tx_ds_active()));
nrf24l01_irq_clear_all(); //clear all interrupts in the 24L01
nrf24l01_set_as_rx(true); //change the device to an RX to get the character back from the other 24L01
//wait a while to see if we get the data back (change the loop maximum and the lower if
// argument (should be loop maximum - 1) to lengthen or shorten this time frame
for(count = 0; count < 20000; count++)
{
//check to see if the data has been received. if so, get the data and exit the loop.
// if the loop is at its last count, assume the packet has been lost and set the data
// to go to the UART to "?". If neither of these is true, keep looping.
if((nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active()))
{
nrf24l01_read_rx_payload(&data, 1); //get the payload into data
break;
}
//if loop is on its last iteration, assume packet has been lost.
if(count == 19999)
data = '?';
}
nrf24l01_irq_clear_all(); //clear interrupts again
printf("%c", data); //print the received data (or ? if none) to the screen
DelayUS(130); //wait for receiver to come from standby to RX
nrf24l01_set_as_tx(); //resume normal operation as a TX
}
}
This is only the Master’s code, I haven’t done the slave one yet. Should I include “uart0.c” ?
I have one error when I compile :
I did a research in my files and there is no "DelayRXHalfBitUART".. Do you see anything wrong in my code, do you have any idea ?Error - could not find definition of symbol ‘DelayRXHalfBitUART’ in file ‘openuart.O’
Thank you