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

📄 rf_blink_led.c

📁 基于msp430f149与CC2420的无线传感器网络平台
💻 C
字号:
/************************************************************************************

  Filename:     rf_blink_led.c

  Description:

    This program demonstrates the use of the CC2420DB library, including the basic RF
    library. The packet protocol being used is a small subset of the IEEE 802.15.4
    standard. It uses an 802.15.4 MAC compatible frame format, but does not implement
    any other MAC functions/mechanisms (e.g. CSMA-CA). The basic RF library can thus
    not be used to communicate with compliant 802.15.4 networks.

    A MSP430F1611 running this program will establish a point-to-point RF link on
    channel 26 with a CC2420DB using the following node addresses:
    - PAN ID: 0x2420 (both nodes)
    - Short address:    	0x5678
    - Destination address:  0x1234

    This application is intended to work with the blinking LED application on a
    CC2420DB. The pot-meter on the CC2420DB can be used to change the blinking
    frequency of the diode on the MSP-FET430P140 board.

    Please note that there is no so-called (PAN) coordinator.

    INSTRUCTIONS:
    Outgoing data packets containing a 10-byte payload will be transmitted each 50th
    cycle of the  main program loop. Byte 0 contains the LED dimmer value to the
    CC2420DBB, byte 1 contains the status byte from CC2420 and bytes 2/3 the number
    of packets received from the CC2420DB. This application uses the value of byte 0
    FROM the CC2420DB to regulate the blinking frequency ofthe LED on the MSP-FET430P140.                                                                                                                                                                             *

    This example is based on the blinking LED example for the CC2420DB board.

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

#include <include.h>

//-----------------------------------------------------------------------------------
// PRIVATE CONSTANTS

#define PAYLOAD_SIZE	10
#define RF_CHANNEL		26
#define TX_PERIOD       50  // Packet sent each n'th cycle

#ifdef __ICC430__
#define FILL_UINT8    0xFF
#else
#define FILL_UINT8    0xEE
#endif

//-----------------------------------------------------------------------------------
// PRIVATE DATA

static UINT8 ledPeriod= 0x80;    // LED blinking frequency
static UINT16 nRecv =  0;       // Counting received packets

// Basic RF transmission and reception structures
static BASIC_RF_RX_INFO rfRxInfo;
static BASIC_RF_TX_INFO rfTxInfo;
static UINT8 pTxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];
static UINT8 pRxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];

//-----------------------------------------------------------------------------------
// PRIVATE FUNCTION PROTOTYPES

static void ledFlash(UINT16 duration, UINT16 period);



//-----------------------------------------------------------------------------------
//	int main (void)
//
//	DESCRIPTION:
//		Startup routine and main loop
//-----------------------------------------------------------------------------------
int main (void)
{
    UINT8 n;
    UINT32 iLoopCount;

    // Initalize ports for communication with CC2420 and other peripheral units
    PORT_INIT();
    SPI_INIT();

    // Wait for the user to select node address, and initialize for basic RF operation
	halWait(1000);

	basicRfInit(&rfRxInfo, RF_CHANNEL, 0x2420, 0x5678);
	rfTxInfo.destAddr = 0x1234;


    // Initalize common protocol parameters
    rfTxInfo.length = PAYLOAD_SIZE;
    rfTxInfo.ackRequest = TRUE;
    rfTxInfo.pPayload = pTxBuffer;
    rfRxInfo.pPayload = pRxBuffer;

    for (n = 0; n < PAYLOAD_SIZE; n++) {
        pTxBuffer[n] = FILL_UINT8;
    }
    iLoopCount= 0;
    CLR_YLED();

    // Turn on RX mode
    basicRfReceiveOn();

	// The main loop:
	while (TRUE) {

        if ( ( iLoopCount % TX_PERIOD) == 0) {	
		    UINT8 status;

		    FASTSPI_UPD_STATUS(status);

		    // Transmit information on number of packets received
		    // and CC2420 status

		    pTxBuffer[0]=  (UINT8)iLoopCount;
			pTxBuffer[1] = status;
			pTxBuffer[2] = nRecv<<8;
			pTxBuffer[3] = (UINT8)(nRecv & 0x00FF);

    		if (basicRfSendPacket(&rfTxInfo)) {

    			// OK -> Blink the LED fast
				ledFlash(10, 5000);
			} else {

				// No acknowledgment received -> Blink the LED slow
				ledFlash(5, 10000);
			}
			
		} else {
		    ledFlash(1,ledPeriod*256);
		}
		iLoopCount++;
		
    }

} // main




//-----------------------------------------------------------------------------------
//  BASIC_RF_RX_INFO* basicRfReceivePacket(BASIC_RF_RX_INFO *pRRI)
//
//  DESCRIPTION:
//      This function is a part of the basic RF library, but must be declared by the application. Once
//		the application has turned on the receiver, using basicRfReceiveOn(), all incoming packets will
//		be received by the FIFOP interrupt service routine. When finished, the ISR will call the
//		basicRfReceivePacket() function. Please note that this function must return quickly, since the
//		next received packet will overwrite the active BASIC_RF_RX_INFO structure (pointed to by pRRI).
//
//  ARGUMENTS:
//		BASIC_RF_RX_INFO *pRRI
//	      	The reception structure, which contains all relevant info about the received packet.
//
//  RETURN VALUE:
//     BASIC_RF_RX_INFO*
//			The pointer to the next BASIC_RF_RX_INFO structure to be used by the FIFOP ISR. If there is
//			only one buffer, then return pRRI.
//-----------------------------------------------------------------------------------
BASIC_RF_RX_INFO* basicRfReceivePacket(BASIC_RF_RX_INFO *pRRI)
{

    ledPeriod= pRxBuffer[0];
    // Continue using the (one and only) reception structure
    return pRRI;

} // basicRfReceivePacket


//-----------------------------------------------------------------------------------
//	void ledFlash(UINT16 cycles, UINT16 period)
//
//  DESCRIPTION:
//      Toggles the led 'cycles' times with a half-cycle duration of 'period' uSeconds.
//
//  ARGUMENTS:
//		UINT16 cycles
//	      	Number of times to toggle the LED
//      UINT16 period
//          Half-cycle period, i.e the time the LED is on respectively off.
//
//-----------------------------------------------------------------------------------
static void ledFlash(UINT16 cycles, UINT16 period)
{
   	for (int i=0; i<cycles; i++) {
    	TOGGLE_YLED();
    	if (period>0)
    	    halWait(period);
    }
}



/***********************************************************************************
  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS

⌨️ 快捷键说明

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