Bonjour,
J'utilise un code pour le module de communiction par wifi du nrf24l01+ je voudrais que vous le corrigiez si il y a des erreur. Le but est d'allumer une led si la donnée est reçu sinon elle reste étiente. Merci d'avance.
Code:#include <pic18.h> #include <stdio.h> #include "prog_principal.h" // indiquer ici le même nom de fichier .h #include "lcd.h" #include "delay.h" #define LED1 RB7 // LED par LED #define LED2 RB6 #define LED3 RB5 #define LED4 RB4 #define LED5 RB3 #define SPI_IRQ RB2 #define SPI_CE RB0 #define SPI_CSN RB1 #define RF_DATA_RATE_250KBPS #define RF_CHANNEL 0x05 #define RF_PAYLOAD 0x01 #define RADIO_ADDRESS_1 0x18 #define RADIO_ADDRESS_2 0x00 #define RADIO_ADDRESS_3 0x29 #define RADIO_ADDRESS_4 0x35 #define RADIO_ADDRESS_5 0x01 void main () { unsigned char i,j; unsigned char TX_Data [RF_PAYLOAD]; unsigned char RX_Data [RF_PAYLOAD]; init(); while(1) if(RA1==1) { while(1) {RB6=1;delay_ms(200);RB6=0;delay_ms(200); if(WCOL==1){RB4=1;} if(SPI_IRQ==0) { RB7=1; read_RX(RX_Data); if(RX_Data[1]==0x31) { LED3=1; } else { LED3=0; } } } } else { for (i=0; i< RF_PAYLOAD; i++) TX_Data[i] = i+ 0x30; while(1) { RB3=1; transmit_data(TX_Data); delay_ms(1); if(SPI_IRQ==0){RB7=1;} if( WCOL==1){RB6=1;} RB3=0; delay_ms(1); } } } } void init(void) { ADCON1=0x0f; // Pour travailler entièrement en numérique TRISA=0b11101111; // TRISA<5> 1=slave 0=master PORTA=0; TRISB=0b00000100; // Port B en sortie PORTB=0; TRISC=0b00010000;// TRISC<3> 1 = slave, 0 = master PORTC=0; SSPSTAT=0b00000000;//master mode SSPCON1=0b00100001; //mastermode //SSPSTAT=0b00000000; //slave mode //SSPCON1=0b00100100;//slave mode //configure_RX(); configure_transmitter(); lcd_init(); lcd_clear(); } unsigned char SPI_Write(unsigned char data) { // Activate the SS SPI Select pin //PORTAbits.RA5 = 0; // Start Data transmission SSPBUF = data; delay_ms(1); // Wait for Data Transmit/Receipt complete while(!SSPSTATbits.BF); // CS pin is not active } void configure_transmitter(void) { int i, j; SPI_CE = 0; SPI_CSN = 0; // PTX, CRC enabled, mask a couple of ints SPI_Write(0x20); SPI_Write(0x38); SPI_CSN = 1; SPI_CSN = 0; //auto retransmit off SPI_Write(0x24); SPI_Write(0x00); SPI_CSN = 1; SPI_CSN = 0; //address width = 5 SPI_Write(0x23); SPI_Write(0x03); SPI_CSN = 1; SPI_CSN = 0; //data rate = SPI_Write(0x26); #if defined ( RF_DATA_RATE_250KBPS ) SPI_Write(0x27); // 0x07 = 1MBPS - 0x27 = 250Kbps #elif defined ( RF_DATA_RATE_1MBPS ) SPI_Write(0x07); #endif SPI_CSN = 1; SPI_CSN = 0; //set channel SPI_Write(0x25); SPI_Write(RF_CHANNEL); SPI_CSN = 1; SPI_CSN = 0; //set address SPI_Write(0x30); SPI_Write(RADIO_ADDRESS_1); SPI_Write(RADIO_ADDRESS_2); SPI_Write(RADIO_ADDRESS_3); SPI_Write(RADIO_ADDRESS_4); SPI_Write(RADIO_ADDRESS_5); // for (j = 0; j < 5; j++) // spi_Send_Read(0xE7); SPI_CSN = 1; SPI_CSN = 0; //disable auto-ack, RX mode //shouldn't have to do this, but it won't TX if you don't SPI_Write(0x21); SPI_Write(0x00); SPI_CSN = 1; }// void transmit_data( unsigned char *TX_Data ) { unsigned char i; SPI_CSN = 0; //clear previous ints SPI_Write(0x27); SPI_Write(0x7E); SPI_CSN = 1; SPI_CSN = 0; //PWR_UP = 1 SPI_Write(0x20); SPI_Write(0x3A); SPI_CSN = 1; SPI_CSN = 0; //clear TX fifo //the data sheet says that this is supposed to come up 0 after POR, but that doesn't seem to be the case SPI_Write(0xE1); SPI_CSN = 1; SPI_CSN = 0; //fill byte payload SPI_Write(0xA0); SPI_Write(TX_Data); SPI_CSN = 1; //Pulse CE to start transmission SPI_CE = 1; delay_ms(10); SPI_CE = 0; }// void configure_RX(void) { unsigned char i, j; SPI_CSN = 0; SPI_CE = 0; //PRX, CRC enabled SPI_Write(0x20); SPI_Write(0x39); SPI_CSN = 1; SPI_CSN = 0; //disable auto-ack for all channels SPI_Write(0x21); SPI_Write(0x00); SPI_CSN = 1; SPI_CSN = 0; //address width = 5 bytes SPI_Write(0x23); SPI_Write(0x03); SPI_CSN = 1; SPI_CSN = 0; //data rate = SPI_Write(0x26); #if defined ( RF_DATA_RATE_250KBPS ) SPI_Write(0x27); // 0x07 = 1MBPS - 0x27 = 250Kbps #elif defined ( RF_DATA_RATE_1MBPS ) SPI_Write(0x07); #else #error "Definir RF_DATA_RATE" #endif SPI_CSN = 1; SPI_CSN = 0; //4 byte payload SPI_Write(0x31); SPI_Write(RF_PAYLOAD); SPI_CSN = 1; SPI_CSN = 0; //set channel SPI_Write(0x25); SPI_Write(RF_CHANNEL); SPI_CSN = 1; SPI_CSN = 0; //set address SPI_Write(0x30); SPI_Write(RADIO_ADDRESS_1); SPI_Write(RADIO_ADDRESS_2); SPI_Write(RADIO_ADDRESS_3); SPI_Write(RADIO_ADDRESS_4); SPI_Write(RADIO_ADDRESS_5); // for (j = 0; j < 5; j++) // spi_Send_Read(0xE7); SPI_CSN = 1; SPI_CSN = 0; //PWR_UP = 1 SPI_Write(0x20); SPI_Write(0x3B); SPI_CSN = 1; SPI_CE = 1; }// void read_RX(unsigned char *RX_Data) { unsigned char i, j; // flush data buffer for (i = 0; i < RF_PAYLOAD; i++) RX_Data[i] = 0; // Read RX payload e coloca no buffer RX_Data SPI_CSN = 0; SPI_Write(0x61); for (j = 0; j < RF_PAYLOAD; j++) { RX_Data[j] = SPI_Write(0); } SPI_CSN = 1; //Flush RX FIFO SPI_CSN = 0; SPI_Write(0xE2); SPI_CSN = 1; SPI_CSN = 0; //reset int SPI_Write(0x27); SPI_Write(0x40); SPI_CSN = 1; }//
-----