切换到宽版
爱科技/爱创意/爱折腾/爱极致;技术知识分享平台,点击进入新版数码之家网站
  • 3221阅读
  • 7回复

[无线]NRF24L01多发单收 [复制链接]

上一主题 下一主题
离线大号
 

发帖
3269
M币
82
专家
4
粉丝
48
只看楼主 倒序阅读 我要置顶 楼主  发表于: 2014-06-27
中断方式
压缩包里是代码文件加sprntlayout文件
接收部分改过在下面。
  
  1. //jieshou.c
  2. #include <reg52.h>
  3. #include <intrins.h>
  4. #include "MAX232.h"
  5. #define uint unsigned int
  6. #define uchar unsigned char
  7. unsigned char RevFlags;
  8. unsigned char sec;
  9. unsigned char TxData[4];
  10. unsigned char Flag;
  11. unsigned char bufTop;
  12. sbit CE=P1^2;  //RX/TX模式选择端
  13. //sbit IRQ=P1^3; //可屏蔽中断端
  14. sbit IRQ=P3^2;
  15. sbit CSN=P1^4; //SPI片选端//就是SS
  16. sbit MOSI=P1^5;    //SPI主机输出从机输入端
  17. sbit MISO=P1^6;    //SPI主机输出从机输出端
  18. sbit SCK=P1^7;    //SPI时钟端
  19. sbit BEEP = P1^1;
  20. sbit LED = P1^0;
  21. sbit LIGHT = P3^4;
  22. unsigned char RxBuf[8]={0};
  23. unsigned char RxBuf1[8]={0};
  24. unsigned char RxBuf2[8]={0};
  25. unsigned char RxBuf3[8]={0};
  26. unsigned char RxBuf4[8]={0};
  27. uchar sta; //状态标志
  28. #define RX_DR (sta & 0x40)
  29. #define TX_DS (sta & 0x20)
  30. #define MAX_RT (sta & 0x10)
  31. #define TX_ADR_WIDTH 5 // 5 uints TX address width
  32. #define RX_ADR_WIDTH 5 // 5 uints RX address width
  33. #define TX_PLOAD_WIDTH 5 // 20 uints TX payload
  34. #define RX_PLOAD_WIDTH 5 // 20 uints TX payload
  35. uchar RX_ADDRESS0[RX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //通道0地址
  36. uchar RX_ADDRESS1[RX_ADR_WIDTH]= {0xc2,0x13,0x54,0xa5,0x76}; //通道1地址
  37. uchar RX_ADDRESS2[1]= {0xc3};  //通道2地址
  38. uchar RX_ADDRESS3[1]= {0xc4};  //通道3地址
  39. uchar RX_ADDRESS4[1]= {0xc5};  //通道4地址
  40. uchar RX_ADDRESS5[1]= {0xc6};  //通道5地址
  41. //***************************************NRF24L01寄存器指令*******************************************************
  42. #define READ_REG 0x00 // 读寄存器指令
  43. #define WRITE_REG 0x20 // 写寄存器指令
  44. #define RD_RX_PLOAD 0x61 // 读取接收数据指令
  45. #define WR_TX_PLOAD 0xA0 // 写待发数据指令
  46. #define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
  47. #define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
  48. #define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
  49. #define NOP 0xFF // 保留
  50. //*************************************SPI(nRF24L01)寄存器地址****************************************************
  51. #define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
  52. #define EN_AA 0x01 // 自动应答功能设置
  53. #define EN_RXADDR 0x02 // 可用信道设置,接收通道允许
  54. #define SETUP_AW 0x03 // 收发地址宽度设置
  55. #define SETUP_RETR 0x04 // 自动重发功能设置
  56. #define RF_CH 0x05 // 工作频率设置
  57. #define RF_SETUP 0x06 // 发射速率、功耗功能设置
  58. #define STATUS 0x07 // 状态寄存器
  59. #define OBSERVE_TX 0x08 // 发送监测功能
  60. #define CD 0x09 // 地址检测
  61. #define RX_ADDR_P0 0x0A // 频道0接收数据地址
  62. #define RX_ADDR_P1 0x0B // 频道1接收数据地址
  63. #define RX_ADDR_P2 0x0C // 频道2接收数据地址
  64. #define RX_ADDR_P3 0x0D // 频道3接收数据地址
  65. #define RX_ADDR_P4 0x0E // 频道4接收数据地址
  66. #define RX_ADDR_P5 0x0F // 频道5接收数据地址
  67. #define TX_ADDR 0x10 // 发送地址寄存器
  68. #define RX_PW_P0 0x11 // 接收频道0接收数据长度
  69. #define RX_PW_P1 0x12 // 接收频道0接收数据长度
  70. #define RX_PW_P2 0x13 // 接收频道0接收数据长度
  71. #define RX_PW_P3 0x14 // 接收频道0接收数据长度
  72. #define RX_PW_P4 0x15 // 接收频道0接收数据长度
  73. #define RX_PW_P5 0x16 // 接收频道0接收数据长度
  74. #define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
  75. //**************************************************************************************
  76. void Delay(unsigned int s);
  77. void inerDelay_us(unsigned char n);
  78. void init_NRF24L01(void);
  79. uchar SPI_RW(uchar byte);
  80. uchar SPI_Read(uchar reg);
  81. void SetRX_Mode(void);
  82. uchar SPI_RW_Reg(uchar reg, uchar value);
  83. uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num);
  84. uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num);
  85. unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
  86. void nRF24L01_TxPacket(unsigned char * tx_buf);
  87. void bin2hex(unsigned char ch,unsigned char* buf);
  88. void delayms(unsigned int count);
  89. void int_init(void);
  90. void inerDelay_us(unsigned char n)
  91. {
  92.     for(;n>0;n--)
  93.         _nop_();
  94. }
  95. //******************************************************************************************
  96. //***********毫秒延时程序********************/
  97. void delayms(unsigned int count)
  98. {
  99.     unsigned int i,j;
  100.     for(i=0;i<count;i++)
  101.         for(j=0;j<120;j++);
  102. }
  103. //****************************************************************************************
  104. //NRF24L01初始化
  105. void init_NRF24L01(void)
  106. {
  107.     delayms(1);
  108.     CE=0; // chip enable
  109.     CSN=1; // Spi disable
  110.     SCK=0; // Spi clock line init high
  111.     //IRQ=1;
  112.     SPI_Write_Buf(WRITE_REG + TX_ADDR, RX_ADDRESS0, TX_ADR_WIDTH); // 写本地地址,自动应答
  113.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS0, RX_ADR_WIDTH); // 写发射端0地址
  114.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P1, RX_ADDRESS1, RX_ADR_WIDTH); // 写发射端1地址
  115.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P2, RX_ADDRESS2, 1); // 写发射端2地址
  116.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P3, RX_ADDRESS3, 1); // 写发射端3地址
  117.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P4, RX_ADDRESS4, 1); // 写发射端4地址
  118.     SPI_Write_Buf(WRITE_REG + RX_ADDR_P5, RX_ADDRESS5, 1); // 写发射端5地址
  119.     SPI_RW_Reg(WRITE_REG + EN_AA, 0x3f); // 频道0自动 ACK应答允许
  120.     SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x3f); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
  121.     SPI_RW_Reg(WRITE_REG + RF_CH, 40); // 设置信道工作为2.4GHZ,收发必须一致
  122.     SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  123.     SPI_RW_Reg(WRITE_REG + RX_PW_P1, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  124.     SPI_RW_Reg(WRITE_REG + RX_PW_P2, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  125.     SPI_RW_Reg(WRITE_REG + RX_PW_P3, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  126.     SPI_RW_Reg(WRITE_REG + RX_PW_P4, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  127.     SPI_RW_Reg(WRITE_REG + RX_PW_P5, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
  128.     SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
  129. }
  130. //***************************************************************************************************
  131. //函数:uint SPI_RW(uint uchar)
  132. //功能:NRF24L01的SPI写时序
  133. //***************************************************************************************************/
  134. uchar SPI_RW(uchar byte)
  135. {
  136.     uchar i;
  137.     for(i=0;i<8;i++) // output 8-bit
  138.     {
  139.         MOSI = (byte & 0x80); // output 'uchar', MSB to MOSI
  140.         //byte = (byte << 1); // shift next bit into MSB..
  141.         byte <<= 1;
  142.         SCK = 1; // Set SCK high..
  143.         if(MISO)
  144.             byte |= 1; // capture current MISO bit
  145.         SCK = 0; // ..then set SCK low again
  146.     }
  147.     return(byte); // return read uchar
  148. }
  149. //***************************************************************************************************
  150. //函数:uchar SPI_Read(uchar reg)
  151. //功能:NRF24L01的SPI时序
  152. /****************************************************************************************************/
  153. uchar SPI_Read(uchar reg)
  154. {
  155.     uchar reg_val;
  156.     CSN = 0; // CSN low, initialize SPI communication...
  157.     SPI_RW(reg); // Select register to read from..
  158.     reg_val = SPI_RW(0); // ..then read registervalue
  159.     CSN = 1; // CSN high, terminate SPI communication
  160.     return(reg_val); // return register value
  161. }
  162. //***************************************************************************************************/
  163. //功能:NRF24L01读写寄存器函数
  164. //***************************************************************************************************/
  165. uchar SPI_RW_Reg(uchar reg, uchar value)
  166. {
  167.     uchar status;
  168.     CSN = 0; // CSN low, init SPI transaction
  169.     status = SPI_RW(reg); // select register
  170.     SPI_RW(value); // ..and write value to it..
  171.     CSN = 1; // CSN high again
  172.     return(status); // return nRF24L01 status uchar
  173. }
  174. //***************************************************************************************************/
  175. //函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
  176. //功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
  177. //***************************************************************************************************/
  178. uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num)
  179. {
  180.     uchar status,i;
  181.     CSN = 0; // Set CSN low, init SPI tranaction
  182.     status = SPI_RW(reg); // Select register to write to and read status uchar
  183.     for(i=0;i<num;i++)
  184.         pBuf[i] = SPI_RW(0); //
  185.     CSN = 1;
  186.     return(status); // return nRF24L01 status uchar
  187. }
  188. //********************************************************************************************************
  189. //函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
  190. //功能: 用于写数据 reg:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
  191. //********************************************************************************************************/
  192. uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num)
  193. {
  194.     uchar status,i;
  195.     CSN = 0; //SPI使能
  196.     status = SPI_RW(reg);
  197.     for(i=0; i<num; i++) //
  198.     SPI_RW(*pBuf++);
  199.     CSN = 1; //关闭SPI
  200.     return(status); //
  201. }
  202. //***************************************************************************************************/
  203. //函数:void SetRX_Mode(void)
  204. //功能:数据接收配置
  205. //***************************************************************************************************/
  206. void SetRX_Mode(void)
  207. {
  208.     CE=0;
  209.     SPI_RW_Reg(WRITE_REG+CONFIG,0x0d);
  210.     SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
  211.     CE = 1;
  212.     delayms(1);
  213. }
  214. //*****************************************************************************************************/
  215. //函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
  216. //功能:数据读取后放如rx_buf接收缓冲区中
  217. /******************************************************************************************************/
  218. unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
  219. {
  220.     unsigned char revale=0;
  221.     sta=SPI_Read(STATUS); // 读取状态寄存器来判断数据接收状况
  222.     if(RX_DR) // 判断是否接收到数据
  223.     {
  224.         CE = 0; //SPI使能
  225.         //delayms(1);
  226.         SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
  227.         revale =1; //读取数据完成标志
  228.         //inerDelay_us(150);
  229.         SPI_RW_Reg(WRITE_REG+STATUS,sta|0x70); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清除中断标志
  230.         CE = 1;
  231.         CSN=0;
  232.         SPI_RW(FLUSH_RX);//清空接收FIFO
  233.         CSN=1;
  234.         //inerDelay_us(20);
  235.     }
  236.     return revale;
  237. }
  238. void showStatus()
  239. {
  240.     unsigned char tmp;
  241.     unsigned char tmps[3];
  242.     //***************************************NRF24L01寄存器指令*******************************************************
  243.     //*************************************SPI(nRF24L01)寄存器地址****************************************************
  244.     /*
  245.     tmp = SPI_Read(CONFIG);
  246.     Max232SendAChar("\nCONFIG:");
  247.     bin2hex(tmp,tmps);// 配置收发状态,CRC校验模式以及收发状态响应方式
  248.     tmps[3] = '\0';
  249.     Max232SendAChar(tmps);
  250.     */
  251.     tmp = SPI_Read(STATUS);
  252.     Max232SendAChar("\nSTATUS:");
  253.     bin2hex(tmp,tmps);// 状态寄存器
  254.     tmps[3] = '\0';
  255.     Max232SendAChar(tmps);
  256. }
  257. void beep(unsigned char s){
  258.     if (s)
  259.     {
  260.         BEEP = 1;
  261.     } else {
  262.         BEEP = 0;
  263.     }
  264. }
  265. void Light(unsigned char s){
  266.     if (s)
  267.         LIGHT = 1;
  268.     else
  269.         LIGHT = 0;
  270. }
  271. void bin2hex(unsigned char ch,unsigned char* buf)
  272. {
  273.     buf[1] = ch & 0x0f;
  274.     buf[1] += 0x30;
  275.     if (buf[1] > 0x39)
  276.     {
  277.         buf[1] += 7;
  278.     }
  279.     buf[0] = ch & 0xf0;
  280.     buf[0] >>= 4;
  281.     buf[0] += 0x30;
  282.     if (buf[0] > 0x39)
  283.     {
  284.         buf[0] += 7;
  285.     }
  286. }
  287. //************************************主函数************************************************************
  288. void main(void)
  289. {
  290.     //unsigned char RX_P_NO;//接收通道号
  291.     unsigned char i;
  292.     //unsigned char tmp;
  293.     unsigned char tmps[3];
  294.     unsigned char _Str_Buf[10];
  295.     unsigned char _RxBuf[10];
  296.     unsigned char detected;
  297.     unsigned char timeout[6];
  298.     for (i=0;i<6;i++)
  299.     {
  300.         timeout[i] = 0x00;
  301.     }
  302.     LED = 0;
  303.     sec = 0;
  304.     delayms(100);
  305.     int_init();
  306.     Max232Int();
  307.     init_NRF24L01();
  308.     bufTop = 1;
  309.     ES=1;
  310.     EA=1;
  311.     delayms(1000);
  312.     Max232SendAChar("Start");
  313.     SetRX_Mode();
  314.     while(1)
  315.     {
  316.         if (sec > 15)  //70s左右没收到指定通道信号超时
  317.         {
  318.             sec = 0;
  319.             for (i=0;i<2;i++)
  320.             {
  321.                 timeout[i]++;
  322.                 if (timeout[i] > 4) {
  323.                     _Str_Buf[0] = 'T';
  324.                     _Str_Buf[1] = ':';
  325.                     _Str_Buf[2] = 0x30+i;
  326.                     _Str_Buf[3] = 0x00;
  327.                     Max232SendAChar(_Str_Buf);
  328.                     timeout[i] = 0;
  329.                 }
  330.             }
  331.         }
  332.         LED = 0;
  333.         if(bufTop > 1)  
  334.         {
  335.             EX0 = 0;  //关接收中断,转移一组数据
  336.             switch (bufTop)
  337.             {
  338.                 case 0x2:
  339.                     for (i=0;i<8;i++)
  340.                         _RxBuf[i] = RxBuf[i];
  341.                     break;
  342.                 case 0x3:
  343.                     for (i=0;i<8;i++)
  344.                         _RxBuf[i] = RxBuf1[i];
  345.                     break;
  346.                 case 0x4:
  347.                     for (i=0;i<8;i++)
  348.                         _RxBuf[i] = RxBuf2[i];
  349.                     break;
  350.                 case 0x5:
  351.                     for (i=0;i<8;i++)
  352.                         _RxBuf[i] = RxBuf3[i];
  353.                     break;
  354.                 case 0x6:
  355.                     for (i=0;i<8;i++)
  356.                         _RxBuf[i] = RxBuf4[i];
  357.                     break;                
  358.             }
  359.             bufTop--;
  360.             EX0 = 1;  //开接收中断
  361.             _RxBuf[8] = 0x00;    
  362.             LED = 1;
  363.             //RX_P_NO=sta&0x0e;
  364.             detected = 0;
  365.             switch(_RxBuf[0])
  366.             {
  367.                 case 0x12:
  368.                     timeout[0] = 0x00;
  369.                     if (_RxBuf[1] == 'K')
  370.                     {
  371.                         _Str_Buf[0] = 'K';
  372.                         _Str_Buf[1] = '1';
  373.                         _Str_Buf[2] = ':';
  374.                         bin2hex(_RxBuf[2],tmps);
  375.                         _Str_Buf[3] = tmps[0];
  376.                         _Str_Buf[4] = tmps[1];
  377.                         _Str_Buf[5] = '\0';
  378.                         Max232SendAChar(_Str_Buf);
  379.                     } else if (_RxBuf[1] == 'S') {
  380.                         Max232SendAChar("S1 Start");
  381.                     } else if (_RxBuf[1] == 'W')
  382.                     {
  383.                         detected = 1;
  384.                         _Str_Buf[0] = 'W';
  385.                         _Str_Buf[1] = ':';
  386.                         bin2hex(_RxBuf[2],tmps);
  387.                         _Str_Buf[2] = tmps[0];
  388.                         _Str_Buf[3] = tmps[1];
  389.                         bin2hex(_RxBuf[3],tmps);
  390.                         _Str_Buf[4] = tmps[0];
  391.                         _Str_Buf[5] = tmps[1];
  392.                         _Str_Buf[6] = '\0';
  393.                         Max232SendAChar(_Str_Buf);
  394.                     }
  395.                     break;
  396.                 case 0xC2:
  397.                     timeout[1] = 0x00;
  398.                     if (_RxBuf[1] == 'K')
  399.                     {
  400.                         _Str_Buf[0] = 'K';
  401.                         _Str_Buf[1] = '2';
  402.                         _Str_Buf[2] = ':';
  403.                         bin2hex(_RxBuf[2],tmps);
  404.                         _Str_Buf[3] = tmps[0];
  405.                         _Str_Buf[4] = tmps[1];
  406.                         _Str_Buf[5] = '\0';
  407.                         Max232SendAChar(_Str_Buf);
  408.                     } else if (_RxBuf[1] == 'S') {
  409.                         Max232SendAChar("S2 Start");
  410.                     } else if (_RxBuf[1] == 'W')
  411.                     {
  412.                         detected = 1;
  413.                         _Str_Buf[0] = 'W';
  414.                         _Str_Buf[1] = ':';
  415.                         bin2hex(_RxBuf[2],tmps);
  416.                         _Str_Buf[2] = tmps[0];
  417.                         _Str_Buf[3] = tmps[1];
  418.                         bin2hex(_RxBuf[3],tmps);
  419.                         _Str_Buf[4] = tmps[0];
  420.                         _Str_Buf[5] = tmps[1];
  421.                         _Str_Buf[6] = '\0';
  422.                         Max232SendAChar(_Str_Buf);
  423.                     }
  424.                     break;
  425.                 case 0xC3:
  426.                     timeout[2] = 0x00;
  427.                     Max232SendAChar("Sender3");
  428.                     break;
  429.                 case 0xC4:
  430.                     timeout[3] = 0x00;
  431.                     Max232SendAChar("Sender4");
  432.                     break;
  433.                 case 0xC5:
  434.                     timeout[4] = 0x00;
  435.                     Max232SendAChar("Sender5");
  436.                     break;
  437.                 case 0xC6:
  438.                     if (BEEP)
  439.                     {
  440.                         beep(0);
  441.                     } else {
  442.                         beep(1);
  443.                     }
  444.                     _Str_Buf[0] = 'B';
  445.                     _Str_Buf[1] = '\0';
  446.                     break;
  447.                 default:
  448.                     if (_RxBuf[1] == 'K')
  449.                     {
  450.                         _Str_Buf[0] = 'K';
  451.                         _Str_Buf[1] = 'u';
  452.                         _Str_Buf[2] = ':';
  453.                         bin2hex(_RxBuf[2],tmps);
  454.                         _Str_Buf[3] = tmps[0];
  455.                         _Str_Buf[4] = tmps[1];
  456.                         _Str_Buf[5] = '\0';
  457.                         Max232SendAChar(_Str_Buf);
  458.                     } else if (_RxBuf[1] == 'S') {
  459.                         Max232SendAChar("Su Start");
  460.                     } else if (_RxBuf[1] == 'W')
  461.                     {      
  462.                         detected = 1;
  463.                         _Str_Buf[0] = 'W';
  464.                         _Str_Buf[1] = ':';
  465.                         bin2hex(_RxBuf[2],tmps);
  466.                         _Str_Buf[2] = tmps[0];
  467.                         _Str_Buf[3] = tmps[1];
  468.                         bin2hex(_RxBuf[3],tmps);
  469.                         _Str_Buf[4] = tmps[0];
  470.                         _Str_Buf[5] = tmps[1];
  471.                         _Str_Buf[6] = '\0';
  472.                         Max232SendAChar(_Str_Buf);
  473.                     }
  474.                     break;
  475.             }
  476.             
  477.             if (detected)
  478.             {
  479.                 detected = '\0';
  480.                 beep(1);
  481.             }
  482.         } else {
  483.             if (RevFlags)  //串口接收的单字节命令
  484.             {
  485.                 ES = 0;  //关串口中断
  486.                 switch (RevFlags)
  487.                 {
  488.                 case 'B':
  489.                     beep(1);
  490.                     Max232SendAChar("BO");
  491.                     break;
  492.                 case 'P':
  493.                     beep(0);
  494.                     Max232SendAChar("PO");
  495.                     break;
  496.                 case 'S':
  497.                     if (BEEP)
  498.                         Max232SendAChar("B:ON");
  499.                     else
  500.                         Max232SendAChar("B:OFF");
  501.                     if (LIGHT)
  502.                         Max232SendAChar("L:ON");
  503.                     else
  504.                         Max232SendAChar("L:OFF");
  505.                     break;
  506.                 case 'L':
  507.                     if (LIGHT)
  508.                         Light(0);
  509.                     else
  510.                         Light(1);
  511.                     Max232SendAChar("LO");
  512.                     break;
  513.                 case 'D':
  514.                     showStatus();
  515.                     break;
  516.                 default:
  517.                     Max232SendAChar("NC");
  518.                 }
  519.                 RevFlags = '\0';
  520.                 ES = 1;
  521.             }
  522.         }
  523.     }
  524. }


  1. //interrupt.c
  2. #include"reg52.h"
  3. #define FLUSH_RX 0xE2
  4. sfr T2MOD = 0xC9;
  5. extern void delayus(unsigned char  n);
  6. extern unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
  7. extern unsigned char SPI_RW(unsigned char);
  8. extern unsigned char sec;
  9. extern unsigned char TxData[4];
  10. extern unsigned char Flag;
  11. extern unsigned char bufTop;
  12. extern unsigned char RxBuf[8];
  13. extern unsigned char RxBuf1[8];
  14. extern unsigned char RxBuf2[8];
  15. extern unsigned char RxBuf3[8];
  16. extern unsigned char RxBuf4[8];
  17. sbit CSN=P1^4;
  18. unsigned char num1;
  19. //*********************74HC165*******************************
  20. sbit PL = P2^0;
  21. sbit CE1 = P2^1;
  22. sbit CE2 = P2^2;
  23. sbit SSCK = P2^3;
  24. sbit SIN = P2^4;
  25. void int_init()
  26. {
  27.     Flag = '\0';
  28.     TMOD &= 0xFC;
  29.     TMOD |= 0x01;
  30.     //TH0=(65536-45872)/256;
  31.     //TL0=(65536-45872)%256;
  32.     TH0=(65536-5000)/256;//计时器低四位附初值
  33.     TL0=(65536-5000)%256;//计时器高四位赋值,11.0592MHz晶振,约5ms
  34.     //EA=1;
  35.     ET0=1;
  36.     TR0=1;
  37.     //外部中断
  38.     //*****************************
  39.     IT0=0;              // INT0高电平触发(为0则为上升沿触发)
  40.     EX0=1;              // INT0中断开
  41.     //EX1=1;              // INT1中断开
  42.     //IT1=0;              // INT1低电平触发(为1则为下降沿触发)
  43.     //******************************  
  44.     //EA=1;//总中断开启
  45. }
  46. //INT0中断  由P3.2引脚产生
  47. void int_0() interrupt 0 using 0
  48. {
  49.     //unsigned char _RxBuf[8];
  50.     //unsigned char i;
  51.     EA = 0;
  52.     EX0 = 0;
  53.     switch (bufTop)
  54.     {
  55.         case 0x01:
  56.             if (nRF24L01_RxPacket(RxBuf)){
  57.                 bufTop++;
  58.             }
  59.             break;
  60.         case 0x02:
  61.             if (nRF24L01_RxPacket(RxBuf1))
  62.             {
  63.                 bufTop++;
  64.             }
  65.             break;
  66.         case 0x03:
  67.             if (nRF24L01_RxPacket(RxBuf2))
  68.             {
  69.                 bufTop++;
  70.             }
  71.             break;
  72.         case 0x04:
  73.             if (nRF24L01_RxPacket(RxBuf3))
  74.             {
  75.                 bufTop++;
  76.             }
  77.             break;
  78.         case 0x05:
  79.             if (nRF24L01_RxPacket(RxBuf4))
  80.             {
  81.                 bufTop++;
  82.             }
  83.             break;
  84.         case 0x06:  //缓冲满时冲掉最末一次接收
  85.             nRF24L01_RxPacket(RxBuf4);
  86.             //if (nRF24L01_RxPacket(RxBuf4))
  87.             //{
  88.             //    for (i=0;i<8;i++)
  89.             //        RxBuf4[i] = _RxBuf[i];
  90.             //}
  91.             break;
  92.     }
  93.      if (bufTop > 6)   //五组接缓冲区满
  94.         bufTop = 6;
  95.   
  96.     EX0 = 1;
  97.     EA = 1;
  98. }
  99. void timer() interrupt 1
  100. {
  101.     //TH0=(65536-45872)/256;
  102.     //TL0=(65536-45872)%256;
  103.      TH0=(65536-5000)/256;//计时器低四位附初值
  104.     TL0=(65536-5000)%256;//计时器高四位赋值,11.0592MHz晶振,约5ms
  105.     num1++;
  106.     if(num1>=200)
  107.     {
  108.         num1=0;
  109.         sec++;
  110.     }
  111. }

  1. //MAX232.c
  2. *********************************************************************/
  3. #include"reg52.h"
  4. extern unsigned char RevFlags;
  5. /*************************串口初始化函数**************/
  6. void Max232Int()
  7. {
  8.     PCON=0x00;   //SMOD=0
  9.     TMOD |= 0x20;  //设置T1为工作方式2
  10.     TH1=0xfd;  //9600
  11.     TL1=0xfd;
  12.     SM0=0;
  13.     SM1=1;//方式1,8位
  14.     //EA=1;
  15.     ES=1;
  16.     TR1=1;//定时器1允许
  17.     REN=1;//允许接收
  18. }
  19. /*************************串口发送一个字节函数********************/
  20. void Max232SendChar(unsigned char Data)
  21. {
  22.    ES=0;
  23.    SBUF=Data;
  24.    while(!TI);
  25.      TI=0;
  26.      ES=1;
  27. }
  28. /**********************发送一个字符串******************************/
  29. void Max232SendAChar(unsigned char *string)
  30. {
  31.     ET0=0;  //关闭T0定时器中断,防止干扰串口时序导致接收乱码
  32.     TR0=0;    
  33.   while(*string!='\0')
  34.    {
  35.       Max232SendChar(*string);
  36.       string++;
  37.    }
  38.     ET0=1;
  39.     TR0=1;
  40. }
  41. /**********************串口中断接收函数****************************/
  42. void ExInt() interrupt 4
  43. {
  44.   ES=0;
  45.   if(RI)
  46.     {
  47.       RI=0;
  48.       RevFlags=SBUF;
  49.     }
  50.   ES=1;
  51. }

interrupt.h
  1. #ifndef _INTERRUPT_H_
  2.     #define _INTERRUPT_H_
  3.     void int_init();
  4. #endif

MAX232.h
  1. #ifndef _MAX232_H_
  2. #define _MAX232_H_
  3. void Max232Int();
  4. void Max232SendChar(unsigned char Data);
  5. void Max232SendAChar(unsigned char *string);
  6. extern void MAX232SendData();
  7. #endif


每次传送数据长度是5字节,依次是一字节地址码,一字节类型码,后面三字节随意。

[ 此帖被大号在2014-06-27 19:59重新编辑 ]
本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
离线似水无痕

发帖
1570
M币
4199
专家
3
粉丝
25
只看该作者 1楼 发表于: 2014-06-27
请登录后查看
离线mhmtjtlk

发帖
48
M币
229
专家
0
粉丝
0
只看该作者 2楼 发表于: 2018-12-21
请登录后查看
离线mhmtjtlk

发帖
48
M币
229
专家
0
粉丝
0
只看该作者 3楼 发表于: 2018-12-21
请登录后查看
离线fryefryefrye

发帖
1298
M币
802
专家
4
粉丝
30
只看该作者 4楼 发表于: 2018-12-21
请登录后查看
离线mhmtjtlk

发帖
48
M币
229
专家
0
粉丝
0
只看该作者 5楼 发表于: 2018-12-21
回 fryefryefrye 的帖子
请登录后查看
离线fryefryefrye

发帖
1298
M币
802
专家
4
粉丝
30
只看该作者 6楼 发表于: 2018-12-21
回 mhmtjtlk 的帖子
请登录后查看
离线mhmtjtlk

发帖
48
M币
229
专家
0
粉丝
0
只看该作者 7楼 发表于: 2018-12-21
回 fryefryefrye 的帖子
请登录后查看
快速回复
限80 字节
温馨提示:欢迎交流讨论,请勿发布纯表情、纯引用等灌水帖子;以免被删除
 
上一个 下一个