/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : UARTInit.c
* Author             : Vina Peng,Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 10-Apr-2017
* Description        : This file contains UART initial function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/
#include <FU68xx_2.h>
#include <Myproject.h>
/* Private variables ----------------------------------------------------------------------------*/
MCUART Uart;

void UART_Init(void)
{
    P0_OE |= P05;
    P0_PU |= P05;  
    P0_OE &= ~P06;
    P0_PU |= P06;  	
		SetBit(PH_SEL, UARTEN);     //使能串口
		ClrBit(PH_SEL, UARTCH);     //ClrBit(PH_SEL, UARTCH)p0[6]as UART_RXD,p0[5]as UART_TXD;SetBit(PH_SEL, UARTCH)p3[3]as UART_RXD,p3[4]as UART_TXD;
		UT_MOD1 = 0;
		UT_MOD0 = 1;                //MODE1:8bit波特率可变UART模式
		SM2 = 0;                    //禁止Mode2和Mode3多机通讯
		REN = 1;                    //使能接收
		ES0 = 0;                    //先关中断
		PUART1=0;                   //中断优先级时最低
		PUART0=0;
		UT_BAUD =0x9b;              //  波特率=fcpu_clk/((16/(1+UT_BAUD[BAUD_SEL]))*(UT_BAUD+1))； UT_BAUD[BAUD_SEL]为UT_BAUD.15 ;default baudrate:9600-0x9b,4800-0x137;2400-0x270;1200-0x4E1
//   fcpu_clk/((16/(1+UT_BAUD[BAUD_SEL]))*(UT_BAUD+1));
		ES0 = 1;                    //ES0 发送/接受中断使能位
	EA=1;
		Uart.R_DATA[0]                     = 0;
	Uart.R_DATA[1]                     = 0;
	Uart.R_DATA[2]                     = 0;
	Uart.R_DATA[3]                     = 0;
  Uart.R_DATA[4]                     = 0;	
  Uart.R_DATA[5]                     = 0;
	Uart.R_DATA[6]                     = 0;
  Uart.R_DATA[7]                     = 0;
  Uart.R_DATA[8]                     = 0;	
  Uart.R_DATA[9]                     = 0;
	
	/**·¢ËÍµÄÊý×é******/
  Uart.T_DATA[0]                     = 0;
	Uart.T_DATA[1]                     = 0;
  Uart.T_DATA[2]                     = 0;
  Uart.T_DATA[3]                     = 0;
  Uart.T_DATA[4]                     = 0;	
  Uart.T_DATA[5]                     = 0;
	Uart.T_DATA[6]                     = 0;
  Uart.T_DATA[7]                     = 0;
  Uart.T_DATA[8]                     = 0;	
  Uart.T_DATA[9]                     = 0;
	
	Uart.Uredata                       = 0;
  Uart.UARxCnt                       = 0;
  Uart.RxFSM                         = 0;
  Uart.UsaRxLen                      = 0;
  Uart.flagUsaRxComm                 = 0;
  Uart.CheckDate                     = 0;
	Uart.ResponceCount                 = 0;
	Uart.ResponceFlag                  = 0;
	Uart.UsaTxlen                      = 0;
	Uart.SendCnt                       = 0;
	
	
#if 0
    SetBit(PH_SEL, UARTEN);     //p0[6]as UART_RXD; p0[5]as UART_TXD
    UT_MOD1 = 0;
    UT_MOD0 = 1;                //8bit波特率可变UART模式
    SM2 = 0;                    //禁止Mode2和Mode3多机通讯
    REN = 1;                    //使能接收
    ES0 = 0;                    //先关中断

    PUART1=0;                   //中断优先级时最低
    PUART0=0;

    UT_BAUD =0x4E1;              //default baudrate:9600-0x9b,1200-0x4E1
    ES0 = 1;                    //发送/接受中断使能
      #endif
}
void UartTxdate(uint16* sndBuf, int32 len)
{
    uint16 i=0;
  for(i=0;i<len;i++)
    {
      UART_SendData(*sndBuf++);
    }
}
void UART_SendData(unsigned char T_Data)
{
    UT_DR = T_Data;                //发送完成中断标志位清零
}
/***************处理串口接收到的数据************/
void UartDealResponse(void)
 {
    
 }
void UartDealComm(void)
{
     uint16 j=0;
     uint16 checkdate=0x00;
     if(Uart.ResponceFlag==1)//10ms
     {
         UartDealResponse();
         Uart.T_DATA[0]     = 0xaa;
         Uart.T_DATA[1]     = 0x00;
         Uart.T_DATA[2]     = 1;// actual speed/16 ,if actual speed=1000rpm/min,then TxBuf[2]=63
         Uart.T_DATA[3]     = 2;// DC bus feedback, 1 stand for 2V
         Uart.T_DATA[4]     = 3;//Fault state
         Uart.T_DATA[5]     = 0x00;
         Uart.T_DATA[6]     = 0x00;
         Uart.T_DATA[7]     = 0x00;
         for( j = 0; j < 7; j++ )
         {
          checkdate += Uart.T_DATA[j];
         }
         Uart.T_DATA[8]     = checkdate;
         Uart.T_DATA[9]     = 0x55;
				  Uart.UsaTxlen =9;
	        Uart.SendCnt=0;
	        UART_SendData(Uart.T_DATA[0]);	 
		      Uart.ResponceFlag=0;
     }
}

static uint8 uartRdata[10]; 
static uint8 uartRecCnt = 0;

static uint8 uartSenddata[10]; 
static uint8 uartSendCnt = 0;



