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

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

这是init.c文件#includecomm.h#defineucharunsignedchar#defineuintunsignedintucharxdatadate_time[6];ucharxdatai2c_buf[8];ucharxdataresult;ucharxdatare_buf[16];ucharxdatare_cnt;ucharcheck_sum;ucharflag_init;ucharxdataflag_end;ucharxdataflag_uart_busy;uintr_cnt;ucharflag_ex0_isr;//ucharxdatayear,month,day0;ucharxdatac_year,c_month,c_day0;ucharxdatatx_buf1[20];ucharxdatatx_buf2[20];ucharxdatatx_buf3[20];ucharxdatatx_buf4[20];uintrun_cnt;/*================================================================================*Name:DelayMs()*Description:delaysometimes*Input:None*Output:None*Note:None================================================================================*/voidinit_uart(void){uchari;TMOD|=0x20;TH1=0Xfd;TL1=0Xfd;SCON=0X50;TR1=1;ES=1;IT0=1;//低电平触发外部中断EX0=1;//允许外部中断//-----------------------------------------for(i=0;i20;i++){tx_buf1[i]=1;tx_buf2[i]=2;tx_buf3[i]=2;tx_buf4[i]=2;}}/*================================================================================*Name:DelayMs()*Description:delaysometimes*Input:None*Output:None*Note:None================================================================================*/////////////////////////////////////////////////////////////////////////////////////////////////voidsend_one_byte(unsignedchartad){uchartemp;if(ES==1){ES=0;temp=1;}TI=0;SBUF=tad;//发送数据os_wait2(K_TMO,1);while(TI==0);TI=0;flag_uart_busy=1;if(temp==1){ES=1;}}/*================================================================================*Name:DelayMs()*Description:delaysometimes*Input:None*Output:None*Note:None================================================================================*/voidjob0(void)//_TASK_0{uchartemp;uchari;run_led=0;temp=0x10;flag_init=0;init_uart();flag_uart_busy=1;P0=0xff;red_led=1;run_led=1;i=1;if(i==2){RX_Mode();TX_Mode();}init_io();delay_ms(550);delay100();TX_Mode();send_one_byte(0);flag_ex0_isr=0;}//---------------------------------------------------voidtask1(void)//_task_1{uchari;run_cnt++;if(run_cnt20){run_cnt=0;flag_init=1;for(i=0;i7;i++){tx_buf1[i]=i2c_buf[6-i];}check_sum=0;for(i=0;i19;i++){check_sum+=tx_buf1[i];}tx_buf1[19]=check_sum;nRF24L01_TxPacket(tx_buf1);for(i=0;i7;i++){tx_buf1[i]=i2c_buf[6-i];send_one_byte(i2c_buf[i]);}for(i=0;i4;i++){send_one_byte(0xff);}send_one_byte(check_sum);}}这是main.c文件#includecomm.h#defineucharunsignedchar#defineuintunsignedint//---------------------------------------------------uchart0_isr_cnt;uintt2_isr_cnt;uchartemp;sbitEXT0=P3^2;//外部中断管脚ucharbdatasta;sbitRX_DR=sta^6;sbitTX_DS=sta^5;sbitMAX_RT=sta^4;//---------------------------------------------------voidmain(void){//uchari;job0();flag_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;}}}//---------------------------------------------------//---------------------------------------------------voidint_ext0(void)interrupt0{red_led=0;sta=SPI_Read(STATUS);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;}elseif(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;}//---------------------------------------------------这是nrf2401.c文件#includecomm.hucharconstTX_ADDRESS[TX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01};//DefineastaticTXaddressucharxdatarx_buf[TX_PLOAD_WIDTH];ucharxdatatx_buf[TX_PLOAD_WIDTH];/**************************************************Function:delay100();Description:delay100ms/**************************************************/voiddelay100(void){ucharx;uchary;for(x=0;x100;x++){for(y=0;y100;y++)_nop_();}}/*************************************************/voiddelay_ms(unsignedintx){unsignedinti,j;i=0;for(i=0;ix;i++){j=208;while(j--);}}/**************************************************/voidinit_io(void){CE=0;CSN=1;SCK=0;}/**************************************************Function:SPI_RW();Description:WritesonebytetonRF24L01,andreturnthebytereadfromnRF24L01duringwrite,accordingtoSPIprotocol/**************************************************/ucharSPI_RW(ucharbyte){ucharbit_ctr;for(bit_ctr=0;bit_ctr8;bit_ctr++)//output8-bit{MOSI=(byte&0x80);//output'byte',MSBtoMOSIbyte=(byte1);//shiftnextbitintoMSB..SCK=1;//SetSCKhigh..byte|=MISO;//capturecurrentMISObitSCK=0;//..thensetSCKlowagain}return(byte);//returnreadbyte}/**************************************************//**************************************************Function:SPI_RW_Reg();Description:Writesvalue'value'toregister'reg'/**************************************************/ucharSPI_RW_Reg(BYTEreg,BYTEvalue){ucharstatus;CSN=0;//CSNlow,initSPItransactionstatus=SPI_RW(reg);//selectregisterSPI_RW(value);//..andwritevaluetoit..CSN=1;//CSNhighagainreturn(status);//returnnRF24L01statusbyte}/***************************************

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功