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

📄 rf_blink_led.c

📁 CC2420+PIC单片机基础实验代码、CC2420无线收发实验代码
💻 C
字号:
#include "include.h"

//-------------------------------------------------------------------------------------------------------
// Defintions used locally in this file.
#define SW1 RB4
#define SW2 RB5
#define SW3 RB6
#define SW4 RB7

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

#define FILL_BYTE    0xEE

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

void ledFlash(UINT16 duration, UINT16 period);

extern volatile BASIC_RF_SETTINGS rfSettings;
extern void halWait(UINT16 timeout);
extern void basicRfInit(BASIC_RF_RX_INFO *pRRI, UINT8 channel, WORD panId, WORD myAddr);
extern void basicRfReceiveOn(void);
extern BOOL basicRfSendPacket(BASIC_RF_TX_INFO *pRTI);
extern void EUSART_Init();
extern void sent_ch(unsigned char d);
//-------------------------------------------------------------------------------------------------------
// 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) {

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

} // basicRfReceivePacket

void port_init()
{
        TRISA1=0;
		TRISA2=0;
		TRISA3=0;
		TRISA5=0;
		TRISD6=0;
		TRISD7=0;
		TRISB=0xFF;
		TRISC0=0;
		TRISC1=0;
		TRISC2=0;
		TRISC3=0;
		TRISC4=1;
		TRISC5=0;
		LED1=0;
		LED2=0;
		LED3=0;
		ADCON1 = 0x0F;
} 

void halSpiInit(void) {	
// Initialize the SPI pins and directions
    LATC3               = 1;    // SCK
    LATC5               = 1;    // SDO
    TRISC3              = 0;    // SCK
    TRISC4              = 1;    // SDI
    TRISC5              = 0;    // SDO

    // Initialize the SPI module
    SSPSTAT = 0xc0;
    SSPCON1 = 0x20;
	SSPIF=0;
} // SpiInit
void ccp2_init()
{
	IPEN=1;
	GIEH=1;
	GIEL=1;
	CCP2IE=1;
	CCP2IF=0;
	CCP2IP=1;
	CCP2CON = 0b00000101;
}
//-------------------------------------------------------------------------------------------------------
//	int main (void)
//
//	DESCRIPTION:
//		Startup routine and main loop
//-------------------------------------------------------------------------------------------------------
void main (void) {
    UINT8 n;
    UINT32 iLoopCount;
    // Initalize ports for communication with CC2420 and other peripheral units
    port_init();
    halSpiInit();
	EUSART_Init();
	ccp2_init();
    // Wait for the user to select node address, and initialize for basic RF operation
	halWait(1000);

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

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

    for (n = 0; n < PAYLOAD_SIZE; n++) {
        pTxBuffer[n] = n+10;
    }
    iLoopCount= 0;
    // Turn on RX mode
    basicRfReceiveOn();
	// The main loop:
	while (TRUE)
	 {		
			LED2=0;
			LED3=0;
			LATA5=0;
			LATA3=0;
			LATD6=0;
			LATD7=0;
			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(CCA)
				{
		    		if (basicRfSendPacket(&rfTxInfo)) 
					{
						LED2=1;
						LATA5=1;
						LATD6=1;
		    			// OK -> Blink the LED fast
						halWait(5000);
					} 
					else 
					{	
						halWait(5000);
					}
				}
				//else LED3=1;
			} 
			else 
			{
		    	halWait(5000);
			}
			iLoopCount++;
		}

} // main

⌨️ 快捷键说明

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