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

📄 main.c

📁 芯科rf资料
💻 C
字号:
/*** ============================================================================**** FILE**  main.c**** DESCRIPTION**  This is the main file of the project.	    **** CREATED**  Silicon Laboratories Hungary Ltd**** COPYRIGHT**  Copyright 2008 Silicon Laboratories, Inc.  **	http://www.silabs.com**** ============================================================================*/							/* ======================================================= *							 *						INCLUDE							   *							 * ======================================================= */						#include "Si4421.h"#include "S8051.h"#include "uart.h"#include "EZLink_Modem_cmd.h"							/*========================================================== *							 *						GLOBAL variables					 *							 * ========================================================= */xdata uint8 length,payload[MAX_PAYLOAD_LENGTH+1]; 					extern idata uint8 BufChar;extern idata uint8 o_ptr;extern idata uint8 i_ptr;xdata uint8 TxPacketPtr;xdata uint8 uart_ch_sent;xdata uint8 opmode;xdata uint8 swit_30ms;xdata uint8 rs232_timer;xdata uint8 rs232_timeout;								/* ============================================================ *								 *						FUNCTION prototypes						* 								 * ============================================================ */								void Hw_Init(void);INTERRUPT_PROTO (UART0_ISR, INTERRUPT_UART0);	INTERRUPT_PROTO (TIMER2_ISR, INTERRUPT_TIMER2); 								/* ============================================================ *								 *							MAIN PROGRAM						* 								 * ============================================================ */void main(void)															{			xdata uint8 str[10];   	xdata uint8 i;		BufChar = 0;	i_ptr = 0;	o_ptr = 0;	opmode = 0;														//default mode(command mode)	TxPacketPtr = 0;			swit_30ms = 0;  	rs232_timer = 0;  	rs232_timeout = 33;                                          	// RS232 rx timeout (1 s)  	Hw_Init();		init_ezradio();													//initialize the IA4421 transceiver		CMD_init();	printAbout();	ia4421_enable_rx();												//start receiving		while(1)														//main loop	{			// Synchronized routines:         //==========================================================         //---- 30ms soft interrupt area:        		 while(swit_30ms)         {           	--swit_30ms;        	if(rs232_timer && rs232_timer <= rs232_timeout) ++rs232_timer;         // increase the rx timer, if it is running            if (CMD_PIN == 1)		    {					opmode = 1;												//data mode		    }		    else		    {		 		opmode = 0;												//command mode		    }			   	         }      // NOT synchronized routines:      //==========================================================      	      		        		if (BufChar)												//if there is any char in the uart buffer		{			if (opmode == 0)										//command mode			{				CmdExec(ReadBuffer());								//call the commandinterpreter			}			else													//data mode			{				if(TxPacketPtr < MAX_PAYLOAD_LENGTH)				{					payload[TxPacketPtr++]= ReadBuffer();				//save the received byte				}							}		}			if( (TxPacketPtr >= MAX_PAYLOAD_LENGTH) || (payload[TxPacketPtr - 1] == RS232_EOP) || ( rs232_timer >= rs232_timeout))		{										TR_LED = 1;					ia4421_send_packet(TxPacketPtr, &payload[0]);		//send the packet					rs232_timer = 0;					TxPacketPtr = 0;					ia4421_enable_rx();									//enable the receiver chain again					TR_LED = 0;					delay_ms(10);		}											/* ================================================== */								/* 				Linktest button pressed				  */								/* ================================================== */			/*PACET RECEIVING*/        while(RF_NIRQ == 0)                            				//polling the nIRQ pin of the chip        {	        if(Receive_packet() == PACKET_RECEIVED)    				//if the whole packet has been received            {									RC_LED = 1;											//packet received -> blink the RX LED																			ia4421_get_received_payload(&length,payload);										for(i=0;i<length;i++)								//print packet content 					putchar(payload[i]);								payload[9] = 0;																			strcpy(str,"Link ??\r\n");				RC_LED = 0;				if(!strcmp(str,payload))							//checked the packet content					{					TR_LED = 1;					delay_ms(5);					strcpy(&payload[0],"Link OK\r\n");					ia4421_send_packet(9, &payload[0]);				//send ACK message					TR_LED = 0;				}												ia4421_enable_rx();									//enable the receiver chain again					             }		        }				/*PACKET TRANSMISSION*/		if (PB == 0)												//sending Linktest message if button pressed		{							while( PB == 0 ); 										//wait for releasing the push button			TR_LED = 1;			delay_ms(10);			strcpy(&payload[0],"Link ??\r\n");			ia4421_send_packet(9, &payload[0]);						//send a PING packet			TR_LED = 0;			ia4421_enable_rx();										//enable receiver, wait for ACK		}		}	}								/* ================================================ */								/*						UART0 ISR					*/								/* ================================================ */INTERRUPT(UART0_ISR, INTERRUPT_UART0){	if( RI0 == 1 )													//check RX interrupt	{						UartRxISR();												//call the UART RX interrupt handler		RI0 = 0;													//clear IT Flag	}	if( TI0 == 1 ) 	{		uart_ch_sent = 1;		TI0 = 0;	} 	}								/* ================================================ */								/*						TIMER2 ISR					*/								/* ================================================ */INTERRUPT(TIMER2_ISR, INTERRUPT_TIMER2){	++swit_30ms;  	TF2H = 0;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  +  + FUNCTION NAME:  void Hw_Init(void)  +  + DESCRIPTION:    This function configures the HW.  +  +	INPUT:			None  +  + RETURN:         None  +  + NOTES:          None  +  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void Hw_Init(void){	DisableGlobalIt();    /*Disable the Watchdog Timer*/	PCA0MD &= ~0x40;                        PCA0MD = 0x00;		/*Precision Internal Oscillator SYSCLK (24.5MHz)*/	OSCICN    = 0x8F;    FLSCL     = 0x40;    CLKSEL    = 0x00;	/*Initialize I/O ports*/	P0MDIN    = 0xFF;					    P1MDIN    = 0xFF;  	P2MDIN	  = 0xFF;	P0MDOUT   = 0x10;	P1MDOUT   = 0x6D;						    P2MDOUT   = 0x09;		P0     	  |= 0xC3;                      //Set P0 inputs	P1		  |= 0x12;						//Set P1 inputs	P2		  |= 0x10;						//Set P2 inputs			       SFRPAGE   = 0x0F;	P0DRV	  = 0x10;	    P1DRV     = 0x6D;    P2DRV     = 0x09;    SFRPAGE   = 0x00;    XBR0      = 0x01;    XBR1      = 0x40;    XBR2      = 0x40;	//Initialize SPI	SetHwMasterSpi();	//Initialize Uart	UartInit();	//Initialize Timer2	Timer2Init();						/*Blink the LEDs*/		TR_LED = 1;	delay_ms(5);	RC_LED = 1;	delay_ms(5);	TR_LED = 0;	delay_ms(5);	RC_LED = 0;	EnableGlobalIt();						// Enable global IT	}

⌨️ 快捷键说明

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