/*******************************************************************************
* File:    main.c 
* Author:  Smart Team
* Version: V1.00
* Date:    2011.06.21
* Brief:   Main program body
********************************************************************************
********************************************************************************
* History:
* 2012.04.22:  V1.00		   initial version
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "lpc11xx.h"
#include "clkconfig.h"
#include "gpio.h"
#include "uart.h"
#include "timer32.h"
#include "ssp.h"
#include "i2c.h"
#include "adc.h"
#include "rom_drivers.h"

#include "seg7led.h"
#include "oled2864.h" 
#include "light.h"
#include "acc.h"
#include "key.h"
#include "rfid.h"
#include "tools.h"
#include "spi_uart.h"
#include "temp_hum.h"
#include "menu.h"
#include "crc16.h"

/* Private typedef -----------------------------------------------------------*/
#define FREQUENT_GET_DATA  4  //采集数据的频率
#define DEV_NO  2  //设备号
#define N 40      //卡类数据长度
#define channel_num 0
#define led_on   0
#define led_off  1
#define fan_on   0
#define fan_off  1

/************CMD定义*************/
#define CMD_M0_FIREALARM_ON		1	//火警：开灯 开蜂鸣器
#define CMD_M0_FIREALARM_OFF	2	//火警解除：关灯 关蜂鸣器
#define CMD_M0_HUMIDALARM_ON	3	//潮湿警报：开灯 开风扇
#define CMD_M0_HUMIDALARM_OFF	4	//潮湿警报解除：关灯 关风扇
#define CMD_M0_LIGHTALARM_ON	5	//光照警报：开灯 
#define CMD_M0_LIGHTALARM_OFF	6	//光照警报：关灯
#define CMD_M0_ON				7	//
#define CMD_M0_OFF				8	//
#define CMD_M0_FAN_CLOSE		9	//风扇 关
#define CMD_M0_FAN_LOW			10	//风扇 1档
#define CMD_M0_FAN_MID			11	//风扇 2档
#define CMD_M0_FAN_HIGH			12	//风扇 3档
 /*******end**CMD定义****end******/


#define banana 0x00

/*******************结构体声明***********************/
typedef struct 	//M0采集数据的结构体
{
//char card_money[N];//卡的钱数
char  head;   //数据包头 
char  dev_no;    //设备号
char  type_data; //数据类型 0x02为一般温度、湿度类型数据 
char  length;    //数据的长度  
char  temp[2];   //温度数据
char  hum[2];    //湿度数据
char  bright[2]; //光照度数据
char  adc[2];    //ADC 转换数据
char  acc[3];    //三轴加速x,y,z
char  cmd;    //命令号 
char  data;    //命令数据  
char  card_data[N]; //卡类信息

int   count_time;
short crc16;	 //crc校验值

}mo_a8_data;
/*************end***结构体声明***end*****************/


/*******************函数声明***********************/
void check_cmd(uint8_t cmd);  //查看ZigBee命令并执行命令
void collect_data(void);	 //数据采集函数
void collect_light(void);
void collect_adc(void);
void collect_acc(void);
//void rfid_msg();
void card_int(void);
void cont_beep(uint8_t data);
void cont_led(uint8_t data);
void cont_fan(uint8_t data);
uint32_t collect_temp_hum(uint8_t *my_temp, uint8_t *my_hum); //采集温度 & 湿度
void msg_send(void *msg,uint32_t len,uint8_t *buf); //发送msg包函数
void msg_receiv(uint8_t *buf_receiv,uint8_t len);//接收msg包函数

//extern volatile uint32_t timer32_0_counter; //蜂鸣器 函数
volatile uint32_t period = 1500;
void speaker_op(uint8_t pwm_set)  //传1响 传0不响
{
	switch(pwm_set)
	{
		case 1:
			LPC_TMR32B1->PR = 0x7f;
			init_timer32PWM(1, period, MATCH0);
			enable_timer32(1);
	   	break;
		case 0:
			init_timer32PWM(1, 0, MATCH0);
			delay_ms(50);
			disable_timer32(1);
		break;
	}
}

/*************end***函数声明***end*****************/

/*******************全局变量***********************/
static int j = 0;
const uint8_t RFID_READ_DATA_21[10] =  {0x0a, 0x21, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 
const uint8_t RFID_WRITE_DATA_22[26] = {0x1a, 0x22, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                                              0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
										      0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x20}; 
const uint8_t RFID_INIT_PERSE_0E[14] =  {0x0e, 0x23, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0x00,0x00,0x00,0x04};
const uint8_t RFID_READ_PERSE_0A[10] =  {0x0a, 0x24, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 
const uint8_t RFID_ADD_PERSE_0E[14] =  {0x0e, 0x25, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0x00,0x00,0x00,0x01};
const uint8_t RFID_SUB_PERSE_0E[14] =  {0x0e, 0x26, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0x00,0x00,0x00,0x01};

	uint8_t my_temp_data[2];
	uint8_t my_hum_data[2];
	uint8_t buf_data[128];  // 发送buf
	uint8_t buf_receiv[128];  //接收buf
	uint8_t buf_rfid[128];
	uint32_t adc_value;	
	uint32_t light_value;
	uint8_t	chksum;
	uint8_t chkrfid;
	uint8_t flag = 0;
	uint8_t int_flag = 0;
	int32_t	xoff = 0;
	int32_t	yoff = 0;
	int32_t	zoff = 0;

    mo_a8_data msg_data,msg_card,msg_cmd;
	uint32_t count_get_data = 0;    //计时器
	uint8_t	screen_alarm = 0;	 //屏幕是否闪烁 1 火警 2 潮湿 3 光线
	int i;
	//char temp_and_hum_show[32];//温度和适度显示buf
	char temp_show[32];//温度显示buf
	char hum_show[32];//湿度显示buf
	char light_show[32];//光照度显示buf
	char adc_show[32];//电压度显示buf
	char acc_show[32];//三轴加速度显示buf
	char temp;
	int flag_send_card = 0; //是否发送卡类信息
	uint16_t crc;//crc16 校验和

//	a8_m0_cmd msg_cmd;
/*************end***全局变量***end*****************/



static uint32_t	Tick=0;  //全局计时  Tick

void SysTick_Handler(void)
{

  //static uint8_t	cnt=0;

  Tick ++;	//1ms 计时

  if(Tick == 500)
  {
    GPIOSetValue(PORT3, 0, 0);		// PIO3_1 output 1, turn on LED2

	if(screen_alarm	== 1)
	{   
		OLED_DisStrLine(3, 0, "        fire!");
		Delay(1);
		Vertical_Scroll(0x00,0x00,64,0x1,0xf0);// Upward
		Delay(1);
		Delay(1);
	}
	if(screen_alarm	== 2)
	{   
		OLED_DisStrLine(3, 0, "        Hum!");
		Delay(1);
		Vertical_Scroll(0x00,0x00,64,0x1,0xf0);// Upward
		Delay(1);
		Delay(1);
	}
	if(screen_alarm	== 3)
	{   
		OLED_DisStrLine(3, 0, "        light!");
		Delay(1);
		Vertical_Scroll(0x00,0x00,64,0x1,0xf0);// Upward
		Delay(1);
		Delay(1);
	}			
  }
  else if(Tick >= 1000)
  {
    GPIOSetValue(PORT3, 0, 1);		// PIO3_1 output 1, turn off LED2
	if((screen_alarm==1)||(screen_alarm==2)||(screen_alarm== 3))
	{
		OLED_ClearScreen();
		Frame();
	}
	Tick = 0;
  }
}


/*******************************************************************************
* Function Name  : main
* Description    : 
*******************************************************************************/
int main(void)
{
  Menu_TypeDef	menu; //菜单
 
  msg_data.head = 0x11;   //数据包头 
  msg_data.dev_no = DEV_NO;    //设备号
  msg_data.type_data = 0x02; //数据类型 0x02为一般温度、湿度类型数据 
  msg_data.length = sizeof(msg_data);    //数据的长度  
  msg_data.count_time = 0;
  msg_data.crc16 = 0;

  SystemInit();



  GPIOInit();						// Enables clock for GPIO



  CLKOUT_Setup(CLKOUTCLK_SRC_MAIN_CLK);
  LPC_IOCON->PIO0_1 &= ~0x07;	
  LPC_IOCON->PIO0_1 |= 0x01;		/* CLK OUT */

  // LED
  GPIOSetDir(PORT3, 0, 1);			// Set PIO3_0 to output
  GPIOSetValue(PORT3, 0, 1);		// PIO3_0 output 1, close LED1

  GPIOSetDir(PORT3, 1, 1);			// Set PIO3_1 to output
  GPIOSetValue(PORT3, 1, 1);		// PIO1_1 output 1, close LED2
 
  // Fan
  GPIOSetDir(PORT0, 2, 1);			// Set PIO0_2 to output    
  GPIOSetValue(PORT0, 2, 1);		// PIO0_2 output 0, close FAN
  
  // Speaker
  LPC_IOCON->R_PIO1_1 &= ~0x07;
  LPC_IOCON->R_PIO1_1 |= 0x01;
  GPIOSetDir(PORT1, 1, 1);			// Set PIO3_2 to output
  GPIOSetValue(PORT1, 1, 1);		// PIO3_2 output 1,	Speaker turn off


  // 7 Segment LED
  Seg7Led_Init();
  Seg7Led_Put(' ');					// Output ' '



  KEY_Init();

  /* Called for system library in core_cm0.h. */
  SysTick_Config(48000);

  // NVIC is installed inside UARTInit file.
  UARTInit(115200);
  //NVIC_SetPriority(UART_IRQn, 0);

 

  printf("\r\n\r\nFS_11C14\r\n");

  
  GPIOSetValue(PORT3, 0, 0);		// PIO3_0 output 0, Turn on LED1
  GPIOSetValue(PORT3, 1, 0);		// PIO3_1 output 0, Turn on LED2
  delay_ms(200);
  GPIOSetValue(PORT3, 0, 1);		// PIO3_0 output 1, Turn off LED1
  GPIOSetValue(PORT3, 1, 1);		// PIO3_1 output 1, Turn off LED2


  SPI_IOConfig(0);
  SPI_Init(0, 8, 2);

  OLED_Init_I();
  //oled_test();

  menu.max_numb = 1; //显示条数
  menu.numb = 1;
  menu.title = "Field-Innovation:";
  menu.item[0] = "you are safe!";


  Dis_Menu(menu);

  delay_ms(250);


  
  /**********************RFID中断初始化*********************/
    // use port2_8 as input event, ZigBee interrupt.
  GPIOSetDir(PORT2, 8, 0);

 

  //port2_8 interrupt. edge, single trigger, falling edges.
  GPIOSetInterrupt(PORT2, 8, 0, 0, 0);


  GPIOIntEnable(PORT2, 8);

/**********************RFID中断初始化*********************/ 
 
   
  SPI_IOConfig(1);
  SPI_Init(1, 8, 2);
  SPI752_Init(1, 115200); //ZigBee初始化函数


  I2CInit(I2CMASTER, 0);
  acc_init();
  		
	msg_data.acc[0] = 0;
	msg_data.acc[1] = 0;
	msg_data.acc[2] = 0;
    
	acc_read((int8_t *)&msg_data.acc[0], (int8_t *)&msg_data.acc[1], (int8_t *)&msg_data.acc[2]);
	xoff = 0-msg_data.acc[0];
    yoff = 0-msg_data.acc[1];
    zoff = 0-msg_data.acc[2];  
 

//static uint32_t	Tick=0;  //全局计时  Tick 
//#define FREQUENT_GET_DATA  5  //采集数据的频率 

/*******************************************/

/*******************************************/
  WDTInit();
  while(1)  //总循环
  {	   
  		
		if(Tick == 10)  //一秒钟计时器
		{	
			WDTFeed();
			count_get_data ++;
			
			msg_receiv(buf_receiv,sizeof(msg_cmd));//查看ZigBee中有无命令
			msg_cmd.head = buf_receiv[0];
			msg_cmd.dev_no = buf_receiv[1];
			msg_cmd.type_data = buf_receiv[2];
			msg_cmd.length = buf_receiv[3];
			msg_cmd.cmd = buf_receiv[15];
			msg_cmd.data = buf_receiv[16];
			memcpy(&msg_cmd.crc16,&buf_receiv[64],2);
		 

			if(flag_send_card == 1)
			{
				msg_card.head = 0xaa;   //数据包头 
  				msg_card.dev_no = DEV_NO;    //设备号
 				msg_card.type_data = 0x04; //数据类型 0x04为卡类数据 
  				msg_card.length = sizeof(msg_data);    //数据的长度  
  				msg_card.count_time = 0;
  				msg_card.crc16 = 0;

				msg_send(&msg_card,sizeof(msg_card),buf_data);
			
				flag_send_card = 0;
			}

			if(msg_cmd.head == 0x22 && msg_cmd.dev_no == DEV_NO)	 //检测包头和设备号
			{
 
				check_cmd(msg_cmd.cmd);  //执行命令

			}  	 
		}	  
				 
/*******************************************************************
	数据采集发送
********************************************************************/
		if(count_get_data > FREQUENT_GET_DATA)  //秒钟 采集一次数据 并通过ZigBee 发送
		{	
			//Temp_Hum_Test();
			msg_data.head = 0xaa;
			msg_data.dev_no = DEV_NO;	 
			collect_temp_hum((uint8_t *)msg_data.temp, (uint8_t *)msg_data.hum);
			collect_light();
			collect_adc();
			collect_acc();
			//rfid_msg();
			
			//msg_data.crc16 = make_crc16(0, (uint8_t *)&msg_data+4,11);
			//设置校验和

   			msg_send(&msg_data,sizeof(msg_data),buf_data); //"123\n",4,buf_data);//

			count_get_data = 0;	 //计数器清零

			strcpy(&temp_show[0],"Temp: ");
			temp = (msg_data.temp[0]/10+'0'); //十位
			memcpy(&temp_show[6],&temp,1);
			temp = (msg_data.temp[0]%10+'0'); //个位
			memcpy(&temp_show[7],&temp,1); //设置温度显示buf

			strcpy(&hum_show[0],"Hum: ");
			temp = (msg_data.hum[0]/10+'0'); //十位
			memcpy(&hum_show[5],&temp,1);
			temp = (msg_data.hum[0]%10+'0'); //个位
			memcpy(&hum_show[6],&temp,1); //设置温度显示buf


			strcpy(&light_show[0],"Light: ");
			temp = (msg_data.bright[0]/100+'0');	//百位
			memcpy(&light_show[6],&temp,1);	 
			temp = ((msg_data.bright[0]/10)%10+'0');//十位
			memcpy(&light_show[7],&temp,1);
			temp = (msg_data.bright[0]%10+'0');	 //个位
			memcpy(&light_show[8],&temp,1); //设置光度显示buf
	
			strcpy(adc_show,"ADC: ");
			temp = (msg_data.adc[0]+'0'); 	 //百位
			memcpy(&adc_show[5],&temp,1);
			memcpy(&adc_show[6],".",1);	 
			temp = (msg_data.adc[1]+'0'); //十位
			memcpy(&adc_show[7],&temp,1);
			memcpy(&adc_show[8]," V",2);
		  
	
			strcpy(acc_show,"Acc:x:");
			temp = (msg_data.acc[0]/100+'0'); 	 //百位
			memcpy(&acc_show[6],&temp,1);	 
			temp = ((msg_data.acc[0]/10)%10+'0'); //十位
			memcpy(&acc_show[7],&temp,1);
			temp = (msg_data.acc[0]%10+'0');	  //个位
			memcpy(&acc_show[8],&temp,1);       //设置显示buf

			strcpy(&acc_show[9]," y:");
			temp = (msg_data.acc[1]/100+'0'); 	 //百位
			memcpy(&acc_show[12],&temp,1);	 
			temp = ((msg_data.acc[1]/10)%10+'0'); //十位
			memcpy(&acc_show[13],&temp,1);
			temp = (msg_data.acc[1]%10+'0');	  //个位
			memcpy(&acc_show[14],&temp,1);       //设置显示buf

			strcpy(&acc_show[15]," z:");
			temp = (msg_data.acc[2]/100+'0'); 	 //百位
			memcpy(&acc_show[18],&temp,1);	 
			temp = ((msg_data.acc[2]/10)%10+'0'); //十位
			memcpy(&acc_show[19],&temp,1);
			temp = (msg_data.acc[2]%10+'0');	  //个位
			memcpy(&acc_show[20],&temp,1);       //设置显示buf
		 
			if(screen_alarm	== 0) //清屏幕
			{
			//	OLED_ClearScreen();
				menu.max_numb = 6; //显示条数
 				menu.numb = 0;
  			   	menu.title = "Field-Innovation:";
  				menu.item[0] = "you are safe!";
				menu.item[1] = temp_show;
				menu.item[2] = hum_show;
				menu.item[3] = light_show;
				menu.item[4] = adc_show;
				menu.item[5] = acc_show;

 				Dis_Menu(menu);
			}
		}

			
  
/*******************************************************************
	查看ZigBee中有无命令
********************************************************************/
    /************** 接收命令 *****************
			msg_receiv(buf_receiv,sizeof(msg_cmd));
			msg_cmd.head = buf_receiv[0];
			msg_cmd.dev_no = buf_receiv[1];
			msg_cmd.type_data = buf_receiv[2];
			msg_cmd.length = buf_receiv[3];
			msg_cmd.cmd = buf_receiv[15];
			msg_cmd.data = buf_receiv[16];
  	
			if(msg_cmd.head == 0x22 && msg_cmd.dev_no == 1)	 //检测包头和设备号
			{
				check_cmd(msg_cmd.cmd);  //执行命令
			}
	*********end** 接收命令 **end************/
  }
 
 /***************End of Loop***************/ 

}

 /***************End of Main***************/


/*******************************************************************
	数据采集类函数

********************************************************************/	  

uint32_t collect_temp_hum(uint8_t *my_temp,uint8_t *my_hum) //采集温度 & 湿度
{
	if(Read_Temp_Hum((uint8_t *)my_temp, (uint8_t *)my_hum))
	{
		return 0;
	} 
	return 1;
}


void collect_light()  //采集光照度
{
  I2CInit(I2CMASTER, 0);
  light_init();
  light_enable();
  light_setRange(LIGHT_RANGE_4000);//light 的初始化
 
  light_value =  light_read();
  msg_data.bright[1] = light_value & 0xff00;
  msg_data.bright[0] = light_value & 0xff;

  return ;
}

void collect_adc()  //采集ADC信息
{
//	float val;
	ADCInit(ADC_CLK);
	adc_value = ADCRead(channel_num); 
	adc_value = (adc_value*3.3/1023)*10;
	msg_data.adc[0] = adc_value/10;//& 0xff00;
   	msg_data.adc[1] = adc_value%10;//& 0xff;
   	
	return ;
}

void collect_acc()	  //采集三轴加速度
{	
  /*
	I2CInit(I2CMASTER, 0);

    acc_init();
	acc_read((int8_t *)&msg_data.acc[0], (int8_t *)&msg_data.acc[1], (int8_t *)&msg_data.acc[2]);
	xoff = 0-msg_data.acc[0];
  	yoff = 0-msg_data.acc[1];
  	zoff = 0-msg_data.acc[2];  */
	acc_read((int8_t *)&msg_data.acc[0], (int8_t *)&msg_data.acc[1], (int8_t *)&msg_data.acc[2]);
    msg_data.acc[0] = msg_data.acc[0]+xoff;	//??????????????????
    msg_data.acc[1] = msg_data.acc[1]+yoff;
    msg_data.acc[2] = msg_data.acc[2]+zoff;

	return ;
}

/*******************************************************************
	命令执行函数

********************************************************************/ 
 /************CMD定义*************
#define CMD_M0_FIREALARM_ON		1	//火警：开灯 开蜂鸣器
#define CMD_M0_FIREALARM_OFF	2	//火警解除：关灯 关蜂鸣器
#define CMD_M0_HUMIDALARM_ON	3	//潮湿警报：开灯 开风扇
#define CMD_M0_HUMIDALARM_OFF	4	//潮湿警报解除：关灯 关风扇
#define CMD_M0_LIGHTALARM_ON	5	//光照警报：开灯 
#define CMD_M0_LIGHTALARM_OFF	6	//光照警报：关灯
#define CMD_M0_ON				7	//
#define CMD_M0_OFF				8	//
#define CMD_M0_FAN_CLOSE		9	//风扇 关
#define CMD_M0_FAN_LOW			10	//风扇 1档
#define CMD_M0_FAN_MID			11	//风扇 2档
#define CMD_M0_FAN_HIGH			12	//风扇 3档
 ************CMD定义*************/


void check_cmd(uint8_t cmd)  //收到ZigBee命令并执行命令
{
	switch(cmd)
	{
	  case CMD_M0_FIREALARM_ON:	//火警：开灯 开蜂鸣器
	  speaker_op(1);
	  cont_led(led_on);
	  screen_alarm	= 1;
	  break;
	  case CMD_M0_FIREALARM_OFF: //火警解除：关灯 关蜂鸣器
	  speaker_op(0);
	  cont_led(led_off);
	  screen_alarm	= 0;
	  break;
	  case CMD_M0_HUMIDALARM_ON:  //潮湿警报：开灯 开风扇
	  cont_fan(fan_on);
	  cont_led(led_on);
	  screen_alarm	= 2;
	  break;
	  case CMD_M0_HUMIDALARM_OFF:  //潮湿警报解除：关灯 关风扇
	  cont_fan(fan_off);
	  cont_led(led_off);
	  screen_alarm	= 0;
	  break;
	  case CMD_M0_LIGHTALARM_ON:  //光照警报：开灯
	  cont_led(led_on);
	  screen_alarm	= 3;
	  break;
	  case CMD_M0_LIGHTALARM_OFF: 	//光照警报：关灯
	  cont_led(led_off);
	  screen_alarm	= 0;
	  break;
	  case CMD_M0_FAN_CLOSE:	//风扇 关
	  cont_fan(fan_off);
	  break;
	  case CMD_M0_FAN_LOW:	//风扇 1档
	  cont_fan(fan_on);
	  break;
	  case  CMD_M0_FAN_MID:	//风扇 2档
	  cont_fan(fan_on);
	  break;
	  case CMD_M0_FAN_HIGH:	//风扇 3档
	  cont_fan(fan_on);
	  break;
	}	
	return ;
}
  
void cont_fan(uint8_t data)	 //风扇控制
{
// Fan
	if(data == fan_on)
	{										   
  		GPIOSetDir(PORT0, 2, 1);			// Set PIO0_2 to output    
  		GPIOSetValue(PORT0, 2, 0);		// PIO0_2 output 0, open FAN
	}
	else
	{
		GPIOSetDir(PORT0, 2, 1);			// Set PIO0_2 to output    
  		GPIOSetValue(PORT0, 2, 1);			 //close FAN
	}
	return ;
}


void cont_led(uint8_t data)	 //LED控制
{
	if(data == led_on)
	{
		GPIOSetDir(PORT3, 0, 1);			// Set PIO3_0 to output
 	 	GPIOSetValue(PORT3, 0, 0);		// PIO3_0 output 1, turn on LED1
		GPIOSetDir(PORT3, 1, 1);			// Set PIO3_1 to output
 	 	GPIOSetValue(PORT3, 1, 0);		// PIO3_1 output 1, turn on LED2
		
  	}
   	else
  	{
		GPIOSetDir(PORT3, 0, 1);			// Set PIO3_0 to output
 	 	GPIOSetValue(PORT3, 0, 1);		// PIO3_0 output 1, close LED1
		GPIOSetDir(PORT3, 1, 1);			// Set PIO3_1 to output
 	 	GPIOSetValue(PORT3, 1, 1);		// PIO3_1 output 1, close LED1
	}
	return ;
}
/*************************RFID******************************/

void card_int()	//封装消息
{
  msg_card.head = 0x11;   //数据包头 
  msg_card.dev_no = 1;    //设备号
  msg_card.type_data = 0x04; //数据类型 0x04为卡类数据 
  msg_card.length = sizeof(msg_data);    //数据的长度  
  msg_card.count_time = 0;
  msg_card.crc16 = 0;
  
  msg_send(&msg_card,sizeof(msg_card),buf_data);
  
  return ; 	
}
/*
void rfid_perse_init()
{
	uint8_t	chksum;
    
   
   RFID_Operate((uint8_t *)RFID_INIT_PERSE_0E,(uint8_t *)buf_rfid);
   chksum = RFID_CheckSum((uint8_t *)msg_data.card_money);
   if(chksum == buf_rfid[buf_rfid[0]])
   	{
	     OLED_DisStrLine(2-1 + 1, 0, " init_perse Ok!");
              
	}
	
}
 
void rfid_card_write()
{
	uint8_t	chksum;
    
   
   RFID_Operate((uint8_t *)RFID_WRITE_DATA_22,(uint8_t *)buf_rfid);
   chksum = RFID_CheckSum((uint8_t *)buf_rfid);

   if(chksum == buf_rfid[buf_rfid[0]])
   	{
	     OLED_DisStrLine(2-1 + 1, 0, " write_card Ok!");
    }
}

void rfid_add_perse()
{
	uint8_t	chksum;
    
   
   	RFID_Operate((uint8_t *)RFID_ADD_PERSE_0E,(uint8_t *)buf_rfid);
   	chkrfid = RFID_CheckSum((uint8_t *)buf_rfid);

   if(chkrfid == buf_rfid[buf_rfid[0]])
   	{
	 OLED_DisStrLine(2-1 + 1, 0, " add_perse Ok!");
     }
	 	
}

void rfid_sub_perse()
{
 	RFID_Operate((uint8_t *)RFID_SUB_PERSE_0E,(uint8_t *)buf_rfid);
   	chkrfid = RFID_CheckSum((uint8_t *)msg_data.card_money);

  	if(chkrfid == buf_rfid[buf_rfid[0]])
   	{
	   OLED_DisStrLine(2-1 + 1, 0, " sub_perse Ok!");
              
	}	
}
*/
 
void PIOINT2_IRQHandler(void)  //读卡中断处理函数
{	 
	delay_ms(200);
	delay_ms(200);	
	   
      RFID_Operate((uint8_t *)RFID_READ_DATA_21,(uint8_t *)msg_card.card_data);
	  chksum = RFID_CheckSum((uint8_t *)msg_card.card_data);

	  if(chksum == msg_card.card_data[msg_card.card_data[0]])
      {	  	
        OLED_DisStrLine(2-1 + 1, 0, "read_card ok!");
      }
	  delay_ms(200);

		/*
	    SPI_IOConfig(1);
  SPI_Init(1, 8, 2);
		*/
	  	//ZigBee_PutChar('a');
	  	//SPI752_RegWrite(1, SPI752_THR_W, 'a');
	    SPI_UART_CS(0);
 		 //SPI_PutGet(1, SPI752_WRITE | (SPI752_THR_W<<3) | (1<<1));
 		 //SPI_PutGet(1, 'a');
    while((LPC_SSP1->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF);
    LPC_SSP1->DR = 'a';
   
    while(LPC_SSP1->SR & SSPSR_BSY);

    
    while((LPC_SSP1->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE);
 		 SPI_UART_CS(1);
		 
  
	 flag_send_card = 1;	  	
	
  	cont_led(0); //中断测试
  	speaker_op(1);
 	 delay_ms(250);
 	 delay_ms(250);

  	 speaker_op(0);
  	 cont_led(1);

  
  if(GPIOIntStatus(PORT2, 8))
  {     
	//p2_8_counter++;
	GPIOIntClear(PORT2, 8);
  }
  
}

/* 
void rfid_msg()	   //采集卡类信息
{
	
	GPIOSetDir(PORT2, 8, 0);

   if(!GPIOGetValue(PORT2, 8))
    { 
	if(int_flag == 0)
	{

	rfid_perse_init();
    rfid_card_write();

   	int_flag = 1;
	}

 	if(int_flag == 1)
	{
	if(flag == 0)
	{
	 	
	//	 rfid_add_perse();
		 flag = 1;
	}
	else
	{
	  
	//	rfid_sub_perse();
		flag = 0;
	}
	 
  	  
	  delay_ms(200);

      RFID_Operate((uint8_t *)RFID_READ_DATA_21,(uint8_t *)msg_card.card_data);
	  chksum = RFID_CheckSum((uint8_t *)msg_card.card_data);

	  if(chksum == msg_card.card_data[msg_card.card_data[0]])
      {
	  	
        OLED_DisStrLine(2-1 + 1, 0, "read_card ok!");
      }
	  delay_ms(200);

	 RFID_Operate((uint8_t *)RFID_READ_PERSE_0A,(uint8_t *)msg_card.card_money);
	 chkrfid = RFID_CheckSum((uint8_t *)msg_card.card_money);
	 
	 if(chkrfid == msg_card.card_money[msg_card.card_money[0]])
      {
	  	
        OLED_DisStrLine(2-1 + 1, 0, "read_perse ok!");
	  }
	    card_int();
	  delay_ms(200); 

	  }
	 
	 }
	OLED_DisStrLine(2-1 + 5, 0, "                     ");
    OLED_DisStrLine(2-1 + 6, 0, " ");
      
	return ;
}
*/
/*******************************************************************
	数据包发送函数
	arg1:要发送msg地址
	arg2:发送msg的长度
	arg3:消息发送buf

********************************************************************/

void msg_send(void *msg,uint32_t len,uint8_t *buf)
{
	memcpy(buf,(uint8_t *)msg,len);
	for(j = 0;j < len;j ++)
	{
		 ZigBee_PutChar(buf[j]);
	}

	return ;
}

 void msg_receiv(uint8_t *buf_receiv,uint8_t len)
 {	  
 	for(j = 0;j < len;j++)
	{
	  ZigBee_GetChar(&buf_receiv[j]);
	 }
 }
/**** End of File ****/
