Giải Mã Remote 21 nút.
Code:
#include <main.h>
#define DATA RC3
unsigned int16 vl;
signed int8 a=0;
unsigned int8 value,i,j;
//_______Giao Tiep Remote 21 phim_______//
int8 check_start()
{
if(DATA==1) return 0;
else
set_timer1(0);
while(DATA==0);//cho cho het bit1
vl=get_timer1();
if(vl<8500 || vl>9500) return 0;//xung thap 9ms
else
set_timer1(0);
while(DATA==1);//cho cho het bit0
vl=get_timer1();
if(vl<4200 || vl>4800) return 0; //xung cao 4,5ms
else
return 1;
}
int8 check_address()
{
if(DATA==1) return 0;
else
for(i=0;i<8;i++) //kiem tra 8bit 0: 562us 1,562us 0
{
set_timer1(0);
while(DATA==0);//trong khi tin hieu =0
vl=get_timer1();
if(vl<430||vl>630)return 0; //xung xuong khoang 530us
else
set_timer1(0);
while(DATA==1);//trong khi tin hieu =1
vl=get_timer1();
if(vl<430||vl>630)return 0;//xung len khoang 530us
}
for(i=0;i<8;i++) //kiem tra 8bit 1: 562us 1,562us 0
{
set_timer1(0);
while(DATA==0);//trong khi tin hieu =0
vl=get_timer1();
if(vl<430||vl>630)return 0; //xung xuong khoang 530us
else
set_timer1(0);
while(DATA==1);//trong khi tin hieu =1
vl=get_timer1();
if(vl<1500||vl>1750)return 0;//xung len khoang 1650us
}
return 1;
}
void check_byte() //nhan tu bitcao truoc
{
unsigned int8 buff;
value=0x00;
for(i=0;i<8;i++)
{
buff=value;
while(DATA==0);//trong khi dang bit thap
set_timer1(0);//dem thoi gian
while(DATA==1);//trong khi dang bit cao
vl=get_timer1();
if(vl<1000) value=(buff>>1);
else value=(buff>>1)+0x80;
}
}
#int_EXT
void ngat_ngoai()
{
IF(check_start()==1)
{
if(check_address()==1)
check_byte();
}
}
//_______Ket thuc giao tiep Remote 21 phim_______//
void main()
{
TRISC7=0;
TRISC3=1;
enable_interrupts(GLOBAL);// kich hoat ngat toan cuc
enable_interrupts(INT_EXT);//kich hoat ngat ngoai
ext_int_edge(H_TO_L);// chon ngat canh xuong
setup_timer_1 ( T1_INTERNAL);
set_timer1(0);
while(1){
if(value==0x07) {RC7=1;value=0;}
if(value==0x15) {RC7=0;value=0;}
}
}
#define DATA RC3
unsigned int16 vl;
signed int8 a=0;
unsigned int8 value,i,j;
//_______Giao Tiep Remote 21 phim_______//
int8 check_start()
{
if(DATA==1) return 0;
else
set_timer1(0);
while(DATA==0);//cho cho het bit1
vl=get_timer1();
if(vl<8500 || vl>9500) return 0;//xung thap 9ms
else
set_timer1(0);
while(DATA==1);//cho cho het bit0
vl=get_timer1();
if(vl<4200 || vl>4800) return 0; //xung cao 4,5ms
else
return 1;
}
int8 check_address()
{
if(DATA==1) return 0;
else
for(i=0;i<8;i++) //kiem tra 8bit 0: 562us 1,562us 0
{
set_timer1(0);
while(DATA==0);//trong khi tin hieu =0
vl=get_timer1();
if(vl<430||vl>630)return 0; //xung xuong khoang 530us
else
set_timer1(0);
while(DATA==1);//trong khi tin hieu =1
vl=get_timer1();
if(vl<430||vl>630)return 0;//xung len khoang 530us
}
for(i=0;i<8;i++) //kiem tra 8bit 1: 562us 1,562us 0
{
set_timer1(0);
while(DATA==0);//trong khi tin hieu =0
vl=get_timer1();
if(vl<430||vl>630)return 0; //xung xuong khoang 530us
else
set_timer1(0);
while(DATA==1);//trong khi tin hieu =1
vl=get_timer1();
if(vl<1500||vl>1750)return 0;//xung len khoang 1650us
}
return 1;
}
void check_byte() //nhan tu bitcao truoc
{
unsigned int8 buff;
value=0x00;
for(i=0;i<8;i++)
{
buff=value;
while(DATA==0);//trong khi dang bit thap
set_timer1(0);//dem thoi gian
while(DATA==1);//trong khi dang bit cao
vl=get_timer1();
if(vl<1000) value=(buff>>1);
else value=(buff>>1)+0x80;
}
}
#int_EXT
void ngat_ngoai()
{
IF(check_start()==1)
{
if(check_address()==1)
check_byte();
}
}
//_______Ket thuc giao tiep Remote 21 phim_______//
void main()
{
TRISC7=0;
TRISC3=1;
enable_interrupts(GLOBAL);// kich hoat ngat toan cuc
enable_interrupts(INT_EXT);//kich hoat ngat ngoai
ext_int_edge(H_TO_L);// chon ngat canh xuong
setup_timer_1 ( T1_INTERNAL);
set_timer1(0);
while(1){
if(value==0x07) {RC7=1;value=0;}
if(value==0x15) {RC7=0;value=0;}
}
}
Không có nhận xét nào:
Đăng nhận xét