示例#1
0
void  main (void)
{

/*------------------------------------------------------------------------------
 *    SETUP PHASE
 *------------------------------------------------------------------------------
 */
        //Set DMD interrupt to high priority,
        // any other interrupts have to stay low priority
        PDMD=1;
        // Disable the Matrix and Roff modes on GPIO[3:1] 
        PORT_CTRL &= ~(M_PORT_MATRIX | M_PORT_ROFF | M_PORT_STROBE);
        PORT_CTRL |=  M_PORT_STROBE;
        PORT_CTRL &= (~M_PORT_STROBE);
        // Turn LED control off 
        GPIO_LED = 0;
        vSys_Setup( 10 );
        vSys_SetMasterTime(0);
        // Setup the bandgap 
        vSys_BandGapLdo( 1 );

        if ((PROT0_CTRL & M_NVM_BLOWN) > 1) //if part is burned to user or run mode.
        {
                // Check the first power up bit meaning keyfob is powered on by battery insertion
                if ( 0 != (SYSGEN & M_POWER_1ST_TIME) )
                {
                        vSys_FirstPowerUp(); // Function will shutdown.
                }
        }
        // Set LED intensity. Valid values are 0 (off), 1, 2 and 3 
        vSys_LedIntensity( 3 );
        lLEDOnTime=20;
        //Get part ID (4byte factory burned unique serial number)
        lPartID = lSys_GetProdId();
        // Initialize isr call counter 
        bIsr_DebounceCount = 0;

        // BSR parameters initialization 
        rBsrSetup.bButtonMask = bButtonMask_c; 
#ifdef CRYSTAL
        rBsrSetup.bButtonMask &= 0xFE;// clear bit0; GPIO0 will be used by crystal
#endif
        rBsrSetup.pbPtsReserveHead = abBsrPtsPlaceHolder;
        rBsrSetup.bPtsSize = 3;
        rBsrSetup.bPushQualThresh = 3;
        // Setup the BSR 
        vBsr_Setup( &rBsrSetup );

        // Setup the RTC to tick every 5ms and clear it. Keep it disabled. 
        RTC_CTRL = (0x07 << B_RTC_DIV) | M_RTC_CLR;
        // Enable the RTC 
        RTC_CTRL |= M_RTC_ENA;
        // Enable the RTC interrupt and global interrupt enable 
        ERTC = 1;
        EA = 1;

        fDesiredFreqOOK 	 = f_433_RkeFreqOOK_c;
        fDesiredFreqFSK 	 = f_433_RkeFreqFSK_c;
        bFskDev 		 = b_433_RkeFskDev_c;
        // Setup the PA.
        rPaSetup.bLevel      = b_433_PaLevel_c;
        rPaSetup.wNominalCap = b_433_PaNominalCap_c;
        rPaSetup.bMaxDrv     = b_433_PaMaxDrv_c;
        rPaSetup.fAlpha      = 0.0;
        rPaSetup.fBeta       = 0.0;
        vPa_Setup( &rPaSetup );

#ifdef OOK
rOdsSetup.bModulationType = bModOOK_c;
// Setup the STL encoding for Manchester. No user encoding function therefore the pointer is NULL. 
vStl_EncodeSetup( bEncodeManchester_c, NULL );
fDesiredFreq = fDesiredFreqOOK;
bPreamble = bPreambleManch_c;
#else //FSK
rOdsSetup.bModulationType = bModFSK_c;
// Setup the STL encoding for none. No user encoding function therefore the pointer is NULL.
vStl_EncodeSetup( bEnc_NoneNrz_c, NULL );
fDesiredFreq = fDesiredFreqFSK;
bPreamble = bPreambleNrz_c;
#endif
// Setup the ODS 
// Set group width to 7, which means 8 bits/encoded byte to be transmitted.
// The value must match the output width of the data encoding function
// set by vStl_EncodeSetup()! 
rOdsSetup.bGroupWidth     = 7;
rOdsSetup.bClkDiv         = 5;
rOdsSetup.bEdgeRate       = 0;
// Configure the warm up intervals 
rOdsSetup.bLcWarmInt  = 0;
rOdsSetup.bDivWarmInt = 5;
rOdsSetup.bPaWarmInt  = 4;
// 24MHz / (bClkDiv+1) / 9.6kbps = 417 
rOdsSetup.wBitRate        = 417;
vOds_Setup( &rOdsSetup );

// Setup frequency casting .. needed to be called once per boot 
vFCast_Setup();

#ifdef CRYSTAL
//XO Setup
rXoSetup.fXoFreq	= 10000000.0;   // Frequency of the external crystal [Hz]
rXoSetup.bLowCap	= 1;			// Capacitance setup of crystal 0 = above 14pf, 1 = below 14pf
vFCast_XoSetup( &rXoSetup );
#endif

// Measure the battery voltage in mV, only once per boot to save power
// Make loaded measurement .. bBatteryWait_c * 17ms wait after loading
iBatteryMv = iMVdd_Measure( bBatteryWait_c );
if (iBatteryMv < iLowBatMv_c) 
{
        bBatStatus = 0;
}
else
{
        bBatStatus = 1;
}
  
// Setup the DMD temperature sensor for temperature mode 
vDmdTs_RunForTemp( 3 ); // Skip first 3 samples 
// Wait until there is a valid DMD temp sensor sample 
while ( 0 == bDmdTs_GetSamplesTaken() )
{
        //wait
}

 /*------------------------------------------------------------------------------
  *    TRANSMISSION LOOP PHASE
  *------------------------------------------------------------------------------
  */

// Application loop, including push button state analyzation and transmission. 
while(1)
{
// Buttons analyzation 
        vButtonCheck();
        if (bButtonState)
        {
                // Packet transmit repeat counter
	        bRepeatCount = bRepeatCount_c;   
                // Transmit. 
                vRepeatTxLoop();
                //Sync counter increment
        }
        else if( (lSys_GetMasterTime() >> 5) > bMaxWaitForPush_c )
        {
	        if ((PROT0_CTRL & M_NVM_BLOWN) > 1) //if part is burned to user or run mode.
  		{
                        //Disable all interrupts
                        EA = 0;
#ifdef CRYSTAL
                        // Disable XO
			bXO_CTRL = 0 ; 		
                        // Wait 20us for XO to stop
                        vSys_16BitDecLoop( 20 );
#endif
                        // Shutdown
                        vSys_Shutdown();      	
		}
    }
  }
}
/*------------------------------------------------------------------------------
 *
 *    FUNCTION DESCRIPTION:
 *      This function tunes the si4010 for transmission and transmits frames.
 *      It consists of two fundamental phases, setup and the transmission loop.
 *
 *      Setup Phase:
 *        The Setup Phase calls all setup function for the need API modules.
 *        These functions are to be called once per boot of the chip.
 *          vSys_Setup
 *          vPa_Setup
 *          vOds_Setup
 *          vStl_EncodeSetup
 *          vFCast_Setup
 *
 *      Transmission Loop Phase:
 *        The Transmission Loop Phase is a loop which transmits the proper frame
 *		  once a button is pushed. Frequency casting, FSK adjustment
 *        and PA tuning are all performed between each transmission.
 *
 *------------------------------------------------------------------------------
 */
void main(void)
{
/*------------------------------------------------------------------------------
 *    SETUP PHASE
 *------------------------------------------------------------------------------
 */
//Set DMD interrupt to high priority,
// any other interrupts have to stay low priority
  PDMD=1;

// Parameters Initialization. 
  vAppInitial();


// Call the system setup. This just for initialization.
// Argument of 1 just configures the SYS module such that the
// bandgap can be turned off if needed.
  vSys_Setup( 1 );

// Setup the bandgap for working with the temperature sensor here 
  vSys_BandGapLdo( 1 );
 
// Setup the BSR 
  vBsr_Setup( &rBsrSetup );

// Setup the RTC to tick every 5ms and clear it. Keep it disabled. 
  RTC_CTRL = (0x07 << B_RTC_DIV) | M_RTC_CLR;

// Enable the RTC 
  RTC_CTRL |= M_RTC_ENA;

// Enable the RTC interrupt and global interrupt enable 
  ERTC = 1;
  EA = 1;

// Setup the PA 
  vPa_Setup( &rPaSetup );

// ODS setup 
  vOds_Setup( &rOdsSetup );

// Setup the STL encoding to NoneNrz code. we
// leave the pointer at NULL. 
//  vStl_EncodeSetup( bEnc_Manchester_c, NULL );
  vStl_EncodeSetup(   bEnc_NoneNrz_c, NULL );


// Setup frequency casting .. needed to be called once per boot 
  vFCast_Setup();



/*------------------------------------------------------------------------------
 *    TRANSMISSION LOOP PHASE
 *------------------------------------------------------------------------------
 */

 // Application loop, including push button state analyzation and transmission.
  while(1)
  {
    // Buttons analyzation and update task flag. 
    vButtonCheck();

    if (bAp_ButtonState)
    {
      // Set frame size and frame header pointer 
      vPacketAssemble();
	  if (lPayLoad)
	  {
      // Start to transmit
      vRtl_RepeatTxLoop();
      }
    }
    else if( (lSys_GetMasterTime() >> 5) > bMaxWaitForPush_c )
    {
      // Shutdown if timeout, currently after about 1.6s,
      // For debugging with IDE, comment it out since it does not support boot.
      vSys_Shutdown();
    }
  }
}