Está en la página 1de 86

MC LC

CHNG 1 ...................................................................................................................4
GII THIU CHUNG..................................................................................................4
1.1 GII THIU CHUNG V VI IU KHIN ........................................................ 4
1.1.1 Cc dng vi iu khin hin nay ..................................................................... 4
1.2 H vi iu khin 8051 ........................................................................................ 5
1.3 Gii thiu board mch Easy8051 .......................................................................... 6
1.3.1 Nhng tnh nng chnh ca Easy8051 ............................................................ 6
1.3.2 Ti nguyn phn cng .................................................................................... 7
CHNG 2: THC HNH ........................................................................................8
BI THC HNH S 1: CHC NNG IU KHIN CNG I/O LED N .....8
1. Mc ch ................................................................................................................. 8
2. Yu cu ................................................................................................................... 8
3. S nguyn l ...................................................................................................... 8
4. La chn thit b ....................................................................................................11
5. Trnh t thc hin ..................................................................................................12
6. Lu : ....................................................................................................................13
BI THC HNH S 2: LED 7 THANH ................................................................14
1.Mc ch .................................................................................................................14
2.Yu cu ...................................................................................................................14
3. S nguyn l .....................................................................................................14
Nguyn l hot ng: .................................................................................................15
4.La chn thit b .....................................................................................................17
5.Trnh t thc hin ...................................................................................................18
6.Lu ......................................................................................................................19
BI THC HNH S 3: CHC NNG NGT TIMER NG H S ..........20
1.Mc ch .................................................................................................................20
2.Yu cu ...................................................................................................................20
3.S nguyn l ......................................................................................................20
1

4. La chn thit b ....................................................................................................32


5.Trnh t thc hin ...................................................................................................32
Lu .........................................................................................................................33
BI THC HNH S 4: CHC NNG NGT NGOI B O TN S ........34
1.Mc ch .................................................................................................................34
2.Yu cu ...................................................................................................................34
3.S nguyn l ......................................................................................................34
4.La chn thit b .....................................................................................................37
5.Trnh t thc hin ...................................................................................................38
6.Lu ......................................................................................................................38
BI THC HNH S 5: CHC NAWG TRUYN THNG NI TIP ..............39
1.Mc ch .................................................................................................................39
2.Yu cu ...................................................................................................................39
3.S nguyn l ......................................................................................................39
4.La chn thit b .....................................................................................................41
5.Trnh t thc hin ...................................................................................................42
6.Lu ......................................................................................................................43
BI THC HNH S 6: LED MA TRN ...............................................................44
1.Mc ch .................................................................................................................44
2.Yu cu ...................................................................................................................44
3.S nguyn l ......................................................................................................44
4.La chn thit bi. ....................................................................................................44
5.Trnh t thc hin ...................................................................................................45
BI THC HNH S 7: O NHIT HIN TH LCD ....................................46
1.Mc ch .................................................................................................................46
2.Yu cu ...................................................................................................................46
3.S nguyn l ......................................................................................................46
4.La chn thit bi. ....................................................................................................52
Trnh t thc hin ......................................................................................................53
BI THC HNH S 8: IU KHIN TC NG C MT CHIU .......54
2

1.Mc ch .................................................................................................................54
2.Yu cu ...................................................................................................................54
3.S nguyn l ......................................................................................................54
4.La chn thit b .....................................................................................................59
5.Trnh t thc hin ...................................................................................................59
BI THC HNH S 9: IU KHIN T XA CC THIT B IN ...............60
1.Mc ch .................................................................................................................60
2.Yu cu ...................................................................................................................60
3.S nguyn l ......................................................................................................60
BI THI: MCH KHA S .....................................................................................64
CHNG 1: PHN TCH H THNG................................................................64
1.Phn tch v la chn phng n ............................................................................64
a) Yu cu ca h thng: .........................................................................................64
b) La chn phng n ...........................................................................................64
2.Xc nh bi ton v gii hn ti ........................................................................64
a) Xc nh bi ton ................................................................................................64
b) Gii hn ca ti ..............................................................................................65
CHNG 2 :THIT K H THNG......................................................................66
1. S khi tng th ca h thng ...........................................................................66
2.Cc khi trong h thng ..........................................................................................66
2.1

Khi iu khin trung tm .............................................................................67

2.2 Khi hin th .....................................................................................................67


2.3

Khi bn phm ...............................................................................................68

2.4. Khi ng c v bo ng ...............................................................................68


2.5. Khi ngun ....................................................................................................68
CHNG 3: H THNG KHA S ......................................................................69
1.S nguyn l ......................................................................................................69
2. Nguyn l hot ng ..............................................................................................69
3.Code chng trnh ..................................................................................................70
3

CHNG 1
GII THIU CHUNG
1.1 GII THIU CHUNG V VI IU KHIN
1.1.1 Cc dng vi iu khin hin nay
Vi iu khin, thc cht, l mt h thng bao gm mt vi x l c hiu sut
dng v gi thnh thp (khc vi cc b vi x l a nng dng trong my tnh) kt hp
vi cc khi ngoi vi nh b nh, cc m un vo/ra, cc m un bin i s sang
tng t v tng t sang s,...
Hin nay, cc b vi iu khin 8 bt ng u l h 8051 c s lng ln cc nh cung
cp a dng: Intel, Atmel, Philips/signe-tics, AMD, Siemens, Matra v Dallas,
semicndictior.
a)Vi iu khin ca Atmel
Atmel Atmel l mt hng cung cp vi iu khin ln, sn phm vi iu khin ca
gm:
- Dng vi iu khin da trn kin trc 8051 ca Intel nh 83xx, 87xx, 89xx...
- Dng vi iu khin AT91CAP nh AT91CAP7S250A,AT91CAP7S450A.... vi tn
s hot ng t 80 n 200 MHz, 2 n 4 knh PWM, 10 knh ADC 10 bt, ghp
ni c vi cc module SDRAM bn ngoi.
- Dng vi iu khin AT91SAM 32-bit ARM- based b nh chng trnh c th ti
2Mb, tn s hot ng n 240 MHz.
- Dng AVR 8-bit kin trc RISC nh AT90PWM1, Atmega 128, Atmega 16,
Atmega 32.
- Dng AVR 32 32- bit MCU/DSP nh AVR32 UC3A, AVR 32 UC3B ..... l nhng
b vi iu khin 32 bit c thm cc lnh x l tn hiu s x l m thanh, hnh
nh.
- Dng FPSLIC nh: AT94K05L, AT94K10L , ATFS40....y l s kt hp vi iu
khin AVR vi mng cng Logic lp trnh FPGA trn 1 chip rt ph hp to ra
cc h thng s trn 1 chip duy nht.
- Dng vi iu khin 4bit cho cc ng dng n gin MARC4 nh: ATAM510,
ATAR940.....
b)Vi iu khin ca Microchip
- Dng 8bit nh: PIC10, PIC12, PIC14, PIC16, PIC18, vi b nh kiu flash, OTP,
ROM hoc ROMless dung lng t 0,5 n 256Kb.
4

- Dng 16bit nh PIC24F, PIC24H.


- Dng x l tn hiu s 16bit nh: dsPIC30Fxxxx, dsPIC33FJxxxx.
c)Vi iu khin ca Cypress
Cypress ni ting vi dng sn phm PsoC, y l nhng vi mch tch hp vi iu
khin, cc linh kin tng t ( cc b khuch i, cc b bin i A/D, D/A, cc b
lc, cc b so snh...) v cc linh kin s( b nh thi, b m, b to xung
PWM,SPI,UART,I2C...) trn mt chip duy nht. Vic tch hp hng trm khi chc
nng cng vi b vi iu khin trn mt chip cho php: gim thi gian thit k, thu
gn kch thc sn phm, gim cng sut tiu th v gim gi thnh sn phm.
d)Vi iu khin ca Hitachi
- H8 l dng vi iu khin c pht trin bi HITACHI, c sn xut bi Rcnesas
Technology. H8 gm cc dng sn phm: H8/300, H8/300H, H8/500, H8S( vi iu
khin 32 bit kiu CISC). Cc vi iu khin h H8 c s dng rng ri trong cc
sn phm dn dng v cng nghip nh: Tivi, u DVD, camera, PLC,bin tn....
e)Vi iu khin ca Motorola .
Motorola sn xut dng vi iu khin 68xx nh: 6801, 6805,6809,6811.....mt sn
phm tiu biu ca Motorolo l 68HC11, y l mt b vi iu khin 8bit; 16bit a
ch; tp lnh tng thch vi cc phin bn trc nh: 6801, 6805, 6809; c tich hp
b bin i A/D, b to xung PWM, cng truyn thng ng b - khng ng b
RS232, SPI.
f)Vi iu khin ca Maxim
Cc sn phm vi iu khin do Maxim cung cp gm:
- Vi iu khin MAXQ 16bit kin trc RISC nh MAXQ3212, MAXQ2000.
- Cc sn phm da trn kin trc 8051 ca Intel nh vi iu khin tch hp ng h
thi gian thc DS87C530, vi iu khin tch hp bin i A/D 10bit DS80CH11, vi
iu khin tch hp giao tip mng Ethernet DS80C400, DS80C430 ( rt ph hp
thit ki IP camera, cc trm o/iu khin phn tn AM nh DS5250,
DS2250,DS2252...)
1.2 H vi iu khin 8051
1.2.1 H vi iu khin 8051
C hai b vi iu khin thnh vin khc ca h 8051 l 8052 v 8031.

Bng: So snh cc c tnh ca cc thnh vin h 8051.


c tnh
ROM trn chip
RAM
B nh thi
Cng ni tip
Ngun ngt

8051
4K byte
128 byte
2
1
6

8052
8K byte
256 byte
3
1
8

Do vy tt c chng trnh vit cho 8051 u chy c trn 8052 nhng iu ngc li
l khng ng.
1.3 Gii thiu board mch Easy8051
Easy8051 l kit pht trin hon chnh v k thut, y tnh nng v d s dng
cho ATMEL 89xxx Microcontroller.
Easy8051 c thit k mt Header ISP, cho php ngi dng c th s dng cc
cng c lp trnh v g li khc nh ICE52 Emulator, SP200S Enhanced,
AT89SXXtheo chun ISP, v vy khng cn tho chip. Bn ch cn vit chng trnh
--> bin dch --> np v kt qu s chy ngay, khng cn phi bn tm g khc.
Easy8051 c ti u thit k theo kiu modules, Cc module c kt ni hoc
ngt khi MCU mt cch linh hot bng Jumper hoc DipSW rt tin li v gn gng.
1.3.1 Nhng tnh nng chnh ca Easy8051
- Power Supply: S dng ngun ngoi AC/DC 7 ~ 12V c cng tc ngun ON/OFF
- C ng ISP programmer sn sng s dng cho cc mch np v g li ISP (V d:
ICE52 Emulator, SP200S Enhanced, AT89SXX ISP programmer)
- Suport 89xxx DIP40 (c th dng cho DIP20 bng cch s dng Adapter chuyn chn)
- RS-232 Comunication giao tip truyn d liu vi PC
- DS1820 Digital thermometer dng o nhit t -55C to 125C.
- RTC DS1307 Real time clock kt ni hoc ngt khi 8051 qua DIPSW
- Battery Backup 3V dng cho RTC DS1307
- 6 Led 7seg Anode chung multiplex mode. kt ni hoc ngt khi 8051 qua DIPSW
- LCD 16x2 4 BIT c bin tr chnh tng phn
6

- Graphic LCD 128x64 / Charracter LCD16x2 8-bit c bin tr chnh tng phn
- 8 led n anod chung ni qua header 5x2
- 8 Switch pull up/down ni qua header 5x2
- Xtal s dng socket d dng thay i
- I/O Direct port kiu header 5x2 (gm 8 bit port v VCC, GND) Pull Up/Down
- RESET Switch - loi ln cho php d dng Reset mch bng tay
- SPI ADC s dng MCP3204 (La chn thm)
- SPI DAC s dng MCP4921 (La chn thm)
- in p tham chiu 4.096V REF s dng MCP1541 (La chn thm)
- Mch in xuyn lp cht lng cao kch thc 140x200 mm
- C Mica bo v bn di khi s ngn mch
1.3.2 Ti nguyn phn cng
Danh sch ti nguyn phn cng

S
1
2
3
4
5
6
7
8

S
9
10
11
12
13
14
15
16

M t
Power module
ISP programmer
RS232 conmunication module
SPI ADC
ISP DAC
6-digital display module
LCD1602 module
GLCD12864/ LCD1602 8bit module
7

M t
DS1370 module
8 button module
8 single LED module
DS18B20 thermometer module
Reset button
40P chip ZIF socket
I/O external output
MCP1541 4,096V REF

CHNG 2: THC HNH


BI THC HNH S 1
LP TRNH CHC NNG I/O VI LED N
1. Mc ch
- Thc hnh vi cc cng c phn mm: phn mm lp trnh KeilC, phn mm m
phng Proteus, phn mm np chng trnh cho vi iu khin ISP_Prog.
- Lp trnh chc nng vo/ra ca 8051
- iu khin LED n
- Vit cc chng trnh con to thi gian tr
2. Yu cu
- Tnh ton, la chn c cc thng s ca linh kin, gi tr in tr, dng, p cp
cho led.
- Lp trnh iu khin c led sng, tt theo yu cu.
- Nm bt c chc nng cc phn t trong s mch.
- Bit cch s dng cc phn mm lp trnh, phn mm m phng, phn mm np
chng trnh cho vi iu khin.
- Lp trnh c cc bi ton yu cu, hiu r tng cu lnh, tng hm, chng
trnh con v quy tc, cu trc cu lnh cng nh chc nng tc dng ca n.
3. S nguyn l

Hnh 1 S nguyn l mch iu khin LED n


Nguyn l hot ng: Theo nh s nguyn l trn, led sng ta cn cp mc
1, led tt ta cp mc 0.
8

V d :
o 8 LED chy sng dn t tri sang phi
o 8 LED chy sng dn t phi sang tri
o 8 LED chy sng t gia ra hai bn
o 8 LED chy sng hai bn vo gia
Begin

Lp v hn

Chy xui

P1=0xfe;
so1[5]={0xff,0xe7,0xc3,0x81,0x00};
so2[5]={0xff,0x7e,0x3c,0x18,0x00};

n=0;k<8

i=0;i<5;

P1= so1(i);

P1=P1<<1;

Tre(5000);

Tre(5000);

Chy ngc

Chy gia ra

Tre(3000);

Tre(3000);

k=0;k<8;

j=0;j<5;
Chy ngoi vo.

P1=P1>>1;

P1=so2(j);

Tre(5000);

Tre(5000);

Tre(3000);

Tre(3000);

Hnh 2 Lu thut ton iu khin led nhy 8 led


9

Chng trnh iu khin 8 led n.


#include <REGX51.H>
unsigned char so1[5]={0xff,0xe7,0xc3,0x81,0x00};
unsigned char so2[5]={0xff,0x7e,0x3c,0x18,0x00};
void tre(long t)
{while(t--) ; }
void chayxuoi()
{ unsigned char n; // Khai bao them bien n cho vong for
P1=0xfe;
for(n=0;n<8;n++)// Lap 8 lan
{

P1=P1<<1; // Dich bit sang trai


tre(5000); } }

void chaygiua()
{ unsigned char i;
for(i=0;i<5;i++)
{ P1=so1[i];
tre(5000); } }
void chayvao()
{ unsigned char j;
for(j=0;j<5;j++)
{ P1=so2[j];
tre(3000); } }
void chaynguoc()
{ unsigned char k;
P1=0xfe;
10

for(k=0;k<8;k++)
{ P1=P1>>1;
tre(5000); }}
void main()
{ while(1)// Lap vo han
{

chayxuoi();
tre(3000) ;
chaynguoc();
tre(3000);
chaygiua();
tre(3000) ;
chayvao();
tre(3000); } }

4. La chn thit b
- My vi tnh ci cc phn mm KeilC, Proteus
- Kit thc hnh 8051:
o Module LED : Gm 8 led n:
8 led n c ni trc tip vi vi iu khin
8 led n c kt ni vi Header 5x2, k hiu CN7 (LED)

Hnh 3 V tr khi 8 Led n trn board mch


Cng iu khin : PORT-1
- Port1 c kt ni vi Header CN2 (PORT-1)
11

Hnh 4 V tr Port 1 trn board mch


5. Trnh t thc hin
- Bc 1: Dng jump cm 2 hng 5x2 kt ni PORT-1 vo LED
- Bc 2: V mch m phng trn Proteus

Hnh 5 S m phng proteus mch iu khin 8 led n


- Bc 3: M chng trnh mu
../ThucHanh8051/Bai1_LedDon/LedDon.uvproj
- Bc 4: Bin dch chng trnh mu, m phng trn Proteus v np chng trnh
vo kit thc hnh Easy8051 chc chn cc khi mch cn dng hot ng tt.
- Bc 5: Vit chng trnh, chy m phng v chy trn kit thc hnh 8051 theo
yu cu sau :
o 8 LED chy sng dn t tri sang phi
o 8 LED chy sng dn t phi sang tri
o 8 LED chy sng t gia ra hai bn
o 8 LED chy sng hai bn vo gia
12

6. Lu :
Cch mc LED
LED (Light-emitting diodes) cn gi l Diode pht quang, c kh nng chuyn
in thnh nh sng. Khi pht sng s gy ra st p trn n vo khong 1.7 2.5V (tu
mu LED), v dng nh mc ln nht qua n khong 20mA (i vi led indoor thng
thng). iu khin Led ta c mt s cch mc sau

Hnh 6 Cc cch mc iu khin led vi vi iu khin 8051


Mch mc theo cch th 2 (Fair) c st p trn LED ln khong 2V, 3V cn li
s st p trn mch TTL, gy tn tht nng lng ln, hoc LED s chy nu dng qua
n ln(khong vi chc mA).
Mch mc theo cch th nht l tt nht, c tr hn dng. Ty tng loi LED m
in p ri trn n khc nhau. Thng in p ri trn LED trong khong 1,7V 3V .
St p trn chn TTL l 0.9V. Do in p ri trn trn tr l (5V- 0,9V 3V = 1,1V)
cho ti (5V 0,9V 1,7V = 2,4V). Dng in qua LED khong 10mA l va sng
(Dng qua led thng nh hn khong 20mA i vi loi LED thng, nh hn khong
40mA vi LED siu sng). Dng qua tr cng bng dng qua LED do mc ni tip. Do
R = Ur/Ir = 1,1V/10mA 2,4V/10mA = 110 240.
Tm li, tr hn dng khng nn di 100Ohm nu khng LED s chng hng.
V khng nn qu 240 led s ti.

13

BI THC HNH S 2
LED 7 THANH
1.Mc ch
- Thc hnh vi cc cng c phn mm: phn mm lp trnh KeilC, phn mm m
phng Proteus, phn mm np chng trnh cho vi iu khin ISP_Prog.
- iu khin LED 7 thanh.
- Vit cc chng trnh con to thi gian tr.
2.Yu cu
- Tnh ton, la chn c cc thng s ca linh kin, gi tr in tr, dng, p cp
cho led.
- Lp trnh iu khin c led sng, tt theo yu cu.
- Nm bt c chc nng cc phn t trong s mch.
- Bit cch s dng cc phn mm lp trnh, phn mm m phng, phn mm np
chng trnh cho vi iu khin.
- Lp trnh c cc bi ton yu cu, hiu r tng cu lnh, tng hm, chng trnh
con v quy tc, cu trc cu lnh cng nh chc nng tc dng ca n.
3. S nguyn l

Hnh 2.1 S nguyn l iu khin led 7 thanh

14

Nguyn l hot ng:


- Khi cm ngun vo mch tt c cc chn ca cc cng IO ca vi iu khin l 5V(
nu cng khng lp in tr treo th s l 0V ). Nhn s mch khng c chnh
in p nn khng c n sng. Chng ta mun sng thanh no ch vic a ra in
p 0V chn vi iu khin ni vi thanh .
- Phn nt bm: Ban u P1.0 mc cao +5V, nu bm 2 u nt bm thng nhau.
Chn P1.0 thng vi GND. Led sng do chnh p.
S

Thanh hin

Thanh tt

Gi tr ( P2)

BC

Cc thanh cn li

1111 1001

ABDEG

Cc thanh cn li

1010 0100

Tt c cc thanh

Khng thanh no

1000 0000

v d: Mt Nt bm c ni vi chn P3.4, hy lp trnh iu khin m s ln n


phm t 0 n 9 v hin th trn Led 7 thanh.

Hnh 2.2: Lu thut ton iu khin led 7 thanh


15

Chng trnh iu khin:


#include<AT89x51.h> // Dinh kem file thu vien
sbit ctac = P1^0;
unsigned char dem=0;
void delay(long time)
{ while(time--); }
void phim_an(void)
{ if(ctac==0) //co phim nhan
{

delay(500); //chong rung phim


while(ctac==0); //Cho nha phim
delay(500); // chong rung phim
dem++; //tang bien dem
if(dem==10) dem=0;

}}

void so lan_an(void)
{

switch(dem)
{

case 0: {P2=0x3f;break;}
case 1: {P2=0x06;break;}
case 2: {P2=0x5b;break;}
case 3: {P2=0x4f;break;}
case 4: {P2=0x66;break;}
case 5: {P2=0x6d;break;}
case 6: {P2=0x7d;break;}
case 7: {P2=0x07;break;}
16

case 8: {P2=0x7f;break;}
case 9: {P2=0x6f;break;} }

void main(void)
{

dataP = 0x81; // ban dau la so 0


while(1)
{ phim_an();
solan_an(); }}

4.La chn thit b


- My vi tnh ci cc phn mm KeilC, Proteus
- Kit thc hnh 8051:
+ Cng iu khin : PORT-1
o Port1 c kt ni vi Header CN2 (PORT-1)

Hnh 2.4 V tr Port 1 trn board mch


+ Mt button:

Hnh 2.5: V tr button T1 trn bo mch

17

+ Mt led 7 thanh

Hnh 2.3 V tr led 7 thanh trn bo mch

Hnh 2.6 S mch ca model led 7 thanh


5.Trnh t thc hin
- Bc 1: Dng jump cm 2 hng 5x2 kt ni PORT-1 vo button
- Bc 2: V mch m phng trn Proteus

18

Hnh 2.4: S m phng proteus mch m s ln n phm


v hin th ra led 7 thanh
- Bc 3: M chng trnh mu
../ThucHanh8051/Bai2_Led7thanh/led7thanh.uvproj
- Bc 4: Bin dch chng trnh mu, m phng trn Proteus v np chng trnh
vo kit thc hnh Easy8051 chc chn cc khi mch cn dng hot ng tt.
- Bc 5: Vit chng trnh, chy m phng v chy trn kit thc hnh 8051 theo
yu cu sau :
+ Hin th t 0 9 tng ng vi s ln bm phm
6.Lu
Khi bm nt theo nguyn l th bm 1 ci l xung 0 lin, nhng do tip im c kh
ca nt bm nn nt bm n s c 1 s xung in ch khng phi bm ci l n xung
0. T 104 s gim nhiu . T 104 cng c th b i khng lp v ta c th kh nhiu
bng phn mm.

19

BI THC HNH S 3
LP TRNH CHC NNG NGT TIMER - NG H S
1.Mc ch
S dng b nh thi vi chc nng ngt timer
Giao tip vi led 7 thanh, phm bm.
2.Yu cu
- Tnh ton, la chn c cc thng s ca linh kin, gi tr in tr, dng, p cp led
7 thanh
- Lp trnh iu khin hin th c ng h s theo yu cu.
- Nm bt c c ngt ca b nh thi, chc nng cc phn t trong s mch.
- Lp trnh c cc bi ton yu cu, hiu r tng cu lnh, tng hm, chng trnh
con v quy tc, cu trc cu lnh cng nh chc nng tc dng ca n.
3.S nguyn l
Nguyn l:
Bin giy : s ; bin pht : m ; bin gi : h
To tr 1 giy, khi ht 1 giy th tng bin s ln 1 n v. Nu s =60 th s= 0 v m
tng ln 1 n v ;nu m= 60 th m=0 v tn h ln 1 n v . nu h =24 th h=0
- Khi mun iu chnh ng h ( cp mc 0 vo chn ca vi iu khin)
+ Nhn phm P2_0 chn ch iu chnh pht - gi - giy
+ Khi mun tng gi hay pht , nhn phm P2_1
+ Khi mun gim gi hay pht , nhn phm P2_2
+ Bm phm P2_0 mt ln na v ch chy bnh thng
Chng trnh iu khin
#include <REGX52.H>
////////////////////
#define SW1 P2_0
20

#define SW2 P2_1


#define SW3 P2_2
///////////////

khai bao bien

int k,m;
int sl,chon=0,gi=0,p=0;
int a,b,c,d,e,f;
unsigned char display[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(int time)
{ int i;
for(i=0;i<time;i++)
{}; }
///////////////////////////////khoi tao ngat
void khoitaongat(void)
{
EA = 0; //cam ngat toan cuc
TMOD = 0x10;//Timer che do 1 , 16 bit
// TH0=0x00; //Gia
//TL0=0x00;
TH1 = 0xfc;
TL1 = 0x66;
ET1 =1;
//ET0=1; //cho phep ngat timer 0
EA =1; //cho phep ngat toan cuc
TR1 =1;
//TR0 =1; //chay timer 0 bat dau dem so chu ky may }
21

///////////////////////////////

hien thi

void hienthi(unsigned int k, unsigned int p, unsigned int gi)


{ a=gi/10; //tram nghin
b=gi 10; //chuc nghin
/////
c=p/10;

// nghin

d=p%10; //tram
///////
e =k/10; //chuc
f=k%10;

//don vi

/////////////////////
if(chon==0)
{ for(sl=0;sl<10;sl++)
{ P0=display[a]; //display chuc gio
P1_0=0;
delay(50);
P1_0=1;
//////////////////////////////
P0=display[b]; //display don vi gio
P1_1=0;
delay(50);
P1_1=1;
////////////////////////
P0=display[c]; //display chuc phut
22

P1_2=0;
delay(50);
P1_2=1;
/////////////////////
P0=display[d];//display don vi phut
P1_3=0;
delay(50);
P1_3=1;
//////////////////////////////
P0=display[e]; //display chuc giay
P1_4=0;
delay(50);
P1_4=1;
////////////////////////////////////
P0=display[f]; //display don vi giay
P1_5=0;
delay(50);
P1_5=1; }
}else
if(chon==1)
{

P0=display[f]; //display don vi giay


P1_5=0;
delay(100);
P1_5=1;
delay(500); }
23

else
if(chon==2)
{ P0=display[e]; //display chuc giay
P1_4=0;
delay(100);
P1_4=1;
delay(500);

else
if(chon==3)
{

P0=display[d];//display don vi phut


P1_3=0;
delay(100);
P1_3=1;
delay(500);

else
if(chon==4)
{

P0=display[c]; //display chuc phut


P1_2=0;
delay(100);
P1_2=1;
delay(500);

else
if(chon==5)
{

P0=display[b]; //display don vi gio


P1_1=0;
24

delay(100);
P1_1=1;
delay(500);

else
if(chon==6)
{ P0=display[a]; //display chuc gio
P1_0=0;
delay(100);
P1_0=1;
delay(500); }
}
////////////////////////////
void timer1ms (void) interrupt 3 //ngat timer 1
{

TR1 =0;
if(chon==0)
{ m=m+1;
if(m==1000)
{
k=k+1;
m=0; }
}
TH1 = 0xfc;
TL1 = 0x66;
TF1 =0;
TR1=1;
25

}
///////////////

chuong trinh chinh

void main(void)
{

khoitaongat();
k=0;
m=0;
while(1)
{ if(SW1==0)
{

do
{

delay(100);
if(SW1==1)
{ chon=chon +1; }
}

while(SW1==0); }
if(chon==7)chon=0;
//////////////////////////////////////////
switch(chon)
{ case 0://chay binh thuong (khong cai dat)
{

if(k==60)
{ k=0;
p=p+1; }

if(p==60)
{

p=0;
gi=gi+1;

if(gi==24) gi=0;
26

hienthi(k,p,gi);
break;

///////////////////////////////////
case 1:

//cai dat don vi cua giay


{ if(SW2==0)

//tang

{ do
{

delay(100);
if(SW2==1)
{

k=k+1;
if(k==10)k=0; }

} while(SW2==0); }
///////////////////////////////
if(SW3==0)//giam
{

do
{ delay(100);
if(SW3==1)
{

k=k-1;
if(k<0)k=0; }

} while(SW3==0);
}hienthi(k,p,gi);
Break;

//////////////////////////////////////////////////////////////////
case 2:
{

//cai dat chuc cua giay

////////////////////////
if(SW2==0)

//tang
27

do
{ delay(100);
if(SW2==1)
{

k=k+10;
if(k>60)
k=k%10;

} while(SW2==0); }
///////////////////////////////
if(SW3==0)//giam
{

do
{

delay(100);
if(SW3==1)
{ if(k>10)
{ k=k-10; }
else
if(k==10)k=k%10;

} while(SW3==0);
}hienthi(k,p,gi);
break; }
///////////////////////////////////////////////////////////////////////
case 3:

//cai dat don vi cua phut

{ ////////////////////////
if(SW2==0)

//tang

{ do
{

delay(100);
28

if(SW2==1)
{

p=p+1;
if(p==10)p=0; }

} while(SW2==0); }
///////////////////////////////
if(SW3==0)//giam
{ do
{

delay(100);
if(SW3==1)
{ p=p-1;
if(p<0)p=0;

} while(SW3==0);
} hienthi(k,p,gi);
break; }
//////////////////////////////////////////////////////////////////
case 4:
{

//cai dat chuc cua phut

////////////////////////
if(SW2==0)

//tang

{ do
{

delay(100);
if(SW2==1)
{

p=p+10;
if(p>=60)
p=p%10; }

29

} while(SW2==0); }
///////////////////////////////
if(SW3==0)//giam
{ do
{

delay(100);
if(SW3==1)
{ if(p>10)
{ p=p-10; }
else
if(p==10)p=p%10; }
} while(SW3==0);
} hienthi(k,p,gi);

break;

///////////////////////////////////////////////////////////////////////
case 5:

//cai dat don vi cua gio

{ ////////////////////////
if(SW2==0)

//tang

{ do
{

delay(100);
if(SW2==1)
{

gi=gi+1;
if(gi==24)gi=0;

} while(SW2==0); }
///////////////////////////////
if(SW3==0)//giam
30

{ do
{

delay(100);
if(SW3==1)
{

gi=gi-1;
if(gi<0)gi=0; }

} while(SW3==0);
} hienthi(k,p,gi);
break;

//////////////////////////////////////////////////////////////////
case 6:
{

//cai dat chuc cua gio

////////////////////////
if(SW2==0)

//tang

{ delay(100);
if(SW2==1)
{

gi=gi+10;
if(gi>24)
gi=gi%10;

while(SW2==0);

} }

///////////////////////////////
if(SW3==0)//giam
{

do
{ delay(100);
if(SW3==1)
{

if(gi>10)
{ gi=gi-10; }
31

else
if(gi==10)gi=gi%10; } }
while(SW3==0); }
hienthi(k,p,gi);
break; } } } }
4. La chn thit b
- Model led:
+ 6 led 7 thanh ( sd model led 7 thanh nh bi thc hnh s 3)
+ 2 led n ( sd trong model led n bi thc hnh s 1)
- 3 button

Hnh 3.1: V tr 3 button T1, T2, T3 trn bo mch


- Cng iu khin: port 0, port 1, port 2, port 3

Hnh 3.2: V tr 3 port P0,P1, P2, P3 trn bo mch


5.Trnh t thc hin
- Bc 1: Dng jump cm 1 hng 5x2 kt ni PORT-2 vo 3 button T1, T2 V T3
- Bc 2: V mch m phng proteus

32

Hnh 3.3 M phng proteus ng h s hin th trn led 7 thanh


- Bc 3: Bin dch chng trnh mu, m phng trn Proteus v np chng trnh
vo kit thc hnh Easy8051 chc chn cc khi mch cn dng hot ng tt.
- Bc 4: Vit chng trnh, chy m phng v chy trn kit thc hnh 8051 theo yu
cu sau : ng chy gn ng vi thi gian thc, iu chnh c tng hay gim gi,
pht.
Lu
Khai bo ng hm timer,la chn ng hm timer
Lm mch thc t ng theo m phng

33

BI THC HNH S 4
B O TN S
1.Mc ch
- S dng b nh thi vi chc nng ngt timer.
- S dng chc nng ngt ngoi.
- Giao tip vi led 7 thanh, phm bm.
2.Yu cu
- Tnh ton tr ng 1s
- Lp trnh iu khin hin th c gi tr tn s theo yu cu.
- Nm bt c c ngt ca b nh thi, chc nng cc phn t trong s mch.
3.S nguyn l
Nguyn l hot ng: s dng b nh thi to tr 1s, m s xung vo trong 1s, s
xung m c chnh l tn s v c hin th ra led 7 thanh
Begin
w = 0;
dem = 0;
bg= 0;

bg = 1;

dem = dem + 1;

w = w;

dem < 1000;

w = w+1:
hienthi(w);

Hnh 4.1: Lu thut ton mch o tn s.


34

Chng trnh iu khin


#include <REGX52.H>
unsigned char display[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
int k,w=0,dem=0;
int sl =0;
int a,a1,b,b1,c,c1,d,d1,e,f,bg=0;
//////////////delay
void delay(int d)
{ int i;
for(i=0;i<d;i++)
{;}}
////////khoi tao ngat
void khoitaongat (void)
{ EA =0; //cam ngat toan cuc
TMOD = 0x01; //Timer 0 che do 16 bit
TH0 = 0xfc; //nap gia tri 252 vao TH0
TL0 = 0x67; //gia tri khoi tao 103
TL0
ET0 = 1; //cho phep ngat timer 0
IT0 = 1; //ngat theo suon xuong
EX0 = 1;//Cho phep ngat ngoai 0
EA = 1; //cho phep ngat toan cuc
TR0 = 1; //chay timer 0 bat dau dem so chu ky may }
//////////////////////
void hienthi(long k)
{ a=k/100000; //tram nghin
a1=k%100000;
b=a1/10000; //chuc nghin
b1=a1%10000;
c=b1/1000; // nghin
c1=b1%1000;
d=c1/100; //tram
d1=c1%100;
e =d1/10; //chuc
f=d1%10;
//don vi
for(sl=0;sl<10;sl++)
{ P0=display[a]; //display tram nghin
P1_0=0;
delay(50);
35

P1_0=1;
//////////////////////////////////////////////
P0=display[b]; //display chuc nghin
P1_1=0;
delay(50);
P1_1=1;
//////////////////////////////////////////////
P0=display[c]; //display nghin
P1_2=0;
delay(50);
P1_2=1;
//////////////////////////
P0=display[d];//display tram
P1_3=0;
delay(50);
P1_3=1;
/////////////////////////////
P0=display[e]; //display chuc
P1_4=0;
delay(50);
P1_4=1;
///////////////
P0=display[f]; //display don vi
P1_5=0;
delay(50);
P1_5=1; } }
////////////// ngat timer 0
void timer0(void) interrupt 1
{ TH0=0xfc;
//
TL0=0x67; //
if(bg==1) dem=dem+1;
TR0 =1; }
//////////////////////////
void main(void)
{ khoitaongat();
while(1)
{ hienthi(w) ; } }
36

////////////ngat ngoai 0
void count0(void) interrupt 0
{ bg=1;
if(dem<1000)
{ w=w+1; }
else
if(dem>=1000)
{ w=w; }
}
4.La chn thit b
- Model led:
+ 6 led 7 thanh ( sd model led 7 thanh nh bi thc hnh s 3)
- Cng iu khin: port 0, port 1, port 3

Hnh 4.2: V tr 4 port P0,P1, P2, P3 trn bo mch

37

5.Trnh t thc hin

Hnh 4.3: M phng proteus mch o tn s


6.Lu
Xc nh chun cc gi tr ngt, m bo sai s 0,5%. S dng hm ngt chng hin
tng nhp nhy led. Nm r chc nng ngt timer,ngt ngoi, ngt theo sn xung.

38

BI THC HNH S 5
LP TRNH CHC NNG NGT TRUYN THNG NI TIP
1.Mc ch
- Bit c cch giao tip gia 8051 vi my tnh qua cng RS232
2.Yu cu
Vit chng trnh truyn d liu t 8051 ln my tnh v ngc li
3.S nguyn l

Hnh 5.1: S nguyn l s dng chc nng ngt truyn thng ni tip
bt cc led trn port 2.
Nguyn l hot ng:
Khi nhn cc bit qua chn RxD ca n th 8051 phi tri qua cc bc sau:
N nhn bit Start bo rng bit sau n l bit d liu u tin cn phi nhn.
K t 8 bit c nhn ln lt tng bit mt. Khi bit cui cng c nhn th mt
byte c hnh thnh v t vo trong SBUF.
Khi bit Stop c nhn th 8051 bt RT = 1 bo rng ton b k t c nhn
v phi ly i trc khi n b byte mi nhn v ghi ln.
Bng vic kim tra bit c RI khi n c bt ln chng ta bit rng mt k t
c nhn v ang nm trong SBUF. Sao ni dung SBUF vo ni an ton trong
mt thanh ghi hay b nh khc trc khi n b mt.
Sau khi SBUF c ghi vo ni an ton th c RI c xo v 0 chun b kim
tra chu trnh tip theo.
39

Begin

char c;

Lp v hn

RI==1;

c =SBUF;

RI=0;

Hien thi ra led 7 thanh

Hnh 5.2: Lu thut ton s dng chc nng ngt truyn thng ni tip
bt cc led trn port 2.
Chng trnh iu khin
V d :
Hy lp trnh cho 8051 nhn cc byte d liu ni tip tc 9600 baud v
bt cc Led trn Port 2 tng ng: My tnh gi xung s 1: 1 Led sng, s 2: 2 Led
sng, , s 8: 8 Led sng, nu cc k t khc th tt tt c cc Led.
Li gii:
#include<at89x51.h>
char c;
main()
{
TMOD=0x20;
TH1=0xFD;

//Khai bo th vin cho 89c51


//Chng trnh chnh
//Chn Timer1, ch 2
//Ci t tc 9600 baud
40

SCON=0x50;
TR1=1;

//0101 0000: Chn ch 1, Cho php nhn


//Khi ng Timer1

while(1)
//Vng lp v hn
{
while(RI==1)
//Vng lp kim tra c nhn RI
{
//Nu RI=1 tc l nhn 1 byte.
c=SBUF;
//lu d liu nhn c vo bin c
RI=0;
//Xa c nhn RI.
}
switch(c)
//Kim tra k t va nhn c: tng ng
{
//trng hp no th thc thi lnh tngng.
case '1':
P2=0xFE;
break;
case '2':
P2=0xFC;
break;
case '3':
P2=0xF8;
break;
case '4':
P2=0xF0;
break;
case '5':
P2=0xE0;
break;
case '6':
P2=0xC0;
break;
case '7':
P2=0x80;
break;
case '8':
P2=0x00;
break;
default:
//mc nh l tt tt c Led.
P2=0xFF;
break;
}
}
}
4.La chn thit b
a. Module LED : Gm 8 led n:
41

i. 8 led n c ni trc tip vi vi iu khin


ii. 8 led n c kt ni vi Header 5x2, k hiu CN7 (LED)

Hnh5.3 V tr khi 8 Led n trn board mch


b. Mt MAX 232

c. Mt COMPIM

5.Trnh t thc hin


- Bc 1: Dng jump cm 1 hng 5x2 kt ni PORT-1 vo LED
- Bc 2: V mch m phng trn Proteus

42

Hnh 5.3: M phng trn proteus chc nng truyn thng ni tip iu khin 8 led n
6.Lu
- Tm quan trng ca c RT
- T m t trn y ta rt ra kt lun rng bng vic kim tra c RI ta bit 8051
nhn c mt byte k t cha. Sai khi c RI=1, nu ta khng sao c ni dung ca
thanh ghi SBUF vo ni an ton th c nguy c ta b mt k t va nhn c. Quan
trng hn l phi nh rng c RI c 8051 bt ln nhng lp trnh vin phi xo n
sau khi nhn c d liu. Cng nn nh rng, nu ta sao ni dung SBUF vo ni an
ton trc khi RI c bt th ta mo him sao d liu cha y .

43

BI THC HNH S 6
LED MA TRN
1.Mc ch
- Gip cho sinh vin bit cch qut led ma trn led hin th hay to hiu ng theo
mun
2.Yu cu
- Bit cch ghp ni cc led ma trn vi nhau( s dng IC 74HC595)
- M phng mch trn proteus chy t tri sang phi dng ch bt k
3.S nguyn l
Nguyn l hot ng: qut dng v qut ct v hin th ra ch mun hin th(vd:ch A)
Chng trnh iu khin:
#include<reg52.h>
void delay(unsigned int ms)
{unsigned int i;
for(i=0;i<ms;i++); }
void main()
{

unsigned int n;
unsigned char mahang[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char macot[8]={0x00,0X7f,0Xff,0X88,0X88,0Xff,0X7f,0X00};

while(1)
{

for(n=0;n<8;n++)
{

P2=mahang[n];
P3=macot[n];
delay(30); } }}

4.La chn thit bi.


Mt matrix 8x8 red.

44

5.Trnh t thc hin


Bc 1: V m phng proteus

Hnh 6.1:M phng trn proteus hin th ch A trn led ma trn


Bc 2: Lp code v chy trn mch.

45

BI THC HNH S 7
O NHIT HIN TH LCD
1.Mc ch
- Sinh vin bit cch giao tip gia 8051 vi lcd
- Giao tip vi cc thit b cm bin : cm bin nhit
2.Yu cu
- Cp ngun v dng cho LCD v vi iu khin hot ng
- Lp trnh vi x l c th tnh ton v hin th nhit theo ng thc t
3.S nguyn l

Hnh 7.1 S nguyn l mch o nhit hin th ra LCD.


Nguyn l hot ng: IC LM35 l IC cm bin nhit , nhit bn ngoi thay i
th tn hiu in xut ra t chn data ca lm35 cng thay i theo. mt nhit xc
nh th gi tr ny l hng s.Tn hiu in do LM35 xut ra c a vo IC
ADC0804 x l tng t, bin i v x l s, gii m a n IC89C52 ri a ra
LCD hin th.
Chng trnh iu khin
#include <reg52.h>
#include <LCDset.c>
#include <stdio.h>

unsigned long x;
46

sbit WR_ADC = P3^4;


sbit RD_ADC = P3^5;
sbit INTR_ADC = P3^3;
unsigned long volt;

void chuyendoi()
{

WR_ADC = 0;
delay_short();
WR_ADC = 1;
while (!INTR_ADC);
x=P0;
volt = x; }

void main()
{

setting();
ghi_lenh(0x01);
ghi_lenh(0x80);
ghi_chuoi("lop d4dtvt:");
ghi_lenh(0xc0);
ghi_chuoi("nhom 2");
delay(100);
ghi_lenh(0x01);
ghi_chuoi("Nhiet do la: ");
RD_ADC = 0;
WR_ADC = 0;
INTR_ADC = 1;
47

delay(10);
P0 = 0xff;
while(1)
{

ghi_lenh(0x80+9);
chuyendoi();
ghi_lenh(0xc0);
ghi_lenh(0x0c);
ghi_so_nguyen(volt);
ghi_lenh(0xc0+3);
ghi_nhanh_chuoi("oC "); } }
Chng trnh LCDset.c

sbit RS_LCD = P1^0;


sbit RW_LCD = P1^1;
sbit E_LCD = P1^2;
void delay_short()
{

unsigned int i;
for(i=0;i<3;i++); }

void delay(unsigned int time)


{

// Thoi gian time ms


TMOD = 0x01;

//Timer 0 che do 1

while (time--){
TH0 = -1000/256;
TL0 = -1000%256;
TR0 = 1;
while (!TF0);
48

TR0 = 0;
TF0 = 0;

} }

void kt_ban()
{

unsigned char x;
P2 = 0xff;
RS_LCD = 0;
RW_LCD = 1;
do

{
E_LCD = 1;
delay_short();
E_LCD = 0;
x=P2;
x=x&0x80; }

while(x!=0x80); }
void ghi_lenh(unsigned char commant)
{

kt_ban();
delay(50);
P2 = commant;
RS_LCD = 0;

// chon thanh ghi lenh

RW_LCD = 0;

// write to LCD

E_LCD = 1;
delay_short();
E_LCD = 0; }
void ghi_kytu(unsigned char kytu)
{

kt_ban();
49

delay(50);
P2 = kytu;
RS_LCD = 1;
RW_LCD = 0;
E_LCD =1;
delay_short();
E_LCD = 0; }
void ghi_nhanh ( unsigned char kytu)
{

delay_short();
P2 = kytu;
RS_LCD = 1;
RW_LCD = 0;
E_LCD = 1;
delay_short();
E_LCD = 0; }

//------------------void ghi_chuoi(char *str)


{

while(*str)

ghi_kytu(*str);
str++; } }

void ghi_nhanh_chuoi(char *str)


{

while(*str)
{ ghi_nhanh(*str);
str++; } }

void lcd_goto(char y,char x)


50

unsigned char dong[] = {0x80,0xC0};


ghi_lenh(dong[y-1]+x); }

void ghi_so_nguyen(unsigned long number)


{

int j,m;
unsigned char d[20];
for(j=0 ; j<20 ; j++)
{

d[j] = number%10;
number /= 10;
if(number==0){
m = j;
break; } }

for(j=m ; j>=0 ; j--)


{

ghi_nhanh(d[j]+0x30);

} }

void ghi_so_thuc2(unsigned long number)


{

int j,m;
unsigned char d[20];
for(j=0 ; j<20 ; j++)
{

d[j] = number%10;
number /= 10;
if(number==0){
m = j;
break; } }

if(m <= 1 )
{

while(m++<3)
d[m] = 0;

}
51

for(j=m; j>=0; j--)


{

if(j == 1)
ghi_nhanh(',');
ghi_nhanh(d[j]+0x30);

} }

void setting()
{

ghi_lenh(0x38);
ghi_lenh(0x01);

//Xoa man hinh

ghi_lenh(0x0f);

//Co dich hien thi

ghi_lenh(0x0c); //Bat hien thi ,tat con tro }


4.La chn thit bi.
- Mt LCD 1602 4bit socket

Hnh 7.1 LCD socket

Hnh 7.2 schematics of LCD1602

- B chuyn i ADC0804

- Mt in tr thanh 8 chn RESPACK-8

52

- Mt cm bin nhit LM35

Trnh t thc hin


Bc 1: V mch theo m phng proteus

M phng trn proteus o nhit hin th trn LCD


Bc 2: Lp code v chy trn mc
6.Lu
LM35 dng in p trong khong 4 - 30 V. IC LM35 c th o c nhit t -55
ti 150oC nhng theo khuyn co ca nh sn xut th ch nn o trong khong t -40
ti 110oC thi m bo tnh chnh xc.

53

BI THC HNH S 8
IU KHIN TC NG C MT CHIU
1.Mc ch
- Gip cho sinh vin bit cch iu khin c tc ca 1 ng c mt chiu t
c th iu khin c tc ca 1 ng c xoay chiu da trn nhng nguyn l
iu khin ng c in 1 chiu
- Lp rp phn cng , thc hnh tay ngh
2.Yu cu
- Lp rp ng phn cng cho lp trnh
- Lp trnh c theo yu cu bi ton
3.S nguyn l

Hnh 8.1: S mch nguyn l iu khin ng c mt chiu


VD: iu khin ng c dng h vi iu khin 8051 bng phng php iu chnh
rng xung PWM
Nguyn l hot ng:
- Khi cha nhn nt, chn 26 (P2.5) v chn 27 (P2.6) mc 0, rle khng hot ng, in
p 2 u ca ng c bng Uvo (12V) => ng c khng hot ng.
- Khi nhn nt quay thun, tn hiu c a n vi iu khin, vi iu khin nhn v x l
tn hiu => sau a tn hiu qua chn 26 (P2.5) v chn 27 (P2.6). Chn 26 tch mc 1,
chn 27 tch mc 0 => transistor khuch i => iu khin rle1 hot ng, rle2 khng hot
ng => in p t vo 2 u ng c l khc nhau (12V ~0V), ng thi vi iu khin
54

to 1 xung PWM ( rng khong 70%) trn chn 28 qua opto (cch ly quang => chng
nhiu cho vi iu khin) => qua MOSFET IRF540 khuch i tn hiu a vo u cn li
ca ng c => ng c quay thun
- Khi nhn nt quay ngc, tn hiu c a n vi iu khin, vi iu khin nhn v x l
tn hiu => sau a tn hiu qua chn 26 (P2.5) v chn 27 (P2.6). Chn 26 tch mc 0,
chn 27 tch mc 1 => transistor C1815 khuch i => iu khin rle2 hot ng, rle1
khng hot ng => in p t vo 2 u ng c l khc nhau (~0V 12`V), ng thi vi
iu khin to 1 xung PWM trn chn 28 qua opto (cch ly quang => chng nhiu cho vi
iu khin) => qua MOSFET IRF540 khuch i tn hiu a vo u cn li ca ng c
=> ng c quay ngc
- Khi nhn nt tng tc , tn hiu trn chn 26 v 27 vn gi nguyn mc lgc. Mt khc,
to mt xung PWM mi ( rng khong 99%) trn chn 28 qua opto (cch ly quang =>
chng nhiu cho vi iu khin) => qua MOSFET IRF540 khuch i tn hiu a vo u
cn li ca ng c => ng c vn quay theo chiu nh trc nhng tc nhanh hn.
- Khi nhn nt gim tc , tn hiu trn chn 26 v 27 vn gi nguyn mc lgc. Mt khc,
to mt xung PWM mi ( rng khong 1%) trn chn 28 qua opto (cch ly quang =>
chng nhiu cho vi iu khin) => qua MOSFET IRF540 khuch i tn hiu a vo u
cn li ca ng c => ng c vn quay theo chiu nh trc nhng tc chm hn.
- Khi nhn nt RESET vi iu khin, vi iu khin tr v trng thi ban u => ng c
ngng hot ng.
Chng trnh iu khin
///////////////////////////// Dieu khien toc do dong co bang xung PWM
///////////////// Khai bao thu vien
#include<reg52.h>
#include<math.h>
#include<stdio.h>
////////////////// Khai bao bien
sbit F=P2^7;
sbit T=P1^4;
sbit G=P1^5;
55

sbit QT=P1^6;
sbit QN=P1^7;
sbit QT1=P2^5;
sbit QN1=P2^6;
unsigned int i;
/////////////////// Ham tao xung PWM
void taoxung(unsigned int i)
{

F=1;
TR0=0;
TH0=-i/256;
TL0=-i%256;
TR0=1;
while(!TF0);
TF0=0;
F=0;
TR0=0;
TH0=-(1000-i)/256;
TL0=-(1000-i)%256;
TR0=1;
while(!TF0);
TF0=0; }

/////////////// Tao momen cho DC


void momen(void)
{ if ((G==1)&&(T==1))
{

i=i+100;
56

taoxung(i); }
if(i>=700) i=i-100;

/////////////// Tang toc do dong co


void tang(void)
{

if(T==1);

else
{ while ((G==1)&&(QT==1)&&(QN==1))
{

i=i+10;
taoxung(i);
if(i>=990) i=i-10; } }

/////////////// Giam toc do dong co


void giam(void)
{

if(G==1);

else
{

while ((T==1)&&(QT==1)&&(QN==1))
{

i=i-10;
taoxung(i);
if(i<=10) i=i+10; }}}

/////////////// Dieu khien dong co quay thuan


void thuan(void)
{

if(QT==1)
{

QT1=0;
QN1=0; }

else
{ while(QN==1)
57

QN1=0;
QT1=1;
tang();
giam();
momen();

}}}

//////////////////// Dieu khien dong co quay nghich


void nghich(void)
{

if(QN==1)

QT1=0;
QN1=0;

else
{

while(QT==1)

QN1=1;
QT1=0;
tang();
giam();
momen();
}}}

/////// Chuong trinh chinh


void main(void)
{

TMOD=0x01;

while(1)
{

thuan();
nghich();

58

4.La chn thit b


- Mt ng c mt chiu.

Hnh 8.2: S ca mt my in 1 chiu vi b phn kch t song song


- Mt IC OPTOCOUPLER-NPN
- 2 PORT iu khin
5.Trnh t thc hin
Bc 1: v mch theo m phng proteus

Hnh 7.3: M phng trn proteus iu khin ng c bc


Bc 2: Lp code v chy trn mch

59

BI THC HNH S 9
IU KHIN T XA CC THIT B IN
1.Mc ch
Gip cho sinh vin bit nguyn l hot ng ca mt thit b iu khin t xa
S dng vi iu khin iu khin cc thit b in
2.Yu cu
Bit c nguyn l ca mch iu khin t xa xut bit tng ng vo vi
iu khin
Lp trnh cho vi iu khin xut tn hiu iu khin thit b theo yu cu bi
ton
iu khin thit b 1 chiu hoc xoay chiu bng cch s dng r le hoc triac
3.S nguyn l
S mch pht hng ngoi

S mch thu hng ngoi v mch iu khin thit b in

60

Nguyn l hot ng:


S dng mch thu pht hng ngoi cp in p mc cao hoc thp vo chn
ca vi iu khin (trong mch s dng mc 1). T lp trnh cho vi iu
khin xut in p ra cc chn tng ng iu khin cc thit b in
S dng bin trong chng trnh lp trnh cho vi iu khin c th s dng
t phm bm m iu khin c nhiu n theo mun
Trong mch s dng 3 phm bm iu khin 8 n sng tt ring bit , c
th lp trnh c th iu khin c nhiu n hn m vn s dng 3 phm
bm
Mch s dng 2 IC thu pht hng ngoi l PT2248 v PT2249 . 2 IC ny c
th to ra 10 knh bm nhng trong mch ch s dng 3 knh iu khin
Lu thut ton

Trong code ca chng trnh s dng 2 bin iu khin tng bt u ra ca


P1. Bin kt1 iu khin 4 bit u . kt2 iu khin 4 bit sau . da trn thut ton
bn trn
Code chng trnh
#include <REGX51.H>
#define set P0_0
#define up P0_1
#define down P0_2
unsigned char kt=0,kt1=0,kt2=0;
bit set0=1,set1=1,up1=1,up0=1,down0=1,down1=1;
void tre(long t)
{ int k;
61

for(k=0;k<t;k++)
{;}
}
void ktphim()
{
set0=set1;
set1=set;
if((set0==1)&&(set1==0))
{
kt=kt+1;
if(kt==2)kt=0;
}
switch(kt)
{
case 0:
{
up0=up1;up1=up;// a=1;
if((up0==1)&&(up1==0))
{
kt1=kt1+1;
if(kt1>=5)kt1=1;}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{
kt1=kt1-1;
if(kt1<=-1)kt1=0;
}
switch(kt1)
{
case 0:{break;}
case 1:{P1=0xfe;break;}
case 2 :{P1=0xfd;break;}
case 3:{P1=0xfb;break;}
case 4:{P1=0xf7;break;
}
}
62

break;
}
case 1:
{
up0=up1;up1=up;// a=1;
if((up0==1)&&(up1==0))
{kt2=kt2+1;
if(kt2>=5)kt2=1;
}
down0=down1;down1=down;
if((down0==1)&&(down1==0))
{ kt2=kt2-1;
if(kt2<=-1)kt1=0;
}
switch(kt2)
{
case 0:{break;}
case 1:{P1=0xef;break;}
case 2 :{P1=0xdf;break;}
case 3:{P1=0xbf;break;}
case 4:{P1=0x7f;break;}
}
break;
}
}
}
void main()
{
P1=0x00;
while(1)
{ ktphim();

63

BI THI: MCH KHA S


CHNG 1: PHN TCH H THNG
1.Phn tch v la chn phng n
a) Yu cu ca h thng:
Qua tham kho cc sn phm kha s trn th trng th yu cu ca mt b sn
phm kha s thng thng:
- Do din ngi dung d s dng.
- Mt khu c di m bo tnh bo mt cao.
- C th thay i c mt khu.
- Tnh nng cnh bo khi nhp mt khu sai 3 ln lin tip.
- ng c quay iu khin ca ht hnh trnh th dng li.
- H thng kha s nh gn.
- in p hot ng ca h thng khng gy nguy him n ngi s dng.
b) La chn phng n
Vi yu cu v tnh nng nh trn, chng em chn cc thit b chnh:
- Vi iu khin 16f877 vi b nh eeprom c kh lu tr d liu ngay c khi
mt in.
- Mn hnh LCD 16x2 vi mc ch hin th thng tin, giao tip vi iu khin
vi ngi dng
- Bn phm 16 phm c cc phm s v phm chc nng a u vo l mt
khu, cc lnh ng m ca ti vi iu khin.
- ng c ko cnh ca ng v m. Vi ti ny c yu cu sn phm m
phng thc t, chng ti chn ng c mt chiu cng sut nh.
- Ngun d tr khi mt in l ngun acquy.
2.Xc nh bi ton v gii hn ti
a) Xc nh bi ton
im quan trng nht ca chng trnh kha s l thut ton qut phm, sau l
lu v thay i mt m trn eeprom. Cn mt s phn khc l hin th thng tin ra LCD
th c cc lnh h ch nn khng phc tp. cui cng l a tn hiu ra iu khin
thit b l kha ca.
V vy bi ton chnh l ta cn quan tm l :
64

- Thut ton qut phm


- Qu trnh lu mt m trong eeprom v thay i eeprom
- Hin th thng tin ra mn hnh LCD
b) Gii hn ca ti
Cc rng buc :
- Thng thng h thng giao din vi ngi dng bn ngoi, nn phi an
ton, trnh c nhng tc ng ca ngoi cnh.
- Chu c qu ti ti khi gp chng ngi vt trong thi gian di.
- p dng iu khin ng c c cng sut nh.

65

CHNG 2 :THIT K H THNG


1. S khi tng th ca h thng

Khi giao tip v


hin th thng tin

Khi iu khin

Thit b chp
hnh

Hnh 2.1: s khi kha s c bn.


Khi giao tip v hin th thng tin : dng a tn hiu, thng s ti khi iu
khin
Khi iu khin tip nhn cc thng tin, v x l cc thng tin . Xut tn hiu
iu khin cc thit b khc.
Thit b chp hnh y c th l ng c, c th l rle iu khin thit b
chnh l ca hoc kha.
2.Cc khi trong h thng

Bo ng

Mn Hnh
Hin Th LCD

Khi Ngun

Vi iu Khin
PIC 16F877a

ng C

Khuch i
Tn Hiu

66

Bn Phm Ma
Trn 4x4

2.1

Khi iu khin trung tm

Hnh 2.2: Khi vi iu khin


Khi vi iu khin s dng vi iu khin Pic16F877A, n ng vai tr ht sc
quan trng trong h thng iu khin h thng kha s. Khi VK bao gm mch
to dao ng thch anh, mch Reset reset h thng li trng thi ban u. Cc chn
ca VK s c kt ni vi cc khi khc nh ng c, bn phm, khi hin th. Lu
tr mt khu qua b nh EEPROM c th lu tr ngay khi mt in. Ton b d liu
m ta thit k iu khin h thng kha s u c cha trong b nh ca VK.
2.2 Khi hin th
Khi hin th chnh l mn hnh hin th LCD 16x2

Hnh 2.3: Khi hin th


Khi hin th c dng l LCD, c 2 ch dng cho LCD l ch LCD 16
bit v ch 8 bit. Trong bi s dng LCD 8 bit th cc ch cn s dng cc chn RW,
RS, E, D4 =>D7.
67

2.3

Khi bn phm

Hnh 2.4: Ma trn 16 phm


Nguyn l hot ng ca vic qut phm chnh l cho cc ct bng 1 v ln lt
cc chn ni vi hng mc 0. Ri kim tra xem c ct no bng 0 hay khng, nu ct
no bng 0 trong thi im mt hng bng 0 th pht hin ra phm c bm.
2.4. Khi ng c v bo ng
H thng bo ng: cnh bo khi nhp sai mt khu qu 3 ln. Thng qua h
thng bo ng.
B khuch i tn hiu v mch cu H :dng a tn hiu iu khin ng c

Hnh 2.5 : Mch cu H khuch i tn hiu


2.5.

Khi ngun
Cung cp ngun nui cho vi iu khin hot ng theo yu cu ti.

68

Hnh 2.6: Khi ngun cung cp

CHNG 3: H THNG KHA S


1.S nguyn l

2. Nguyn l hot ng
Khi mun m ca th phi g ng mt khu vi di 8 k t. Nu g ng th
ng c s quay thun, v ca s m. V g sai th mn hnh LCD hin th ch nhp sai
mt khu. Trong khi nhp mt khu, nu sai mt hoc mt vi k t ta c th xa mn
hnh v nhp li mt khu.
Khi ng c quay ht hnh trnh, cng tc hnh trnh m xong ng, khi LCD
thng bo m xong.
Nu nhp mt khu sai 3 ln lin tip th h thng bo ng s hot ng, thng
qua chn RB5.
Trong khi m xong, nu nhn kha th ng c s quay v ca kha li, ng c
quay th nt cng tc hnh trnh kha xong s m, v nu cng tc hnh trnh m xong
ng li th ng c ngng quay, LCD bo m xong.
Mun i mt khu ta nhn nt i mt khu trn bn phm, khi ny LCD bo
nhp mt khu c, v ta phi nhp ng mt khu c th mi i c mt khu, nu
69

nhp ng mt khu th LCD thng bo nhp mt khu mi. nhp xong th nhn OK,
vy l mt khu c i.
3.Code chng trnh
#include <16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use delay(clock=20M)
#include <lcd_4bit.c>
#byte PORTB=0x06
#byte portc=0x07
#byte portd=0x08
#byte porte=0x09
#define LCD_ENABLE_PIN portd.0
#define LCD_RS_PIN

portd.1

#define LCD_RW_PIN

portd.2

#define LCD_DATA4

portd.4

#define LCD_DATA5

portd.5

#define LCD_DATA6

portd.6

#define LCD_DATA7

portd.7

#bit RB0=PORTB.0
#bit RB1=PORTB.1
#bit RB2=PORTB.2
#bit RB3=PORTB.3
#bit RB4=PORTB.4
70

#bit RB5=PORTB.5
#bit RB6=PORTB.6
#bit RB7=PORTB.7
#bit RC0=portc.0
#bit RC1=portc.1
#bit RC2=portc.2
#bit RC3=portc.3
#bit RC4=portc.4
#bit RC5=portc.5
#bit RC6=portc.6
#bit RC7=portc.7
int8 a[8],b[8];
int8 m=0,j=0,i=0,h=0,k=0;
void sairoi();
void kiemtra();
void quetphimthaydoi();
void quetphimsosanh();
void quetphim();
void sairoi()
{

LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"nhap sai roi");
if(read_eeprom(0x09)==3)
{ RB5=1;
71

lcd_putcmd(line_2);
printf(LCD_PutChar,"ban la ke trom");
delay_ms(2000);delay_ms(2000);delay_ms(2000);delay_ms(2000);
RB5=0;write_eeprom(0x09,0);
reset_cpu(); }
else
{ lcd_putcmd(line_2);
printf(LCD_PutChar,"nhap lai");
delay_ms(1000);
reset_cpu(); } }
void kiemtra()
{ lcd_putcmd(line_1);
printf(LCD_PutChar,"dang kiem tra");
delay_ms(600);
for(j=0;j<=7;j++)
if(read_eeprom(0x00+j)-b[j]!=0)
{

write_eeprom(0x09,read_eeprom(0x09)+1);
sairoi();

else
{ LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_1);
RB6=1;RB7=0;;i=0;
write_eeprom(0x09,0);
72

printf(LCD_PutChar,"dang mo cua");
void quetphimthaydoi()
{ portc=0xfe;
if(RC4==0)
{

write_eeprom(0x00+j,7);
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

write_eeprom(0x00+j,8);
j++;
LCD_PutChar('*');
while(RC5==0){;} }

if(RC6==0)
{

write_eeprom(0x00+j,9);
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

while(RC7==0){;}

portc=0xfd;
if(RC4==0)
{

write_eeprom(0x00+j,4);
73

j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

write_eeprom(0x00+j,5);
j++;
LCD_PutChar('*');
while(RC5==0){;} }

if(RC6==0)
{

write_eeprom(0x00+j,6);
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

while(RC7==0){;}

portc=0xfb;
if(RC4==0)
{ write_eeprom(0x00+j,1);
j++;
LCD_PutChar('*');
while(RC4==0){;} }
if(RC5==0)
{

write_eeprom(0x00+j,2);
74

j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{ write_eeprom(0x00+j,3);
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

while(RC7==0){;}

portc=0xf7;
if(RC4==0)
{

LCD_PutChar('*');
while(RC4==0){;} }

if(RC5==0)
{

write_eeprom(0x00+j,0);
j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{

j=j-1;
LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"nhap pass moi");
75

lcd_putcmd(line_2);
for(h=0;h<j;h++)
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_1);
printf(LCD_PutChar,"da thay doi");
delay_ms(1000);
while(RC7==0){;}
reset_cpu();

} }

void quetphimsosanh()
{

portc=0xfe;
if(RC4==0)
{

a[j]=7;
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

a[j]=8;
j++;
LCD_PutChar('*');
while(RC5==0){;}

}
76

if(RC6==0)
{

a[j]=9;
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

while(RC7==0){;}

portc=0xfd;
if(RC4==0)
{

a[j]=4;
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{ a[j]=5;
j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{

a[j]=6;
j++;
LCD_PutChar('*');
while(RC6==0){;}

}
77

if(RC7==0)
{

while(RC7==0){;}

portc=0xfb;
if(RC4==0)
{

a[j]=1;
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

a[j]=2;
j++;
LCD_PutChar('*');
while(RC5==0){;} }

if(RC6==0)
{

a[j]=3;
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

while(RC7==0){;}

portc=0xf7;
if(RC4==0)
{

LCD_PutChar('*');
78

while(RC4==0){;} }
if(RC5==0)
{ a[j]=0;
j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{

while(RC6!=0) {;} }

if(RC7==0)
{

j=0;
LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"dang kiem tra");delay_ms(1000);
for(j=0;j<=7;j++)
if(read_eeprom(0x00+j)-a[j]!=0)
{ LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_2);
printf(LCD_PutChar,"nhap sai roi !");delay_ms(2000);
reset_cpu();

else
{

LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"nhap pass moi");
lcd_putcmd(line_2);

j=0;
79

while(RC7!=0)
{

quetphimthaydoi();

void quetphim()
{ portc=0xfe;
if(RC4==0)
{

b[j]=0x07;
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

b[j]=0x08;
j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{

b[j]=0x09;
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

kiemtra();
while(RC7==0){;}

portc=0xfd;
80

if(RC4==0)
{

b[j]=0x04;
j++;
LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{ b[j]=0x05;
j++;
LCD_PutChar('*');
while(RC5==0){;} }
if(RC6==0)
{

b[j]=0x06;
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{ //LCD_putc('*');
while(RC7==0)
{

RB6=0;RB7=1;
LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_1);
printf(LCD_PutChar,"dang khoa cua"); }

}
81

portc=0xfb;
if(RC4==0)
{

b[j]=0x01;
j++;
LCD_PutChar('*');
while(RC4==0){;} }

if(RC5==0)
{

b[j]=0x02;
j++;
LCD_PutChar('*');
while(RC5==0){;} }

if(RC6==0)
{

b[j]=0x03;
j++;
LCD_PutChar('*');
while(RC6==0){;}

if(RC7==0)
{

LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"doi mat khau");
lcd_putcmd(line_2);
printf(LCD_PutChar,"mat khau cu");
delay_ms(1000);
delay_ms(1000);
82

LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"mat khau cu :");
lcd_putcmd(line_2);
j=0;
while(RC7!=0)
{

quetphimsosanh(); }

portc=0xf7;
if(RC4==0)
{

LCD_PutChar('*');
while(RC4==0){;}

if(RC5==0)
{

b[j]=0x00;
j++;
LCD_PutChar('*');
while(RC5==0){;}

if(RC6==0)
{

j=j-1;
LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"mat khau cu :");
lcd_putcmd(line_2);
for (k=0;k<j;k++)

LCD_PutChar('*');
83

while(RC6==0){;}

if(RC7==0)
{ while(RC7==0){;} } }
void main()
{

set_tris_b(0x03);RB6=0;RB7=0;
set_tris_d(0x00);
set_tris_e(0x00);porte=0x00;
set_tris_c(0xf0);
portc=0xff;
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
ext_int_edge(H_TO_L);
lcd_init();delay_ms(100);
lcd_putcmd(line_1);
for(i=0;i<=9;i++)
{

//chu y doan nay|=> rat quan trong de thay doi duoc pass

write_eeprom(0x00+i,0);//xoa eeprom ve 0 // muon doi pass, va co bao dong thi xoa


doan nay di
}

// doan nay co tac dung gan pass mac dinh la: "8 so 0"

printf(LCD_PutChar,"nhap pass nao");


lcd_putcmd(line_2);
while(true)
{

quetphim();
84

if(RB0==0)
{

LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_2);
RB6=0;RB7=0;
printf(LCD_PutChar,"da khoa xong");
delay_ms(500);
LCD_PutCmd(Clear_Scr);
lcd_putcmd(line_1);
printf(LCD_PutChar,"nhap phim nao");
lcd_putcmd(line_2);
while(RB0==0)
{

quetphim();
while(RB0!=0)
{

if(RB1==0)
{ reset_cpu();
while(RB1!=0) {reset_cpu();}
}

}
}
}
if(RB1==0)
{

lcd_putcmd(line_2);
RB6=0;RB7=0;
85

for(m=0;m<=7;m++)
{

b[m]=0; }

printf(LCD_PutChar,"da mo xong");
delay_ms(500);
LCD_PutCmd(Clear_Scr);
printf(LCD_PutChar,"nhap phim nao");
lcd_putcmd(line_2);
while(RB1==0)
{

quetphim();
while(RB1!=0)
{ if(RB0==0)
{ reset_cpu();
while(RB0!=0) {reset_cpu();}
}
}
}
}
}
}

86