📄 rf_blink_led.c
字号:
/*******************************************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC2420DBK EXAMPLES *
* *** + + *** Simple wireless dimmer / RF range tester demo *
* *** +++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************************************
* 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 pair of CC2420DBs running this program will establish a point-to-point RF link on channel 26, *
* using the following node addresses: *
* - PAN ID: 0x2420 (both nodes) *
* - Short address: *
* 0x1234 if the joystick is moved in any direction at startup *
* 0x5678 if the joystick button is pressed down at startup *
* *
* Please note that there is no so-called (PAN) coordinator. *
* *
* INSTRUCTIONS: *
* Data packets containing a 5-byte payload will be transmitted when the pot meter is turned, or S2 is *
* held down. The first byte of the payload contains the pot meter value, which is used to control the *
* PWM duty cycle on the receiving node. The other bytes are random (never initialized). *
* *
* LED indicators: *
* - Red: Transmission failed (acknowledgment not received) *
* - Yellow: Transmission OK (acknowledgment received) *
* - Orange: Remote controlled dimmer *
* - Green: Packet received *
*******************************************************************************************************
* Compiler: AVR-GCC *
* Target platform: CC2420DB (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>
//-------------------------------------------------------------------------------------------------------
// 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) {
// Adjust the led brightness
PWM0_SET_DUTY_CYCLE(pRRI->pPayload[0]);
// Blink the green LED
SET_GLED();
halWait(10000);
CLR_GLED();
// Continue using the (one and only) reception structure
return pRRI;
} // basicRfReceivePacket
//-------------------------------------------------------------------------------------------------------
// void main (void)
//
// DESCRIPTION:
// Startup routine and main loop
//-------------------------------------------------------------------------------------------------------
void main (void) {
UINT16 ledDutyCycle, dimmerDifference;
UINT8 n;
// Initalize ports for communication with CC2420 and other peripheral units
PORT_INIT();
SPI_INIT();
// Initialize PWM0 with a period of CLK/1024
PWM0_INIT(TIMER_CLK_DIV1024);
// Initialize and enable the ADC for reading the pot meter
ADC_INIT();
ADC_SET_CHANNEL(ADC_INPUT_0_POT_METER);
ADC_ENABLE();
// Wait for the user to select node address, and initialize for basic RF operation
while (TRUE) {
if (JOYSTICK_CENTER_PRESSED()) {
basicRfInit(&rfRxInfo, 26, 0x2420, 0x1234);
rfTxInfo.destAddr = 0x5678;
break;
} else if (JOYSTICK_UP_PRESSED() || JOYSTICK_DOWN_PRESSED() || JOYSTICK_LEFT_PRESSED() || JOYSTICK_RIGHT_PRESSED()) {
basicRfInit(&rfRxInfo, 26, 0x2420, 0x5678);
rfTxInfo.destAddr = 0x1234;
break;
}
}
// Initalize common protocol parameters
rfTxInfo.length = 10;
rfTxInfo.ackRequest = TRUE;
rfTxInfo.pPayload = pTxBuffer;
rfRxInfo.pPayload = pRxBuffer;
for (n = 0; n < 10; n++) {
pTxBuffer[n] = n;
}
// Turn on RX mode
basicRfReceiveOn();
// The main loop:
while (TRUE) {
// Sample the pot meter value
ADC_SAMPLE_SINGLE();
ADC_GET_SAMPLE_8(ledDutyCycle);
// If the dimmer value has changed by more than 1, then transmit the new value automatically
// Transmit also when the S2 button is pressed
dimmerDifference = (ledDutyCycle & 0xFF) - pTxBuffer[0];
if ((ABS(dimmerDifference) > 2) || (JOYSTICK_CENTER_PRESSED())) {
pTxBuffer[0] = ledDutyCycle;
if (basicRfSendPacket(&rfTxInfo)) {
// OK -> Blink the yellow LED
SET_YLED();
halWait(50000);
CLR_YLED();
} else {
// No acknowledgment received -> Blink the red LED
SET_RLED();
halWait(50000);
CLR_RLED();
}
}
}
} // main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -