void main(void)
{
GPIO_INIT();
DISI(); // 禁用所有未屏蔽的中斷
//AWUCON = 0X80; //0010 0000
// TMR0 = 0; // 加載0x00到TMR0(初始定時器寄存器)
//T0MD = 0X01; // 將precaler0賦給Timer0, precaler0分割率= 1:8,時鐘源為指令時鐘
//INTE = 0X01; // 使能定時器0中斷&PABIE狀態變化中斷
//;啟用Timer0和全局中斷位
//PCON1 = C_TMR0_En; // 啟用Timer0
ENI();
while(1)
{
CLRWDT();
RX_SHUT=0;
CLRWDT();
Timer1_INIT();
soft_decodetask();
}
}
void isr(void) __interrupt(0)
{
if(INTFbits.T1IF) // 定時器0中斷 132 = 1MS
{
if(RFDecodeOK == 0)
{
if(!RX_DATA)
{
IR_Time_L++;
IR_Time_H=0;
if(JumpFlag && IR_Time_L > 4)
{
JumpFlag=0;
}
}
else
{
IR_Time_H++;
if(!JumpFlag && IR_Time_H > 4)
{
JumpFlag=1;
soft_decode();
IR_Time_L=0;
IR_Time_H=0;
}
}
}
INTF= (unsigned char)~(C_INT_TMR1); // 清除T0IF標志位
}
}
/******************************定時器配置*************************************************/
void Timer1_INIT(void)
{
TMRH = 0;
TMR1 = 50; //50us
T1CR1 = 0x03;
T1CR2 = C_TMR1_ClkSrc_Inst | C_PS1_Div4;
INTE = 0x08;
}
/******************************中斷*************************************************/
void soft_decode() //解碼
{
static unsigned char RxBitCnt = 0; //接收數據位
static unsigned long RxData = 0; //數據緩沖
if(IR_State==0)
{
if((IR_Time_L >= 19) && (IR_Time < 25))
{
IR_State=1;
RxData=0;
RxBitCnt=0;
}
else
{
IR_State=0;
}
}
else if(IR_State==1 && RxBitCnt < 32)
{
if((IR_Time_L > 6) && (IR_Time_L < 9))
{
RxData = RxData<<1;
RxBitCnt++;
}
else if((IR_Time_L > 11) && (IR_Time_L < 14))
{
RxData = RxData<<1;
RxData |=1;
RxBitCnt++;
}
else
{
RxBitCnt=0;
IR_State=0;
}
if(IR_cnt>=32)
{
RxBitCnt=0;
IR_State=0;
RxUserCode = RxData >> 24;
if(RxUserCode == K1_NUM1)
{
RFDecodeOK = 1;
RX_flag = 1;
}
else if(RxUserCode == K1_NUM2)
{
RFDecodeOK = 1;
RX_flag = 2;
}
else if(RxUserCode == K2_NUM1)
{
RFDecodeOK = 1;
RX_flag = 3;
}
else if(RxUserCode == K2_NUM2)
{
RFDecodeOK = 1;
RX_flag = 4;
}
}
}
}
void soft_decodetask(void)
{
static unsigned int RxTimerCnt = 0;
if(RFDecodeOK)
{
RFDecodeOK = 0;
if(RX_flag==1){LED123=0;}
if(RX_flag==2){LED123=1;}
if(RX_flag==3){LED123=0;}
if(RX_flag==4){LED123=1;}
}
if(RFDecodeOK)
{
RxTimerCnt++;
if(RxTimerCnt >= 2000)
{
RxTimerCnt=0;
RFDecodeOK=0;
}
}
}
|