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

//HS16F1211
//INT & INT1
//configuration introduction
//updata：20240612

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

 
//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FINT1IF) 
	{ 
		FINT1IF = 0; 	 
		PORTB = PORTB^0x80;	
		OPTION^= 0b10000000; // -000 0001
	} 
	if(FINTIF) 
	{
		FINTIF = 0; 	 
		PORTB = PORTB^0x40;	
		OPTION^= 0b01000000; // -000 0001
	}
} 


//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	EXIT_INT_INIT();
	EXIT_INT1_INIT();
	CLR_BANK0_RAM();
	CLR_BANK1_RAM();
	
	while(1) 
	{ 
		CLRWDT  
		PORTA = PORTA^0xff;		
		//;PORTB = PORTB^0xff;		
		DelayUs(1);
	} 
} 
 


//****EXIT_INT configuration****
//;PB0-EXIT_INT pin-PB0：input with Up_pull
//;PCON.bit6-INT Set pin;
//;***********************************************************************
void EXIT_INT_INIT(void)
{
//option
	OPTION|= 0b00000001; // -000 0001
						//;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
	PCON  = 0b11000000;	//010- ----
						//PCON.bit7:WDTE:WDT enable;1-EN;0-DIS
						//PCON.bit6:EIS:INT pin define;1=INT;0=IOB0，INT Function Disable
						//PCON.bit5:LVDTE:Low Voltage Detect enable
	
	IOSTB |= 0b00000001;	//(0-OUT;1-IN) 
	
	PHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN  

	FINTIF = 0;		//INTIF【INTFLAG.bit2】INT interrupt flag,software reset
	
	FINTIE = 1;		//INTIE【INTEN.bit2】INT interrupt enable (1=EN,0=DIS)	
	
	FGIE  = 1;		//GIE【INTEN.bit7】:global interrupt enable bit
}


//***********************************************************************
//EXIT1_INT configuration;
//PB1-EXIT_INT1 pin-PB1：input with Up_pull
void EXIT_INT1_INIT(void)
{
//option attentions
	OPTION|= 0b10000001; // -000 0001
						//;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
	//PCON  = 0b00000000;	//010- ----
						//PCON.bit7:WDTE:WDT enable;1-EN;0-DIS
						//PCON.bit6:EIS:INT pin define;1=INT;0=IOB0，INT Function Disable
						//PCON.bit5:LVDTE:Low Voltage Detect enable
	
	IOSTB |= 0b00000010;	//(0-OUT;1-IN) 
	
	PHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN    

	FINT1IF = 0;	//INTIF【INTFLAG.bit5】INT interrupt flag,software reset
	
	FINT1IE = 1;	//INTIE【INTEN.bit5】INT interrupt enable (1=EN,0=DIS)
	
	FGIE  = 1;		//GIE【INTEN.bit7】:global interrupt 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  = 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--);    
} 