基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc

上传人:文库蛋蛋多 文档编号:4141566 上传时间:2023-04-07 格式:DOC 页数:13 大小:45.50KB
返回 下载 相关 举报
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc_第1页
第1页 / 共13页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc_第2页
第2页 / 共13页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc_第3页
第3页 / 共13页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc_第4页
第4页 / 共13页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc》由会员分享,可在线阅读,更多相关《基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序.doc(13页珍藏版)》请在三一办公上搜索。

1、这是init.c文件#include comm.h#define uchar unsigned char #define uint unsigned int uchar xdata date_time6;uchar xdata i2c_buf8;uchar xdata result;uchar xdata re_buf16;uchar xdata re_cnt;uchar check_sum;uchar flag_init;uchar xdata flag_end;uchar xdata flag_uart_busy;uint r_cnt;uchar flag_ex0_isr;/uchar x

2、data year ,month ,day0;uchar xdata c_year,c_month,c_day0;uchar xdata tx_buf120;uchar xdata tx_buf220;uchar xdata tx_buf320;uchar xdata tx_buf420;uint run_cnt;/*=* Name : DelayMs( )* Description : delay some times* Input : None* Output : None* Note : None=*/void init_uart(void)uchar i; TMOD|=0x20;TH1

3、=0Xfd;TL1=0Xfd;SCON=0X50;TR1=1;ES=1;IT0 =1;/低电平触发外部中断EX0 =1;/允许外部中断/-for(i=0;i20) run_cnt=0;flag_init=1;for(i=0;i7;i+) tx_buf1i=i2c_buf6-i;check_sum=0;for( i=0;i19;i+) check_sum+= tx_buf1i;tx_buf119= check_sum;nRF24L01_TxPacket(tx_buf1);for(i=0;i7;i+) tx_buf1i=i2c_buf6-i;send_one_byte(i2c_bufi);for(

4、i=0;i4;i+) send_one_byte(0xff);send_one_byte(check_sum);这是main.c文件#include comm.h#define uchar unsigned char #define uint unsigned int/-uchar t0_isr_cnt;uint t2_isr_cnt;uchar temp;sbit EXT0= P32;/外部中断管脚 uchar bdata sta;sbitRX_DR=sta6;sbitTX_DS=sta5;sbitMAX_RT=sta4;/-void main(void )/uchar i;job0();f

5、lag_end=0;t2_isr_cnt=0;EA=1;/check_sum=0;/-while(1)task1();if(flag_ex0_isr=1) flag_ex0_isr=0;sta=SPI_Read(STATUS);if(RX_DR=1 | TX_DS=1 | MAX_RT=1)IE0=1;/请求中断if(sta!=0x0e & sta!=0x1e)send_one_byte(sta);if(flag_end=1) red_led=1;re_cnt=0;/-/-void int_ext0(void ) interrupt 0red_led=0;sta=SPI_Read(STATUS

6、);if(RX_DR=1)SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);SPI_RW_Reg(WRITE_REG + STATUS, sta);/清除中断temp=0x55;if(TX_DS=1) SPI_RW_Reg(WRITE_REG + STATUS, sta);/清除中断temp=0x66;else if(MAX_RT=1)SPI_RW_Reg(FLUSH_TX,0 );SPI_RW_Reg(WRITE_REG + STATUS, sta);/清除中断temp=0x77;red_led=1;flag_ex0_isr=1;/-这是nrf2

7、401.c文件#include comm.huchar const TX_ADDRESSTX_ADR_WIDTH = 0x34,0x43,0x10,0x10,0x01; / Define a static TX addressuchar xdata rx_bufTX_PLOAD_WIDTH;uchar xdata tx_bufTX_PLOAD_WIDTH;/*Function: delay100();Description: delay 100ms/*/void delay100(void )uchar x;uchar y;for(x=0;x100;x+)for(y=0;y100;y+)_no

8、p_();/*/void delay_ms(unsigned int x) unsigned int i,j; i=0; for(i=0;ix;i+) j=208; while(j-); /*/void init_io(void)CE =0;CSN=1;SCK=0;/*Function: SPI_RW();Description: Writes one byte to nRF24L01, and return the byte read from nRF24L01 during write, according to SPI protocol/*/uchar SPI_RW(uchar byte

9、)uchar bit_ctr; for(bit_ctr=0;bit_ctr8;bit_ctr+) / output 8-bit MOSI = (byte & 0x80); / output byte, MSB to MOSI byte = (byte 1); / shift next bit into MSB. SCK = 1; / Set SCK high. byte |= MISO; / capture current MISO bit SCK = 0; / .then set SCK low again return(byte); / return read byte/*/*Functi

10、on: SPI_RW_Reg();Description: Writes value value to register reg/*/uchar SPI_RW_Reg(BYTE reg, BYTE value)uchar status; CSN = 0; / CSN low, init SPI transaction status = SPI_RW(reg); / select register SPI_RW(value); / .and write value to it. CSN = 1; / CSN high again return(status); / return nRF24L01

11、 status byte/*/*Function: SPI_Read();Description: Read one byte from nRF24L01 register, reg/*/BYTE SPI_Read(BYTE reg)BYTE reg_val; CSN = 0; / CSN low, initialize SPI communication. SPI_RW(reg); / Select register to read from. reg_val = SPI_RW(0); / .then read registervalue CSN = 1; / CSN high, termi

12、nate SPI communication return(reg_val); / return register value/*/*Function: SPI_Read_Buf();Description: Reads bytes #of bytes from register reg Typically used to read RX payload, Rx/Tx address/*/uchar SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)uchar status,byte_ctr; CSN = 0; / Set CSN low, init

13、SPI tranaction status = SPI_RW(reg); / Select register to write to and read status byte for(byte_ctr=0;byte_ctrbytes;byte_ctr+) pBufbyte_ctr = SPI_RW(0); / Perform SPI_RW to read byte from nRF24L01 CSN = 1; / Set CSN high again return(status); / return nRF24L01 status byte/*/*Function: SPI_Write_Buf

14、();Description: Writes contents of buffer *pBuf to nRF24L01 Typically used to write TX payload, Rx/Tx address/*/uchar SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)uchar status,byte_ctr; CSN = 0; / Set CSN low, init SPI tranaction status = SPI_RW(reg); / Select register to write to and read status

15、byte for(byte_ctr=0; byte_ctr10us) on CE will now send this packet and expext an acknowledgment from the RX device./*/void TX_Mode(void)uchar tmp_r=0;CE=0; SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); / Writes TX_Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS,

16、TX_ADR_WIDTH); / RX_Addr0 same as TX_Adr for Auto.Ack SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); / Writes data to TX payload SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); / Enable Auto.Ack:Pipe0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); / Enable Pipe0 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x2a); / 500us

17、 + 86us, 10 retrans. SPI_RW_Reg(WRITE_REG + RF_CH, 40); / Select RF channel 40 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); / TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR /SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); / Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled.SPI_RW_Reg(WRITE_REG + CONFIG

18、, 0x1e); CE=1;CSN = 0; /* tmp_r=SPI_Read(0);send_one_byte(tmp_r);tmp_r=SPI_Read(1);send_one_byte(tmp_r);tmp_r=SPI_Read(2);send_one_byte(tmp_r);tmp_r=SPI_Read(7);*/tmp_r=SPI_Read(7);if(tmp_r&0x01)=0x01)SPI_RW_Reg(STATUS,tmp_r); SPI_RW_Reg(FLUSH_TX,0);CSN = 1; /*/*发送数据包*/void nRF24L01_TxPacket(uchar *

19、tx_buf)CE=0;SPI_RW_Reg(FLUSH_TX, 0x00); SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); / Writes TX_Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); / RX_Addr0 same as TX_Adr for Auto.Ack SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); / Writes d

20、ata to TX payloadCE=1;delay_ms(1);这是公共头文件comm.h#include #include reg52.h#include init.h#include api.h#include NRF2401.H/-#define uchar unsigned char #define uint unsigned int #define ulong unsigned long #define TX_ADR_WIDTH 5 / 5 bytes TX(RX) address width#define TX_PLOAD_WIDTH 20 / 20 bytes TX payload#define RX_PLOAD_ADDR 0X61 /READ RX BUFFER#define LEFT1#define RIGHT0#define TIMER1_EN0/0=用来作UART波特率发生器/#define TIMER1_EN1/1=用来作定时器sbit run_led= P21;sbit red_led= P20;sbit CE = P14; /dsgsdfgdgsdsbit CSN= P12;sbit SCK= P10;sbit MOSI= P11; /发送sbit MISO= P13; /接收sbit IRQ = P32; /中断

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号