giao tiẾp nỐi tiẾp vi ĐiỀu khiỂn 8051

Download GIAO TIẾP NỐI TIẾP VI ĐIỀU KHIỂN 8051

Post on 16-Oct-2015

65 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

GIAO TIẾP NỐI TIẾP VI ĐIỀU KHIỂN 8051

TRANSCRIPT

  • I HC QUC GIA TP.H CH MINHTRNG I HC BCH KHOA

    KHOA IN-IN T B MN K THUT IN T

    11

    CHNG 3

    H VI IU KHIN 8051

  • B mn K Thut in T - HBK

    3.6 Cng ni tip (Serial Port)

    2

  • B mn K Thut in T - HBK

    Ni dung

    Gii thiu

    Chun EIA(Electronic Industries Association)-232E hoc RS232

    Giao tip cng ni tip MCS-51/52

    ng dng ca ch 0

    ng dng ca ch 1

    ng dng ca ch 2 & 3

    Cc th d.

    3

  • B mn K Thut in T - HBK

    Truyn d liu ni tip

    Truyn d liu khong cch xa Tc truyn d liu thp Truyn d liu ni tip ng b

    Dng tn hiu clock ring ng b ha mch thu v pht.

    Thng dng khung d liu ln hn v nhanh hn.

    Truyn d liu ni tip bt ng b Khng cn tn hiu clock ring ng b ho chuyn

    d liu. Da trn k{ t. Mi k{ t c ng khung bng start

    bit v stop bit. Bn thu cn nhn dng start bit v stop bit nhn

    ng k{ t d liu.

    4

  • B mn K Thut in T - HBK

    Truyn d liu ni tip ng b (synchronous) v bt ng b (asynchronous) vi 8051

    5

  • B mn K Thut in T - HBK

    ng truyn d liu c bn

    Ngi ta c th s dng ng in thoi cng cng hoc dnh ring lm mi trng truyn d liu ni tip bt ng b.

    Modem c dng chuyn i d liu s thnh dng sng analog thch hp cho vic truyn trn ng dy in thoi v ngc li.

    C 2 kiu ng truyn: im n im v a im.

    6

    DTE DCE DCE DTE

    Computer or

    terminal

    Modem Modem Computer or

    terminal

    Communication linkDTE: Data Terminal EquipmentDCE: Data Communications Equipmnent

  • B mn K Thut in T - HBK

    ng truyn im n im v a im

    7

    Master

    Slave 1 Slave 2 Slave n

    Address 1 Address 2 Address n

    Station Station

    Peer Peer

    Tx

    TxRx

    Rx

  • B mn K Thut in T - HBK

    C bn v cc ng truyn

    H thng 2 dy v 4 dy:

    2 dy: tn hiu v t.

    4 dy: 2 b 2 dy.

    Cc kiu ng truyn:

    n cng (Simplex link): ng dy dnh ring cho thu hoc pht ch khng c hai.

    Bn song cng (Half-duplex link): ng dy c th dng cho pht hoc thu nhng mi ln ch c 1 chiu.

    Song cng (Full-duplex link): Pht v thu c th tin hnh ng thi. ng truyn cn 4 dy.

    8

  • B mn K Thut in T - HBK

    C bn v cc ng truyn

    9

    5V

    0V

    >+3V

  • B mn K Thut in T - HBK

    EIA-232-E or RS-232

    EIA-232-E or RS-232 can apply to the following data communication schemes:

    Serial communications

    Synchronous and asynchronous

    Dedicated leased or private lines

    Switched service

    Two wire or four wire

    Point to point or multipoint

    4 aspects in EIA-232-E interface: mechanical, functional, procedural, and electrical.

    10

  • B mn K Thut in T - HBK

    EIA-232-E Mechanical Characteristics

    25-pin DB25 /9-pin DB9 plug connector.

    11

  • B mn K Thut in T - HBK

    DB-9 Signals

    12

  • B mn K Thut in T - HBK

    Cng ni tip ca 8051

    Cng ni tip ca 8051 c th hot ng trn mt di rng tn s

    Chc nng:

    Chuyn i song song ni tip khi xut

    Chuyn i ni tip song song khi nhn

    13

  • B mn K Thut in T - HBK

    S khi cng ni tip ca 8051

    14

  • B mn K Thut in T - HBK

    Cc c tnh ca cng ni tip 8051

    Song cng (ful-duplex)

    C m khi thu (b m 1 byte): nu CPU c k t th nht trckhi k t th 2 c nhn y th d liu s khng b mt

    C 4 ch lm vic (0 - 3)

    Lp trnh d dng :

    Chn tc (c nh (on-chip OSC/12 hoc OSC/64), hoc thay i c (T1))

    Chn ch lm vic

    Cho chy (SCON)

    D s dng

    Truyn d liu: MOV SBUF, A

    Nhn d liu: MOV A, SBUF

    15

  • B mn K Thut in T - HBK

    Thanh ghi iu khin cng ni tip SCON

    16

  • B mn K Thut in T - HBK

    Cc ch hot ng ca cng ni tip

    17

    Trc khi s dng cng ni tip, SCON c khi to tr vi ch ng, v v.v... Th d, cc lnh sau

    MOV SCON, #01010010B

    khi to tr cho cng ni tip ch 1 (SM0/SM1=0/1), cho php b thu (REN=1) v t c ngt pht (T1=1) ch b pht sn sng hot ng.

  • B mn K Thut in T - HBK

    Ch 0:Thanh ghi dch 8 bit (1/3)

    RXD l chn xut/nhp d liu

    TXD l ng ra xung nhp dch

    Tm bit c pht ra hay thu vo vi LSB i trc.

    Tc baud c nh l 1/12 tn s mch dao ng trn chip

    18

  • B mn K Thut in T - HBK

    Ch 0:Thanh ghi dch 8 bit (2/3)

    19

    Pht: c khi ng bng lnh ghi vo SBUF

  • B mn K Thut in T - HBK

    Ch 0:Thanh ghi dch 8 bit (3/3)

    20

    Vic thu: c khi ng khi bit cho php b thu REN = 1 v RI = 0

  • B mn K Thut in T - HBK

    Ch 1: UART 8 bit c tc baud thayi c (1/3)

    ch 1, 10 bit c pht i trn TXD hoc c thu vo RXD. Cc bit ny bao gm 1 bit bt u (lun lun l 0), 8 bit d liu (LSB i trc), v 1 bit dng (lun lun l 1).

    hot ng b thu, bit dng i vo RB8 trong SCON. Trong 8051, tc baud c t bng tc trn ca Timer 1.

    21

  • B mn K Thut in T - HBK

    Ch 1: UART 8 bit c tc baud thayi c (2/3)

    Vic pht c khi ng bng cch ghi vo SBUF, nhng khng tht s bt u cho n khi cnh xung k ca b m chia 16 cung cp tc baud cng ni tip. D liu c dch ra trn ng TXD bt u bng bit bt u (start bit), theo sau l 8 bit d liu, v k n l bit dng. Chu kz ca mi bit l nghch o ca tc baud c lp trnh trong timer. C ngt pht (TI) c t ln 1 ngay khi bit dng xut hin trn TXD .

    22

    s kin ng b ha

  • B mn K Thut in T - HBK

    Ch 1: UART 8 bit c tc baud thay i c (3/3)

    Vic thu c khi ng bi chuyn tip 1 sang 0 RXD. B m chia 16 creset tc thi ng chnh cc s m vi chui bit n. Chui bit n cly mu gia 16 ln m.

    Gi s b thu pht hin bit bt u hp l th tip tc nhn k t. Bit bt uc b qua v 8 bit d liu c nhp vo thanh ghi dch cng ni tip. Khi ttc 8 bit c nhp vo th xy ra:

    Bit th 9 (bit dng) c nhp vo RB8 trong SCON.

    SBUF c np vi 8 bit d liu, v

    C ngt ca b thu (RI) c t ln 1.

    Tuy nhin cc iu trn ch xy ra nu c cc iu kin sau:

    RI = 0, v

    SM2 = 1, v bit dng nhn c = 1, hoc SM2=0.

    Yu cu RI = 0 bo m rng phn mm c k t trc (v xa RI).iu kin th 2 th hi phc tp, nhng ch p dng trong ch truyn thnga x l. N hm { Khng c t RI ln 1 trong ch truyn thng a x lkhi bit d liu th chn l 0.

    23

  • B mn K Thut in T - HBK

    Ch 2: UART 9 bit c tc baud c nh

    Khi SM1 =0 v SM0 = 1, cng ni tip hot ng ch 2 nh UART 9 bit vi tc baud c nh. Mi mt bit c pht hoc thu: 1 bit bt u, 8 bit

    d liu, 1 bit d liu th 9 lp trnh c, v 1 bit dng. Khi pht, bit th 9 l ni dung ca bit TB8 trong

    SCON. Khi thu, bit th 9 thu c c t trong RB8. Tc baud ch 2 l 1/32 hay 1/64 tn s mch

    dao ng trn chip (xem thm mc tc baud cngni tip).

    24

  • B mn K Thut in T - HBK

    Ch 3: UART 9 bit c tc baud thay i c

    Ch 3 ging vi ch 2 ngoi tr tc baud c lp trnh v c cung cp bi timer.

    Tht ra cc ch 1, 2, v 3 rt ging nhau. Skhc bit l tc baud (c nh trong ch 2, thay i c trong ch 1 v 3) v s bit dliu (8 trong ch 1, 9 trong ch 2 v 3)

    25

  • B mn K Thut in T - HBK

    Khi to tr v truy cp cc thanh ghi cng ni tip

    Cc tc v vi cng ni tip

    Cho php b thu

    Truyn (thu/pht) d liu ni tip

    Thm bit kim tra chn l

    Cc c ngt

    t tc baud cho cng ni tip

    Truyn thng a x l

    26

  • B mn K Thut in T - HBK

    S dng cc bit trong thanh ghi SCON

    Cho php b thu (REN bit 4)

    SETB REN Hoc

    MOV SCON, #xxx1xxxxB ; vi x=0/1

    Cc c ngt RI, TI: Cc c ny c bt ln bi phn cng nhng phi c xa bng phn mm.

    RI = 1: B m nhn y; TI = 1: B m pht rng

    27

    Cho php b thu Truyn d liu ni tip

    1. Thu ni tip:

    NHAN: JNB RI, $

    CLR RI

    MOV A, SBUF

    RET

    2. Pht ni tip:

    PHAT: JNB TI, $

    CLR TI

    MOV SBUF,A

    RET

  • B mn K Thut in T - HBK

    bit kim tra chn l - TB8 kim tra chn/l (nu khng c dng (*) l chn)

    ; qua bit nh C

    MOV C, P

    CPL C ; kim tra l (*)

    MOV TB8, C

    MOV SBUF, A

    ; ch 1: 7-bit + 1 parity-bit

    CLR ACC.7

    MOV C, P

    MOV ACC.7, C

    MOV SBUF, A

    Nu Parity lun lun bng 1 c 2 bit dng

    28

    Thm bit kim tra chn l

  • B mn K Thut in T - HBK

    t tc baud cho cng ni tip

    29

    Cch t SMOD ln 1MOV A,PCON

    SETB ACC.7

    MOV PCON,A

  • B mn K Thut in T - HBK

    S dng Timer 1 lm xung nhp tc baud

    Khi to tr TMOD ch timer 8 bit t ng np li tr u (ch 2 ca timer) v t gi tr np li vo TH1 cho tc trn bng tc baud. TMOD c khi to tr nh sau:

    MOVTMOD, #0010xxxxB

    vi x c gi tr 0 hay 1 cn cho Timer 0.

    t tc baud rt thp bng cch dng Timer 1 ch 16 bit , vi TMOD = #0001xxxxB. Tuy nhin phi khi to tr li cc thanh ghi TL1/TH1 sau mi ln trn. Vic ny s c thc hin trong chng trnh phc v ngt.

    Mt la chn khc l cung cp xung nhp Timer1 bng xung nhp bn ngoi qua chn T1 (P3.5). Tc baud l 1/32 tc trn ca Timer 1.

    Do cng thc xc nh tc baud trong cc ch 1 v 3 l

    Tc baud = 1/32 Tc trn ca Timer 1

    Th d: hot ng 1200 baud cn tc trn nh sau

    1200 = 1/32 Tc trn ca Timer 1

    Tc trn ca Timer 1 = 32 x 1200 = 38400 = 38.4 KHz -> Toverflow = 1/38.4 kHz = 26.04 s

    Vi XTAL=12 MHz -> TCLK = 1 s. Overflow cn Toverflow/TCLK 26 clocks

    Gi tr np cho timer 1 l -26

    30

  • B mn K Thut in T - HBK

    Tm tt tc baud

    31

  • B mn K Thut in T - HBK

    Th d 1: Khi to tr cho cng ni tip

    Vit chui lnh khi to tr cng ni tip hot ng nh UART 8 bit vi tc 2400 baud. S dng Timer 1 cung cp xung nhp tc baud.

    Bi gii.

    Vi th d ny, c 4 thanh ghi phi c khi to tr l: SCON, TMOD, TCON, v TH1. Gi tr cn khi to tr cho cc thanh ghi nh sau

    32

    ORG 8100H

    INIT: MOV SCON, #52H

    MOV TMOD, #20H

    MOV TH1, #-13

    SETB TR1

    END

Recommended

View more >