PIC18F4520 + NRF24L01
来源:互联网 发布:mysql root登录不了 编辑:程序博客网 时间:2024/05/18 02:11
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 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_SCKLATCbits.LATC3// Clock pin, PORTC pin 3 #define SPI_SOLATCbits.LATC5// Serial output pin, PORTC pin 5 #define SPI_SIPORTCbits.RC4// Serial input pin, PORTC pin 4 #define SPI_CSNLATCbits.LATC2// CSN output pin, PORTC pin 2#define SPI_CELATCbits.LATC1// CE output pin, PORTC pin 1#define SPI_IRQPORTBbits.RB0// IRQ input pin, PORTB pin 0#define SPI_SCALE4 // postscaling of signal #define LEDLATDbits.LATD1#define PBPORTAbits.RA1// Macros#define nop() _asm nop _endasmvoid main(void){init();configure_transmitter(); while (1) {transmit_data();LED = 1;dly(63973);//200 ms delayLED = 0; dly(40000);//3.27 s delay nop(); }}void init(void){// run internal oscillator at 8 MHzOSCCON = OSCCON | 0x70;while (OSCCONbits.IOFS == 0);PORTA = 0x00;PORTD = 0X00;ADCON1 = 0x0F;// set up PORTA to be digital I/OsTRISA = 0x02;// PORTA<7.2,0> outputs PORTA<1> inputTRISD = 0XFD;TRISCbits.TRISC3 = 0;// SDO outputTRISCbits.TRISC4 = 1;TRISCbits.TRISC5 = 0; // SCK outputTRISCbits.TRISC2 = 0;// CSN outputTRISCbits.TRISC1 = 0;// CE outputTRISBbits.TRISB0 = 1;// IRQ inputOpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1OpenTimer0( 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 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_SCKLATCbits.LATC3// Clock pin, PORTC pin 3 #define SPI_SOLATCbits.LATC5// Serial output pin, PORTC pin 5 #define SPI_SIPORTCbits.RC4// Serial input pin, PORTC pin 4 #define SPI_CSNLATCbits.LATC2// CSN output pin, PORTC pin 2#define SPI_CELATCbits.LATC1// CE output pin, PORTC pin 1#define SPI_IRQPORTBbits.RB0// IRQ input pin, PORTB pin 0#define SPI_SCALE4 // postscaling of signal #define LEDLATDbits.LATD1#define PBPORTAbits.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 MHzOSCCON = OSCCON | 0x70;while (OSCCONbits.IOFS == 0);PORTA = 0x00;PORTD = 0X00;ADCON1 = 0x0F;// set up PORTA to be digital I/OsTRISA = 0x02;// PORTA<7.2,0> outputs PORTA<1> inputTRISD = 0XFD;TRISCbits.TRISC3 = 0;// SDO outputTRISCbits.TRISC4 = 1;TRISCbits.TRISC5 = 0; // SCK outputTRISCbits.TRISC2 = 0;// CSN outputTRISCbits.TRISC1 = 0;// CE outputTRISBbits.TRISB0 = 1;// IRQ inputOpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1OpenTimer0( 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 enabledspi_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** ** RA0LED (output)** RA1PB (input)*/#include <p18f4520.h>#include <spi.h>//function prototypesunsigned char spi_Send_Read(unsigned char);// Defines#define SPI_SCKLATCbits.LATC3// Clock pin, PORTC pin 3 #define SPI_SOLATCbits.LATC5// Serial output pin, PORTC pin 5 #define SPI_SIPORTCbits.RC4// Serial input pin, PORTC pin 4 #define SPI_CSNLATCbits.LATC2// CSN output pin, PORTC pin 2#define SPI_CELATCbits.LATC1// CE output pin, PORTC pin 1#define SPI_IRQPORTBbits.RB0// IRQ input pin, PORTB pin 0#define SPI_SCALE4 // postscaling of signal #define LEDLATAbits.LATA0#define PBPORTAbits.RA1// Macros#define nop() _asm nop _endasmvoid main(void){unsigned char status = 0;unsigned char data[5];int i;// run internal oscillator at 8 MHzOSCCON = OSCCON | 0x70;while (OSCCONbits.IOFS == 0);OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1PORTA = 0x00;ADCON1 = 0x0F;// set up PORTA to be digital I/OsTRISA = 0x02;// PORTA<7.2,0> outputs PORTA<1> inputTRISCbits.TRISC3 = 0;// SDO outputTRISCbits.TRISC5 = 0; // SCK outputTRISCbits.TRISC4 =1;TRISCbits.TRISC2 = 0;// CSN outputTRISCbits.TRISC1 = 0;// CE outputSPI_CSN = 1;// CSN highSPI_SCK = 0;// SCK lowSPI_CE= 0;// CE lownop();//write TX_ADDRESS registerSPI_CSN = 0;//CSN lowspi_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 debuggerSPI_CSN = 0;//CSN lowstatus = spi_Send_Read(0x10);data[0] = spi_Send_Read(0x00);// 0x11data[1] = spi_Send_Read(0x00);// 0x22data[2] = spi_Send_Read(0x00);// 0x33data[3] = spi_Send_Read(0x00);// 0x44data[4] = spi_Send_Read(0x00);// 0x55SPI_CSN = 1;//CSN high// test PB and LEDwhile(1){if (!PB)LED = 1;elseLED = 0;}}unsigned char spi_Send_Read(unsigned char byte){SSPBUF = byte;while(!DataRdySPI());return SSPBUF;}
0 0
- PIC18F4520 + NRF24L01
- nrf24l01+
- NRF24L01+
- PIC18F4520 UART模板
- NRF24L01 接收
- nRF24L01+使用说明
- nRF24L01随笔
- NRF24L01驱动程序
- stm32 NRF24L01
- nRF24L01介绍
- STM32F103: NRF24L01
- NRF24L01学习
- PIC18F4520单片机AD转换程序与注释
- PIC18F4520的ADC与LCD1602显示程序
- NRF24L01芯片基本特性
- nrf24l01+的接收代码
- nrf24l01+无线发送
- nRF24L01无线 使用
- Hadoop配置项(hdfs-site.xml)
- 总结(持续整理中。。。)
- PHP 根据IP地址获取所在城市
- 那场雨落进谁的心里
- 不存在介意不介意,只是有点小小的遗憾而已
- PIC18F4520 + NRF24L01
- 关于Spring的69个面试问答——终极列表
- 深入理解ServletRequest与ServletResponse
- Linux用户空间与内核空间
- Pentaho ETL安装使用(KETTLE)
- linux学习资料
- 堆排序完整版,含注释
- Mysql常用操作命令
- Linux 平台下 RMAN 全备 和 增量备份 shell 脚本