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

📄 cc2420db_library.c

📁 MatLab图像传感器网络仿真平台WiSNAP
💻 C
📖 第 1 页 / 共 2 页
字号:

	/* add packet payload */
	for (i = 0; i < rfTxInfo.length; i++)
	{
		status = sprintf(&response[12+2*i], "%02X", rfTxInfo.pPayload[i]);
	}

	/* add packet footer */
	status = sprintf(&response[12+2*rfTxInfo.length], "%02X\r\n",
		rfTxInfo.ackRequest);

	/* execute command */
	status = RfSendPacket(&rfTxInfo);

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X\r\n",
		command[0], status);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Toggle Location LED.                                              *
 *******************************************************************************/
int toggle_location_led(char *command, char *response)
{
	int				status;

	/* execute command */
	PORTE = PINE ^ 0x80;

	/* generate command response */
	status = sprintf(&response[0], ":%c\r\n", command[0]);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Execute command.                                                  *
 *******************************************************************************/
void execute_command(char *command, char *response)
{
	int				status;

	/* decode command */
	command[0] = toupper(command[0]);
	switch (command[0])
	{
				
		/* read atmega128 register */
		case 'A':
				
			/* call command routine */
			status = read_at_register(&command[0], &response[0]);
			break;
					
		/* write atmega128 register */
		case 'B':
					
			/* call command routine */
			status = write_at_register(&command[0], &response[0]);
			break;
					
		/* read cc2420 register */
		case 'C':
					
			/* call command routine */
			status = read_cc_register(&command[0], &response[0]);
			break;
					
		/* write cc2420 register */
		case 'D':
					
			/* call command routine */
			status = write_cc_register(&command[0], &response[0]);
			break;

		/* initialize cc2420 transceiver */
		case 'I':
					
			/* call command routine */
			status = init_cc_transceiver(&command[0], &response[0]);
			break;

		/* receive cc2420 packet */
		case 'R':
					
			/* call command routine */
			status = recv_cc_packet(&command[0], &response[0]);
			break;

		/* send cc2420 packet */
		case 'S':
					
			/* call command routine */
			status = send_cc_packet(&command[0], &response[0]);
			break;

		/* toggle location led */
		case 'T':
					
			/* call command routine */
			status = toggle_location_led(&command[0], &response[0]);
			break;
					
		/* undefined command */
		default:

			/* set status */
			status = 0;

	}

	/* check for error */
	if (!status)
		sprintf(&response[0], ":?\r\n");

	/* return */
	return;
}

/*******************************************************************************
 * Function: Remote chat mode.                                                 *
 *******************************************************************************/
void remote_chat()
{
	char				command[256], response[256];
	int				i, status;

	/* turn on green led */
	SET_GLED();

	/* new receive packet available? */
	if (sig_packet_rx == TRUE)
	{

		/* ignore command responses */
		if (rfRxInfo.pPayload[0] == ':')
			return;

		/* read command */
		i = 0; while(TRUE)
		{

			/* read character from receive packet payload */
			command[i] = rfRxInfo.pPayload[i];

			/* carriage return or line feed? */
			if ((command[i] == 0x0D) || (command[i] == 0x0A))
				break;

			/* receive packet payload length reached? */
			if (++i == rfRxInfo.length)
				break;

			/* wrap-around command buffer */
			i &= 0x7f;

		}

		/* execute command */
		execute_command(&command[0], &response[0]);

		/* define transmit packet payload */
		i = 0; while(TRUE)
		{

			/* write character to transmit payload */
			rfTxInfo.pPayload[i] = response[i];

			/* line feed? */
			if (response[i++] == 0x0A)
				break;

		}

		/* define transmit packet header */
		rfTxInfo.destPanId = rfRxInfo.srcPanId;
		rfTxInfo.destAddr = rfRxInfo.srcAddr;
		rfTxInfo.length = i;

		/* define packet footer */
		rfTxInfo.ackRequest = rfRxInfo.ackRequest;

		/* send response */
		status = RfSendPacket(&rfTxInfo);
		
	}

	/* clear packet reception */
	sig_packet_rx = FALSE;

	/* turn off green led */
	CLR_GLED();

	/* return */
	return;
}

/*******************************************************************************
 * Function: Serial chat mode.                                                 *
 *******************************************************************************/
void serial_chat()
{
	char				command[256], response[256];
	int				i;

	/* turn on green led */
	SET_GLED();

	/* enable uart1 */
	// ENABLE_UART1();

	/* data received on uart1? */
	if (UCSR1A & BM(RXC1))
	{

		/* read command */
		i = 0; while(TRUE)
		{

			/* read character from uart1 */
			UART1_WAIT_AND_RECEIVE(command[i++]);

			/* carriage return or line feed? */
			if ((command[i-1] == 0x0D) || (command[i-1] == 0x0A))
				break;

			/* wrap-around command buffer */
			i &= 0x7f;

		}
			
		/* execute command */
		execute_command(&command[0], &response[0]);

		/* send response */
		i = 0; while(TRUE)
		{

			/* write character to uart1 */
			UART1_WAIT_AND_SEND(response[i++]);

			/* line feed? */
			if (response[i-1] == 0x0A)
				break;

		}
			
	}

	/* disable uart1 */
	// DISABLE_UART1();

	/* turn off green led */
	CLR_GLED();

	/* return */
	return;
}

/*******************************************************************************
 * Function: Packet ping mode.                                                 *
 *******************************************************************************/
void packet_ping()
{
	char				packet[] = ":PING_MODE";
	int				i, status;

	/* packet ping mode requested? */
	if (JOYSTICK_CENTER_PRESSED())
	{

		/* initialize transmit packet */
		rfTxInfo.destPanId = 0x2420;
		rfTxInfo.destAddr = 0x0000;
		rfTxInfo.length = 10;
		rfTxInfo.ackRequest = TRUE;

		/* fill transmit buffer */
		for (i = 0; i < 10; i++)
		{
			rfTxInfo.pPayload[i] = packet[i];
		}

		/* ping forever */
		while (TRUE)
		{

			/* wait to set cycle time */
			for (i = 0; i < 25; i++)
		   halWait(8000);

			/* send transmit packet */
			status = RfSendPacket(&rfTxInfo);

			/* toggle location led */
			PORTE = PINE ^ 0x80;

		}

	}

	/* return */
	return;
}

/*******************************************************************************
 * Main: Startup routine and main loop.                                        *
 *******************************************************************************/
int main (void) {

	/* initalize ports for communication with cc2420 and other peripherals */
	PORT_INIT();
	SPI_INIT();

	/* initialize cc2420 transceiver */
	basicRfInit(&rfRxInfo, 26, 0x2420, 0x0815);
	rfRxInfo.pPayload	= pRxBuffer;
	rfTxInfo.pPayload = pTxBuffer;
	basicRfReceiveOn();

	/* initalize uart1 port */
	INIT_UART1(UART_BAUDRATE_115K2, (UART_OPT_ONE_STOP_BIT | UART_OPT_NO_PARITY
		| UART_OPT_8_BITS_PER_CHAR));
	ENABLE_UART1();

	/* define direction for port e[7] pin */
	DDRE = DDRE | 0x80;

	/* main loop */
	while (TRUE)
 	{

		/* remote chat mode */
		remote_chat();

		/* serial chat mode */
		serial_chat();

		/* packet ping mode */
		packet_ping();

	}

	/* clean exit */
	return 0;
}

⌨️ 快捷键说明

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