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

📄 ptr6000pa.c

📁 PTR6000PA驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include "spi.h"
#include "RF.h"
#include "nRF24L01.h"
#include "PTR6000PA.h"
#define Configtrynum 10 


/************************************************************************/
/*  Function : Write commend and data to PTR6000PA			*/
/*  Parameter: command is the Control Byte				*/
/*	       SendRFData[32] is the pointer of the data to be send	*/
/*	       ByteNum is the byte number to be send			*/
/************************************************************************/
char Write_RF(char command,char SendRFData[32],char ByteNum)
{
    char i;
    char *pDataToSend;
    pDataToSend=SendDataBaseAddr;
    *pDataToSend=command;
    for (i=0;i<ByteNum;i++)
    {
	pDataToSend++;
	*pDataToSend=SendRFData[i];	
    }
    SendByteNum=ByteNum+1;/* Send the Num of to be send and enable data to be send*/			
    return 1;
}

/************************************************************************/
/*  Function : Read RF							*/
/*  Parameter: ReadRFData[32] is the pointer of the receive data	*/
/*	       ByteNum is the byte number to be receive			*/
/************************************************************************/
char Read_RF(char commend,char ReadRFData[32],char ByteNum)
{ 
    char *pData;
    char i,j;
    char *pDataReceive;
    char temp;
    char *pDataToSend;
    pDataReceive=ReceiveDataBaseAddr;
    pData=ReadRFData;
    Write_RF(commend,pData,ByteNum); 
    temp=0;
    while (temp==0) 		/* if RF idle ,send data*/
    {
	temp=sendfinish;
	temp=(temp&0x01);
    }
    for(i=0;i<(ByteNum+40);i++) ;
    for (i=0;i<=ByteNum;i++)
    {
	pDataReceive++;
        ReadRFData[i]=*pDataReceive;	
    }
    return 1; 
}

/************************************************************************/
/*  Function : Set Send mode and send data				*/
/*  Parameter: SendData[] is the pointer of the data to be send		*/
/*	       ByteNum is the byte number to be send			*/
/************************************************************************/
char Send(char SendData[],short ByteNum)
{
    int i,j;
    short ByteCounter=0;
    short Counter;
    char Flag;
    char SendBuff[32];
    char SEND_REG[32];
    char Rec_REG[32];
    SEND_REG[0]=CONFIG & 0xfe;		/* Program to send mode*/
    Write_RF(0x20,SEND_REG,1);
    Write_RF(Clear_TX,Rec_REG,1);	/* Clear TX FIFO*/
    Send_to_Receive=0x0;		/* Ready to send data*/
    j=0;
    while(ByteCounter<ByteNum)
    {
	Counter=ByteNum-ByteCounter;
	if(Counter>32)
	{
	    for(i=0;i<32;i++)
	    {
	    	SendBuff[i]=SendData[j];
	        j++;
	    }	
	    Write_RF(Clear_TX,Rec_REG,1);
	    Write_RF(Send_CMD,SendBuff,32);
	    for(i=0;i<110;i++);		/* Wait data send to PTR6000PA from FPGA*/
	    Send_to_Receive=0x1;	
	    for(i=0;i<100;i++);		/* CE Pin Hole 32us (at less 10us)*/
	    Send_to_Receive=0x0;
	    for(i=0;i<1600;i++);
	}
	else
	{
	    for(i=0;i<Counter;i++)
	    {
	    	SendBuff[i]=SendData[j];
	        j++;
	    }
	    Write_RF(Clear_TX,Rec_REG,1);
	    Write_RF(Send_CMD,SendBuff,32);
	    for(i=0;i<110;i++);		/* Wait data send to PTR6000PA from FPGA*/
	    Send_to_Receive=0x1;	
	    for(i=0;i<100;i++);		/* CE Pin Hole 32us (at less 10us)*/
	    Send_to_Receive=0x0;
	}
	ByteCounter=ByteCounter+32;
    }
    for(i=0;i<200000;i++);
    Rec_REG[0]=CONFIG; 			/* Send finish turn to receive*/
    Write_RF(0x20,Rec_REG,1);
    Rec_REG[0]=0x70;
    Write_RF(0x27,Rec_REG,1);
    Send_to_Receive=0x1;		/* Hole the CE pin to 1*/
    SEND_REG[0]=StatusReg0 | 0x40 ;
    Write_RF(0x27,SEND_REG,1);		/* Clear interrupt*/
    Write_RF(Clear_RX,Rec_REG,1);	/* Clear RX FIFO*/
}

/************************************************************************/
/*  Function : Set Receive mode and receive data			*/
/*  Parameter: ReceiveData[] is the pointer of the receive data		*/
/*	       ByteNum is the byte number to be receive			*/
/************************************************************************/

char Receive(char ReceiveData[],short ByteNum)
{
    int TURN;
    short i,j;
    short ByteCounter=0;
    short Counter;
    char Flag;
    char ReceiveBuff[ByteNum+32];
    char SEND_REG[32];
    char Rec_REG[32];
    char T_StatusReg;
    char temp;
/*
    Rec_REG[0]=CONFIG;
    Write_RF(0x20,Rec_REG,1);
    Rec_REG[0]=0x70;
    Write_RF(0x27,Rec_REG,1);
*/
    i=0;
    Counter=0;
    TURN=0;
    while(ByteCounter<ByteNum)
    {  
	Read_RF(Receive_CMD,Rec_REG,32);	/* Read Data form RF to Receive[]*/
	while(i<32 && Counter<ByteNum)
	{
	    ReceiveData[Counter]=Rec_REG[i];
	    i++;
	    Counter++;
	}
	ByteCounter=ByteCounter+32;
	Write_RF(0x27,SEND_REG,1);
	Write_RF(Clear_RX,Rec_REG,1);		/* Clear RX FIFO for next data	*/
    }
/*    Send_to_Receive=0x0;	*/
				/* Hole the CE pin to 0*/
    return 1;
}

/************************************************************************/
/*  Function : Initial the RF						*/	
/*  Parameter: None							*/
/************************************************************************/
void PTA6000PA_initialization()
{
    int temp;
    temp=PTR6000PA_init();          
    if(temp==1)
    {
     	printf("\n		***************************************\n");
     	printf("		*                                     *\n");
     	printf("		*  PTA6000PA initialization success!  *\n");
    	printf("		*                                     *\n");
    	printf("		***************************************\n");
    }
    else
    {
	printf("RF initial have some problem.\n");
	printf("RF may be can't work correct!\n");
	printf("Please Check RF!\n");
    }
}

/************************************************************************/
/*  Function : Change the operate Channel				*/
/*  Parameter: num is the channel number				*/
/************************************************************************/

void SETChannel(char num)
{
    char REG[4];
    REG[0]=num;
    Write_RF(0x25,REG,1);
}

/************************************************************************/
/* Function : Change the operate power					*/
/*  Parameter: Power is  0: -18dbm					*/
/*		 	 1: -12dbm					*/
/*			 2: -6dbm					*/
/*		 	 3: 0dbm					*/
/*  The output power is the RF ,not the PRT6000PA  			*/    
/************************************************************************/
void SETRFPOWER(char Power)
{
    char REG[4];
    Read_RF(0x06,REG,1);
    REG[0]=REG[0] & 0xf9;
    REG[0]=REG[0] | (Power<<1);
    Write_RF(0x26,REG,1);
}

/************************************************************************/
/*  Function : Change RF send speed					*/
/*  Parameter: Power is  0: 1Mbsp					*/
/*		 	1: 2Mbps     					*/
/************************************************************************/
void SETRFSPEED(char Speed)
{
    char REG[4];
    Read_RF(0x06,REG,1);
    REG[0]=REG[0] & 0xf7;
    REG[0]=REG[0] | (Speed<<3);
    Write_RF(0x26,REG,1);
}

/************************************************************************/
/*  Function : Detect the carrier					*/
/*  Parameter: None							*/
/*  Return   : 0 : Current frequence no Carrier 			*/   
/*  	     : 1 : Current frequence is being use			*/
/************************************************************************/
char CDetect()
{
    char REG[4];
    Read_RF(0x09,REG,1); 
    REG[0]=REG[0] & 0x1;
    if(REG[0]==1)
	return 1;
    else
	return 0; 
}

/************************************************************************/
/*  Function : Initial PTA6000PA					*/
/*  Parameter: None							*/
/*  Return   : 1 : Initial Success    					*/
/*  	     : 0 : Initial fail						*/
/************************************************************************/
int PTR6000PA_init()
{
     char Temp=0;
     char *pSendData;
     char ReadTemp;
     char trynum=0;
     char ConfigSuccess;
     char Reg_Temp[32];
     char CMD;
     int RF_ADDR;
     printf("PTR6000PA initialization ......\n\n");
     printf("PTA6000PA initilizating......\n");
     pSendData=pDatatobeSend;		/* "pSendData" is the first addr of send data to RF*/
     /****** Configure REGISTER 0 *****/
     ConfigSuccess=0;
     trynum=0;
     printf("\nConfigure REGISTER 0 :");
     printf("Enable RX interrupt\n");
     printf("		      Enable TX interrupt\n");
     printf("		      Disable Max RT\n");
     printf("		      Enable CRC\n");
     printf("		      1 Byte CRC encoding\n");
     printf("		      POWER UP\n");
     printf("		      Receive mode\n");
     ConfigSuccess=0;
     while(ConfigSuccess==0)
     {
	Reg_Temp[0]=CONFIG;
	CMD=0x20;			/* Write Num 0 Register addr */
	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 0 */
	CMD=0x00;			/* Read Num 0 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==CONFIG)    	/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 0 Success! Configure REGISTER 0: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 0 faile ! ! ! \n");
	    return 0;
	}
	
     }

     /****** Configure REGISTER 1 *****/

     printf("\nConfigure REGISTER 1:\n");
     printf("		      Disable auto ack datapipe 0\n");
     printf("		      Disable auto ack datapipe 1\n");
     printf("		      Disable auto ack datapipe 2\n");
     printf("		      Disable auto ack datapipe 3\n");
     printf("		      Disable auto ack datapipe 4\n");
     printf("		      Disable auto ack datapipe 5\n");
     ConfigSuccess=0;
     trynum=0;
     while(ConfigSuccess==0)
     {
 	Reg_Temp[0]=EN_AA;
	CMD=0x21;
     	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 1 */
	CMD=0x01;			/* Read Num 1 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==EN_AA)    		/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 1 Success! Configure REGISTER 4: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 1 faile!\n");
	    return 0;
	}
     }

     /****** Configure REGISTER 2 *****/

     printf("\nConfigure REGISTER 2:\n");
     printf("		      Enable  data pipe 0\n");
     printf("		      Disable data pipe 1\n");
     printf("		      Disable data pipe 2\n");
     printf("		      Disable data pipe 3\n");
     printf("		      Disable data pipe 4\n");
     printf("		      Disable data pipe 5\n");

     ConfigSuccess=0;
     trynum=0;
     while(ConfigSuccess==0)
     {
	Reg_Temp[0]=EN_RXADDR;
	CMD=0x22;
	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 2 */
	CMD=0x02;			/* Read Num 2 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==EN_RXADDR)    	/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 2 Success! Configure REGISTER 2: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 2 faile!\n");
	    return 0;
	}
     }

     /****** Configure REGISTER 3 *****/

     printf("\nConfigure REGISTER 3:\n");
     printf("		      4 Byte Addr\n");

     ConfigSuccess=0;
     trynum=0;
     while(ConfigSuccess==0)
     {
	Reg_Temp[0]=SETUP_AW;
	CMD=0x23;
	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 3 */
	CMD=0x03;			/* Read Num 3 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==SETUP_AW)    	/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 3 Success! Configure REGISTER 3: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 3 faile!\n");
	    return 0;
	}
     }


     /****** Configure REGISTER 4 *****/
     printf("\nConfigure REGISTER 4:\n");
     printf("		      Auto Re-transmit Delay\n");
     printf("		      Auto Retransmit disable\n");

     ConfigSuccess=0;
     trynum=0;
     while(ConfigSuccess==0)
     {
 	Reg_Temp[0]=SETUP_RETR;
	CMD=0x24;
	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 4 */
	CMD=0x04;			/* Read Num 4 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==SETUP_RETR)    	/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 4 Success! Configure REGISTER 4: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 4 faile!\n");
	    return 0;
	}
     }


     /****** Configure REGISTER 5 *****/

     printf("\nConfigure REGISTER 5:\n");
     printf("		      Select Channel 110\n");

     ConfigSuccess=0;
     trynum=0;
     while(ConfigSuccess==0)
     {
 	Reg_Temp[0]=RF_CH;
	CMD=0x25;
	Write_RF(CMD,Reg_Temp,1);	/* Write the data to Refister 5 */
	CMD=0x05;			/* Read Num 5 Register addr */
	Reg_Temp[0]=0;
	Read_RF(CMD,Reg_Temp,1);
	if(Reg_Temp[0]==RF_CH)    		/* Read bacck and judge */
	    {
		printf("		      Configure REGISTER 5 Success! Configure REGISTER 5: %d  times\n",ConfigSuccess+1);
	    	ConfigSuccess=1;
	    }
	trynum++;
        if(trynum==Configtrynum)
	{
	    printf("		      Configure REGISTER 5 faile!\n");
	    return 0;
	}
     }


     /****** Configure REGISTER 6 *****/

     printf("\nConfigure REGISTER 6:\n");

⌨️ 快捷键说明

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