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

📄 rf_blink_led.c

📁 基于ATmega128的无线通信节点程序
💻 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+1];
BYTE pRxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE+1];
//-------------------------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------------------------
//  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.
//-------------------------------------------------------------------------------------------------------

typedef union{
WORD addr;
BYTE val[2];
}SHORT_ADDR;

typedef struct {
	BYTE Num;
	SHORT_ADDR PanId;
	SHORT_ADDR Addr;
} NODE_INFO;

NODE_INFO NODE[7]={0,0x2420,0x0101,1,0x2420,0x7971,2,0x2420,0x7972,3,0x2420,0x7973,4,0x2420,0x7974,5,0x2420,0x7975,6,0x2420,0x7976};

#define NODE_ID 1

/*****************************************************************/
typedef struct {
	int startnode;
	int endnode;
	int channel;
}com;


com communicate[30]={1,2,102,1,3,103,1,4,124,1,5,135,1,6,136,
			2,1,201,2,3,213,2,4,204,2,5,265,2,6,206,
			3,1,301,3,2,312,3,4,364,3,5,365,3,6,306,
	        4,1,421,4,2,402,4,3,463,4,5,465,4,6,406,
			5,1,531,5,2,562,5,3,503,5,4,564,5,6,506,
			6,1,631,6,2,602,6,3,603,6,4,604,6,5,605
};//静态路由
int cha(int n,int m)//查找函数
{

	for (int i=0;i<30;i++)
	{

		if ((n==communicate[i].startnode)&&(m==communicate[i].endnode))
		{
			return communicate[i].channel;
		}
			
	}
	return -1;
}

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

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();
ConsolePutString(pRxBuffer);
  /***************************************************/
    UINT16 ledDutyCycle, dimmerDifference;
    UINT8 n;
	BYTE temp;
SHORT_ADDR dest;

dest.val[0]=pRxBuffer[5];
dest.val[1]=pRxBuffer[4];

  if(dest.val[0]!=NODE[NODE_ID].Addr.val[0]){
  int road1, goal1,tiao1;
	goal1=dest.val[0]-0x70;
	road1=cha(NODE_ID,goal1);	
	
  if((tiao1=road1%100/10)!=0)
	{ dest.val[0]=tiao1+0x70;}
	else{dest.val[0]=road1%10+0x70;}
	
pTxBuffer[0]=NODE[NODE_ID].Addr.val[1];//79本机
pTxBuffer[1]=NODE[NODE_ID].Addr.val[0];//71
pTxBuffer[4]=pRxBuffer[4];
pTxBuffer[5]=pRxBuffer[5];
pTxBuffer[6]=pRxBuffer[6];
pTxBuffer[2]=dest.val[1];
pTxBuffer[3]=dest.val[0];

//ConsolePut(pTxBuffer[0]);
//ConsolePut(pTxBuffer[1]);
//ConsolePut(pTxBuffer[2]);
//ConsolePut(pTxBuffer[3]);
//ConsolePut(pTxBuffer[4]);
//ConsolePut(pTxBuffer[5]);
//ConsolePut(pTxBuffer[6]);


rfTxInfo.length= pTxBuffer[6];
for(n=7;n<rfTxInfo.length+1;n++){pTxBuffer[n]=pRxBuffer[n];}
 rfTxInfo.destAddr=dest.addr;//71

  		if (!basicRfSendPacket(&rfTxInfo)) 			    // No acknowledgment received -> Blink the red LED
		{
			SET_RLED();
			 halWait(50000);
			CLR_RLED();
		}
  	}	
  	
  else {
  		SET_RLED();
  		halWait(500000000);
		CLR_RLED();
	   }
  /************************************************************/
      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;
	BYTE temp;
SHORT_ADDR dest;
    // Initalize ports for communication with CC2420 and other peripheral units
    PORT_INIT();
	PORTE=1<<3;
//	DDRD=0xFF;
//	PORTD=0xFF;
    SPI_INIT();

ConsoleInit() ;
//ConsolePutString("hello!");
    // 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

basicRfInit(&rfRxInfo, 11,NODE[NODE_ID].PanId.addr, NODE[NODE_ID].Addr.addr);


   // Turn on RX mode
    basicRfReceiveOn();



    // Initalize common protocol parameters
    rfTxInfo.length = 10;
    rfTxInfo.ackRequest = TRUE;
    rfTxInfo.pPayload = pTxBuffer;
    rfRxInfo.pPayload = pRxBuffer;
SET_YLED();
    for (n = 0; n < 10; n++) {
        pTxBuffer[n] = n;
    }

	// The main loop:
	while (TRUE) {


//刷新缓冲区
while ( UCSR0A & (1<<RXC0) ) 
	temp= UDR0;	

 ConsoleGet(temp);
 	while(temp!=0x3F)
		ConsoleGet(temp);


	SET_RLED();
halWait(5000000);
	CLR_RLED();

rfTxInfo.length= ConsoleGetString(pTxBuffer,BASIC_RF_MAX_PAYLOAD_SIZE);
rfTxInfo.length= rfTxInfo.length+5;


pTxBuffer[7]=pTxBuffer[2];
pTxBuffer[8]=pTxBuffer[3];
pTxBuffer[9]=pTxBuffer[4];
pTxBuffer[10]=pTxBuffer[5];
pTxBuffer[11]=pTxBuffer[6];

dest.val[0]=pTxBuffer[1];
dest.val[1]=pTxBuffer[0];
rfTxInfo.destAddr=dest.addr;//71
pTxBuffer[4]=dest.val[1];//79
pTxBuffer[5]=dest.val[0];//71目

⌨️ 快捷键说明

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