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

📄 spif.c

📁 制作Zigbee模块的详细电路原理图和C代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	Address = (Address << 5) | 0x8000;

//  	_INT_OFF();

	*P_NAND_GPIO_SETUP &= ~PCSN;		//使能线置0
	Delay(0);
	
	SPI_SB(Address>>8);
	SPI_SB(Address);

  	while(Length--)
  	{
      *DataPtr = SPI_RB();
      DataPtr++;
  	}
  	
	*P_NAND_GPIO_SETUP |= PCSN;		//使能线置1
//  	_INT_ON();
}

/****************************************************************************************
*  	Function: 
*  	Description: 读ZigBee模块接收数据包
*  	Syntax: 
*	Modify: 
*	parameter:  UINT8 *DataPtr:读出的数据存放的指针
				UINT8 Length:  读出的数据的长度
*  	Returns:    无
*  	Notes: 
*	Date: 2007-02-27
*	Author: ZijianWang
****************************************************************************************/ 
void spi_rd_rx_fifo(UINT8 *DataPtr, UINT8 *RxLength)
{
	UINT8 Length;

//  	_INT_OFF();

	*P_NAND_GPIO_SETUP &= ~PCSN;		//使能线置0
	Delay(0);
	
	SPI_SB(0xE0);
	SPI_SB(0x00);

//	Length = SPI_RB();	//接收的第一个数据为帧界定符,忽略
  	Length = SPI_RB();

  	*RxLength = Length;

  	while(Length--)
  	{
      	*DataPtr = SPI_RB();
      	DataPtr++;
  	}
	
	*P_NAND_GPIO_SETUP |= PCSN;		//使能线置1
//  	_INT_ON();
}

/****************************************************************************************
*  	Function: 
*  	Description: 中断中使用的 SPI短地址读,及地址为1字节,读出的数据为1字节
*  	Syntax: 
*	Modify: 
*	parameter:  UINT8 Address:需要读的寄存器地址
				UINT8 *DataPtr:读出的数据存放的指针
*  	Returns:    无
*  	Notes: 
*	Date: 2007-02-27
*	Author: ZijianWang
****************************************************************************************/ 
void spi_sr_IRQ(UINT8 Address, UINT8 *DataPtr)
{ 	
  	Address = (Address << 1);

//  	_INT_OFF();

	*P_NAND_GPIO_SETUP &= ~PCSN;		//使能线置0
	Delay(0);
	
  	SPI_SB(Address);
  	
  	*DataPtr = SPI_RB();

	*P_NAND_GPIO_SETUP |= PCSN;		//使能线置1
	
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//以下为使用芯片SPI驱动的函数
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#else

void InitSPI()
{// Init MCU's SPI Register

	
    *P_SPI_CONF = 0x3;		// SPI Clock Enable
    
	*P_SPI_MODE_CTRL = 0x0; 
	
    *P_SPI_MISC  = 0x00000000;                //SPI smart mode,skip data 
    *P_SPI_TXSTS = 0x80000000;                //SPITXLEVEL1, Tx interrupt disable; 
    *P_SPI_RXSTS = 0x80000000;                //SPIRXLEVEL1, Rx interrupt disable; 
    //*P_SPI_RXSTS = 0x80000000;                //SPIRXLEVEL1, Rx interrupt enable; 
    *P_SPI_MODE_CTRL  = 0x80000002;                             //open SPI clock = 27M/64,Master Mode         
    *P_SD_INTERFACE_SEL |= 0x100;                      //Enable SPI     
    
    while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE	  
 
    
}

void spi_sw(UINT8 Address, UINT8 Value)
{
	UINT8 temp;
	
	Address = (Address << 1) | 0x0001; //Shfit the Address

  	_INT_OFF();

  	*P_SPI_TXDATA = Address; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag      
  		  
  	*P_SPI_TXDATA = Value; //Write the data
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag  
  		
    while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE	    	
  	temp = *P_SPI_RXDATA;     //read dummy data     	  
  	temp = *P_SPI_RXDATA;     //read dummy data     	
  	 
  	_INT_ON();	
}
 
UINT8 spi_sr(UINT8 Address)
{ 	
  	UINT8 Value;
  	Address = (Address << 1);

	_INT_OFF();
	

  	*P_SPI_TXDATA = Address; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));	 
  
  	Value = *P_SPI_RXDATA;     //read dummy data  	  
  	
  	*P_SPI_TXDATA = 0; //Write the data (For generating the SCLK)
  	while((*P_SPI_MISC & 0x04)==0x00);		  
  	while((*P_SPI_RXSTS & 0x07)>0) 
  	{	  	  
        while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE
      	Value = *P_SPI_RXDATA;  	                             
  	}
       
  	_INT_ON();
  	return Value;
}


void spi_lw(UINT16 Address, UINT8 Value)
{
	UINT8 temp;

  	Address = (Address << 5) | 0x8010;

  	_INT_OFF();

  	*P_SPI_TXDATA = Address>>8; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag  	
  

  	*P_SPI_TXDATA = Address; //Write the data
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag  	
  	
  	
  	*P_SPI_TXDATA = Value; //Write the data
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag
  	
    while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE	  
	temp = *P_SPI_RXDATA;     //read dummy data 
	temp = *P_SPI_RXDATA;     //read dummy data 	
	temp = *P_SPI_RXDATA;     //read dummy data   		 	

  	_INT_ON();	
}

UINT8 spi_lr(UINT16 Address)
{
	UINT8 Value;
  	Address = (Address << 5) | 0x8000;

  	_INT_OFF();
  	
  	*P_SPI_TXDATA = Address>>8; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag

  	*P_SPI_TXDATA = Address; //Write the data
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag
  	
//	  	*DataPtr = *P_SPI_RXDATA;     //read dummy data	  	//此操作无效,只有IDLE时才能读数 by zijian
//	  	*DataPtr = *P_SPI_RXDATA;     //read dummy data   	  	
 	
	*P_SPI_TXDATA = 0; //Write the data (For generating the SCLK)		
			
	while((*P_SPI_MISC & 0x04)==0x00);		  	
  	while((*P_SPI_RXSTS & 0x07)>0) 
    { 	  	    
  	    while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE
        Value = *P_SPI_RXDATA;                          
    }  
  	_INT_ON();	
  	return Value;
}

void spi_fill_fifo(UINT16 Address, UINT8 *DataPtr, UINT8 Length)
{
	UINT8 temp,i,len;
	
	len = Length+2;
	
	Address = (Address << 5) | 0x8010;

  	_INT_OFF();

	*P_SPI_TXDATA = Address>>8; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag

  	*P_SPI_TXDATA = Address; //Write the data
  	while(!(*P_SPI_TXSTS & 0x08000000));
  	*P_SPI_TXSTS |= 0x80000000; //clear the flag	
	  	
	while (Length--)
	{
      	*P_SPI_TXDATA = *DataPtr; //Write the data
	  	while(!(*P_SPI_TXSTS & 0x08000000));
	  	*P_SPI_TXSTS |= 0x80000000; //clear the flag
      	DataPtr++;
  	}
	
	for(i=0;i<len;i++)
	{
		while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE	  
		temp = *P_SPI_RXDATA;     //read dummy data 
	}
	
  	_INT_ON();
}


void spi_dump_fifo(UINT16 Address, UINT8 *DataPtr, UINT8 Length)
{
	while(Length--)
	{
		*DataPtr = spi_lr( Address );
		Address++;
		DataPtr++;
	}
}


void spi_rd_rx_fifo(UINT8 *DataPtr, UINT8 *RxLength)
{
	UINT8 Length;
	UINT16 Address;

	Address = 0x300;
	Length = spi_lr(Address);
  	*RxLength = Length;
    Address++;
  	while(Length--)
  	{
      	*DataPtr = spi_lr( Address );
		Address++;
		DataPtr++;
  	}
}

void spi_sr_IRQ(UINT8 Address, UINT8 *DataPtr)
{ 	
  	Address = (Address << 1);

  	_INT_OFF();

  	*P_SPI_TXDATA = Address; //Write the address
  	while(!(*P_SPI_TXSTS & 0x08000000));	 
  
  	*DataPtr = *P_SPI_RXDATA;     //read dummy data  	  
  	
  	*P_SPI_TXDATA = 0; //Write the data (For generating the SCLK)
  	while((*P_SPI_MISC & 0x04)==0x00);		  
  	while((*P_SPI_RXSTS & 0x07)>0) 
  	{	  	  
//	  	  i=0;
//	  	  while(i++<0xff4); 	
      	while((*P_SPI_MISC & 0x10)==0x10);	//wait for IDLE
      	*DataPtr = *P_SPI_RXDATA;  	                             
  	}
	
}
#endif

⌨️ 快捷键说明

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