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

📄 main.c

📁 Integration的EZMac Plus
💻 C
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 *                                                                          *
 * FILE NAME:   main.c                                                      *
 *                                                                          *
 * DESIGNER:	Kovacs, Krisztian; Lukacs, Miklos                           *
 * PROGRAMMER: Kovacs, Krisztian; Lukacs, Miklos; Szell Andras              *
 *                                                                          *
 * DESCRIPTION: EZMac test environment                                      *
 *                                                                          *
 * REVISION:    01_2  March  04, 2005   deviation 75kHz                     *
 *              01_3  March  23, 2005   Transmit and receive packet lenght  *
 *                                      can be different in fixed leght mode*
 *              01_4  April  20, 2005   EZradio initialization fime tuning, *
 *                                      Wake-up timer is in on state        *
 *                                      between Wake_Up() and Sleep()       *
 *                                      (not correct POR fix)               *
 *              01_5  May    24, 2005                                       *
 *              01_6  Aug    11, 2005                                       *
 *			    02_0  March	 21, 2006	Finalize the HiTech porting			*
 *	EZMac Plus:																*
 *				01_0  Sept	 28, 2006	Create EZMac Plus from EZMac		*	
 *                                                                          *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

//only for internal usage!
//#define EZMAC_TEST_SYSTEM

/***************************************************************************\
 *			Set EZMac main parameters					   				   *
 *	    NOTE: only one parameter has to be selected from every group!	   *
\***************************************************************************/
//Select data rate
#define DATA_RATE_9600						//The clock of the PIC should be 4MHz
//#define DATA_RATE_19200						//The clock of the PIC should be 8MHz
//#define DATA_RATE_38400						//The clock of the PIC should be 16MHz
//#define DATA_RATE_57600						//The clock of the PIC should be 24MHz
//#define DATA_RATE_115200						//The clock of the PIC should be 48MHz

//Select frequency band
//#define BAND_315
//#define BAND_434
//#define BAND_868
#define BAND_915

#include "IAI_EZMac_Plus_PIC.c"
#ifdef EZMAC_TEST_SYSTEM
 #include "IAI_EZMac_SerialControl.c"
#endif


//===============================================================================================
// system initialization
#pragma separate
void MAIN_init(void)
{
   //these ports are mainly used for debug; the definitions are made for the PIC SW development board
   PB1_DIR = 1;
   PB2_DIR = 1;
   PB3_DIR = 1;
   PB4_DIR = 1;

   LED1_DIR = 0;
   LED2_DIR = 0;
   LED3_DIR = 0;
   LED4_DIR = 0;

   LED1 = 1;
   LED2 = 1;
   LED3 = 1;
   LED4 = 1;
}


//===============================================================================================
#pragma separate
void PrintPacket(void)
{
int8 Buffer[16];
int8 i,Value,Header[5];

   //read the buffer
   EZMacBuf_Read(Buffer);

   //read the Rx Status register and the header
   EZMacReg_Read(RSR,&Header[0]);
   EZMacReg_Read(R_CID,&Header[1]);
   EZMacReg_Read(SID,&Header[2]);
   EZMacReg_Read(DID,&Header[3]);
   EZMacReg_Read(PLEN,&Header[4]);
   //print the status register
   printf("RSR: %02x, Packet: ",Header[0]);
   //print the header
   printf("%02x ",ControlField);
   for(i=1;i<5;i++)
      printf("%02x ",Header[i]);
   printf("  ");
   //print the Data
   for(i=0;i<Header[4];i++)
      printf("%02x ",EZ_reg.bits.DBuff[i]);
#ifdef EZMAC_BOARD_WITH_ADC
      printf("ARSSI: %x",ARSSI_value);
#endif
   printf("\n\r");
}


//===============================================================================================
#pragma separate
int8 PrintErrorCounters(int8 print)
{
int8 i,iRet;
int8 ec;
int8 Deleted;

   Deleted = FALSE;
   ec = EC0;
   if(print)
      printf("E.C: ");
   for(i=0;i<4;i++)
      {
      EZMacReg_Read(ec,&iRet);
      if(print)
         printf("%x ",iRet);
      if(iRet == 255)
         {//reset the error counter
         EZMacReg_Write(ec,0);
         Deleted = TRUE;
         }
      ec++;
      }
   if(print)
      printf("\n\r");
   return Deleted;
}

//===============================================================================================
void Rx_Test(void)
{
static BOOL Receiving;
int8 iRet;
int8 Buffer[16];
int8 SID_1;
//Init the devices
   EZMacReg_Write (MCR,0x3a);      	//ciden,dnpl,3 freq   changed for ver1.3 test -> fix packet length
   EZMacReg_Write (CID,0xCD);		//set CID
   EZMacReg_Write (RPL_MPL,0x03);   //added for ver1.3 test -> the device will receive 3 bytes
   EZMacReg_Write (PLEN,0x03);      //added for ver1.3 test -> the device will send 3 bytes
   EZMacReg_Write (PFWCR,0x81);		//Enable ACK and forwarding

//set the freq registers
   EZMacReg_Write (F0,1);
   EZMacReg_Write (F1,3);
   EZMacReg_Write (F2,5);
   EZMacReg_Write (F3,7);

// RxConfig
   EZMacReg_Write (RCR,0x08);      //schen,0.freq
   EZMacReg_Write (FMASK,0xf8);    //F[7-4] are masked
   EZMacReg_Write (EDCR,0x40);     //error counting: Tx-Rx Sync. error
   EZMacReg_Write (SFID,0x01);
   EZMacReg_Write (PFCR,0x20);     //Destination filter on
   
//delete the error counters
   EZMacReg_Write (EC0,0);
   EZMacReg_Write (EC1,0);
   EZMacReg_Write (EC2,0);
   EZMacReg_Write (EC3,0);

  //wake up the board
   EZMac_Wake_Up();
   do {
      EZMacReg_Read(MSR,&iRet);
   }while(iRet != EZMac_S_Idle);
  
   //start test
   while(1)
   {
      delay_ms(1);

	  LED1 = 1;
      EZMac_Receive();
      Receiving = TRUE;

      //wait for receiving
      while( Receiving )
      {
         EZMacReg_Read(MSR,&iRet);
         if (iRet==EZMac_S_PacketValid)                       //wait for a valid packet
         {
            //switch on the RX success LED
            LED1 = 0;
            //print the packet
			PrintPacket();
			EZMac_Idle();
			EZMacReg_Read(SID,&SID_1);
            //end of receiving
            Receiving = FALSE;
         }
      }
	}
}

//===============================================================================================
#pragma separate
void Tx_Test(void)
{
   #define TCR_VALUE 0x08 //+3db

   int8 i,iRet,Freq,node,TR_String[3];
   static int16 PacketNmbr;
   static Transmitting;
   
   //Init the devices
   EZMacReg_Write (MCR,0x3a);      	//ciden,dnpl,3 freq   changed for ver1.3 test -> fix packet length
   EZMacReg_Write (TCR,TCR_VALUE);  //lbten,+6dB,0.freq
   EZMacReg_Write (CID,0xCD);		//set CID
   EZMacReg_Write (RPL_MPL,0x03);   //added for ver1.3 test -> the device will receive 3 bytes
   EZMacReg_Write (PLEN,0x03);      //added for ver1.3 test -> the device will send 3 bytes
   
   //set the freq registers
   EZMacReg_Write (F0,1);
   EZMacReg_Write (F1,3);
   EZMacReg_Write (F2,5);
   EZMacReg_Write (F3,7);
   
   // TxConfig
   EZMacReg_Write (PFWCR,0x01);		// EzMAC plus Enable ACK and forwarding
   EZMacReg_Write (RCR,0x09);      //schen,1.freq
   EZMacReg_Write (FMASK,0xf8);    //F[7-4] are masked
   EZMacReg_Write (EDCR,0x80);     //error counting: channel is busy
   EZMacReg_Write (SFID,0x01);
   EZMacReg_Write (PFCR,0x20);     //Destination filter on
	
   //delete the error counters
   EZMacReg_Write (EC0,0);
   EZMacReg_Write (EC1,0);
   EZMacReg_Write (EC2,0);
   EZMacReg_Write (EC3,0);

   //wake up the board
   EZMac_Wake_Up();
   do {
      EZMacReg_Read(MSR,&iRet);
   }while(iRet != EZMac_S_Idle);

   Freq = 0;
   PacketNmbr = 0;
 
   //start test
   while(1)
   {
      //wait for send the packet
	  delay_ms(100);
    
	  //switch off the LEDs
      LED1 = 1;                      //switch off action successfull made LED
      LED2 = 1;                      //switch off error LED
    
	  delay_ms(100);
      //go to idle state
      EZMac_Idle();

	  //set the Frequency
      EZMacReg_Write(TCR,((TCR_VALUE & 0xF8 ) | Freq));

	  //set destination
	  EZMacReg_Write(DID,0x01);                

      //set packet length
      EZMacReg_Write(PLEN,3);
      
	  //set the packet
      TR_String[0] = Freq;
      TR_String[1] = (int8)((PacketNmbr & 0xff00) >> 8);
      TR_String[2] = (int8)(PacketNmbr & 0x00ff);
      
	  EZMacBuf_Write (&TR_String[0]);

	  LED1 = 0;                      			  //switch on TX LED
      //start the TX
      EZMac_Transmit();
      Transmitting = TRUE;
	  
      while(Transmitting)
      {
         EZMacReg_Read(MSR,&iRet);
		 if ((iRet==EZMac_S_TxError)||(iRet==EZMac_S_AckReceiveError))
         {
            LED2 = 0;							 //switch on TX error LED
			Transmitting = FALSE;
         }
		 
         if(iRet == EZMac_S_Idle)
 		 {
			Transmitting = FALSE;
		 }
      }
	  //inc the counters, set next frequency
      if(Freq < 0x02)
         Freq++;
      else
         Freq = 0;
	  PacketNmbr++;
   }
}

//===============================================================================================
void main(void)							// Prg step in point
{
#ifdef HITECH_COMPILER
   init_uart();
#endif

#ifdef EZMAC_TEST_SYSTEM				//if test system is compiled into the code, the application doesn't run
   enable_INT_RDA;
#endif

   MAIN_init();
   EZMac_SysInit();
   EZMac_Config();
   EZMac_Init();
   delay_ms(100);

#ifdef EZMAC_TEST_SYSTEM
   IAI_EZMac_SerialControl_Task();
#else
   printf("\n\rEZmac Plus Demo software 09/28/2006\n\r");
   Rx_Test();
  // Tx_Test();
#endif
}



⌨️ 快捷键说明

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