⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nrf905_driver.c

📁 nrf905驱动
💻 C
字号:
/****************************************************************************
 **             - nRF905_driver.c -
 **
 **     NewMsg-RF905无线收发模块驱动程序
 **
**     芯片型号 : NORDIC公司 nRF905
 **
 **             mader:高其良
 **    
 **     File version: $2007.9.12$
 **
 ***************************************************************************/

#include "nRF905_driver.h"


/******************************************基本函数********************************************/

/*8bit写入*/
void nRF905Write8Bit(unsigned char byte)
{
  unsigned char temp;
  
  for(temp=8;temp>0;temp--)
  {  
    nRF905_MOSI = (byte&0x80)>>7; 
    byte = byte<<1;
    nRF905_SCK = 1;
    nRF905_SCK = 0;
  }
}

/*8bit读出*/
unsigned char nRF905Read8Bit(void)
{

  unsigned char temp;
  unsigned char byte = 0x00;
   
  for(temp=8;temp>0;temp--)
  {
    byte = byte<<1;
    nRF905_SCK = 1;   
    byte = byte|(nRF905_MISO_PIN&0x01);
    nRF905_SCK = 0;
    
  }
  return(byte);

}

/****************************************扩展函数*******************************************/

/*配置寄存器*/
void nRF905ConfigRegister(unsigned char* p)
{
  unsigned char temp;
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_W_CONFIG(0));
  for(temp=0; temp<10; temp++)
  {
    nRF905Write8Bit(p[temp]);
  }
  nRF905_SPI_DISABLE();
}

/*写目标地址(4字节)*/
void nRF905WriteTxAddress(
  unsigned char addr0 , unsigned char addr1 ,
  unsigned char addr2 , unsigned char addr3 )
{
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_W_TX_ADDRESS(0));
  nRF905Write8Bit(addr0);
  nRF905Write8Bit(addr1);
  nRF905Write8Bit(addr2);
  nRF905Write8Bit(addr3);
  nRF905_SPI_DISABLE();
}

/*写本机地址(4字节)*/
void nRF905WriteRxAddress(
  unsigned char addr0 , unsigned char addr1 ,
  unsigned char addr2 , unsigned char addr3 )
{
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_W_CONFIG(5));
  nRF905Write8Bit(addr0);
  nRF905Write8Bit(addr1);
  nRF905Write8Bit(addr2);
  nRF905Write8Bit(addr3);
  nRF905_SPI_DISABLE();
}

/*读状态*/
unsigned char nRF905ReadStatus(void)
{
  unsigned char temp;
  
  nRF905_SPI_ENABLE();
  temp = nRF905Read8Bit(); 
  nRF905_SPI_DISABLE();
  return (temp);
}

/*读接收数据寄存器*/
void nRF905ReadRxPayload(unsigned char* p,unsigned char total)
{
  unsigned char temp;
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_R_RX_PAYLOAD(0x00));
  for(temp=0; temp<total; temp++)
  {
    p[temp] = nRF905Read8Bit();
  }
  nRF905_SPI_DISABLE();
}

/*写发送数据寄存器*/
void nRF905WriteTxPayload(unsigned char* p,unsigned char total)
{
  unsigned char temp;
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_W_TX_PAYLOAD(0x00));
  for(temp=0; temp<total; temp++)
  {
   nRF905Write8Bit(p[temp]);
  }
  nRF905_SPI_DISABLE();
}


/****************************************常用函数*******************************************/
/*初始化函数*/
void nRF905Initializtion()
{
  nRF905_IO_INITIALIZTION();
  nRF905_STANDBY_MODE();
  nRF905_SPI_DISABLE();
  nRF905_SPI_ENABLE();
  nRF905Write8Bit(nRF905_W_CONFIG(0));
  nRF905Write8Bit(nRF905Config_Byte0);
  nRF905Write8Bit(nRF905Config_Byte1);
  nRF905Write8Bit(nRF905Config_Byte2);
  nRF905Write8Bit(nRF905Config_Byte3);
  nRF905Write8Bit(nRF905Config_Byte4);
  nRF905Write8Bit(nRF905Config_Byte5);
  nRF905Write8Bit(nRF905Config_Byte6);
  nRF905Write8Bit(nRF905Config_Byte7);
  nRF905Write8Bit(nRF905Config_Byte8);
  nRF905Write8Bit(nRF905Config_Byte9);
  nRF905_SPI_DISABLE();
  nRF905WriteTxAddress(0xcc,0xcc,0xcc,0xcc);
  nRF905_RX_MODE();
  _NOP_ms(3);
}

/*接收数据包(32字节)*/
unsigned char nRF905Scanf(unsigned char* p)
{
  nRF905_RX_MODE();
  if (nRF905_DR_PIN==0)
    return (0);
  
  nRF905ReadRxPayload(p,32);
  return(1);
}

/*发送数据包(32字节)*/
void nRF905Printf(unsigned char* p)
{

  nRF905_STANDBY_MODE();
  nRF905WriteTxPayload(p,32);
  nRF905_TX_MODE();
  while(nRF905_DR_PIN==0);
  nRF905_RX_MODE();
}

/**********************************************/



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -