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

📄 test2_main(tx).c

📁 PIC18F4620控制CC2420发送数据
💻 C
字号:
//****************************************************************
//**** Stub program for PICDEM-Z board using CC2420  		******
//**** Jeff Neasham 9/9/2005 								******
//**** MAY BE ALTERED BY STUDENTS							******
//****************************************************************
#include <p18f4620.h>   //device declarations
#pragma config OSC = HS		//set HS oscillator
#pragma config WDT = OFF	//disable watchdog timer
#pragma config LVP = OFF	//disable low voltage programming
#include <stdlib.h>
#include <spi.h>			//serial peripheral interface functions
#include <delays.h>			//time delay functions
#include <usart.h>			//USART functions
#include <string.h>			//string manipulation functions
#include "cc2420.h"			//driver function definitions for CC2420
#define TX					//************* select TX or RX *****************
//**************************************************************************
//** Function to initialise all I/O resources used by Processor ************
//** MUST NOT BE CHANGED BY STUDENTS                            ************
//**************************************************************************
void Init_IO(void)
{
PORTA = 0;
TRISA = 0;								//PORT A all set to outputs
PORTCbits.RC2 = 1; 						//RESETn initially set high (CC2420)
TRISCbits.TRISC2 = 0;					//output: RESETn
ADCON0 = 0x1C; 							//turn off analog input
TRISBbits.TRISB0 = 1; 					//input: FIFO (CC2420)
TRISBbits.TRISB1 = 1; 					//input: CCA (CC2420)
TRISBbits.TRISB2 = 1; 					//input: SFD (CC2420)
TRISBbits.TRISB3 = 1; 					//input: FIFOP (CC2420)
PORTCbits.RC0 = 1; 						//CSn initially set high (CC2420)
TRISCbits.TRISC0 = 0; 					// output: CSn
OpenSPI(SPI_FOSC_4,MODE_00,SMPMID);		//setup SPI bus (SPI mode 00, 1MHz SCLK) (CC2420)
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
		  USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH,25 );	//setup USART @ 38400 Baud
}
//**************************************************************************
//** Function to output an array of bytes to the USART (RS232 port) ********
//** MUST NOT BE CHANGED BY STUDENTS                            	********
//**************************************************************************
void USART_Out(char *data, char bytes)
{
int i;
for(i=0; i<bytes; i++)
	{
	while(BusyUSART());
	WriteUSART(data[i]);
	}
}
//**************************************************************************
//*** MAIN function of program - transmits or receives text message	********
//*** THIS WILL BE MODIFIED AND EXPANDED WITH STUDENTS' CODE	    ********
//**************************************************************************
char DataBuffer[128];		//buffer for data transmitted to/received from CC2420
void main (void)
{
char MessageLength=0,Counter=0;	
char Tmp[5],Text[] = "Hello World! This is a 2.4GHz IEEE 802.15.4 Transmission ",LF[]="\r\n";	//strings to be transmitted
Init_IO();			//initialise all processor I/O resources used by the program
Init_CC2420();		//initialise CC2420 Transceiver
while(1)
	{
	#ifdef TX									//******Transmitter code*********
	if(PORTBbits.RB1)							//Check CCA to see if channel is clear
		{
		strcpy(DataBuffer,Text);				//Copy text message into buffer	
		btoa(Counter,Tmp);						//convert counter to ASCII string
		strcat(DataBuffer,Tmp);					//append counter string to buffer
		strcat(DataBuffer,LF);					//append <CR><LF> to buffer
		//***********************************************************************************************
		Transmit_CC2420(DataBuffer,strlen(DataBuffer));	//*** Transmit data on CC2420 (Max 116 bytes) ***
		//***********************************************************************************************
	    PORTA = Counter++;						//Increment counter and toggle LEDs
		Delay10KTCYx(100);						//wait 100000 instruction cycles
		}
	#endif
	#ifdef RX									//******Receiver code***********
    WriteUSART('?');						//write ? to RS232 port
	//******************************************************************************************************
	MessageLength = Receive_CC2420(DataBuffer,30000);	//receive data from CC2420 (timeout = 32767 max) **
	//*****************************************************************************************************
	if(MessageLength) 							//if data received
		{
		USART_Out(DataBuffer,MessageLength);	//output received data to USART (RS232 port)
		PORTA = Counter++;						//Increment counter and toggle LEDs
		}
	else							 			//if timed out
		{
		WriteUSART('?');						//write ? to RS232 port
		USART_Out(LF,strlen(LF));						
		}
	#endif
	} 
}

⌨️ 快捷键说明

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