#include <STC8H.h>
#include <stdio.h>
#include "intrins.h"

//========================================================================
//                           数据类型定义
//========================================================================
typedef unsigned char   u8;     //  8 bits 
typedef unsigned int    u16;    // 16 bits 
typedef unsigned long   u32;    // 32 bits 

typedef signed char     int8;   //  8 bits 
typedef signed int      int16;  // 16 bits 
typedef signed long     int32;  // 32 bits 

typedef unsigned char   uint8;  //  8 bits 
typedef unsigned int    uint16; // 16 bits 
typedef unsigned long   uint32; // 32 bits 

#define MAIN_Fosc        22118400L	//定义主时钟		// 11059200UL
#define UART3_BRT         (65536 - MAIN_Fosc / 115200 / 4)
#define UART4_BRT         (65536 - MAIN_Fosc/ 115200 / 4)

#define COM3_BUF_LENGTH	64
#define COM4_BUF_LENGTH	128

bit B_COM3_Tx_Busy;
u16 COM3_Rx_Cnt;
u16 COM3_Tx_Cnt;
u8 xdata COM3_Rx_Buffer[COM3_BUF_LENGTH];

bit B_COM4_Tx_Busy;
u16 COM4_Rx_Cnt;
u16 COM4_Tx_Cnt;
u8 xdata COM4_Rx_Buffer[COM4_BUF_LENGTH];

void UART3_Isr() interrupt 17
{
    if (S3CON & 0x02)
    {
        S3CON &= ~0x02;
        B_COM3_Tx_Busy = 0;
    }
    if (S3CON & 0x01)
    {
        S3CON &= ~0x01;
		COM3_Rx_Buffer[COM3_Rx_Cnt] = S3BUF;	
        if(++COM3_Rx_Cnt >= COM3_BUF_LENGTH - 1)   COM3_Rx_Cnt = 0;    //防溢出
		COM3_Rx_Buffer[COM3_Rx_Cnt] = 0;
    }
}

void Uart3_Init()
{
    S3CON = 0x50;
    T3L = UART3_BRT;
    T3H = UART3_BRT >> 8;
    T4T3M |= 0x0A;
	
	IE2 |= 0x08;
	
    COM3_Rx_Cnt = 0;
    COM3_Tx_Cnt= 0;
    B_COM3_Tx_Busy = 0;
}


// -------- 串口3 发送字符串 -------------------------------------------
void COM3_PrintString(unsigned char *puts)		//发送一个字符串
{
	for (; *puts != 0;  puts++)     //遇到停止符0结束
    {
        S3BUF = *puts;
        B_COM3_Tx_Busy = 1;
        while(B_COM3_Tx_Busy);
    }
}

void Uart3_SendChar(char dat)
{
    while (B_COM3_Tx_Busy);
    B_COM3_Tx_Busy = 1;
    S3BUF = dat;
}

void Uart3_SendStr(char *p)
{
    while (*p)
    {
        Uart3_SendChar(*p++);
    }
}

void Uart4_Isr() interrupt 18
{
    if (S4CON & 0x02)		//	((S4CON & 0x02) != 0)
    {
        S4CON &= ~0x02;
        B_COM4_Tx_Busy = 0;
    }
    if ((S4CON & 0x01) != 0)		//	(S4CON & 0x01)
    {
        S4CON &= ~0x01;

		COM4_Rx_Buffer[COM4_Rx_Cnt] = S4BUF;	
        if(++COM4_Rx_Cnt >= COM4_BUF_LENGTH - 1)   COM4_Rx_Cnt = 0;    //防溢出
		COM4_Rx_Buffer[COM4_Rx_Cnt] = 0;
    }
}

void Uart4_Init()
{
    S4CON = 0x50;
    T4L = UART4_BRT;
    T4H = UART4_BRT >> 8;
	T4T3M |= 0xA0;

	IE2 |= 0x10;
	
    COM4_Rx_Cnt = 0;
    COM4_Tx_Cnt= 0;
    B_COM4_Tx_Busy = 0;
}

void COM4_PrintString(unsigned char *puts)		//发送一个字符串
{
	for (; *puts != 0;  puts++)     //遇到停止符0结束
    {
        S4BUF = *puts;
        B_COM4_Tx_Busy = 1;
        while(B_COM4_Tx_Busy);
    }
}

void Uart4_SendChar(char dat)
{
    while (B_COM4_Tx_Busy);
    B_COM4_Tx_Busy = 1;
    S4BUF = dat;
}

void Uart4_SendStr(char *p)
{
    while (*p)
    {
        Uart4_SendChar(*p++);
    }
}


void GPIO_Init(void)		// 管脚状态初始化
{
// I/O口工作模式PnM1和PnM0组合，00-准双向口，01-推挽输出，10-高阻输入，11-开漏输出
	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	P3M0 = 0x00;
	P3M1 = 0x00;
	P4M0 = 0x00;
	P4M1 = 0x00;
	P5M0 = 0x00;
	P5M1 = 0x00;

// 端口内部4.1K上拉电阻控制寄存器，0-为禁止端口内部上拉电阻，1-使能端口内部上拉电阻	
//	P0PU=0xFF;
//	P1PU=0xFF;
//	P2PU=0xFF;
//	P3PU=0xFF;
//	P4PU=0xFF;
//	P5PU=0xFF;
}

void Pin_Switch(void)		// 功能管脚切换
{
//	P_SW1 &= ~0xC0;						//UART1/USART1: RxD(P3.0), TxD(P3.1)
//	P_SW2 &= ~0x01;						//UART2/USART2: RxD2(P1.0), TxD2(P1.1)
//	P_SW2 &= ~0x02;						//UART3: RxD3(P0.0), TxD3(P0.1)
	P_SW2 &= 0xFD;		// B1=0 设置串口3的功能脚为P00和P01
	P_SW2 &= ~0x04;						//UART4: RxD4(P0.2), TxD4(P0.3)
}


void main()
{
	u16 i,j,n;
	u8 temp[20];
	u8 tt[] = "temp data \r\n";
    GPIO_Init();		// 管脚状态初始化
	Pin_Switch();		// 功能管脚切换
	
//	P0M0 = 0x00;
//	P0M1 = 0x00;
//	P1M0 = 0x00;
//	P1M1 = 0x00;
//	P2M0 = 0x00;
//	P2M1 = 0x00;
//	P3M0 = 0x00;
//	P3M1 = 0x00;
//	P4M0 = 0x00;
//	P4M1 = 0x00;
//	P5M0 = 0x00;
//	P5M1 = 0x00;
//	
//	P_SW2 &= 0xFD;		// B1=0 设置串口3的功能脚为P00和P01
	
    Uart3_Init();
	Uart4_Init();

    EA = 1;
	i=0;
	j=0;

    while (1)
    {
		if(i>1000)
		{
			i=0;
			j++;
			if(j>1000)
			{
				j=0;
				P52 = ~P52;
				Uart3_SendStr("Uart 3 Test !\r\n");
//				COM3_PrintString("COM3_PrintString ------ \r\n");
				Uart4_SendStr("Uart 4 Test !\r\n");
//				Uart4_SendStr(tt);  // UART1发送一个字符串	
			sprintf(temp,"COM4_Rx_Cnt = %d \n",COM4_Rx_Cnt);
			Uart4_SendStr(temp);  // UART1发送一个字符串	
				
			}
		}
		i++;

		if(COM3_Rx_Cnt > 2)
		{
//			Uart3_SendStr(COM3_Rx_Buffer);
			COM3_PrintString(COM3_Rx_Buffer);
			COM3_Rx_Cnt = 0;
		}	
		
//		if((COM4_Tx_Cnt != COM4_Rx_Cnt) && (!B_COM4_Tx_Busy))   //收到数据, 发送空闲
//        {
//            S4BUF = COM4_Rx_Buffer[COM4_Tx_Cnt];
//            B_COM4_Tx_Busy = 1;
//            if(++COM4_Tx_Cnt >= COM4_BUF_LENGTH)   COM4_Tx_Cnt = 0;
//        }	
		

		if(COM4_Rx_Cnt > 2)
		{
			for(n = 0; n < COM4_Rx_Cnt; n++)
			{	
				Uart4_SendChar(COM4_Rx_Buffer[n]);
			}

//			Uart4_SendStr(COM4_Rx_Buffer);
//			Uart4_SendStr("Uart 4 Get data ---- !\r\n");
////			COM4_PrintString(COM4_Rx_Buffer);
			COM4_Rx_Cnt = 0;
		}
    }
}	

/*
1、串口3和4初始化后不正常，单独一个正常。
答：因为串口3和4使用相同的定时器控制器，需要根据实际情况使用只操作对应起作用的设置，不影响其他的设置。

2、串口正常发送，接收数据返回时数据不正确，什么原因？
答：
*/



//#define FOSC        22118400L
//#define BRT         (65536 - FOSC / 115200 / 4)

//bit busy;
//char wptr;
//char rptr;
//char buffer[16];

//void Uart4Isr() interrupt 18
//{
//    if (S4CON & 0x02)
//    {
//        S4CON &= ~0x02;
//        busy = 0;
//    }
//    if (S4CON & 0x01)
//    {
//        S4CON &= ~0x01;
//        buffer[wptr++] = S4BUF;
//        wptr &= 0x0f;
//    }
//}

//void Uart4Init()
//{
//    S4CON = 0x50;
//    T4L = BRT;
//    T4H = BRT >> 8;
//    T4T3M |= 0xa0;
//    wptr = 0x00;
//    rptr = 0x00;
//    busy = 0;
//}

//void Uart4Send(char dat)
//{
//    while (busy);
//    busy = 1;
//    S4BUF = dat;
//}

//void Uart4SendStr(char *p)
//{
//    while (*p)
//    {
//        Uart4Send(*p++);
//    }
//}

//void main()
//{
//	u16 i = 0,j = 0;
//	
//    P0M0 = 0x00;
//    P0M1 = 0x00;
//    P1M0 = 0x00;
//    P1M1 = 0x00;
//    P2M0 = 0x00;
//    P2M1 = 0x00;
//    P3M0 = 0x00;
//    P3M1 = 0x00;
//    P4M0 = 0x00;
//    P4M1 = 0x00;
//    P5M0 = 0x00;
//    P5M1 = 0x00;
//	
//	P_SW2 &= ~0x04;						//UART4: RxD4(P0.2), TxD4(P0.3)
//	
//    Uart4Init();
//    IE2 |= 0x10;
//    EA = 1;
//    Uart4SendStr("Uart Test !\r\n");

//    while (1)
//    {
//		
//		if(i>1000)
//		{
//			i=0;
//			j++;
//			if(j>680)
//			{
//				j=0;
//				P52 = ~P52;
//				Uart4SendStr("Uart 4 Test  OK !\r\n");
//			}
//		}
//		i++;		

//        if (rptr != wptr)
//        {
//            Uart4Send(buffer[rptr++]);
//            rptr &= 0x0f;
//        }
//    }
//}


