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

//HS23P1820
//RollingCode
//configuration introduction

//designer:LvBuWei
//updata：20210909

//*********************************************************************** 
#include 	"hs23p1820.h" 
#include 	"main.h"  


//****RollingCode define****// 
const unsigned char romRllingCode[4] @ 0x10 = {0x10,0x11,0x12,0x13};


//****interrupt function****//
void interrupt myIsr(void)
{ 
	if(FTC0IF) 
	{ 
		FTC0IF = 0; 
	} 
} 


unsigned char RllingCodeL; 
unsigned char RllingCodeM; 
unsigned char RllingCodeH;

//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	CLR_BANK0_RAM();
	
	RllingCodeL = romRllingCode[0]; 
	RllingCodeM = romRllingCode[1]; 
	RllingCodeH = romRllingCode[2];
	
	while(1) 
	{ 
		CLRWDT  
		PORTB = PORTB^0x20;		
		
		DelayUs(100); 
		DelayUs(100);
		DelayUs(100);
		
		SendDataRF(0x55);
		PORTB = PORTB^0xff;	
		SendDataRF(RllingCodeH);	//PB0
		PORTB = PORTB^0xff;	
		SendDataRF(RllingCodeM); 
		PORTB = PORTB^0xff;	
		SendDataRF(RllingCodeL); 
	} 
} 
 
 

//****RollingData Send For Test**** 
#define	SendPin		FPB0 
#define	TimePart	10	 
void SendDataRF(unsigned char sendData) 
{ 
	unsigned char Cnt; 
	CLRWDT
	SendPin=0; 
	for(Cnt = 0;Cnt < 8;Cnt++) 
	{ 
		if(sendData & 0x80) 
		{ 
			SendPin = 1; 
			DelayUs(TimePart*3); 
			SendPin = 0; 
			DelayUs(TimePart);			 
		} 
		else 
		{ 
			SendPin = 1; 
			DelayUs(TimePart); 
			SendPin = 0; 
			DelayUs(TimePart*3);	 
		} 
		sendData = sendData<<1; 
	} 
} 
 


//****GPIO configuration****//
void GPIO_Init(void) 
{
	OPTION= 0b000000000;//00-- -000	
						//;Bit7：INT1EDG：外部中断1触发方式选择位（PB1）;0-falling edge, 1-rising edge
						//;Bit6：INTEDG：外部中断触发方式选择位（PB0）	;0-falling edge, 1-rising edge			
						//;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;	//--11 1111 :1-EN;0-DIS
	
	WUCON = 0b00000000;	//--00 0000	:1-EN;0=DIS 

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

	PHCON = 0b11111111;	//--11 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	
	CLRR	_FSR
#endasm	   	
}

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