📄 rf_blink_led.c
字号:
/*******************************************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC2420DBK EXAMPLES *
* *** + + *** RF Blinking LED Demo - Application Note 033 *
* *** +++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************************************
* 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 of *
* the LED on the MSP-FET430P140. * *
* This example is based on the blinking LED example for the CC2420DB board. *
*******************************************************************************************************
* Compiler: MSP430 IAR C/C++ compiler or msp430-gcc GNU C compiler *
* Target platform: MSP-FET430P140 (can easily be ported to other platforms) *
*******************************************************************************************************
* Revision history: *
* $Log: rf_blink_led.c,v $
* Revision 1.5 2004/07/26 11:18:13 mbr
* Changed PANID from 0xDEAD to 0x2420
*
* Revision 1.4 2004/04/05 08:25:52 mbr
* Comments changed in header
*
* Revision 1.3 2004/03/30 14:58:27 mbr
* Release for web
*
*
*
* *
*
*
*******************************************************************************************************/
#include <include.h>
//-------------------------------------------------------------------------------------------------------
// Defintions used locally in this file.
#define PAYLOAD_SIZE 10
#define RF_CHANNEL 26
#define TX_PERIOD 50 // Packet sent each n'th cycle
#ifdef __ICC430__
#define FILL_BYTE 0xFF
#else
#define FILL_BYTE 0xEE
#endif
static BYTE ledPeriod= 0x80; // LED blinking frequency
static UINT16 nRecv = 0; // Counting received packets
void ledFlash(UINT16 duration, UINT16 period);
//-------------------------------------------------------------------------------------------------------
// Basic RF transmission and reception structures
BASIC_RF_RX_INFO rfRxInfo;
BASIC_RF_TX_INFO rfTxInfo;
BYTE pTxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];
BYTE pRxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// 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
//-------------------------------------------------------------------------------------------------------
// 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_BYTE;
}
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]= (BYTE)iLoopCount;
pTxBuffer[1] = status;
pTxBuffer[2] = nRecv<<8;
pTxBuffer[3] = (BYTE)(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
//-------------------------------------------------------------------------------------------------------
// 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.
//
//-------------------------------------------------------------------------------------------------------
void ledFlash(UINT16 cycles, UINT16 period) {
for (int i=0; i<cycles; i++) {
TOGGLE_YLED();
if (period>0)
halWait(period);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -