义隆单片机PT2262无线解码程序
扫描二维码
随时随地手机看文章
Start(void);
voidStop(void);
u8CurrentRead(void);
u8RandomRead(u8addr);
voidByteWrite(u8addr,u8data);
voidSendByte(u8data);
voidI2c_Ack(void);
voidI2c_NoAck(void);
u8I2c_CheckAck(void);
u8ReadByte(void);
voidCommandWrite(u8addr);
voidPageWrite(u8*data,u8cnt,u8BeginAddr);
voidSequentialRead(u8*data,u8cnt,u8BeginAddr);
#defineSDAR60
#defineSCLR61
u8array[2];
////////////////////////////////////////////////////////////
//readRoutine
u8ReadByte(void)
{
u8i;
u8data;
P6CR|=0x01;//changetoinput
for(i=0;i<8;i++)
{
data<<=1;
if(SDA)data++;
nop();
SCL=1;
nop();
nop();
SCL=0;
}
P6CR&=0Xfe;//changetooutput
returndata;
}
u8CurrentRead(void)
{
u8data;
u8ack;
Start();
SendByte(0xA1);//readcommand
ack=I2c_CheckAck();
data=ReadByte();
I2c_NoAck();
Stop();
returndata;
}
u8RandomRead(u8addr)
{
u8data;
CommandWrite(addr);
data=CurrentRead();
returndata;
}
voidSequentialRead(u8*data,u8cnt,u8BeginAddr)
{
u8i;
u8ack;
CommandWrite(BeginAddr);
Start();
SendByte(0xA1);//readcommand
ack=I2c_CheckAck();
for(i=0;i
*data=ReadByte();
I2c_Ack();
}
I2c_NoAck();
Stop();
}
////////////////////////////////////////////////////////////
//sendBytedataviai2c
voidCommandWrite(u8addr)
{
u8ack;
Start();
SendByte(0xA0);
ack=I2c_CheckAck();
nop();
SendByte(addr);
ack=I2c_CheckAck();
nop();
}
voidByteWrite(u8addr,u8data)
{
u8ack;
CommandWrite(addr);
SendByte(data);
ack=I2c_CheckAck();
nop();
Stop();
}
voidPageWrite(u8*data,u8cnt,u8BeginAddr)
{
u8i;
u8ack;
CommandWrite(BeginAddr);
for(i=0;i
SendByte((u8)*data);
ack=I2c_CheckAck();
}
Stop();
}
////////////////////////////////////////////////////////////
//send8bitdata
voidSendByte(u8data)
{
u8i;
for(i=0;i<8;i++)
{
if(data&0x80)
{
SDA=1;//sda=1
}else{
SDA=0;//sda=0
}
nop();
nop();
SCL=1;
nop();
nop();
SCL=0;
data<<=1;
}
}
////////////////////////////////////////////////////////////
//acknowLEDgeofi2c
voidI2c_Ack(void)
{
SDA=0;
nop();
SCL=1;
nop();
nop();
SCL=0;
}
voidI2c_NoAck(void)
{
SDA=1;
nop();
SCL=1;
nop();
nop();
SCL=0;
}
u8I2c_CheckAck(void)
{
u8rst;
rst=0;
SDA=1;
P6CR|=0x01;//SDAsettoinput
nop();
SCL=1;
if(!SDA)rst=1;//haveack
nop();
SCL=0;
P6CR&=0xfe;//SDAsettooutput
returnrst;
}
////////////////////////////////////////////////////////////
//startandstopofi2cbus
voidStart(void)
{
SDA=1;
SCL=1;
nop();
nop();
SDA=0;
nop();
nop();
SCL=0;
nop();
nop();
}
voidStop(void)
{
SDA=0;
nop();
nop();
SCL=1;
nop();
nop();
SDA=1;
nop();
nop();
}#include"EM78x156xx.h"
#include"HEAD156.H"
///////////////////////////////////////////////////
//functiondeclare
voidSysInit(void);
u8KeySCAN(void);
voidBeep(s8time);
voidScanBuz(void);
voidStopBeep(void);
voidScanIR(void);
u8CheckIR(void);
///////////////////////////////////////////////////
//GlobalVariable
u8volatileIntrCnt;
u8MainStatus;
enum{//mainstatus
PROTECT,
RELEASE,
RUNNING,
ALARM,
};
u8MsCnt;
#defineKEY_DOOR0X01
#defineKEY_ACC0X02
#defineKEY_BREAK0X04
u8SaveKey;
u8Cont;
u8Trg;
voidmain()
{
SysInit();
sei();
while(1)
{
wdtc();
if(IntrCnt==160)//20ms
{
IntrCnt=0;
MsCnt++;
ScanBuz();
ScanIR();
if(KeyScan())
{
if(Trg&KEY_DOOR)
{
Beep(1);
nop();
}
if(Trg&KEY_ACC)
{
Beep(1);
nop();
}
if(Trg&KEY_BREAK)
{
nop();
}
}
switch(MsCnt)
{
case50://1s
MsCnt=0;
break;
default:
break;
}
}
}
}
/**************************************************
BUZZERPROCESSROUTINE
p61
**************************************************/
#defineBUZPINR65
bitbBuzEn=0;
bitbBuzSW;
s8cntBuzTimes;
u8cntBuz;
voidScanBuz(void)
{
if(bBuzEn){
if(cntBuzTimes==-1){
BUZPIN=1;//ENDOFSOUND
return;
}
if(bBuzSW){
BUZPIN=1;
if(cntBuz--==0){
cntBuz=25;//RELOADDATA
bBuzSW=0;//CHANGESTATUS
}
}else{
BUZPIN=0;
if(cntBuz--==0){
cntBuz=25;
bBuzSW=1;
cntBuzTimes--;//1TIMES
}
}
if(cntBuzTimes==0){
bBuzEn=0;//ENDOFSOUND
}
}
}
voidBeep(s8time)//time=n*tICk
{
bBuzEn=1;
bBuzSW=1;
cntBuz=25;
cntBuzTimes=time;
}
voidStopBeep(void)
{
bBuzEn=0;
BUZPIN=0;
}
/**************************************************
KEYSCANROUTINE
P60door0:close1:open
P61ACC0:open1:close
P62Break0:open1:close
**************************************************/
u8KeyScan(void)
{
u8temp=0;
temp=PORT6;
temp&=0x07;
temp^=0x06;
if(temp!=SaveKey){
SaveKey=temp;
return0;
}else{
Trg=SaveKey&(SaveKey^Cont);//triggerentry
Cont=SaveKey;//continuousentry
return1;
}
}
/**************************************************
IRPROCESSROUTINE
**************************************************/
#defineir_port0x05
#defineir_pin2
#defineir_pass0
#defineir_lead1
#defineir_receive2
#defineir_syn3
#defineir_delay4
#defineir_dat_code6
#defineir_dat_bit24
u8ir_flag=0;
u8ir_reg_bit;
u8ir_reg_code;
u8ir_addr_hig;
u8ir_addr_low;
u8ir_user;
#defineCODE_LOCK0x41
#defineCODE_UNLOCK0x42
#defineCODE_SOUND0x44
#defineCODE_BACK0x49
#defineCODE_FIND0x48
voidScanIR(void)
{
if(CheckIR()==0)//ifcheckerror,return
{
return;
}
ir_flag=0;
switch(ir_user)
{
caseCODE_LOCK:
Beep(2);
break;
caseCODE_UNLOCK:
Beep(2);
break;
caseCODE_SOUND:
Beep(2);
break;
caseCODE_BACK:
Beep(2);
break;
caseCODE_FIND:
Beep(2);
break;
default:
break;
}
}
u8CheckIR(void)
{
u8temp;
temp=ir_flag^0xff;
temp&=0x01;
if(temp)//ifir_pass==0
{
ir_flag&=0xfe;//clearpassflag
return0;
}
if((ir_addr_hig==0xf6)&&(ir_addr_low==0x7c))
{
return1;//success
}else{
ir_flag=0;//clearpassflag
return0;//fail
}
}
//==================================
void_iNTCallinterrupt_l(void)@0x08:low_int0
{
_asm{
MOV 0x1f,a
swap0x1f
swapa0x03
MOV 0x1e,a
}
}
void_intcallinterrupt(void)@int0
{
///////////////////////////////////////
//intrruptcounterincrease
IntrCnt++;
///////////////////////////////////////
//RFreceive
_asm{
int_ir:
jbc%ir_flag,ir_pass
jmpint_ir_end
jbc%ir_flag,ir_receive
jmpcode_receive
jbc%ir_flag,ir_lead
jmpcode_lead
jbcir_port,ir_pin//ifdetectlowlevel
jmpint_ir_end
bs%ir_flag,ir_lead
clr%ir_reg_code
/***********************************************/
code_lead:
jbcir_port,ir_pin
jmpdetect_hig
detect_low:
jz%ir_reg_code
jmpint_ir_end
detect_error:
clr%ir_reg_code//timeoverflowerror
bc%ir_flag,ir_lead
jmpint_ir_end
detect_hig:
//calculatethelowpulsewidth
MOV a,@85
suba,%ir_reg_code//reg>85
jbs0x03,0
jmpdetect_error
MOV a,@87//reg<87
suba,%ir_reg_code
jbc0x03,0
jmpdetect_error
bs%ir_flag,ir_receive//leadhavereceived,begintoreadcode
bs%ir_flag,ir_delay
MOV a,@ir_dat_code
MOV %ir_reg_code,a
MOV a,@ir_dat_bit
MOV %ir_reg_bit,a
//jmpint_ir_end
/***********************************************/
code_receive:
jbs%ir_flag,ir_syn
jmpcode_r1
jbcir_port,ir_pin
jmpint_ir_end
bc%ir_flag,ir_syn
code_r1:
jbc%ir_flag,ir_delay
jmpcode_r2
jbsir_port,ir_pin//highlevelbegin
jmpint_ir_end
bs%ir_flag,ir_delay
code_r2:
djz%ir_reg_code
jmpint_ir_end
bc%ir_flag,ir_delay
jbcir_port,ir_pin
jmpcode_one
code_zero:
bc0x03,0
jmpram_shift
code_one:
bs%ir_flag,ir_syn
bs0x03,0
ram_shift:
rlc%ir_user
rlc%ir_addr_low
rlc%ir_addr_hig
code_end:
MOV a,@ir_dat_code
MOV %ir_reg_code,a
djz%ir_reg_bit
jmpint_ir_end
bs%ir_flag,ir_pass;接收成功标志
int_ir_end:
nop
}//endofasmcode
///////////////////////////////////////
//clearinterruptflag
TCIF=0;
_asm{
swapa0x1e
MOV 0x03,a
swapa0x1f
}
}
voidSysInit(void)
{
cli();
/*---------------------------------------------------------------------------
CONT_SETUP
argument:TCC_1_1~TCC_1_256prescal
TCC_SRC_INT/TCC_SRC_EXTsourceselection
TCC_EDGE_RISE/TCC_EDGE_FALLtrigeedgeselection
TCC_PRESCAL/WDT_PRESCALTCCORWDTUSEPRESCAL
---------------------------------------------------------------------------*/
_asm
{
MOV a,@(WDT_PRESCAL|WDT_1_32|TCC_SRC_INT)//16.34msintrupttime
contw
}
/*
IOWMACROARG1,ARG2
IORREG1,REG2IORREG2->REG1
IOWREG1,REG2IOWREG2->REG1
*/
P5CR=0X04;//port5I/O
P6CR=0X07;//port6I/O
PDCR=0XFF;//PULLDOWN0:ENABLE,1:DISABLE
ODCR=0X00;//OPENDRAIN0:DISABLE,1:ENABLE
PHCR=0XF8;//PULLHIGH0:ENABLE,1:DISABLE
PORT5=0x0c;
PORT6=0x08;
/*---------------------------------------------------------------------------
WDT_SETUP
argument:WDT_ENABLE/WDT_DISABLEenableconrtol
ROC_ENABLE/ROC_DISABLEROPTION
EIS_IO/EIS_INTP60ACTASIOORINTPIN
---------------------------------------------------------------------------*/
WDTCR=WDT_ENABLE|ROC_DISABLE|EIS_IO;
/*---------------------------------------------------------------------------
IMR1_SETUP
argument:IMR_TCC/IMR_EXT
---------------------------------------------------------------------------*/
IMR=IMR_TCC;
}