//*********************************************************************** 
//Copyright (c) 2022   HuaXin Micro-electronics Co.,Ltd 

//HS16F1200
//TC0
//configuration introduction

//designer:LvBuWei
//updata：20221111

//*********************************************************************** 
//
//EEPROM: 128bytes
//RAM: 64*bytes
//ROM: 1K×14
//STACK: 8-level
//
//*********************************************************************** 
//****************************************************  
//	   	 VDD -1		8 - VSS	                     
//	   	 PB5 -2		7 - PB0	                     
//	OUT1 PB4 -3		6 - PB1 KEY1
//	OUT2 PB3 -4		5 - PB2	KEY2                    
//****************************************************
//Files include 
#include 	"hs16f1200.h" 
#include 	"main.h"

 
 
//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FTC0IF) 
	{ 
		FTC0IF = 0; 
	 /*TC0_Mode_8bit*/
		TC0L = 58;	//TC0 Low_8bit
		
		PORTB = PORTB^0x20;	
	} 
} 


//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	CLR_BANK0_RAM();
	
	//TC0_Init(TC0_Mode_8bit); 
	TC0_Init(TC0_Mode_LOW);
	
	while(1) 
	{ 
		CLRWDT  
		PORTB = PORTB^0x01;		
		//;PORTB = PORTB^0xff;		
		DelayUs(1);
	} 
} 

//****TC0 configuration ****//
/*
	Example of this program：
	MODE1:8bit-Timer(no AutoLoad)
	MODE2:16bit-Timer(no AutoLoad)
	MODE3:BUZ(AutoLoad)
	MODE4:PWM(PWM1 can choose PB1 or PB5),(PWM2 can choose PB0 or PB4),(PWM0 can choose PB0 or PB2);
	MODE5:TC0-internal Low_Mode
*/
void TC0_Init(unsigned char mode) 
{ 
	if(mode == TC0_Mode_8bit) 
	{
		/*
		8bit Timer interrupt：1ms
		Config：Fcpu = 4Mhz 4'
		*/
		//;TC0 init value
	 	TC0L = 58;	//TC0 Low_8bit
	 
	 
	 	TC0M = 0x48;	//0100 1000;//Fosc 4' Fosc=8MHz;8bit
	 					//;Bit7：TC0TR:TC0 启动控制位。
						//;Bit6~Bit4：TC0S[2:0]:TC0 分频选择位
						//;Bit3：TCCKS:TC0 时钟信号控制位
		
		FTC0IF = 0;		//TC0 overflow flag		 
		FTC0IE = 1;  	//TC0 overflow enable bit(1=EN,0=DIS)	
	}
	else if(mode == TC0_Mode_LOW)
	{
		/*
		8bit Timer interrupt：24ms
		Config：Fcpu = 4Mhz 4'
		*/
		//;TC0 init value
	 	TC0L = 0x06;	//TC0 Low_8bit  
	 
	 	TC0M = 0x70;	//0111 0000;//Fcpu 2';8bit
		
		FTC0IF = 0;		//TC0 overflow flag		 
		FTC0IE = 1;   	//TC0 overflow enable bit(1=EN,0=DIS)	
		
		OSCM = 0x10;	//-001 0000 ;TC0时钟源选低速
	}
	
	FTC0TR = 1; 	//;TC0 Start bit
	
	FGIE  = 1;	//【INTEN.bit7】GIE:global inturrupt enable bit 
}


//****GPIO configuration****//
void GPIO_Init(void) 
{
	OPTION= 0b000000000;//0000 0000	
			//;Bit7：INT1EDG：外部中断1触发方式选择位（PB1）;0-falling edge, 1-rising edge
			//;Bit6：INTEDG：外部中断触发方式选择位（PB0）	;0-falling edge, 1-rising edge
			//;Bit5：T0CS：TMR0时钟源选择位					;0-fcpu, 1-ExitClock
			//;Bit4：T0SE：TMR0时钟源边沿选择位				;0-falling edge, 1-rising edge
			//;Bit3：PSA：预分频分配位						;0-TMR0, 1-WDT
			//;Bit2~Bit0：PS2:PS0：预分频比率选择位

	PCON  = 0b10100000;	//101- ----		
			//;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
			//;Bit6：EIS: IOB0/INT引脚定义位			;0-DIS, 1-EN
			//;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
		 
//PORTB //----------------------------------------------------------------------

	PORTB = 0b00000000;	

	TRISB = 0b00000000;	//(0-OUT;1-IN) 	
	
	PBIC0 = 0b00000000;	//1111 1111 :1-EN;0-DIS
	
	WUCON = 0b00000000;	//0000 0000	:1-EN;0=DIS 

	ODCON = 0b00000000;	//1111 -111 :1-EN;0-DIS 	

	PHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN 	
	
	PORTB = 0b00000000;	
	 //----------------------------------------------------------------------
} 


//****************************************************  
//CLR_BANK0_RAM: 
//C中嵌入ASM，指令操作特殊寄存器前面需要加下划线“_”;
//****************************************************    
void CLR_BANK0_RAM(void)
{
#asm  
	MOVLW	0X3F
	MOVWF	040H
	MOVLW	0X7F
	MOVWF	_FSR
	
BANK0_RAM_CLR_LOOP:	
	CLRF	_INDF
	DECF	_FSR,F
	DECFSZ	040H,F
	GOTO	BANK0_RAM_CLR_LOOP		
#endasm	   
}


//****delay function****// 
//;Fcpu=1MHz;DelayUs = 11+7x     
void DelayUs(unsigned char delayUs)     
{
	while(delayUs--);    
} 