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 <p18cxxx.h>
#include <spi.h>
#include <timers.h> // Pragmas
#pragma config OSC = INTIO67
#pragma config PWRT = ON
//#pragma config MCLRE = OFF
#pragma config BOREN = OFF //function prototypes
void 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 _endasm void 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 <p18cxxx.h>
#include <spi.h>
#include <timers.h> // Pragmas
#pragma config OSC = INTIO67
#pragma config PWRT = ON
#pragma config MCLRE = OFF
#pragma config BOREN = OFF //function prototypes
void 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 _endasm void 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 18F4520
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 );
} //configure nRF24L01 for receive
void 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 <p18f4520.h>
#include <spi.h> //function prototypes
unsigned 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 _endasm void 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;
}

PIC18F4520 + NRF24L01的更多相关文章

  1. NRF24L01 无线模块的使用

    NRF24L01 是一款工作在2.4-2.5GHz通用ISM频段的单片收发芯片 工作电压:1.9-3.6V低电压工作 高速率:2Mbps,由于空中传输时间很短,极大的降低了无线传输中的碰撞现象 多频点 ...

  2. [nRF51822] 13、浅谈nRF51822和NRF24LE1/NRF24LU1/NRF24L01经典2.4G模块无线通信配置与流程

    前言:  nRF51可以支持基于2.4G的互相通信.与NRF24LE1的通信.与NRF24LU1的通信.与NRF24L01的通信. 一.nRF51822基于2.4G和nRF51822通信 其中nRF5 ...

  3. nRF24L01芯片控制——迈向无线的第一步

    nRF24L01芯片是一款专供单片机的射频收发芯片.工作于2.4GHz~2.5GHz ISM频段.融合了shockburst技术. 我先列出该芯片的硬件参数资料: 至于每个引脚的具体用途,可以参见技术 ...

  4. 2.4G无线射频通信模块nRF24L01+开发笔记(基于MSP430RF6989与STM32f0308)(1.(2)有错误,详见更正)

    根据网上的nRF24L01+例程和TI提供的MSP430RF6989的硬件SPI总线例程编写程序,对硬件MSP-EXP430RF6989 Launch Pad+nRF24L01P射频模块(淘宝购买)进 ...

  5. 51单片机对无线模块nRF24L01简单的控制收发程序

    它的一些物理特性如工作频段.供电电压.数据传输速率就不详细介绍了,直接上代码. 1.首先是发送端: // Define SPI pins #include <reg51.h> #defin ...

  6. [stm32] NRF24L01+USART搞定有线和无线通信

    前言 一般进行远程监控时,2.4G无线通信是充当远程数据传输的一种方法.这时就需要在现场部分具备无线数据发送装置,而在上位机部分由于一般只有串口,所以将采集到的数据送到电脑里又要在上位机端设计一个数据 ...

  7. [51单片机] SPI nRF24L01 无线简单程序 1

    main.c #include <reg51.h> #include <api.h> #define uchar unsigned char /**************** ...

  8. [51单片机] SPI nRF24L01无线 [可以放在2个单片机里实现通信]

    main.c #include<reg51.h> #include"2401.h" #define uint unsigned int #define uchar un ...

  9. [51单片机] nRF24L01 无线模块 测试 按键-灯-远程控制

    哈哈,穷吊死一个,自己做的一个超简单的板还没有电源提供,只得借助我的大开发板啦.其实这2个模块是完全可以分开的,无线嘛,你懂得!进入正题,这个实验的功能就是一个发送模块(大的那个板)连接4个按键,通过 ...

随机推荐

  1. poi API大全

    一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二. HSSF概况 HSSF 是 ...

  2. Lock-less and zero copy messaging scheme for telecommunication network applications

    A computer-implemented system and method for a lock-less, zero data copy messaging mechanism in a mu ...

  3. 使用Modernizr检测支持CSS3

    使用Modernizr检测支持CSS3 如果支持某个属性,会增加一个class,名字就是该属性: 不支持,名字是no-某属性 还提供了一个全局Modernizr对象,使用如下: <script ...

  4. POJ 1496 POJ 1850 组合计数

    Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8256 Accepted: 3906 Description Tran ...

  5. android选择图片或拍照图片上传到server(包含上传參数)

    在9ria论坛看到的.还没測试,先Mark与大家分享一下. 近期要搞一个项目,须要上传相冊和拍照的图片.不负所望,最终完毕了! 只是须要说明一下,事实上网上非常多教程拍照的图片.都是缩略图不是非常清晰 ...

  6. Visual C++文件后缀名释义

    [1] .APS:存放二进制资源的资源辅助中间文件(可加快资源装载速度). [2] .BMP:位图资源文件. [3] .BSC:浏览信息文件.由浏览信息维护工具(BSCMAKE)从原始浏览信息文件(. ...

  7. Educational Codeforces Round 6 B. Grandfather Dovlet’s calculator 暴力

    B. Grandfather Dovlet’s calculator   Once Max found an electronic calculator from his grandfather Do ...

  8. node11---相册

    app.js /* littleAlbum --.idea --controller(控制层相当于action层) --package.json --router.js --models(做事的是mo ...

  9. nyoj--1087--摆方格(规律)

    摆方格 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给你一个n*n的方格,每个方格里的数必须连续摆放如 1 2 4 3 ,下图为不连续的,请输出从左上角到右下角的对角 ...

  10. 关于github里readme编辑的方法

    实验室的老师昨天改完论文发我后,说按照例子改.于是才发现github里readme编辑满满的极客思维. 看了一下csdn给的教程 https://blog.csdn.net/Kaitiren/arti ...