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

//HS16F1211
//PBx
//configuration introduction
//designer:LvBuWei
//updata：20240612

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


 
//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FPBIF) 
	{ 
		FPBIF = 0; 
		PORTA = PORTA^0x0f;
	} 	
} 

//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	PB_CHANGE_INIT();	
		
	while(1) 
	{ 
		CLRWDT 
		PORTA = PORTA^0xf0;		
		//;PORTB = PORTB^0xff;		
		DelayUs(1);
	} 
} 
 


//****PB_CHANGE_INT Config****//
//PB input state change can make interrupt;
//PB pin must be set input with up_pull or down_pull;
//WUCON pin must be enable；
void PB_CHANGE_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：预分频比率选择位
	
//PB2&PB3 as example 
	IOSTB = 0b00001100;	
	
//UP_PULL	:bit must be set 
//input with UP_Pull;
//bit:PORTB7~PORTB0；
	PHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN  	

//pin state change interrupt enable
	WUCON = 0b00001100;	//0000 0000	:1-EN;0=DIS 
	
	FPBIF = 0;	//INTIF【INTFLAG.bit1】PB interrupt flag,software reset
	
	FPBIE = 1;	//FPBIE【INTEN.bit1】PB 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--);    
} 