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

//HS16F1211 
//TMR0
//configuration introduction

//designer:LvBuWei
//updata：20240612

//*********************************************************************** 
//Files include 
#include 	"hs16f1211.h" 
#include 	"main.h"

#define		TMR0_VAL	200


unsigned char u8Temp;
unsigned char TMR0_Mode;
bit F_OPTION_Updata;
//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FT0IF) 
	{ 
		FT0IF = 0; 
		TMR0 = 256-TMR0_VAL; 
		PORTB = PORTB^0xff;			
		u8Temp++;
		if(u8Temp>=3)
		{
			u8Temp = 0;
			F_OPTION_Updata = 1;
		}		
	}
}


#define		iTMR0FrqWDT1_Fcpu		0	//0-TMR0;1-WDT

#define		iFrq_Fcpu2		0
#define		iFrq_Fcpu4		1
#define		iFrq_Fcpu8		2
#define		iFrq_Fcpu16		3
#define		iFrq_Fcpu32		4
#define		iFrq_Fcpu64		5
#define		iFrq_Fcpu128	6
#define		iFrq_Fcpu256	7
//****TMR0 configuration****//
/*
TMR0 no have Start Bit，So TMR0 is awayls work.
;TMR0：定时器没有启动位一直运行中，分频可选，无自动装载
If user want read T0IF bit,T0IE must be enable;if not,The T0IF of user read is not right;
*/
//1211-4M 2T 
//Ftmr0=2M 2T
void TMR0_Init(unsigned char clkSelect,unsigned char frq,unsigned char tmr0_valInit) 
{
	OPTION= 0b00000000; //0000 0000
	//OPTION= 0b00100000; //0010 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：预分频比率选择位
	if(clkSelect)	OPTION |= 0x08;
	//else			OPTION &= ~0x08;
	//OPTION &= ~0x07;
	OPTION |= frq;
	
	TMR0 = 256 - tmr0_valInit; 
	FT0IF = 0;	//INTFLAG.bit0
	FT0IE = 1;	//INTEN.bit0	 
	FGIE  = 1;	//INTEN.bit7
}

//****main function****//
void main(void)
{
	GPIO_Init();
	CLR_BANK0_RAM();
	CLR_BANK1_RAM();
	
	//TMR0_Mode=iTMR0Frq_Fcpu2;
	TMR0_Init(iTMR0FrqWDT1_Fcpu,iFrq_Fcpu2,TMR0_VAL);
	
	while(1)
	{
		//CLRWDT 
		PORTA = PORTA^0xff;
		//;PORTB = PORTB^0xff;
		//OPTION = OPTION|(u8Temp & 0x07);
		if(F_OPTION_Updata)//
		{			
			F_OPTION_Updata = 0;
			TMR0_Mode++;
			switch(TMR0_Mode)
			{
				default:	TMR0_Mode = 0; //break;
				case iFrq_Fcpu2:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu2;	break;
				case iFrq_Fcpu4:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu4;	break;
				case iFrq_Fcpu8:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu8;	break;
				case iFrq_Fcpu16:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu16;	break;
					
				case iFrq_Fcpu32:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu32;	break;
				case iFrq_Fcpu64:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu64;	break;
				case iFrq_Fcpu128:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu128;	break;
				case iFrq_Fcpu256:	OPTION &= ~0x07;	OPTION |= iFrq_Fcpu256;	break;
			}
		}
		//DelayUs(1);
	}
} 

 
//****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  = 0b00100000;	//101- ----		
			//;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
			//;Bit6：EIS: IOB0/INT引脚定义位			;0-DIS, 1-EN
			//;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
					
//PORTA //----------------------------------------------------------------------
	PORTA = 0b00000000;	
	TRISA = 0b00000000;	//(0-OUT;1-IN) 		
	PAHCON= 0b11111111;	//--11 1111 :1-DIS;0-EN 
	PADCON= 0b11111111;	//--11 ---- :1-DIS;0-EN 	
	PAIC0 = 0b00000000;	//--00 0000 :1-EN;0-DIS
	
	PDCON = 0b11111111;	//0000 0000 :1-DIS;0-EN[pdb3/pdb2/pdb1/pdb0/ /pda3/pda2/pda1/pda0]
	PDCON2= 0b11111111;	//--00 0000 :1-DIS;0-EN[-/-/pda5/pda4/ /pdb7/pdb6/pdb5/pdb4]
//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 
	//----------------------------------------------------------------------
}


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


//****************************************************  
//CLR_BANK1_RAM: 
//C中嵌入ASM，指令操作特殊寄存器前面需要加下划线“_”;
//****************************************************    
void CLR_BANK1_RAM(void)
{
#asm  	
	MOVLW	(0x80 | 03FH)
	MOVWF	_FSR
	MOVLW	020H	//;32*reg-bank1
	MOVWF	020H
	
BANK1_RAM_CLR_LOOP:	
	CLRF	_INDF
	DECF	_FSR,F
	DECFSZ	020H,F
	GOTO	BANK1_RAM_CLR_LOOP	
	RETURN	
#endasm	   
}

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