#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include <stdio.h>
#include <string.h>
// Leader code range 13.5ms
#define IR_LDC_MAX 14500 //14.5ms
#define IR_LDC_MIN 12500 //12.5ms
// Bit = 1 range 2.25ms
#define IR_BIT_1_MAX 2500 //2.5ms
#define IR_BIT_1_MIN 2000 //2ms
// Bit = 0 range 1.12ms
#define IR_BIT_0_MAX 1250 //1.25ms
#define IR_BIT_0_MIN 1000 //1ms
volatile uint8_t IR_State = 0; // IR State
volatile uint8_t IR_LDC_Ready = 0; // LeaDer Code is Ready
volatile uint8_t IR_CTC_Ready = 0; // CusTomer Code is Ready
volatile uint8_t IR_CTC0 = 0; // Received CusTomer Code 0
volatile uint8_t IR_CTC1 = 0; // Received CusTomer Code 1
volatile uint8_t IR_DAC = 0; // Received DAta Code
volatile uint8_t IR_DAB = 0; // Received DAta Bar code
volatile uint8_t IR_cnt = 0;
uint8_t IR_CODE[4] = {0x00, 0x00, 0x00, 0x00};
void IrDa_NEC_Rx(uint16_t Capture_Time)
{
if(IR_State == 0)
{
IR_LDC_Ready = 0; // Clear LeaDer Code Ready
IR_CTC_Ready = 0; // Clear CusTomer Code Ready
IR_State++;
}
// Leader or Repeater code
else if(IR_State == 1)
{
// Leader code
if((Capture_Time >= IR_LDC_MIN) && (Capture_Time <= IR_LDC_MAX))
{
IR_LDC_Ready = 1; // Set LeaDer Code Ready
IR_State++;
}
else
{
IR_State = 1;
IR_LDC_Ready = 0; // Clear LeaDer Code Ready
IR_CTC_Ready = 0; // Clear CusTomer Code Ready
}
}
// Customer code 0
else if((IR_State >= 2 && IR_State < 10) && (IR_LDC_Ready == 1))
{
IR_State++;
IR_CTC0 = IR_CTC0 >> 1;
if((Capture_Time >= IR_BIT_0_MIN) && (Capture_Time <= IR_BIT_0_MAX)) // 1.12ms = 0
IR_CTC0 &= 0x7f;
else if((Capture_Time >= IR_BIT_1_MIN) && (Capture_Time <= IR_BIT_1_MAX)) // 2.25ms = 1
IR_CTC0 |= 0x80;
else
IR_State = 0;
}
// Customer code 1
else if((IR_State >= 10 && IR_State < 18) && (IR_LDC_Ready == 1))
{
IR_State++;
IR_CTC1 = IR_CTC1 >> 1;
if((Capture_Time >= IR_BIT_0_MIN) && (Capture_Time <= IR_BIT_0_MAX)) // 1.12ms = 0
IR_CTC1 &= 0x7f;
else if((Capture_Time >= IR_BIT_1_MIN) && (Capture_Time <= IR_BIT_1_MAX)) // 2.25ms = 1
IR_CTC1 |= 0x80;
else
IR_State = 0;
}
// Data
else if((IR_State >= 18 && IR_State < 26) && (IR_LDC_Ready == 1))
{
IR_State++;
IR_DAC = IR_DAC >> 1;
if((Capture_Time >= IR_BIT_0_MIN) && (Capture_Time <= IR_BIT_0_MAX)) // 1.12ms = 0
IR_DAC &= 0x7f;
else if((Capture_Time >= IR_BIT_1_MIN) && (Capture_Time <= IR_BIT_1_MAX)) // 2.25ms = 1
IR_DAC |= 0x80;
else
IR_State = 0;
}
// Data bar
else if((IR_State >= 26 && IR_State < 34) && (IR_LDC_Ready == 1))
{
IR_State++;
IR_DAB = IR_DAB >> 1;
if((Capture_Time >= IR_BIT_0_MIN) && (Capture_Time <= IR_BIT_0_MAX)) // 1.12ms = 0
IR_DAB &= 0x7f;
else if((Capture_Time >= IR_BIT_1_MIN) && (Capture_Time <= IR_BIT_1_MAX)) // 2.25ms = 1
IR_DAB |= 0x80;
else
IR_State = 0;
if(IR_State == 34)
{
if((IR_DAC ^ IR_DAB) == 0xff)
{
IR_LDC_Ready = 0; // Clear LeaDer Code Ready
IR_CODE[0] = IR_CTC0;
IR_CODE[1] = IR_CTC1;
IR_CODE[2] = IR_DAC;
IR_CODE[3] = IR_DAB;
IR_cnt++;
printf("IR_cnt=%Bd,CTC0=%bX,CTC1=%bX,DAC=%bX,DAB=%bX\n", IR_cnt,IR_CTC0,IR_CTC1,IR_DAC,IR_DAB);
}
IR_State = 0;
}
}
}
/************************************************************************************************************
* Timer2 Capture interrupt subroutine
************************************************************************************************************/
void Capture_ISR (void) interrupt 12
{
uint16_t T2_count;
T2_count =(C0H<<8)+C0L;
IrDa_NEC_Rx(T2_count);
clr_CAPF0; // clear capture0 interrupt flag
clr_TF2;
}
/************************************************************************************************************
* Main function
************************************************************************************************************/
void main(void)
{
Set_All_GPIO_Quasi_Mode;
InitialUART0_Timer3(115200);
P15_Input_Mode;
P15 = 1;
T2MOD = 0XA9; // 定时器2时钟定义为16分频,捕获事件发生后计数值自动清0
T2CON&=~SET_BIT0; //定义定时器2功能为自动重装载模式
IC7_P15_CAP0_FallingEdge_Capture; //下降沿捕获
TL2 = 0;
TH2 = 0;
set_EA;
set_ECAP; //Enable Capture interrupt
set_TR2; //Triger Timer2
while(1);
}
|