Professional Documents
Culture Documents
Modul Thu Phát RF
Modul Thu Phát RF
1 chip PIC 16F84 lm nhim v x l tn hiu thu t mch thu RF: gii
m tn hiu ri truyn thng tin ni tip sang mch trung tm AVR.
CHNG I: GII THIU
1. Vi iu khin 16F84A:
S chn:
c tnh ni bt ca b vi x l:
+ Tc hot ng ln ti 20 MHz.
+ 68 byes Ram
+ 64 byes EEPROM
Kh nng ca b vi x l:
+ C th hot ng vi thch anh ni ln ti 8 MHz.
+ Truy nhp b nh trc tip hoc gin tip.
+ C 4 ngun ngt:
Ngt ngoi chn RB0/INT
Trn timer TMRO
PORTB <7:4> ngt bin i
a. Modul pht:
Modul pht truyn d liu s cha c thnh phn m ha c
thit k vi 4 chn vo ra chnh l:
+ Chn 1:VCC cp ngun 1 chiu 5V.
+ Chn 2: Data in : chn ny a cc tn hiu 0, 1 vo y (mc
tn hiu chun ca TTL l 0 : 0 vn v 1 : 5 vn ).
Khi a tn hiu 0 vo chn data in th mch pht vn khng c g
xy ra, khi a tn hiu 1 vo chn data in th mch pht c kch hot v
pht sng in t.
C da xung lien tc 0, 1 vo chn data in khi mch s
ngng, pht, ngng, pht
Hnh di y m t vic sng in t c pht thnh chm
hnh sin ngt qung khi u data in c cc tn hiu 0 v 1
+ Chn 3: GND ni t.
+ Chn 4: ANT ni vi anten pht (Ngoi tr nhng module pht
c ch to c bit c anten t sn trn mch) th phi ni anten ngoi
vo m ny. Mt module pht RF phi c anten th mi c th pht sng
ra xa ngoi khng gian.
b. Modul thu
Modul thu cu to gm cc chn:
+ Chn 1:VCC ngun nui 5V.
+ Chn 2: Data d liu thu a ra.
+ Chn 3: GND ni t.
+ Chn 4: GND ni t.
+ Chn 5: ANT ni vi anten thu.
c im ca module thu: bnh thng khi cp ngun cho module thu th u ra data out
c mc 0, 1 ngu nhin khng xc nh. Mc 0 hoc 1 ny do yu t mi trng tc
ng. Bi th khi lp trnh gii m cho cc b thu th ta khng s dng ngt nhn
u vo m ta s dung phng php hi vng, loi tr. Khi dung ngt nhn tn hiu th
b nhy vo ngt lin tc v kt qu chng nhn c ci g.
y cng l im khc bit gia module thu RF v module thu IR (hng ngoi). tn hiu
t module thu IR l rt sch v th c th dng ngt nhn tn hiu t module IR cn RF
th khng
c im ca modul thu_pht:
+Tn s hot ng l: 315 MHz
+ Hot ng mc TTL nn c th kt ni trc tip vi cng vo/ra ca
vi iu khin.
+Cng sut tiu th thp
+ Khong cch truyn nhn ti a: 300m(khi c anten)
+ Tc truyn d liu c th t t 300bps ti 100kbps.
+ Di nhit hot ng: -35o ti 80o.
Cc setup point ng b.
V d m ha chui 00110111011 c kt qu: 1010010110010101100101
v cc
Y 1
R 4
R
J1
R 5
R
sw 9
C 2
33
R 3
R
sw 10
C R Y S TA L
gnd
R 2
R
sw 12
C 1
33
osc2
sw 11
osc1
U 1
2
1
C O N 2
s
d
s
s
s
R 1
R
w9
a ta
w10
w11
w12
17
18
1
2
3
osc1
osc2
16
15
R
R
R
R
R
R B 0 /IN
R B
R B
R B
R B
R B
O S C 1 /C L K IN
R B
O S C 2 /C L K O U T
R B
4
v cc
A
A
A
A
A
0
1
2
3
4 /TO C K I
T
1
2
3
4
5
6
7
6
7
8
9
10
11
12
13
s
s
s
s
s
s
s
s
w
w
w
w
w
w
w
w
1
2
3
4
5
6
7
8
MC LR
14
VD D
P IC 1 6 F 8 4
E1
A N TE N N A
J2
C 3
104
d a ta
1
2
3
4
C O N 4
J3
s
s
s
s
s
s
s
s
s
s
s
s
w
w
w
w
w
w
w
w
w
w
w
w
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
C O N 12
b. Mch thu RF
S dng thu li cc tn hiu t mch pht, bin cc trng thi pht hoc khng pht
thnh dng s 1 hoc 0.
Nguyn tc khi mch thu rnh khng nhn d liu t mch pht th mch vn c th thu
cc tn hiu nhiu t mi trng lm cho u ra ca n c nhng tn hiu 1, 0 khng xc
nh. Hoc trong qu trnh pht c mt chui di bit 1 hoc 0 lin tc.
loi b nhiu t mi trng pha mch pht cn pht mt chui tn hiu trong mt
khong thi gian(ty vo mch thu, khong 25ms), trc khi chnh thc pht d liu,
iu ny s m bo mch thu thu ng d liu t pha mch pht. y l bc cn thit
chnh li li(gain) cho b thu trc khi hot ng.
nhy ca mch thu ph thuc rt nhiu vo ngten
R 2
100k
4
17
18
1
2
3
15
d a ta
SW 1
S W T A C T -S P S T /S M
C 1
16
33p
5
Y 1
C 2
M
R
R
R
R
R
O
J4
C
A
A
A
A
A
S
LR
R B 0 /IN T
0
R B1
1
R B2
2
R B3
3
R B4
4 /T0 C K I
R B5
C 2 /C L K O
R B6
R B7
6
7
8
9
10
11
12
13
1
2
3
4
C O N 4
O S C 1 /C L K I
Vss
Vdd
VC C
14
P IC 1 6 F 8 4 A
C R Y S TA L
33p
R 1
100R
J1
2
1
C 3
D 12
VC C
J2
104
C O N 5
1
2
3
4
5
E1
A N TE N N A
d a ta
#use delay(clock=4M)
// dinh nghia chan data
#define TX PIN_A1
C O N 2
LED
VC C
char buff[3],i;
INT b1=0;
INT b2=0;
INT b3=0;
INT b4=0;
INT b5=0;
INT b6=0;
INT b7=0;
INT b8=0;
INT b9=0;
INT b10=0;
INT b11=0;
INT b12=0;
void send_data(char*buff)
{
char i,j;
delay_cycles(1);//same a NOP
//---------------chuoi init------------------for(i=1;i<=35;i++)
{
output_high(TX);
delay_us(416);
output_low(TX);
delay_us(416);
}
//--------------dong bo-----------------delay_us(416);
delay_us(416);
delay_us(416);
//---------------start bit--------------output_high(TX);
delay_us(416);
output_low(TX);
delay_us(416);
//--------------send data-------------for(j=0;j<3;j++)
{
for(i=0;i<8;i++)
{
if((buff[j] & 0x80)==0x80)
{
output_high(TX);
delay_us(416);
output_low(TX);
delay_us(416);
}
else
{
output_low(TX);
delay_us(416);
output_high(TX);
delay_us(416);
}
buff[j]=buff[j]<<1;
}
}
//--------------stop bit-------------------output_high(TX);
delay_us(416);
output_low(TX);
delay_us(416);
}
//----------------------------
send_data(buff);
}
while(!BUTTON1);
delay_us(20);
}
}
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON3);
delay_us(20);
}
}
void read_button4(char cmd)
{
if(BUTTON4==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON4);
delay_us(20);
}
}
void read_button5(char cmd)
{
if(BUTTON5==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON5);
delay_us(20);
}
}
void read_button6(char cmd)
{
if(BUTTON6==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON6);
delay_us(20);
}
}
void read_button7(char cmd)
{
if(BUTTON7==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON7);
delay_us(20);
}
}
void read_button8(char cmd)
{
if(BUTTON8==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON8);
delay_us(20);
}
}
void read_button9(char cmd)
{
if(BUTTON9==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON9);
delay_us(20);
}
}
void read_button10(char cmd)
{
if(BUTTON10==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON10);
delay_us(20);
}
}
void read_button11(char cmd)
{
if(BUTTON11==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON11);
delay_us(20);
}
}
void read_button12(char cmd)
{
if(BUTTON12==0)
{
do
{
buff[0]=adr;
buff[1]=cmd;
buff[2]=buff[0]^buff[1];
send_data(buff);
}
while(!BUTTON12);
delay_us(20);
}
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
delay_us(5);
set_tris_b(0xff);
set_tris_a(0xfd);
// PIN_A1(TX) la dau ra
output_low(TX);
port_b_pullups(true);
while(TRUE)
{
IF(BUTTON1==0)
{
if(b1==0)
{read_button1(0x01);b1=1;}
else
{read_button1(0x11);b1=0;}
}
IF(BUTTON2==0)
if(b2==0)
{read_button2(0x02);b2=1;}
else
{read_button2(0x12);b2=0;}
}
IF(BUTTON3==0)
{
if(b3==0)
{read_button3(0x03);b3=1;}
else
{read_button3(0x13);b3=0;}
}
IF(BUTTON4==0)
{
if(b4==0)
{read_button4(0x04);b4=1;}
else
{read_button4(0x14);b4=0;}
}
IF(BUTTON5==0)
{
if(b5==0)
{read_button5(0x05);b5=1;}
else
{read_button5(0x15);b5=0;}
}
IF(BUTTON6==0)
{
if(b6==0)
{read_button6(0x06);b6=1;}
else
{read_button6(0x16);b6=0;}
}
IF(BUTTON7==0)
{
if(b7==0)
{read_button7(0x07);b7=1;}
else
{read_button7(0x17);b7=0;}
}
IF(BUTTON8==0)
{
if(b8==0)
{read_button8(0x08);b8=1;}
else
{read_button8(0x18);b8=0;}
}
IF(BUTTON9==0)
{
if(b9==0)
{read_button9(0x09);b9=1;}
else
{read_button9(0x19);b9=0;}
}
IF(BUTTON10==0)
{
if(b10==0)
{read_button10(0x10);b10=1;}
else
{read_button10(0x20);b10=0;}
}
IF(BUTTON11==0)
{
if(b11==0)
{read_button11(0x21);b11=1;}
else
{read_button11(0x22);b11=0;}
}
IF(BUTTON12==0)
{
if(b12==0)
{read_button12(0x23);b12=1;}
else
{read_button12(0x24);b12=0;}
}
}
Code thu
#use delay(clock=4M)
#define RX input(PIN_A0)
#define adr 0x02
char data[3];
short rx_bit;
short get_data(char*buff);
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
while(get_data(data));
if((data[2])==(data[0]^data[1]))
{
if(data[0]==adr)
{
//if(data[1]==0x00)
{output_b(0x00);output_low(PIN_A2);output_low(PIN_A3);}
if(data[1]==0x01){output_high(PIN_B0);}
else if(data[1]==0x02){output_high(PIN_B1);}
else if(data[1]==0x03){output_high(PIN_B2);}
else if(data[1]==0x04){output_high(PIN_B3);}
else if(data[1]==0x05){output_high(PIN_B4);}
else if(data[1]==0x06){output_high(PIN_B5);}
else if(data[1]==0x07){output_high(PIN_B6);}
else if(data[1]==0x08){output_high(PIN_B7);}
else if(data[1]==0x09){output_high(PIN_A2);}
else if(data[1]==0x10){output_high(PIN_A3);}
else if(data[1]==0x11){output_low(PIN_B0);}
else if(data[1]==0x12){output_low(PIN_B1);}
else if(data[1]==0x13){output_low(PIN_B2);}
else if(data[1]==0x14){output_low(PIN_B3);}
else if(data[1]==0x15){output_low(PIN_B4);}
else if(data[1]==0x16){output_low(PIN_B5);}
else if(data[1]==0x17){output_low(PIN_B6);}
else if(data[1]==0x18){output_low(PIN_B7);}
else if(data[1]==0x19){output_low(PIN_A2);}
else if(data[1]==0x20){output_low(PIN_A3);}
else if(data[1]==0x21){output_b(0xff);output_a(0xff);}
else if(data[1]==0x22){output_b(0x00);output_a(0x00);}
else if(data[1]==0x23){output_b(0xff);output_a(0x00);}
else if(data[1]==0x24){output_b(0x00);output_a(0xff);}
}
}
short get_data(char*buff)
{
char i,j,time,count;
i=35;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
// chuoi 35 xung INit
while(--i)
{
delay_cycles(1);
delay_cycles(1);
delay_cycles(1);
while(RX);
set_rtcc(0);
delay_cycles(3);
while(!RX);
time=get_rtcc();
if(time==255)return 1;
if((time>198)&&(time<218))
{
count++;
if((count>1)||(i==0))break;
}
}
if(i==0)return 1;
// bit start
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
time=0;
while(!((time>=94)&&(time<=114)))
{
delay_cycles(3);
while(RX);
set_rtcc(0);
delay_cycles(4);
while(!RX);
time=get_rtcc();
}
while(RX);
delay_us(624);
for(j=0;j<3;j++)
{
i=8;
buff[j]=0;
while(i--)
{
buff[j]=buff[j]<<1;
rx_bit=RX;
buff[j]=buff[j]|rx_bit;
if(rx_bit==0)
while(!RX);
else
while(RX);
delay_us(624);
}
}
if(rx_bit==0)
if(!RX)return 1 ;
else
if(!RX)return 1;
return 0;
}