找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2452|回复: 0
收起左侧

51控制超声波测距并用nrf905传播信息程序

[复制链接]
ID:85375 发表于 2015-10-6 20:19 | 显示全部楼层 |阅读模式
3黑币
**********************请主要看主函数部分及后边的注释*********************************
******************************求大神指点迷津********************************************
#include <reg52.h>
#include <stdio.h>
#include <math.h>
#include "1602.h"        // 没用到  先暂时
#include "delay.h"
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int

#define WC                0x00                        // Write configuration register command
#define RC                0x10                         // Read  configuration register command
#define WTP                0x20                         // Write TX Payload  command
#define RTP                0x21                        // Read  TX Payload  command
#define WTA                0x22                        // Write TX Address  command
#define RTA                0x23                        // Read  TX Address  command
#define RRP                0x24                        // Read  RX Payload  command
/*******************************************************************************************/
typedef struct RFConfig
{
        uchar n;
        uchar buf[10];
}RFConfig;

code RFConfig RxTxConf =
{
        10,
        0x4c, 0x0c, 0x44, 0x20, 0x20, 0xcc, 0xcc, 0xcc,0xcc, 0x58       
};
// The content of this struct is nRF905's initialize data.初始化数据
// CH_NO=1;433MHZ;Normal Opration,No Retrans;RX,TX Address is 4 Bytes
// RX TX Payload Width is 32 Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable
/*******************************************************************************************/
sbit TRIG = P2^0 ;                   //超声波发
sbit ECHO = P2^1 ;                   //超声波收

unsigned char DisTempData[16];
uchar data TxBuf[32];
uchar data RxBuf[32];
//uchar key,rsbrf,delayf,tf;        //暂时改成下一句
uchar tf=0;
float distance;
/*******************************************************************************************/
uchar bdata DATA_BUF;
sbit        flag        =DATA_BUF^7;
sbit        flag1        =DATA_BUF^0;
/*******************************************************************************************/
sbit        TX_EN        =P1^7;
sbit        TRX_CE        =P1^6;
sbit        PWR_UP        =P1^5;
sbit        MISO        =P1^1;
sbit        MOSI        =P1^0;
sbit        SCK                =P3^7;
sbit        CSN                =P3^3;

sbit        AM                =P1^3;
sbit        DR                =P3^2;
sbit        CD                =P1^4;

sbit    LED1    =P3^4;
sbit    LED2    =P3^5;          
//sbit    CHE    =P3^0;                   //什么意思,怎么忘了???
//sbit    KEY2    =P3^1;           //用不到
float ceju(void);//超声波测距
void nrf_init(void);//nrf初始化
void TIM0init(void); //定时器0初始化
void Config905(void);// Config nRF905 module配置NRF905模式
void SetTxMode(void);// Set nRF905 in Tx mode
void SetRxMode(void);// Set nRF905 in Rx mode
void TxPacket(void);// Send data by nRF905
void RxPacket(void);// Recive data by nRF905
void SpiWrite(uchar);// Write data to nRF905
uchar SpiRead(void);// Read data to nRF905
void Delay(uchar n);// Delay 100us
/*------------------------------------------------
                    主函数
------------------------------------------------*/
void main()
{
  TIM0init();    //初始化定时器0
  LCD_Init();    //初始化LCD                         没用到  先暂时
  nrf_init();    //NRF905初始化
  Config905();                                // <主机通过SPI接口向905配置寄存器写入信息>Config nRF905 module
  TxBuf[0]=1;//第一个字节对应地址编码,如1号停车位
LCD_Write_String(0,1,"LY-UR04 demo");
  while(1)
  {
        distance=ceju();
   DelayMs(250);            //延时决定采样速度
        if(distance<50.0){tf=1;LED1 = 0; TxBuf[1] = 1 ;}//可以TxBuf初始零,然后加一;
      else {tf = 0;LED1 = 1; TxBuf[1] = 0 ;}         //为什么不加此句无论大于or小于50灯都亮,加上就好了
   sprintf(DisTempData,"distance=%3.1f CM     ",distance);//打印输出结果
   LCD_Write_String(0,0,DisTempData);       //在液晶屏上显示
   DelayMs(2500);            //延时决定采样速度/* 有无不影响
/*          if(tf!=1) ;
           else
            {       
                          LED1 = 1;
                  //===============NRF905发送相应的按键          
                     SetTxMode();                // Set Tx Mode
                     TxPacket();                // Transmit Tx buffer data
                        TxBuf[0] = 0x01;
                        TxBuf[1] = 0x00;        //数据清零??好像也不需要,都行???
                        SetRxMode();                // Set nRF905 in Rx mode
                        tf=0;
                        Delay(500);
        //                LED1 = 1;
                //        LED2 = 1;                 // 没用到  先暂时
                  }
*/
    if (tf==1)                          //他们怎么一直不能很好的实现,??????????????????/
             {       
                   LED1 = 1;            //灯亮后不会灭,这是我一直不能理解的,不应该有问题啊
                  //===============NRF905发送相应的按键          
                     SetTxMode();                // Set Tx Mode
                     TxPacket();                // Transmit Tx buffer data
                        TxBuf[0] = 0x01;
                        TxBuf[1] = 0x00;        //数据清零??好像也不需要,都行???
                        SetRxMode();                // Set nRF905 in Rx mode
                        tf=0;
                        Delay(500);
                        LED1 = 1;
                //        LED2 = 1;                 // 没用到  先暂时
                  }
//          else ;

         //=====================等待NRF905接收
/*           if (DR)                        // If recive data ready... 加上他们测距就不对了,怎么回事,因为DR悬空,一直等待;
           RxPacket();                        // ... recive data          
              else ;
*/
DelayMs(2500);
  }
}

/*------------------------------------------------
                 定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
ECHO=0;
}
/*------------------------------------------------
                    定时器0初始化
------------------------------------------------*/
void TIM0init(void)
{

  TMOD|= 0x01;//定时器0工作方式1
  TH0=0x00;   
  TL0=0x00;
  ET0=1;
  EA=1;
}
/*------------------------------------------------
                    ceju
------------------------------------------------*/
float ceju(void)
{
  float S;       //距离变量
   TRIG=1;       //触发信号是高电平脉冲,宽度大于10us
   DelayUs2x(10);
   TRIG=0;
   while(!ECHO); //等待高电平
   TR0=1;
   while(ECHO);  //等待低电平
   TR0=0;
   S=TH0*256+TL0;//取出定时器值高8位和低8位合并
   S=S/58;       //为什么除以58等于厘米,  Y米=(X秒*344)/2
                             // X秒=( 2*Y米)/344 -> X秒=0.0058*Y米 -> 厘米=微秒/58
   TH0=0;
   TL0=0;        //清除定时器0寄存器中的值
   return S;
}
void Delay(uchar n)
{
        uint i;
        while(n--)
        for(i=0;i<80;i++);       
}
/*******************************************************************************************/
//function nrf_init();
/*******************************************************************************************/
void nrf_init(void)
{
    CSN=1;                                                // Spi         disable                                               
        SCK=0;                                                // Spi clock line init high
        DR=1;                                                // Init DR for input
        AM=1;                                                // Init AM for input
        PWR_UP=1;                                        // nRF905 power on
        TRX_CE=0;                                        // Set nRF905 in standby mode
        TX_EN=0;                                        // set radio in Rx mode
}
/*******************************************************************************************/
//function Config905();
/*******************************************************************************************/
void Config905(void)
{
        uchar i;                                       
        CSN=0;                                                // Spi enable for write a spi command
        SpiWrite(WC);                                // Write config command写放配置命令
        for (i=0;i<RxTxConf.n;i++)        // Write configration words  写放配置字
        {
                SpiWrite(RxTxConf.buf[i]);
        }
        CSN=1;                                                // Disable Spi
}

/*******************************************************************************************/
//function SpiWrite();
/*******************************************************************************************/
void SpiWrite(uchar  byte)
{
        uchar i;       
        DATA_BUF=byte;                                // Put function's parameter into a bdata variable               
        for (i=0;i<8;i++)                        // Setup byte circulation bits
        {       
               
                if (flag)                                // Put DATA_BUF.7 on data line
                        MOSI=1;
                else
                        MOSI=0;
                SCK=1;                                        // Set clock line high
                DATA_BUF=DATA_BUF<<1;        // Shift DATA_BUF         高位在前
                SCK=0;                                        // Set clock line low
        }       
}
/*******************************************************************************************/
//function SpiRead();
/*******************************************************************************************/
uchar SpiRead(void)
{
        uchar i;       
        for (i=0;i<8;i++)                        // Setup byte circulation bits
        {       
                DATA_BUF=DATA_BUF<<1;        // Right shift DATA_BUF
                SCK=1;                                        // Set clock line high
                if (MISO)
                        flag1=1;                        // Read data
                else
                        flag1=0;
                                                       
               
                SCK=0;                                        // Set clock line low
        }
        return DATA_BUF;                        // Return function parameter
}
/*******************************************************************************************/
//function TxPacket();
/*******************************************************************************************/
void TxPacket(void)
{
        uchar i;
        //Config905();                               
        CSN=0;                                                // Spi enable for write a spi command       
        SpiWrite(WTP);                                // Write payload command
        for (i=0;i<32;i++)
        {
                SpiWrite(TxBuf[i]);                // Write 32 bytes Tx data
        }
        CSN=1;                                                // Spi disable                                               
        Delay(1);
        CSN=0;                                                // Spi enable for write a spi command       
        SpiWrite(WTA);                                // Write address command
        for (i=0;i<4;i++)                        // Write 4 bytes address
        {
                SpiWrite(RxTxConf.buf[i+5]);
        }       
        CSN=1;                                                // Spi disable
        TRX_CE=1;                                        // Set TRX_CE high,start Tx data transmission
        Delay(1);                                        // while (DR!=1);
        TRX_CE=0;                                        // Set TRX_CE low
}
/*******************************************************************************************/
//function RxPacket();
/*******************************************************************************************/
void RxPacket(void)
{
        uchar i;       
        TRX_CE=0;                                        // Set nRF905 in standby mode       
        CSN=0;                                                // Spi enable for write a spi command
        SpiWrite(RRP);                                // Read payload command       
        for (i=0;i<32;i++)
        {
                RxBuf[i]=SpiRead();                // Read data and save to buffer               
        }
        CSN=1;                                                // Disable spi
        while(DR||AM);       
//        P0=        RxBuf[0];                                // Buffer[0] output from P0 port...
        TRX_CE=1;
        //xx=(RxBuf[0]>>4)&0x0f;
        //TxData(xx);
         //=====================
           if(RxBuf[0] == 1) LED1 = 0 ;
           if(RxBuf[1] == 1) LED2 = 0 ;
          
        Delay(500);                                        // ...light led
//        P0=0xff;                                        // Close led
      LED1 = 1 ;
          LED2 = 1 ;                                                               
}
/*******************************************************************************************/
//function SetTxMode();
/*******************************************************************************************/
void SetTxMode(void)                               
{       
        TX_EN=1;
        TRX_CE=0;
        Delay(1);                                         // delay for mode change(>=650us)
}                               
/*******************************************************************************************/
//function SetRxMode();
/*******************************************************************************************/
void SetRxMode(void)
{
        TX_EN=0;
        TRX_CE=1;
        Delay(1);                                         // delay for mode change(>=650us)                               
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表