基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序_第1页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序_第2页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序_第3页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序_第4页
基于STC89C52的控制NRF24L01的程序这个是发送程序改一下可以作为接收程序_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 *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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论