PIC18F4520 + NRF24L01
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的更多相关文章
- NRF24L01 无线模块的使用
NRF24L01 是一款工作在2.4-2.5GHz通用ISM频段的单片收发芯片 工作电压:1.9-3.6V低电压工作 高速率:2Mbps,由于空中传输时间很短,极大的降低了无线传输中的碰撞现象 多频点 ...
- [nRF51822] 13、浅谈nRF51822和NRF24LE1/NRF24LU1/NRF24L01经典2.4G模块无线通信配置与流程
前言: nRF51可以支持基于2.4G的互相通信.与NRF24LE1的通信.与NRF24LU1的通信.与NRF24L01的通信. 一.nRF51822基于2.4G和nRF51822通信 其中nRF5 ...
- nRF24L01芯片控制——迈向无线的第一步
nRF24L01芯片是一款专供单片机的射频收发芯片.工作于2.4GHz~2.5GHz ISM频段.融合了shockburst技术. 我先列出该芯片的硬件参数资料: 至于每个引脚的具体用途,可以参见技术 ...
- 2.4G无线射频通信模块nRF24L01+开发笔记(基于MSP430RF6989与STM32f0308)(1.(2)有错误,详见更正)
根据网上的nRF24L01+例程和TI提供的MSP430RF6989的硬件SPI总线例程编写程序,对硬件MSP-EXP430RF6989 Launch Pad+nRF24L01P射频模块(淘宝购买)进 ...
- 51单片机对无线模块nRF24L01简单的控制收发程序
它的一些物理特性如工作频段.供电电压.数据传输速率就不详细介绍了,直接上代码. 1.首先是发送端: // Define SPI pins #include <reg51.h> #defin ...
- [stm32] NRF24L01+USART搞定有线和无线通信
前言 一般进行远程监控时,2.4G无线通信是充当远程数据传输的一种方法.这时就需要在现场部分具备无线数据发送装置,而在上位机部分由于一般只有串口,所以将采集到的数据送到电脑里又要在上位机端设计一个数据 ...
- [51单片机] SPI nRF24L01 无线简单程序 1
main.c #include <reg51.h> #include <api.h> #define uchar unsigned char /**************** ...
- [51单片机] SPI nRF24L01无线 [可以放在2个单片机里实现通信]
main.c #include<reg51.h> #include"2401.h" #define uint unsigned int #define uchar un ...
- [51单片机] nRF24L01 无线模块 测试 按键-灯-远程控制
哈哈,穷吊死一个,自己做的一个超简单的板还没有电源提供,只得借助我的大开发板啦.其实这2个模块是完全可以分开的,无线嘛,你懂得!进入正题,这个实验的功能就是一个发送模块(大的那个板)连接4个按键,通过 ...
随机推荐
- SpringBoot2.0 监听器ApplicationListener的使用-监听ApplicationReadyEvent事件
参考:http://www.shareniu.com/article/73.htm 一.需求是想将我的写一个方法能在项目启动后就运行,之前使用了redis的消息监听器,感觉可以照着监听器这个思路做,于 ...
- tp框架报错 Namespace declaration statement has to be the very first statement in the script
Namespace declaration statement has to be the very first statement in the script tp框架报这个错误,错误行数就是nam ...
- ASP.NET-ActionFilter过滤器用法实例
ActionFilter可以对每一个传过来的action请求进行过滤,非常有用,但是如果在这里判断过多,那么网站的性能和速度会不会变慢,这个问题值得思考,现在先放在这里. public class A ...
- com.alibaba.fastjson.JSONPathException: expect '], but 'y'
今天遇到这样的一个错误 网上查找了各种资料,终于找到了报错的原因: String dataType = (String) JSONPath.eval(dataset.getSchema(), &quo ...
- jQuery调用WebService ( 同源调用)
转自原文 jQuery调用WebService 1.编写4种WebService方法 [WebService(Namespace = "http://tempuri.org/&quo ...
- Android使用有道翻译API实如今线翻译功能
在Android应用中,加入在线翻译的功能,这里调用的是有道翻译的API. 使用有道翻译API.首先要申请一个key,申请地址为:path=data-mode">有道翻译API申请地址 ...
- C++编写绚丽的界面
近期项目特别的操蛋,要用C++写出各种变态界面,今晚上赶工总算有了一点小的收货. 因为没有时间去写博文 ,等项目期完了 准备 写一系列 怎样在C++/win32/mfc开发高质量 可扩展界面组建 ...
- [NOI.AC#32]sort 构造
链接 50分做法(只有0,1) 根据归并排序的思想,假设我们现在已经把 \(l\dots mid\) 和 \(mid+1\dots r\) 排好序 只要把左边连续的1和右边连续的0翻转即可 inlin ...
- Google Maps API 将开始收费
Google Maps API 将开始收费 一.总结 一句话总结:国外的话openstreetmap或许不错 国内的话就高德吧 二.Google Maps API 将开始收费 曾经免费的 Google ...
- BZOJ 1283 费用流
思路: 最大费用最大流 i->i+1 连边k 费用0 i->i+m (大于n的时候就连到汇) 连边1 费用a[i] //By SiriusRen #include <queue> ...