
//****************************************************************************
// @Module        Project Settings
// @Filename     Uart.C
// @Project        Sem3000 
//----------------------------------------------------------------------------
// @Controller    SI1010
//
// @Compiler      KEIL uVision V
//
// @ Frequency   Using internal clock MHz
//
// @Codegenerator 0.1
//
// @Description   This file contains the Project voice recoding  function.
//
//----------------------------------------------------------------------------
// @Date          2013-05-28  
//
//****************************************************************************

#define _Uart__C_


#include "PROJECT_INCLUDES.H"

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//-----------------------------------------------------------------------------

void UART0_Init (void)
{
      XBR0    |= 0x01;				// enable UART
   P0MDIN  |= 0x10;                        
   P0MDOUT |= 0x10;             // TX push-pull
   SFRPAGE  = CONFIG_PAGE;
   P0DRV   |= 0x10;				// TX high-current mode
   SFRPAGE  = LEGACY_PAGE;

   SCON0 = 0x10;				// SCON0: 8-bit variable bit rate
                                //        level of STOP bit is ignored
                                //        RX enabled
                                //        ninth bits are zeros
                                //        clear RI0 and TI0 bits

#if (SYSCLK/BAUDRATE/2/256 < 1) 
	TH1 = -(SYSCLK/BAUDRATE/2);
	CKCON &= ~0x0B;				// T1M = 1; SCA1:0 = xx
	CKCON |=  0x08;
#elif (SYSCLK/BAUDRATE/2/256 < 4) 
	TH1 = -(SYSCLK/BAUDRATE/2/4);
	CKCON &= ~0x0B;				// T1M = 0; SCA1:0 = 01
	CKCON |=  0x01;
#elif (SYSCLK/BAUDRATE/2/256 < 12) 
	TH1 = -(SYSCLK/BAUDRATE/2/12);
	CKCON &= ~0x0B;				// T1M = 0; SCA1:0 = 00
#else 
	TH1 = -(SYSCLK/BAUDRATE/2/48);
	CKCON &= ~0x0B;				// T1M = 0; SCA1:0 = 10
	CKCON |=  0x02;
#endif

	//TH1 = 0x2C;
	TL1 = TH1;					// Init Timer1
	TMOD &= ~0xf0;				// TMOD: timer 1 in 8-bit autoreload
	TMOD |=  0x20;	
										/*
										Bit 		7 	6	       5  4       3      2           1 0
										Name GATE1 C/T1 T1M[1:0] GATE0 C/T0 T0M[1:0]	
										
										Bit 	Name 
										7 	GATE1 Timer 1 Gate Control.
												0: Timer 1 enabled when TR1 = 1 irrespective of INT1 logic level.
												1: Timer 1 enabled only when TR1 = 1 AND INT1 is active as defined by bit IN1PL in register IT01CF (see SFR Definition 12.7).
										6 	C/T1 Counter/Timer 1 Select.
												0: Timer: Timer 1 incremented by clock defined by T1M bit in register CKCON.
												1: Counter: Timer 1 incremented by high-to-low transitions on external pin (T1).
										5:4 	T1M[1:0] Timer 1 Mode Select.
												These bits select the Timer 1 operation mode.
												00: Mode 0, 13-bit Counter/Timer
												01: Mode 1, 16-bit Counter/Timer
												10: Mode 2, 8-bit Counter/Timer with Auto-Reload
												11: Mode 3, Timer 1 Inactive
												
										3 	GATE0 Timer 0 Gate Control.
												0: Timer 0 enabled when TR0 = 1 irrespective of INT0 logic level.
												1: Timer 0 enabled only when TR0 = 1 AND INT0 is active as defined by bit IN0PL in register IT01CF (see SFR Definition 12.7).
										2   C/T0 Counter/Timer 0 Select.
										              0: Timer: Timer 0 incremented by clock defined by T0M bit in register CKCON.
												1: Counter: Timer 0 incremented by high-to-low transitions on external pin (T0).
										1:0 T0M[1:0] Timer 0 Mode Select.
												These bits select the Timer 0 operation mode.
											00: Mode 0, 13-bit Counter/Timer
											01: Mode 1, 16-bit Counter/Timer
											10: Mode 2, 8-bit Counter/Timer with Auto-Reload
											11: Mode 3, Two 8-bit Counter/Timers
										*/

										
   TR1 = 1;                            //1 START Timer1
   TI0 = 1;                            //1 Indicate TX0 ready

   IP |= 0x10;                         // Make UART high priority
   ES0 = 1;                            //1 Enable UART0 interrupts       0 debugging

	UART0_TX_OUT_PORT_Enable();
	 
}

extern xdata volatile CQUEUE		 Qa;


void Uart0_Putchar (char c)
{
   //while (!TI0);
   // TI0 = 0;
   //SBUF0 = c; 

	put_tx_queue(&Qa, (unsigned char *)&c, 1);

}


void Uart0_PutString(unsigned char *str)
{
       while(*str){
		    Uart0_Putchar(*str++);
	}
}


void HexToAsc(U8 ConvValue, char *AscHigh, char *AscLow)
{
	U8 Asc;
	
	Asc = (ConvValue >> 4) & 0x0f;
	if(Asc > 9){ Asc -= 9; Asc |= 0x40;}
	if(Asc <= 9)Asc |=0x30;
       *AscHigh = Asc;

	Asc = ConvValue & 0x0f;
	if(Asc > 9){ Asc -= 9; Asc |= 0x40;}
	if(Asc <= 9)Asc |=0x30;
       *AscLow = Asc;
}


U8  BIN2BCD_(U8 Value)
{
	U8  BCDValue;
	
	BCDValue=((Value/10)<<4)+(Value%10);
	return BCDValue;
}



U16 BIN2BCD16(U16 Value)
{
	U16 BCD;
	U16 Remnant,Quotient;
	
	Quotient=Value/1000;
	Remnant=Value%1000;
	BCD=Quotient;
	BCD<<=4;
	Quotient=Remnant/100;
	Remnant%=100;
	BCD|=Quotient;
	BCD<<=4;
	Quotient=Remnant/10;
	Remnant%=10;
	BCD|=Quotient;
	BCD<<=4;
	BCD|=Remnant;
	return BCD;
}


void ByteHexTxSend(U16 uartPutData)
{
	char AscH,  AscL;
		
	HexToAsc((U8)(uartPutData), &AscH, &AscL);
	Uart0_Putchar(AscH);
	Uart0_Putchar(AscL); 
		
}

void TwoByteHexTxSend(U16 uartPutData)
{
	char AscH,  AscL;
		
	HexToAsc((U8)(uartPutData >> 8), &AscH, &AscL);
	Uart0_Putchar(AscH);
	Uart0_Putchar(AscL); 

	HexToAsc((U8)(uartPutData), &AscH, &AscL);
	Uart0_Putchar(AscH);
	Uart0_Putchar(AscL); 
		
}

INTERRUPT(UART0_ISR, INTERRUPT_UART0)
{
	unsigned char ch;

	if(RI0 == 1)
	{
		RI0 = 0;		// Clear interrupt flag

		ch = SBUF0;

		put_rx_queue(&Qa, ch);
	}

	if(TI0 == 1)       // Check if transmit flag is set
   	{
      	    TI0 = 0; 
		if(get_tx_queue(&Qa, &ch))
			SBUF0 = ch; 
	}
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the Hyperterminal.
//
//-----------------------------------------------------------------------------
/*
INTERRUPT(UART0_ISR, INTERRUPT_UART0)
{

   if (RI0 == 1)
   {
      if( UART_Buffer_Size == 0)  {      // If new word is entered
         UART_Input_First = 0;    }

      RI0 = 0;                           // Clear interrupt flag

      Byte = SBUF0;                      // Read a character from UART

      if (UART_Buffer_Size < UART_BUFFERSIZE)
      {
         UART_Buffer[UART_Input_First] = Byte; // Store in array

         UART_Buffer_Size++;             // Update array's size

         UART_Input_First++;             // Update counter
      }
   }

   if (TI0 == 1)                   // Check if transmit flag is set
   {
      TI0 = 0;                           // Clear interrupt flag

      if (UART_Buffer_Size != 1)         // If buffer not empty
      {
         // If a new word is being output
         if ( UART_Buffer_Size == UART_Input_First ) {
              UART_Output_First = 0;  }

         // Store a character in the variable byte
         Byte = UART_Buffer[UART_Output_First];

         if ((Byte >= 0x61) && (Byte <= 0x7A)) { // If upper case letter
            Byte -= 32; }

         SBUF0 = Byte;                   // Transmit to Hyperterminal

         UART_Output_First++;            // Update counter

         UART_Buffer_Size--;             // Decrease array size

      }
      else
      {
         UART_Buffer_Size = 0;            // Set the array size to 0
         TX_Ready = 1;                    // Indicate transmission complete
      }
   }
}

*/
