#include "STC8H.h"

#include "stdio.h"
#include "intrins.h"

typedef 	unsigned char	u8;
typedef 	unsigned int	u16;
typedef 	unsigned long	u32;

#define MAIN_Fosc        22118400L   //定义主时钟（精确计算115200波特率）
//#define UART1_BUF_LENGTH    128
#define  P1_MODE_IO_PU(Pin)				P1M1 &= ~(Pin), P1M0 &= ~(Pin);
#define  P3_MODE_IO_PU(Pin)				P3M1 &= ~(Pin), P3M0 &= ~(Pin);
#define  P1_MODE_IN_HIZ(Pin)			P1M1 |= (Pin), P1M0 &= ~(Pin);


/*************  本地变量声明    **************/

u8  TX1_Cnt;    //发送计数
u8  RX1_Cnt;    //接收计数
bit B_TX1_Busy; //发送忙标志

//u8  RX1_Buffer[UART1_BUF_LENGTH]; //接收缓冲


char code *STCRTC  = "@STCRTC#";    //= "@STCRTC#";  命令头   
char indexrtc=0;                    //当前的命令头索引
char length =0;                     //长度
char rtctime[12] ;               //rtc时间数据
bit  Rec_OK = 0;                    //rtc时间获取完成标志

bit B_1S;
bit B_Alarm;

/****************  RTC初始化函数 *****************/
void RTC_config(void)
{
  u16 timeout = 10000;
  
  X32KCR = 0xc0; // 启动外部32K晶振
  
  while (!(X32KCR & 0x01) && timeout--) 
    _nop_();
  
  if(!(X32KCR & 0x01)) {
    printf("X32K Fail! Status: %02x\n", X32KCR);
  }
  
  RTCCFG &= ~0x02; // 选择外部32K时钟源
  INIYEAR = 24;    // 初始化时间
  INIMONTH = 12;
  INIDAY = 31;
  INIHOUR = 23;
  INIMIN = 59;
  INISEC = 50;
  INISSEC = 0;
  
  RTCCFG |= 0x01;  // 触发初始化
  timeout = 10000;
  while((RTCCFG & 0x01) && timeout--) 
    _nop_();
  
  if(RTCCFG & 0x01) {
    printf("RTC Init Timeout!\n");
  }
  
  RTCIF = 0;       // 清中断标志
  RTCIEN = 0x08;   // 使能秒中断
  RTCCR = 0x01;    // 使能RTC
  
  printf("RTC Init Done. Status: %02x\n", RTCCR);
}

char putchar(char dat)
{
	B_TX1_Busy = 1;			//标志发送忙
	SBUF = dat;					//发一个字节
	while(B_TX1_Busy);	//等待发送完成
    return dat;
}  

void Uart1_Init(void)	//115200bps@24.000MHz
{
	SCON = 0x50;		//8位数据,可变波特率
	AUXR |= 0x01;		//串口1选择定时器2为波特率发生器
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0xCC;			//设置定时初始值
	T2H = 0xFF;			//设置定时初始值
	AUXR |= 0x10;		//定时器2开始计时
	ES = 1;				//使能串口1中断
}


void main(void)
{
	EAXSFR();
	EA = 1;
	P1_MODE_IO_PU(0x80 | 0x40);//P16、P17用于外部晶振
	P3_MODE_IO_PU(0x00 | 0x01);//P30、P31用于串口
	Uart1_Init();
	RTC_config();
//	EA = 1;
	
	while(1)
	{
		if(B_1S)
		{
				B_1S = 0;
				printf("Year=20%bd ", YEAR);
				printf("Month=%bd ", MONTH);
				printf("Day=%bd ", DAY);
				printf("Hour=%bd ", HOUR);
				printf("Minute=%bd ", MIN);
				printf("Second=%bd ", SEC);
				printf("\r\n");
		}
	}
	
}


void UART1_int (void) interrupt 4
{
    u8 dat;
    if(RI)
    {
        RI = 0;
        dat = SBUF;
//        RX1_Buffer[RX1_Cnt] = SBUF;
//        if(++RX1_Cnt >= UART1_BUF_LENGTH)   RX1_Cnt = 0;
//-------------------------------串口RTC对时 -------------------------------   
        if( length>0 )
        {     
            rtctime[length-1]=dat;
            length++;
            if( length>=12 )
            {
                length = 0;
                Rec_OK = 1;
                indexrtc=0;
            }
        }        
        if (dat == STCRTC[ indexrtc])
        {
            indexrtc++;
            if(STCRTC[indexrtc] == '\0')
            {
                length = 1;   //开启接收
                indexrtc=0;
            }
        }
        else
        {
            indexrtc = 0;
            if (dat ==STCRTC[ indexrtc])
                indexrtc++;
        }         
    }

    if(TI)
    {
        TI = 0;
        B_TX1_Busy = 0;
    }
}


void RTC_ISR_Handler (void) interrupt 13//RTC_VECTOR
{
	// TODO: 在此处添加用户代码
	if(RTCIF & 0x80)    //闹钟中断
	{
		P01 = !P01;
		RTCIF &= ~0x80;
		B_Alarm = 1;
	}

	if(RTCIF & 0x08)    //秒中断
	{
		P00 = !P00;
		RTCIF &= ~0x08;
		B_1S = 1;
	}
}


