Hi everyone,
I’m using brennen’s C librarys to make a communication work between two PIC 18F4620. I get some problems when I run my code: it doesn’t work.
I wrote the receive code and the transmit code, the way it is told in the tutuorial1 . I’m using Microchip C18 compiler and my code is building OK. At the end of the post I show my receive code.
I configured the pins as followed and I think maybe I made something wrong???( the Chip Select CSN has to be on RA3 to match my hardware)
#define nrf24l01_CE_IOREGISTER TRISA
#define nrf24l01_CE_PINMASK PORTAbits.RA1
#define nrf24l01_CSN_IOREGISTER TRISA
#define nrf24l01_CSN_PINMASK PORTAbits.RA3
#define nrf24l01_IRQ_IOREGISTER TRISA
#define nrf24l01_IRQ_PINMASK PORTAbits.RA2
I wrote the SPI function and the delays functions as followed:
unsigned char spi_send_read_byte(unsigned char byte)
{
volatile char dummy;
char x;
dummy = SSPBUF; // force clearance of buffer full flag
SSPBUF = byte;
while (!SSPSTATbits.BF)
;
x = ReadSPI();
}
void delay_us(unsigned int microseconds)
{
int i;
for(i=0 ; i<= microseconds;i++)
{
Delay1TCY();
Delay1TCY();
}
}
When I run the program it is stuck in the spi_send_read_byte() function at the following line:while (!SSPSTATbits.BF). It doesn’t pass the nrf_initialize_debug() function.
I think maybe I didn’t initialise the SPI module of my MCU?
If anyone could help me it would be nice
Thank you very much
Gregory
Here is the main code:
#include "nrf24l01.h"
#include <p18f4620.h>
#include <usart.h>
#include <stdio.h>
void main (void)
{
unsigned char byte = 0x00;
unsigned char status = 0x00;
//Initialise l'Horloge à 8 MHZ
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF0=1;
OpenUSART(
USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,
51 );//9600 bauds COM 4
printf("Debut de la Routine de reception\r\n");
nrf24l01_initialize_debug(true,0x01,false);
printf("En attente de reception");
while(! (nrf24l01_irq_pin_active()&&nrf24l01_irq_rx_dr_active()));
status = nrf24l01_read_rx_payload(&byte,0x01);
printf("Octet reçus, enregistrement dans l'EEPROM");
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
EEADR = 0x00;
EEDATA = byte;
EECON2 = 0x55;
EECON2 = 0xaa;
EECON1bits.WR = 1; //detail Nombre de Cycles ?
while (!PIR2bits.EEIF);
PIR2bits.EEIF = 0;
printf("Enregistrement effectué, voir à l'adresse 0");
nrf24l01_irq_clear_all();
while(1);
}