博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PIC18F4520 + NRF24L01
阅读量:4686 次
发布时间:2019-06-09

本文共 10034 字,大约阅读时间需要 33 分钟。

SI SO应该对调过来用。。

TX

/*** Tx.c** Transmit test program for PIC18F4520 and nRF24L01 or nRF24L01+** Uses the Microchip C18 compiler** Based on SFE code for the CC5X compiler in 24L01demo_V01.c*/#include 
#include
#include
// Pragmas#pragma config OSC = INTIO67#pragma config PWRT = ON//#pragma config MCLRE = OFF#pragma config BOREN = OFF//function prototypesvoid init(void);void transmit_data(void);void configure_transmitter(void);unsigned char spi_Send_Read(unsigned char);unsigned char spi1_send_read_byte(unsigned char byte);void dly(unsigned int);// Defines#define SPI_SCK LATCbits.LATC3 // Clock pin, PORTC pin 3 #define SPI_SO LATCbits.LATC5 // Serial output pin, PORTC pin 5 #define SPI_SI PORTCbits.RC4 // Serial input pin, PORTC pin 4 #define SPI_CSN LATCbits.LATC2 // CSN output pin, PORTC pin 2#define SPI_CE LATCbits.LATC1 // CE output pin, PORTC pin 1#define SPI_IRQ PORTBbits.RB0 // IRQ input pin, PORTB pin 0#define SPI_SCALE 4 // postscaling of signal #define LED LATDbits.LATD1#define PB PORTAbits.RA1// Macros#define nop() _asm nop _endasmvoid main(void){ init(); configure_transmitter(); while (1) { transmit_data(); LED = 1; dly(63973); //200 ms delay LED = 0; dly(40000); //3.27 s delay nop(); }}void init(void){ // run internal oscillator at 8 MHz OSCCON = OSCCON | 0x70; while (OSCCONbits.IOFS == 0) ; PORTA = 0x00; PORTD = 0X00; ADCON1 = 0x0F; // set up PORTA to be digital I/Os TRISA = 0x02; // PORTA<7.2,0> outputs PORTA<1> input TRISD = 0XFD; TRISCbits.TRISC3 = 0; // SDO output TRISCbits.TRISC4 = 1; TRISCbits.TRISC5 = 0; // SCK output TRISCbits.TRISC2 = 0; // CSN output TRISCbits.TRISC1 = 0; // CE output TRISBbits.TRISB0 = 1; // IRQ input OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1 OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_256 );}void configure_transmitter(void){ unsigned char i, j, data, cmd; SPI_CE = 0; SPI_CSN = 0; // PTX, CRC enabled, mask a couple of ints spi_Send_Read(0x20); spi_Send_Read(0x38); SPI_CSN = 1; SPI_CSN = 0; //auto retransmit off spi_Send_Read(0x24); spi_Send_Read(0x00); SPI_CSN = 1; SPI_CSN = 0; //address width = 5 spi_Send_Read(0x23); spi_Send_Read(0x03); SPI_CSN = 1; SPI_CSN = 0; //data rate = 1MB spi_Send_Read(0x26); spi_Send_Read(0x07); SPI_CSN = 1; SPI_CSN = 0; //set channel 2, this is default but we did it anyway... spi_Send_Read(0x25); spi_Send_Read(0x02); SPI_CSN = 1; SPI_CSN = 0; //set address E7E7E7E7E7, also default... spi_Send_Read(0x30); 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_Send_Read(0x21); spi_Send_Read(0x00); SPI_CSN = 1;}void transmit_data(void){ unsigned char i, data, cmd; SPI_CSN = 0; //clear previous ints spi_Send_Read(0x27); spi_Send_Read(0x7E); SPI_CSN = 1; SPI_CSN = 0; //PWR_UP = 1 spi_Send_Read(0x20); spi_Send_Read(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_Send_Read(0xE1); SPI_CSN = 1; SPI_CSN = 0; //4 byte payload spi_Send_Read(0xA0); spi_Send_Read(0x34); spi_Send_Read(0x33); spi_Send_Read(0x32); spi_Send_Read(0x31); SPI_CSN = 1; //Pulse CE to start transmission SPI_CE = 1; dly(65000); //delay 69 ms SPI_CE = 0;}unsigned char spi_Send_Read(unsigned char byte){ SSPBUF = byte; while(!DataRdySPI()) ; return SSPBUF;} void dly(unsigned int c){ INTCONbits.TMR0IF = 0; WriteTimer0(c); while (INTCONbits.TMR0IF == 0) ;}

RX

/*** Rx.c** Receive test program for PIC18F4520 and nRF24L01 or nRF24L01+** Uses the Microchip C18 compiler** Based on SFE code for the CC5X compiler in 24L01demo_V01.c**** The LED is flashed five times when data are received.** The received data in the buffer may be checked using the ** debugger Watch window.*/#include 
#include
#include
// Pragmas#pragma config OSC = INTIO67#pragma config PWRT = ON#pragma config MCLRE = OFF#pragma config BOREN = OFF//function prototypesvoid init(void);void reset_RX(void);void configure_RX(void);unsigned char spi_Send_Read(unsigned char);void dly(unsigned int);// Defines#define SPI_SCK LATCbits.LATC3 // Clock pin, PORTC pin 3 #define SPI_SO LATCbits.LATC5 // Serial output pin, PORTC pin 5 #define SPI_SI PORTCbits.RC4 // Serial input pin, PORTC pin 4 #define SPI_CSN LATCbits.LATC2 // CSN output pin, PORTC pin 2#define SPI_CE LATCbits.LATC1 // CE output pin, PORTC pin 1#define SPI_IRQ PORTBbits.RB0 // IRQ input pin, PORTB pin 0#define SPI_SCALE 4 // postscaling of signal #define LED LATDbits.LATD1#define PB PORTAbits.RA1// Macros#define nop() _asm nop _endasmvoid main(void){ unsigned char i; init(); configure_RX(); while(1) { if (SPI_IRQ == 0) //wait for anything { for (i = 0; i < 5; i++) //flash LED 5 times if data received { LED = 1; dly(63973); // 200 ms delay LED = 0; dly(63973); // 196 ms } dly(63973); // 196 ms reset_RX(); } }}// initialise 18F4520void init(void){ // run internal oscillator at 8 MHz OSCCON = OSCCON | 0x70; while (OSCCONbits.IOFS == 0) ; PORTA = 0x00; PORTD = 0X00; ADCON1 = 0x0F; // set up PORTA to be digital I/Os TRISA = 0x02; // PORTA<7.2,0> outputs PORTA<1> input TRISD = 0XFD; TRISCbits.TRISC3 = 0; // SDO output TRISCbits.TRISC4 = 1; TRISCbits.TRISC5 = 0; // SCK output TRISCbits.TRISC2 = 0; // CSN output TRISCbits.TRISC1 = 0; // CE output TRISBbits.TRISB0 = 1; // IRQ input OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1 OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_256 );}//configure nRF24L01 for receivevoid configure_RX(void){ unsigned char i, j; SPI_CSN = 0; SPI_CE = 0; //PRX, CRC enabled spi_Send_Read(0x20); spi_Send_Read(0x39); SPI_CSN = 1; SPI_CSN = 0; //disable auto-ack for all channels spi_Send_Read(0x21); spi_Send_Read(0x00); SPI_CSN = 1; SPI_CSN = 0; //address width = 5 bytes spi_Send_Read(0x23); spi_Send_Read(0x03); SPI_CSN = 1; SPI_CSN = 0; //data rate = 1MB spi_Send_Read(0x26); spi_Send_Read(0x07); SPI_CSN = 1; SPI_CSN = 0; //4 byte payload spi_Send_Read(0x31); spi_Send_Read(0x04); SPI_CSN = 1; SPI_CSN = 0; //set channel 2 spi_Send_Read(0x25); spi_Send_Read(0x02); SPI_CSN = 1; SPI_CSN = 0; //set address E7E7E7E7E7 spi_Send_Read(0x30); for (j = 0; j < 5; j++) spi_Send_Read(0xE7); SPI_CSN = 1; SPI_CSN = 0; //PWR_UP = 1 spi_Send_Read(0x20); spi_Send_Read(0x3B); SPI_CSN = 1; SPI_CE = 1; }void reset_RX(void){ unsigned char i, j; unsigned char buffer[4]; //Read RX payload SPI_CSN = 0; spi_Send_Read(0x61); for (j = 0; j < 4; j++) { buffer[j] = spi_Send_Read(0); } SPI_CSN = 1; //Flush RX FIFO SPI_CSN = 0; spi_Send_Read(0xE2); SPI_CSN = 1; SPI_CSN = 0; //reset int spi_Send_Read(0x27); spi_Send_Read(0x40); SPI_CSN = 1;}unsigned char spi_Send_Read(unsigned char byte){ SSPBUF = byte; while(!DataRdySPI()) ; return SSPBUF;} void dly(unsigned int c){ INTCONbits.TMR0IF = 0; WriteTimer0(c); while (INTCONbits.TMR0IF == 0) ;}

用PICKIT3 DEBUGER 看SSPBUF 来test addr

/*** test.c** SPI test program for PIC18F4520 and nRF24L01 or nRF24L01+** Checks SPI comms between PIC and wireless chip** ** RA0	LED (output)** RA1	PB (input)*/#include 
#include
//function prototypesunsigned char spi_Send_Read(unsigned char);// Defines#define SPI_SCK LATCbits.LATC3 // Clock pin, PORTC pin 3 #define SPI_SO LATCbits.LATC5 // Serial output pin, PORTC pin 5 #define SPI_SI PORTCbits.RC4 // Serial input pin, PORTC pin 4 #define SPI_CSN LATCbits.LATC2 // CSN output pin, PORTC pin 2#define SPI_CE LATCbits.LATC1 // CE output pin, PORTC pin 1#define SPI_IRQ PORTBbits.RB0 // IRQ input pin, PORTB pin 0#define SPI_SCALE 4 // postscaling of signal #define LED LATAbits.LATA0#define PB PORTAbits.RA1// Macros#define nop() _asm nop _endasmvoid main(void){ unsigned char status = 0; unsigned char data[5]; int i; // run internal oscillator at 8 MHz OSCCON = OSCCON | 0x70; while (OSCCONbits.IOFS == 0) ; OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1 PORTA = 0x00; ADCON1 = 0x0F; // set up PORTA to be digital I/Os TRISA = 0x02; // PORTA<7.2,0> outputs PORTA<1> input TRISCbits.TRISC3 = 0; // SDO output TRISCbits.TRISC5 = 0; // SCK output TRISCbits.TRISC4 =1; TRISCbits.TRISC2 = 0; // CSN output TRISCbits.TRISC1 = 0; // CE output SPI_CSN = 1; // CSN high SPI_SCK = 0; // SCK low SPI_CE = 0; // CE low nop(); //write TX_ADDRESS register SPI_CSN = 0; //CSN low spi_Send_Read(0x30); spi_Send_Read(0x11); spi_Send_Read(0x22); spi_Send_Read(0x33); spi_Send_Read(0x44); spi_Send_Read(0x55); SPI_CSN = 1; //CSN high //read TX_ADDRESS register //Check that values are correct using the MPLAB debugger SPI_CSN = 0; //CSN low status = spi_Send_Read(0x10); data[0] = spi_Send_Read(0x00); // 0x11 data[1] = spi_Send_Read(0x00); // 0x22 data[2] = spi_Send_Read(0x00); // 0x33 data[3] = spi_Send_Read(0x00); // 0x44 data[4] = spi_Send_Read(0x00); // 0x55 SPI_CSN = 1; //CSN high // test PB and LED while(1) { if (!PB) LED = 1; else LED = 0; }}unsigned char spi_Send_Read(unsigned char byte){ SSPBUF = byte; while(!DataRdySPI()) ; return SSPBUF;}

转载于:https://www.cnblogs.com/liguangsunls/p/7142007.html

你可能感兴趣的文章
使用ASP.Net WebAPI构建REST服务(六)——Self-Host
查看>>
asp.net 的三种开发模式
查看>>
Android 交叉编译 IPerf3
查看>>
Android原生Gallery关于图像Orientation的问题
查看>>
Android开发之ViewPager
查看>>
【NOIP2017】列队【可持久化线段树】
查看>>
python学习——通过while循环语句实现九九乘法表的四种表达方式
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
MvvmCross[翻译] 使用Xamarin与MvvmCross完成一个跨平台App
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
027-chown命令
查看>>
Python 线程、进程和协程
查看>>
赛普系统自动拨号
查看>>
platform_device与platform_driver
查看>>
[iOS] iPad与iPhone上各种标准控件的大小
查看>>
动态规划(游船费用问题)
查看>>
[原创]Windows利用BitNami搭建Redmine
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
Linux命令学习一日一命令(RM)
查看>>
5-1
查看>>