📄 driver.c
字号:
KBCON = 0x00;//clear flag
EKBI = 0;
g_bKBI = TRUE;
}
/* ======================================= */
void NVWriteNByte(Byte n,Byte *pbuffer)
{
I2CStart_InterruptControl();
NvI2c.Direction = NV_WRITE;
NVStartTransfer(n,pbuffer);
I2CEnd_InterruptControl();
}
/* ======================================= */
void NVReadNByte(Byte n,Byte *pbuffer)
{
I2CStart_InterruptControl();
NvI2c.Direction = NV_READ;
NVStartTransfer(n,pbuffer);
I2CEnd_InterruptControl();
}
/* ======================================= */
static void NVStartTransfer(Byte number,Byte *pbuffer)
{
NvI2c.DataNum = number;
NvI2c.pbuffer = pbuffer;
NvI2c.RetryCount = 0;
bSubAddrSended = FALSE;
g_bI2cError = FALSE;
g_bI2cOk = FALSE;
I2CON = 0x64;// I2EN = 1; STA = 1; AA = 1;
}
/* ======================================= */
void Delay1ms(Word num)
{
/*
TR1 = 0;
TL1 = LOWBYTE(ISR_TIME_1MS_RELOAD_VALUE);
TH1 = HIGHBYTE(ISR_TIME_1MS_RELOAD_VALUE);
TR1 = 1;
ucIrSendDelayType = DELAY_1MS;
ucDelay1msCount = num;
while(ucDelay1msCount) ;
TR1 = 0;
ucIrSendDelayType = DELAY_NONE;
*/
Byte i;
do
{
i = 200;
while (i--)
_nop_();
}
while (num--);
}
/* ======================================= */
#ifdef DEBUG
void IRSend_Test(void)
{
/*
IRSendLeadingCode();
IRSendByte(0x40);
IRSendByte(0xbf);
IRSendByte(0x5b);
IRSendByte(0xa4);
IRSendStop();
*/
// IRSend_SendReady();
PIN_LED4 = 0;
IRSendDelay(DELAY_125US);
PIN_LED4 = 1;
IRSendDelay(DELAY_125US);
PIN_LED4 = 0;
IRSendDelay(DELAY_125US);
PIN_LED4 = 1;
IRSendDelay(DELAY_125US);
}
#endif
/* ======================================= */
void IRSend_SendReady(void)
{
IRSendStart_InterruptControl();
//begin
IRSendLeadingCode();
IRSendByte(0xAA);
IRSendByte(0x55);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
/* ======================================= */
void IRSend_RecReady(void)
{
IRSendStart_InterruptControl();
//begin
IRSendLeadingCode();
IRSendByte(0xAC);
IRSendByte(0x53);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
/* ======================================= */
/*
void IRSend_Address(Word address)
{
Byte checkSum;
Byte addr[2];
addr[0] = LOWBYTE(address);
addr[1] = HIGHBYTE(address);
checkSum = CalculateCheckSum(2, addr);
IRSendStart_InterruptControl();
//begin
IRSendLeadingCode();
IRSendByte(0xAB);
IRSendByte(0x54);
IRSendByte(addr[1]);
IRSendByte(addr[0]);
IRSendByte(checkSum);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
*/
/* ======================================= */
void IRSend_nByteData(Byte n,Byte *pbuffer)
{
Byte checkSum;
Byte i;
IRSendStart_InterruptControl();
checkSum = CalculateCheckSum(n, pbuffer);
//begin
IRSendLeadingCode();
IRSendByte(0xAD);
IRSendByte(0x52);
IRSendByte(n);
for(i = 0; i < n; i++)
{
IRSendByte(*pbuffer);
pbuffer++;
}
IRSendByte(checkSum);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
/* ======================================= */
void IRSend_16ByteData(Byte *pbuffer)
{
Byte checkSum;
IRSendStart_InterruptControl();
checkSum = CalculateCheckSum(16, pbuffer);
//begin
IRSendLeadingCode();
IRSendByte(0xAE);
IRSendByte(0x51);
IRSendByte(*pbuffer);
IRSendByte(*(pbuffer + 1));
IRSendByte(*(pbuffer + 2));
IRSendByte(*(pbuffer + 3));
IRSendByte(*(pbuffer + 4));
IRSendByte(*(pbuffer + 5));
IRSendByte(*(pbuffer + 6));
IRSendByte(*(pbuffer + 7));
IRSendByte(*(pbuffer + 8));
IRSendByte(*(pbuffer + 9));
IRSendByte(*(pbuffer + 10));
IRSendByte(*(pbuffer + 11));
IRSendByte(*(pbuffer + 12));
IRSendByte(*(pbuffer + 13));
IRSendByte(*(pbuffer + 14));
IRSendByte(*(pbuffer + 15));
IRSendByte(checkSum);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
/* ======================================= */
void IRSend_IrEnd(void)
{
IRSendStart_InterruptControl();
//begin
IRSendLeadingCode();
IRSendByte(0xAF);
IRSendByte(0x50);
IRSendStop();
//end
IRSendEnd_InterruptControl();
}
/* ======================================= */
static void IRSendLeadingCode(void)
{
IRModulateCarrierStart();
IRSendDelay(DELAY_9MS);
PIN_IR_OUT = 1;
IRSendDelay(DELAY_4D5MS);
}
/* ======================================= */
static void IRSendByte(Byte value)
{
Byte i;
for(i = 0; i < 8; i++)
{
if(value & BIT0)
{
IRModulateCarrierStart();
IRSendDelay(DELAY_560US);
PIN_IR_OUT = 1;
IRSendDelay(DELAY_1685US);
}
else
{
IRModulateCarrierStart();
IRSendDelay(DELAY_560US);
PIN_IR_OUT = 1;
IRSendDelay(DELAY_565US);
}
value >>= 1;
}
}
/* ======================================= */
static void IRSendStop(void)
{
IRModulateCarrierStart();
IRSendDelay(DELAY_560US);
PIN_IR_OUT = 1;
}
/* ======================================= */
static void IRModulateCarrierStart(void)
{
ET0 = 1;
TR0 = 0;
TL0 = LOWBYTE(ISR_TIME0_38K_RELOAD_VALUE);
TH0 = HIGHBYTE(ISR_TIME0_38K_RELOAD_VALUE);
TR0 = 1;
bIrOutPinStatus = 0;
PIN_IR_OUT = 0;
}
/* ======================================= */
static void IRSendDelay(Byte delayType)
{
// bIrSendTimer1Use = TRUE;
ucIrSendDelayType = delayType;
TR1 = 0;
switch(ucIrSendDelayType)
{
case DELAY_9MS:
TL1 = LOWBYTE(ISR_TIME_9MS_RELOAD_VALUE);
TH1 = HIGHBYTE(ISR_TIME_9MS_RELOAD_VALUE);
break;
case DELAY_4D5MS:
TL1 = LOWBYTE(ISR_TIME_4D5MS_RELOAD_VALUE);
TH1 = HIGHBYTE(ISR_TIME_4D5MS_RELOAD_VALUE);
break;
case DELAY_1685US:
TL1 = LOWBYTE(ISR_TIME_1685US_RELOAD_VALUE);
TH1 = HIGHBYTE(ISR_TIME_1685US_RELOAD_VALUE);
break;
case DELAY_565US:
TL1 = LOWBYTE(ISR_TIME_565US_RELOAD_VALUE );
TH1 = HIGHBYTE(ISR_TIME_565US_RELOAD_VALUE );
break;
case DELAY_560US:
TL1 = LOWBYTE(ISR_TIME_560US_RELOAD_VALUE );
TH1 = HIGHBYTE(ISR_TIME_560US_RELOAD_VALUE );
break;
case DELAY_125US:
TL1 = LOWBYTE(ISR_TIME_125US_RELOAD_VALUE );
TH1 = HIGHBYTE(ISR_TIME_125US_RELOAD_VALUE );
break;
default:
return;
}
TR1 = 1;
while(ucIrSendDelayType != DELAY_FINISH) ;
TR1 = 0;
TR0 = 0;
ET0 = 0;
ucIrSendDelayType = DELAY_NONE;
// bIrSendTimer1Use = FALSE;
}
/* ======================================= */
void IR_CheckLeadingHander(void)
{
Word irTimer;
if(ucIrDetectStatus == IR_CHECK_LEADING)
{
irTimer = (TH1<<8) + TL1;
if((irTimer < IR_TIME_7MS) && (INT1 == 1))
{
ucIrDetectStatus = IR_STANDBY;
}
}
}
/* ======================================= */
static void I2CStart_InterruptControl(void)
{
EX1 = 0;
ET0 = 0;
ET1 = 0;
EKBI = 0;
EI2C = 1;
}
/* ======================================= */
static void I2CEnd_InterruptControl(void)
{
while(!g_bI2cOk) ;
Delay1ms(10);
EX1 = 1;
ET1 = 1;
EI2C = 0;
}
/* ======================================= */
static void IRSendStart_InterruptControl(void)
{
Delay1ms(200);
EX1 = 0;
EI2C = 0;
EKBI = 0;
}
/* ======================================= */
static void IRSendEnd_InterruptControl(void)
{
EX1 = 1;
}
/* ======================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -