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

📄 24l01.c

📁 无线芯片24L01的驱动程序
💻 C
字号:
/****************************************Copyright (c)**************************************************
**
**------------------------------------------------------------------------------------------------------
** Modified by:       liu jiandong
** Modified date:     2006.9.14       
** Version:           ver 2
** Descriptions:      test for second board
**
********************************************************************************************************/
#include "config.h"

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

#define TX_ADR_WIDTH    5   // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH  20  // 20 bytes TX payload

uint8 TX_ADDRESS[TX_ADR_WIDTH]  = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address

uint8 rx_buf[TX_PLOAD_WIDTH];
uint8 *tx_buf;
uint8 flag;
/**************************************************/
#define CE        0x01000000; //P1.24  //CE 
#define CSN       0x02000000; //P1.25  //CSN
//#define SCK       0x00000010; //P0.4   //SCK
//#define MOSI      0x00000040; //P0.6   //MOSI
//#define MISO      0x00000020; //P0.5   //MISO

#define MASK_CE   0xFeFFFFFF;
#define MASK_CSN  0xFdFFFFFF;
//#define MASK_SCK  0xFFFFFFeF;
//#define MASK_MOSI 0xFFFFFFbF;
//#define MASK_MISO 0xFFFFFFdF;


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

/**************************************************
Function: init_io();
Description:
  flash led one time,chip enable(ready to TX or RX Mode),
  Spi disable,Spi clock line init high
**************************************************/
void Init24L01(void)
{
    
    SCS = 0x03;
	/*设置GPIO口输入输出方向 1为输出,0为输入*/
	FIO1DIR = (FIO1DIR & 0xFeFFFFFF) | CE;   //CE输出
	FIO1DIR = (FIO1DIR & 0xFdFFFFFF) | CSN;  //CSN输出
//	FIO0DIR = (FIO0DIR & 0xFFFFFFeF) | SCK;  //SCK输出
//	FIO0DIR = (FIO0DIR & 0xFFFFFFbF) | MOSI; //MOSI输出


	/* 设置管脚连接,P0.30为EINT3*/
	PINSEL1 = (PINSEL1 & 0xcFFFFFFF) | 0x20000000;
	EXTMODE = 0x00;					/* 设置EINT3为电平触发	*/
	//IRQEnable();					// 使能IRQ中断
    MSPI_Init();						
}
/**************************************************/


/**************************************************/
void DelayNS(uint32 dly)
{
	uint32 i;
	for ( ; dly>0; dly--)
		for (i=50000; i>0; i--);
}

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

/********************************************************************************************************
** 函数名称:MSPI_Init()
** 函数功能:初始化SPI接口,设置为主机。
** 入口参数:无
** 出口参数:无
*********************************************************************************************************/
void  MSPI_Init(void)
{  
    PINSEL0 = (PINSEL0 & 0xFFFFaaFF) | 0x00005500;
    //PINSEL0 = (PINSEL0 & (~(0xFF << 8))) | (0x55 << 8) ;
    S0SPCCR = 0x52;		            // 设置SPI时钟分频
 	S0SPCR  = (0 << 3) |				// CPHA = 0, 数据在SCK 的第一个时钟沿采样
 			  (0 << 4) |				// CPOL = 0, SCK 为高有效
 			  (1 << 5) |				// MSTR = 1, SPI 处于主模式
 			  (0 << 6) |				// LSBF = 0, SPI 数据传输MSB (位7)在先
 			  (0 << 7) ;				// SPIE = 0, SPI 中断被禁止
 							// 设置数据位n,当n=0时,数据位为16。
 			
}

/**************************************************
Function: SPI_RW();

Description:
  Writes one byte to nRF24L01, and return the byte read
  from nRF24L01 during write, according to SPI protocol
**************************************************/
uint8 SPI_RW(uint8 byte)
{
   //hardware spi
    S0SPDR = byte;
    while((S0SPSR & 0x80) == 0);		// 等待SPIF置位,即等待数据发送完毕
    
    return(S0SPDR);
   //software spi
	/*uint8 bit_ctr,retbyte=0x00;
	uint32  byte1;
	byte1 = byte;
   	for(bit_ctr=0;bit_ctr<8;bit_ctr++)   // output 8-bit
   	{
   		FIO0MASK = MASK_MOSI;
   		FIO0PIN = (byte1 & 0x00000080)>>1;//MOSI = (byte & 0x80);         // output 'byte', MSB to MOSI
   		
   		byte1 = (byte1 << 1);           // shift next bit into MSB..
   		
   		FIO0MASK = MASK_SCK;
   		FIO0SET  = SCK;
   		//SCK = 1;                      // Set SCK high..
   		FIO0MASK = MASK_MISO;
   		retbyte |= ((FIO0PIN<<2)&0x00000080)>>7;//byte |= MISO;       		  // capture current MISO bit
   		
   		FIO0MASK = MASK_SCK;
   		FIO0CLR  = SCK;
   		retbyte = retbyte<<1;
   		//SCK = 0;            		  // ..then set SCK low again
   	}
   	
    return(retbyte);           		  // return read byte
*/
}
/**************************************************/

/**************************************************
Function: SPI_RW_Reg();

Description:
  Writes value 'value' to register 'reg'
**************************************************/
uint8 SPI_RW_Reg(uint8 reg, uint8 value)
{
	uint8 status;
    FIO1MASK = MASK_CSN;
  	FIO1CLR = CSN;    //CSN = 0;                   // CSN low, init SPI transaction
  	status = SPI_RW(reg);      // select register
  	SPI_RW(value);             // ..and write value to it..
  	FIO1MASK = MASK_CSN;
  	FIO1SET = CSN;    //CSN = 1;                   // CSN high again

  	return(status);            // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: SPI_Read();

Description:
  Read one byte from nRF24L01 register, 'reg'
**************************************************/
uint8 SPI_Read(uint8 reg)
{
	uint8 reg_val;
	
    FIO1MASK = MASK_CSN;
  	FIO1CLR  = CSN;//CSN = 0;                // CSN low, initialize SPI communication...
  	SPI_RW(reg);            // Select register to read from..
  	reg_val = SPI_RW(0);    // ..then read registervalue
  	FIO1MASK = MASK_CSN;
  	FIO1SET  = CSN; //CSN = 1;                // CSN high, terminate 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
**************************************************/
uint8 SPI_Read_Buf(uint8 reg, uint8 *pBuf, uint8 bytes)
{
	uint8 status,byte_ctr;
    
    FIO1MASK = MASK_CSN;
  	FIO1CLR  = CSN;    //CSN = 0;  		// Set CSN low, init SPI tranaction
  	status = SPI_RW(reg);       		// Select register to write to and read status byte

  	for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
    	pBuf[byte_ctr] = SPI_RW(0);    // Perform SPI_RW to read byte from nRF24L01
    FIO1MASK = MASK_CSN;
  	FIO1SET  = CSN;    //CSN = 1;                           // Set CSN high again

  	return(status);                    // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: SPI_Write_Buf();

Description:
  Writes contents of buffer '*pBuf' to nRF24L01
  Typically used to write TX payload, Rx/Tx address
**************************************************/
uint8 SPI_Write_Buf(uint8 reg, uint8 *pBuf, uint8 bytes)
{
	uint8 status,byte_ctr;
    
    FIO1MASK = MASK_CSN;
  	FIO1CLR  = CSN;//CSN = 0;                   // Set CSN low, init SPI tranaction
  	status = SPI_RW(reg);    // Select register to write to and read status byte
  	for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
    	SPI_RW(*pBuf++);
  	FIO1MASK = MASK_CSN;
  	FIO1SET = CSN;//CSN = 1;                 // Set CSN high again
  	return(status);          // return nRF24L01 status byte
}
/**************************************************/

/**************************************************
Function: RX_Mode();

Description:
  This function initializes one nRF24L01 device to
  RX Mode, set RX address, writes RX payload width,
  select RF channel, datarate & LNA HCURR.
  After init, CE is toggled high, which means that
  this device is now ready to receive a datapacket.
**************************************************/
void RX_Mode(void)
{
	FIO1MASK = MASK_CE;
	FIO1CLR  = CE;//CE=0;
  	SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device

  	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 + RF_CH, 40);        // Select RF channel 40
  	SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
  	SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
  	SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);     // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
    FIO1MASK = MASK_CE;
  	FIO1SET  = CE;//CE = 1; // Set CE pin high to enable RX device

  //  This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
  //  '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.

}
/**************************************************/

/**************************************************
Function: TX_Mode();

Description:
  This function initializes one nRF24L01 device to
  TX mode, set TX address, set RX address for auto.ack,
  fill TX payload, select RF channel, datarate & TX pwr.
  PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.

  ToDo: One high pulse(>10us) on CE will now send this
  packet and expext an acknowledgment from the RX device.
**************************************************/
void TX_Mode(void)
{
	FIO1MASK = MASK_CE;
	FIO1CLR  = CE;//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, 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, 0x1a); // 500us + 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..
	FIO1MASK = MASK_CE;
	FIO1SET  = CE;//CE=1;

}
/**************************************************/

/**************************************************
Function: check_ACK();

Description:
  check if have "Data sent TX FIFO interrupt",if TX_DS=1,
  all led light and after delay 100ms all led close
**************************************************
void check_ACK()
{
	uchar test;
	test=SPI_Read(READ_REG+STATUS);	// read register STATUS's
	test=test&0x20;					// check if have Data sent TX FIFO interrupt (TX_DS=1)
	if(test==0x20)					// TX_DS =1
	{
		P0=0x00;					// turn on all led
	    delay100();					// delay 100ms
		P0=0xff;
	}
}
**************************************************/

/**************************************************
Function: ISR_int0() interrupt 0;

Description:
  if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
  if RX_DR=1,read the payload from RX_FIFO and set flag;
**************************************************/
void EINT3_Exception(void)
{   
      
    uint8 	 sta;
   
    OS_ENTER_CRITICAL();
    //Beep(20);     
	sta=SPI_Read(STATUS);	// read register STATUS's value
	if(sta & 0x40)				// if receive data ready (RX_DR) interrupt
	{
		SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
		flag=1;
	}
	if(sta & 0x10)
	{
		SPI_RW_Reg(FLUSH_TX,0);
	    
	}
	SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
          
    
    /* 	等待外部中断信号恢复为高电平
	 	若信号保持为低电平,中断标志会一直置位。*/
	while ((EXTINT & 0x08) != 0)
	{
		EXTINT = 0x08;				/* 清除EINT0中断标志		*/
	}

   VICVectAddr = 0x00;			// 中断处理结束
   OS_EXIT_CRITICAL();
}


/**************************************************
Function: void nRF_GetData();

Description:
  
**************************************************/
uint8* nRF_GetData(void)
{
    uint8 *dat="0";
    if(flag)
  	{
  	  //RX_Mode();
  	  dat = rx_buf;
  	}
	return dat;
}

/**************************************************
Function: void nRF_SendData();

Description:
  
**************************************************/
void nRF_SendData(uint8 *dat)
{
	tx_buf = dat;	// Save to tx_buf[0]
	TX_Mode();			// set TX Mode and transmitting
	SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS));	// clear interrupt flag(TX_DS)
    DelayNS(50);
    RX_Mode();
}
/**************************************************
Function: uint8 get_rf_flag();

Description:
  
**************************************************/
uint8 get_rf_flag(void)
{
	if(flag)
	 return 1;
	else
	 return 0;
	
}
/**************************************************
Function: uint8 get_rf_flag();

Description:
  
**************************************************/
void set_rf_flag(uint8 flg)
{
	flag = flg;	
}
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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