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

📄 cc2420main(tx).c

📁 pic18F2580控制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
#include <p18f2580.h>   //device declarations
#pragma romdata CONFIG1H = 0x300001
 const rom unsigned char config1H = 0b00000010;      // HS oscillator   16M 
#pragma romdata CONFIG2L = 0x300002
 const rom unsigned char config2L = 0b00011111;      // Brown-out Reset Enabled in hardware @ 2.0V, PWRTEN disabled
#pragma romdata CONFIG2H = 0x300003
 const rom unsigned char config2H = 0b00010100;      // HW WD disabled, 1:1024 prescaler 1024*4ms=4s
#pragma romdata CONFIG3H = 0x300005
 const rom unsigned char config3H = 0b10000000;      // PORTB digital on RESET
#pragma romdata CONFIG4L = 0x300006
 const rom unsigned char config4L = 0b10000001;       // DEBUG disabled,
                                                        // XINST disabled
                                                        // LVP disabled
                                                        // STVREN enabled
#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
unsigned char receive232[2]; 			//接收数据数组
unsigned char receive_count=0; 			//接收数据个数计数
unsigned char i;    					//程序中用到的循环变量
#define CC2420_CS_SET PORTCbits.RC0 = 0;
#define CC2420_CS_UNSET PORTCbits.RC0 = 1;
#define TX					//************* select TX or RX ********
void Init_IO(void)
{
	PORTA=0;
    TRISA=0;
    PORTAbits.RA0 = 1;
	TRISAbits.TRISA0 = 1;					//PORT A all set to outputs
	PORTCbits.RC1 = 1; 						//RESETn initially set high (CC2420)
	TRISCbits.TRISC1 = 0;					//output: RESETn
	ADCON0 = 0x1C; 							//turn off analog input
	TRISBbits.TRISB0 = 1; 					//input: FIFO (CC2420)
	TRISBbits.TRISB4 = 1; 					//input: CCA (CC2420)
	TRISBbits.TRISB5 = 1; 					//input: SFD (CC2420)
	TRISBbits.TRISB1 = 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_ON & USART_ASYNCH_MODE &
		  USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH,25 );	//setup USART @ 38400 Baud
}

//////////////////////////USART发送数据////////////////////////////////////////////////////
void USART_Out(char *data, char bytes)
{
int i;

for(i=0; i<bytes; i++)
	{
	while(BusyUSART());
	WriteUSART(data[i]);
	}
}
//////////////////////////USART接收数据//////////////////////////////////////////////
void USART_In(char *data, char bytes)
{
int i;
for(i=0;i<bytes;i++)
	{
	while(!DataRdyUSART());
	data[i]=ReadUSART();
	}
}
//**************************************************************************
//*** 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
char RXAttemptCount,MessageLength=0,Counter=0;	
void main (void)
{
    char MessageLength=0,Counter=0;	
    //char Tmp[5],Text[] = "Hello",LF[]="\r\n";	//strings to be transmitted
	//Delay10KTCYx(1);				
	Init_IO();									//初始化MCU 
	Init_CC2420();								//初始化CC2420收发器
while(1)
	{
	#ifdef TX									//******Transmitter code*********
	if(PORTBbits.RB4)							//Check CCA to see if channel is clear
		{
	
		//***********************************************************************************************
		Transmit_CC2420();	//*** 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 + -