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

📄 smac_per_tx.c

📁 利用MC13213做的基于SMAC的接受并发送到串口的程序.可作为点对点实验中的协调器.
💻 C
字号:
/****************************************************************************
application smac_per_tx.c.
This is a basic PER test.  The TX portion of the application
sends out 1000 packets.

Author: Freescale

Application Note: 
\****************************************************************************/

#include <hidef.h> /* for EnableInterrupts macro */
#include "pub_def.h"
#include "APP_SMAC_API.h"
#include "freescale_radio_hardware.h"
#include "smac_per_tx.h"
#include "eventDefs.h"
#include "timer.h"
#include "ledFunctions.h"
#include "kbi.h"


/* Global Variables */
byte gu8RTxMode; /* needed for s-mac, application can read this variable */
				/* rtx_mode is the software state variable of the radio */
				/* This variable reflects the current state of MC13192 */
int app_status = 0; 
byte led_status = 0;
UINT8 gu8Channel = CHANNEL_NUMBER;
UINT16 gu16Events = 0;
extern UINT16 gu16msTimer;
extern UINT16 gu16timerEvent[2];
UINT8 t = 0;
UINT8 ensend = 0 ;
	tRxPacket rx_packet;  //SMAC structure for RX packets
	tTxPacket tx_packet;	//SMAC structure for TX packets
	void produce_packet(void);
	void ad_init(void);
	UINT8 DataBuffer = 0;

void main(void) {


   MCUInit();		//Initialize the mcu, setup GPIOs, SPI, clock scheme, etc.
   RadioInit();
   KBIInit();
   ad_init();   
   KBI_ENABLEINTERRUPTS;  
   app_init();    		
	(void)MLMESetMC13192ClockRate(0);  /* Set initial Clk speed from MC13192s CLKO*/
	UseExternalClock();	/* switch clock sources to EXTERNAL clock */
  EnableInterrupts; /* Turn on system interrupts */
	(void)MLMESetChannelRequest(CHANNEL_NUMBER);	//Set channel zero, can be changed to users preference
  /******************************************************************
  To adjust output power call the MLME_MC13192_PA_output_adjust() with:
   MAX_POWER     (+3 to +5dBm)
  NOMINAL_POWER (0 dBm)
  MIN_POWER     ~(-16dBm)
  or somewhere custom ? (0-15, 11 (NOMINAL_POWER) being Default power)
	******************************************************************/
  //MLMEMC13192PAOutputAdjust(MAX_POWER);	//Set MAX power setting
  //MLMEMC13192PAOutputAdjust(MIN_POWER);	//Set MIN power setting
  (void)MLMEMC13192PAOutputAdjust(OUTPUT_POWER);	//Set Nominal power setting	
	//Loop forever
	for (;;) {

	  if(ensend == 1){
	  	  PTDDD_PTDDD2 = 1;
   PTDD_PTDD2 =1;
	  produce_packet();
	  ensend = 0;
	  	 PTDDD_PTDDD2 = 1;
   PTDD_PTDD2 =0; 
	  }
 
	}
}
/*
设计的数据报格式为
0x7e 包头;
0x0a 类型;
包数 两个字节
10个字节的数据; 共计27个字节;
CRC校验 两个字节;
0X7E 包尾; 
设定为全局变量;
*/

unsigned int calcByte(unsigned int crc, int b) {
		int i = 0;
		crc = crc ^ (int) b << 8;
		for (i = 0; i < 8; i++) {
			if ((crc & 0x8000) == 0x8000)
				crc = crc << 1 ^ 0x1021;
			else
				crc = crc << 1;
		}

		return crc & 0xffff;
	}
unsigned int crc16l(unsigned char *ptr,unsigned char len) {
		unsigned int crc = 0;
		while (len > 0) {
			crc = calcByte(crc, *ptr++);
			len--;
		}
		return crc;
	}
/*byte temptx_data[18]={0x7e,0x42,0x0a,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x7e
};   //采集数据产生的数据包
byte realtx_data[] = {
                      0x7e,0x42,0x0a,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x7e

};
byte realrx_data[] = {
                      0x7e,0x0a,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x7e

}; */
byte temptx_data[20]={0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0
};
byte realrx_data[] = {
                      0x7e,0x0a,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x0,
                      0x0,0x0,0x0,0x0,0x7e

};
UINT8 ad_loop = 0;  //记录每个包中的采样循环次数;
UINT16 packet_acc = 0;  //记录包数;
void produce_packet(void){
//	byte rx_data_buffer[20];	//Data buffer to hold RX data, if you want larger packets change 20 to what you need.
//	byte tx_data_buffer[20];	//Data buffer to hold TX data, if you want larger packets change 20 to what you need.
//	UINT8 i;
	UINT16 packet_count=0;
	SWITCHCONDIR=1;
  SWITCHCON=0;
	//Initialize the packet.
  tx_packet.u8DataLength = 0;				//Set TX default to 0
 // tx_packet.pu8Data = &tx_data_buffer[0];	//Load the address of our txbuffer into tx structure.
 tx_packet.pu8Data = &temptx_data[0];	//Load the address of our txbuffer into tx structure.
  rx_packet.u8DataLength = 0;				//Set RX default to 0
  rx_packet.pu8Data = &realrx_data[0];	//Load the address of our rxbuffer into rx structure
  rx_packet.u8MaxDataLength = 100;		//Define the max buffer we are interested in.
  rx_packet.u8Status = 0;					//initialize the status packet to 0.
  ATD1SC = 0;      //设置为8位的
  while((ATD1SC & 0x80) != 0x80);  //ccf位如果等于0,
  DataBuffer = ATD1RH;  
 // DataBuffer = 0x03;
  if(ad_loop < 20){
    temptx_data[ad_loop] = DataBuffer;
    ad_loop++;
  } else{
    temptx_data[ad_loop] = DataBuffer;
    tx_packet.u8DataLength = 20;	//Set the data length of the packet.  in this case, 6.
        // while(MCPSDataRequest(&tx_packet) == SUCCESS)
		if (MCPSDataRequest(&tx_packet) == SUCCESS)	//TX Packet, Blocking will stay here until the packet is in the air
		LED1 ^= 1;
    ad_loop = 0;
  }

/*	for (i=0; i<18; i++) {
				    tx_data_buffer[i] = 0x0a;
				}  */
 // UINT8 index = 0;  //形成数据包时由于每次只有两个数,所以要填写十次才成为一个数据包,index为每次填包时的数据;
//	UINT16 crc_packet = 0;  //crc校验值;
//	UINT8 i = 0,t1_packet = 0;
//	UINT8 *ptr_packet;
//	index = ad_loop*2;	 //adloop_position是第几次填写位移数据包;
 //  tx_packet.u8DataLength = 27;				//Set TX default to 0
   //tx_packet.pu8Data = &temptx_data[0];	//Load the address of our txbuffer into tx structure.
  // rx_packet.u8DataLength = 0;				//Set RX default to 0
  // rx_packet.pu8Data = &realrx_data[0];	//Load the address of our rxbuffer into rx structure
 //  rx_packet.u8MaxDataLength = 100;		//Define the max buffer we are interested in.
 //  rx_packet.u8Status = 0;					//initialize the status packet to 0.
  
//  if (MCPSDataRequest(&tx_packet) == SUCCESS)	//TX Packet, Blocking will stay here until the packet is in the air
//	LED1 ^= 1;
  //形成数据包;
/*	if(ad_loop < 9) //填写九次,
	{
			temptx_data[4+index] = DataBuffer;				
			ad_loop++;//设计位移包;每10个数形成一个数据包;每100ms产生一个数据包;
	}
	else  //第十次不但要进行填写而且要发送出去;
	{
	   
  //数据采样;
			packet_acc++;
			temptx_data[2] = packet_acc;  //产生包数;
			temptx_data[3] = packet_acc>>8;
			temptx_data[22] = DataBuffer;
		//	temptx_data[23] = RPValue[0]>>8;
			ptr_packet = &temptx_data[1];  //赋予指针;
			crc_packet = crc16l(ptr_packet,23);  //计算CRC
			temptx_data[24] = crc_packet;   
			temptx_data[25] = crc_packet>>8;  //赋值CRC;
			for(i = 2; i<26; i++ )  //escape替换;
					{
						if(temptx_data[i] == 0x7e)
						{
							realtx_data[i+t1_packet] = 0x7d;    
							realtx_data[i+t1_packet+1] = 0x5e;
							t1_packet++;
						}
						else if(temptx_data[i] == 0x7d)
						{
							realtx_data[i+t1_packet] = 0x7d;
							realtx_data[i+t1_packet+1] = 0x5d;
							t1_packet++;
						}
						else
              realtx_data[i+t1_packet] = temptx_data[i];	
					}
								
					realtx_data[26+t1_packet] = 0x7e;  //替换的末尾加上尾符号

⌨️ 快捷键说明

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