You are on page 1of 15
TRUONG DAI HOC BACH KHOA TP. HCM KHOA CO’ KHi Bai tap Ion KY THUAT GIAO TIEP MAY TINH 20701122 20702358 Sinh vién : LE TRUNG KHOA PHAM BA THO DO NGUYEN TIEN THONG 20702358 : CKO7CD2 TP. HCM, 12-2010 KY THUAT GIAO TI DE BAI Sir dung chudn truyén RS232, thiét ké mach giao tiép gitta cng COM may tinh va vi diéu khién, Miy tinh: Vigt chuomg trinh xudt/nhap dir ligu d& digu khin cde eo cd chap hinh duge gin véi kkhign duge gin véi 01 man hinh LCD va 01 bin phim 16 mit Neuyén tic hoat dong: 1. Khi user g@ vio I chit hoje 1 chuai ki tu trén may tinh va bim niit Send thi chit hodc chudi ki tue nay s@ hign lén trén LCD. 2. Khi user nhan 1 phim bat ki trén ban phim 16 nat thi két qua phim nao duge nhin s8 duge hién thi trén LCD va trén giao dign ciia chuong trinh trén méy tink. [2] KY THUAT GIAO TIEP MAY TINH PHAN THUC HIEN 1. CAu trite céng ndi tiép tip duge sir dug 48 truyén dt liu hai chigu gita may tinh va ngoat vi, e6 cée i ~ Khoaing cdch truyén xa hon truyén song song, = 86 day két ndi it = 6 thé truyén khong day ding hing ngoai = Cé thé ghép ndi véi vi digu khién hay PLC (Programmable Logic Device). ~ Cho phép néi mang, = C6 thé thio lip thidt bj trong lie méy tinh dang lam vige. ~ Cé thé cung cép ngudn cho cée mach dign don gin Cée thigt bi ghép ndi chia thinh 2 logi: DTE (Data Terminal Equipment) vi DCE (Data Communication Equipment). DCE li eae thigt bj trung gian nhs MODEM cén DTE li cde thigt bi j inh, PLC, vi diéu khién, ... Vige trao di tin higu thong thuémg qua 2 chan RxD (nbn) va TxD (truyén). Céc tin higu cdn Iai c6 chire ning ha tro dé thiét lgp va digu khién qua trinh truyén, durge goi la céc tin higu bit tay (handshake). Uu diém ‘cla qué trinh truyén diing tin higu bit tay kd c6 thé kiém soat during truyén, theo chudn RS-232 ciia EIA (Electronics Industry Associations). V dén -25V (mark), mie logic 0 img dong tir 10. mA dn 20 mA. Chun RS-232 quy dinh mite logie 1 ting vi dign dp tr ‘Gi dign ap tir 3V dén 25V (space) va c6 kha ning cung Ngoai ra, tit cd ede ngd ra déu e6 dac tinh chéng chip mach. Chuan RS-232 cho phép truyén tin hhigu v6i tbe 46 dén 20.000 bps nhung néu edp truyén di ngin 06 thé lén dén 115.200 bps. (Cée phurong thie néi gitta DTE va DCE: - Don cng (simplex connection): dit ligu chi durge truyén theo 1 husng. ~ Ban song céng ( half-duplex): dit ligu truyén theo 2 hudng, nhung mai thai diém chi duge truyén theo 1 hudng. 3] KY THUAT GIAO TIEP MAY TINH ~ Song c6ng (full-duplex): s6 ligu duge truyén ding théi theo 2 hudng. Dinh dang ctia khung truyén dir ligu theo chudin RS-232 nhur sau: stat [D0 [or [o2 [ps [ps [ps [pe [p7_[P [Stop 0 1 Khi khong truyén di figu, dudmg truyén s& 6 trang théi mark (dign ép -1OV). Khi bit du truyén, DTE & dua ra xung Start (space: 10V) va sau d6 ln lugt truyén t DO dn D7 va Parity, cubi cing 14 xung Stop (mark: -10V) dé khdi phyc trang thai dung truyén. Dang tin higu truyén md. 1 mur sau (truyen ky ty A) ts i: 7 = 3 7 Sate cE me #n Ty pp srentome | = seein ~~ 7 — am acketcomerrecing tobe ASC chaacter A Tin higu truyén eda ky tu ‘A’ Cée dic tinh kj thuat eda chuan RS-232 nhur sau: (Chidu dit eable eye dai | 15m Toe dG dit ligu ewe dai_|20 Kbps. Dign Ap ngd ra cue dai |= 25V Dign dp ngd racé wai | + SV dén = 15V Tro king tai 3K dén 7K Dign dp ngd vio. =15V ‘DG nhay ngd vio Vv. Tro khing ngd vio [3K d&n 7K 4 KY THUAT GIAO TIEP MAY TINH Cée tée 46 truyén ait liu thong dung trong céng ndi tigp la: 1200 bps, 4800 bps, 9600 bps va 19200 bps. So dé chan céng néi tiép ‘Céng COM ¢6 hai dang: dau ndi DB2S (25 chan) va dau néi DB9 (9 chan) m6 ta nh hinh trén, ‘Y nghia cia ede chin md ta nhur sau: (3 KY THUAT GIAO TIEP MAY TINH Hung Mou - Protected ground: abi Git bao ve TxD |DTEDDCE_| Transmitted data: dt ligu wuyén RxD | DCEDTE | Received data: df ligu nbn RTS |DTESDCE | Request o send: DTE yéu clu truyén dt liga CIS | DCE>DTE__|Clear to send: DCE sin sing nhin di ligu DSR | DCEDTE | Data set ready: DCE sin sing lam vige 3 ‘Ground: ndi dit (OV) DCD __ | DCESDTE_| Data carier detect: DCE phat hign sing mang DTR | DTESDCE | Data terminal ready: DTE sin sing lim vige RI DCEDTE | Ring indicator: bio chung DSRD | DCEDTE | Data signal rate detector: db ted ruyén TSET —[DTESDCE | Transmit Signal Element Timing: tin higu diah hot truyén di tir DTE 1S [>] TSET |DCESDTE | Transmitter Signal Element Timing: tn higu dinh hoi truyén tie DCE dé truyén di Tiga 17 |= [RSET [DCESDTE | Receiver Signal Element Timing: tn higu dink thot truyén tie DCE dé truyén oi igu 1s |= [oe ‘Local Loopback: kiém tra cing 21 [= [RL |DCESDTE | Remote Loopback: Teo ra boi DCE Khi tin higu nin ti DCE Ii 14 |= _[StxD | DTESDCE_| Secondary Transmitted Data 16 |= [RxD | DCESDTE _| Secondary Received Data 19_|- [SRT | DTE->DCE_| Secondary Request To Send 13_|-_[SCTS [DCESDTE_| Secondary Clear To Send 12_|-[SDSRD | DCESDTE | Secondary Received Line Signal Detector 25_[-_|™ ‘Test Mode of ‘Dank rigng cho ehé a5 test 10 | Dinh riéng cho ché d6 test im ‘King ding. 2. Truyén thong giita hai mit ‘Cie so dd khi két ndi ding céng néi tiép: TxD b<| TxD ‘TxD |} —f TxD RxD RxD RxD fxd GND GND GND GND DTEI pred DTE DcE Két ndi don gian trong truyén thong ndi tiép He KY THUAT GIAO TIEP MAY TINH Khi thye hign két néi nur trén, qué trinh truyén phai bao dim téc d6 6 dau phat va thu gidng nha, Khi o6 dlr ligu dén DTE, dit ig nay s& durge dura vio b@ dm va tao net, Ngoai ra, khi thue hign két ndi gitta hai DTE, ta cn ding so dd sau: Bp Lr RxD RxD GND GND Ris |_____) rts crs cts Dsk Dsk pep [7] peo bre DIR DTEI pTE2 Két ndi trong truyn thong néi tip ding tin higu bit tay Khi DTE1 cdn truyén dit ligu thi cho DTR tich eye > téc dng lén DSR ciia DTE2 cho bit sin sing nhan dir ligu va cho biét d& nhin duge song mang cia MODEM (ao). Sau d6, DTE! tich cue chan RTS dé tae dng dén chan CTS ciia DTE2 cho biét DTE! e6 thé nhfin dit ligu. Khi thye hign két néi gta DTE va DCE, do téc 46 truyén kh nhau nén phai thye hign diéu khién hu Iugng. Qué trinh diéu khién nay c6 thé thye hign bing phin mém hay phn cig, Qua trinh digu khién bing phan ute hign bang hai ky tw Xon vi Xoff. Ky tw Xon duge DCE géi di khi ranh (c6 thé nhan dir ligu). Néu DCE ban thi sé g6i ky tw Xoff. Qua trinh diéu khién bang phin cimg ding hai chin RTS va CTS. Néu DTE muén truyén dit ligu thi sé goi RTS dé yéu céu truyén, DCE néu c6 kha ning nbn dit ligu (dang ranh) thi géi lai CTS. 3. Truy xuat true tiép théng qua cong: Cie cing ndi tiép trong may tinh duge danh sé la COM1, COM2, COM3, COM4 véi cde dia chi hur sau: ‘Tén_| Dja chi | Neit | Vj ri chia dja chi COMI [3F8h_ [4 | 0000h:0400h COM2 [2F8h [3 | 0000h:0402h. ‘COM3 [3E8h [4 | 0000h:0404h_ ‘COMA [2E8h [3 | 0000h:0406h Mot sb gif ri thong dung xée dinh te 48 trun cho nh sa 3] KY THUAT GIAO TIEP MAY TINH Tosa (ops) [BRDA] BRDL 1,200 [00h [60h 2.400 [00h [30 800 Toh —[ 18h 9,500 [00 [oc 19.200 [00h 06h 38.400 [00h [03h 57,600 | 00h 02h 115,200 [00h [oth SBCB (Set Break Control Bit) =I: cho phép truyén tin higu Break (0) trong khong thei gian lin hon mot khung PS (Parity Select) PS2 [PSI] PSO] Mota Khong x _|x_[o nia 0 [0 [1 | Kignm ra le oir 1_jo [1 1 (i {a Kigm ra chia Parity fa mark arity 1 space STB (Stop Bit) = 0: 1 bit stop, =1: 1.5 bit stop (Ki ding 5 bit dr ligu) hay 2 bit stop (khi ding 6, 7, 8 bit dit igu), WLS (Word Length Select): WES! | WLS0 [D6 dil di ligu | 0 [0 [sbi | 0 |) [ee To [rei T[e [abit 4, Giao tiép véi vi digu khién Khi thyc hign giao tiép voi vi diéu khién, ta phai ding thém mach chuyén mite logic tir TTL A 232 vi ngurgc Iai. Céc vi mach thurimg sit dung la MAX232 cia Maxim hay DS275 ctia Dallas. Mach chuyén mite logic m6 ta nhur sau: HE KY THUAT GIAO TIEP MAY TINH oe cel oe eye Se eal ] Mach chuyén mite logic TTL «+ RS232 ‘Tuy nhién, khi sir dung mgch chuyén mite logic diing céc vi mach thi di hdi phai ding chung GND gita may tinh va vi mach — 6 kha nang lim héng cng néi tiép Khi xay ra hign tuong chp mach & mach ngoai. Do dé, ta c6 thé ding thém opto 4N35 dé cach ly vé din. Sor d@ mach ccéch ly m6 ta nhur sau: Mach chuyén mite logic TTL «+ RS232 cfch ly Khi giao tiép, vi diéu khign chinh la mt DTE nén sé ni RxD ciia may tinh voi TxD cua vi dic khign va nguge la Mech ket nbi gta vi didu ki ‘va méy tinh nhur sau: 3] KY THUAT GIAO TIEP MAY TINH ‘Trong so dé mach trén 6 2 chin 25,26 duge ndi voi max232 dé giao tiép voi may tinh. Code chuong trinh didu khién: Hinclude <16£077a.b> #EUSES NONOT, HS, NOFUS, NOEROTECT, NODEBUG, NOBROWNOUE, NOLVE, NOCED, NORE thee delay (clook-20000000) fee £5232 (paude9600,parity=txmitepin_c6,xovepin_©7) Hnelude [Hinclide char const kitu[]=" 1234967890"#"; int key=0, vitei=0; int int rem{1617 void memory (int value, vited) 1 we KY THUAT GIAO TIEP MAY TINH ram[vitei]=value; 1 #INT_TIMeRt void Sendpata() 1 char c Eprints ("onLine\n\r pute(e delay_ms (100) #INT_RDA vorD RECEVICE() t char ys yegeteOs eprint t (yl: void quet) {int be10; while (1) i output_a(oy; output_high (pin_DS) if (input (PIW_D1)) Af (input (PIN_D2)) if (input (PIN_D3)) Af (input (PIN_D4)) output_d(oys eutput_high (pin_D6); Gf (input (IN_DN)) Af (input (PIN_D2)) Af (input (PIN_D3)) Af (input (PIN_D4)) output_a (0); output_high (pin_D7); is aE ie if void main( 1 (inpue (P1_D1)) (input (PIN_D2)) (input (PIN_D3)) (input (IN_p4)) ( key-11;break; } key=2;breaks) key-S:break;) key=3:breaks) key-6;break;) key=9;break;) Key=12;break; } KY THUAT GIAO TIEP MAY TINH ’ enable_interrupts (global) ; enable_interrupts [INT_RDA) + enable_interrupts (INT_TIMER1) : setup_Ciner_1(T1_INTERNAL | T1_DIV_BY_8); set_ci meri (15536) led_inits while (true) t quet (0 led_put e(kitutkey) + deLay_ms (15); 1 5. Thiét ké giao dién trén may tinh Ta sir dung ngon ngtt lip trinh C# dé thiét ké giao dign, ‘Dau tign, ta tao 1 giao dign (form) nhu sau: ‘Va day 1d code ca giao dign trén: ueing using using using using using ueing using using using system; systen. system. system. System. System system. system. system. system. Collections.ceneric; Componenttiodel Datar Drawings Drawing, Drawing2D; Lings Text; Windows. Forms; To.Ports; namespace RS232 t public partial class Forml i FORM private int{] values = new int{200)+ rival Priva’ ue int count = sce int maxVal = 200; HE KY THUAT GIAO MAY TINH public Forml() fl InitializeComponent 0+ , private void Forml_toad_i (object sender, Hventarss e) a if (serialPort1.1s0pen) serialPort1.Close(); serialPort!.Open()# , private void Forml_FormClosed (object sender, sormclosedsventargs e) ni Af (seriaiPortt.1sopen) serialeorti.closet); , private void btnWeite Click(object sender, SventArgs e) i byte data = (byte) convert. Tornt16(tatwrite.Text, 10); serialPorti.Write(new byte[] { data ), 0, id drawhine(): ’ private void serialport1_DataReceived (object sender, Serlalbatakeceivedzventarge e) ( byte nhan = (byte) serialPort1.Readayte (): object {] para = ( nhan }y txtRead.BeginInvoke (new deleFunc (Display), para); , Me Ham con nién th} do 1igu-- private delegate void delerunc {byte mhan)? private void Display (byce han) a string receive = Convert.ToSteing(nhan, 1D); tutRead.AppendText {receive +" "); values (count++] = (intyabany redraw |) , private void txtWrite TextChanged(object sender, EventArgs e) i , private void btnClear_Click (object sender, SventArgs e) i tutRead-text =" , U1 nam ve do thi private void redraw() ci Graphics g = panell.CreateGraphies (Vs int width = panell.Widthy EE KY THUAT GIAO TIEP MAY TINH height - space * 2 yFactor): space / 2, Ant height = panel1.Heights int space = 10; g.FillRectangle (Srushes.White, D, 0, width, height); g.DrawRectangle(Pens.Black, space, space, width - space * 2, double yFactor = (height - space) / (maxVal + 1.06); g.Smoothinglode = snocthingviode.wighQualitys Float x, y, lastx, last¥; y = (Float) ((max¥al = (float) Int32.Parse (txtWrite.Text)) * g.DrawLine(2ens.DarkBlue, space, y, width - space, y) i Y8 c&e vach trye float yFactorDefault = (height - space * 2) / 20; for (float } = 0; 3 <= 20; j#4) ‘ g.Drawhine(Pens.Black, space, space + j * yFactorDefault, space | 3 * yFactorDefault); ) Te Ye ce vach trye Xo TT float xFactorDefault = (float) (width - space * 2) / 100; for (float 1] = 0; 3) <= 100; 33+) ' g-Drawhine(Pens.Black, space + 3j * xfactorDefault, height - space, space + 3} * xFactorDefault, height - space / 2); , ) Me double step = (double) (width - space / 2) / values.Length; lastx = (float) space; lastY = (float) (yFactor * (maxVal - values{01))+ for int i= 1y 4 < county i++) { x = (float) (space + i * step); y = (float) (yFactor * (maxval ~ values(il)); g-Drawhine(Pens.DarkBlue, lastx, last¥, x, y)i lastx = x; lastY = yi ) g-Dispose(); private void drawLine() ni height - space * 2 yeactor): Graphics g = panell.CreateGraphics(); int width = panell.widths Ant height = paneli.Heights Float space = 5 g-FillRectangle (rushes.White, D, 0, width, height); g.DrawRectangle(Pens.Black, space, space, width ~ space * 2, double yFactor = (height - space) / (maxval + 1.06); g.Smoothingode = snocthingiiode Mighgualitys Float y = (Float) ({maxval ~ (£1Gat) ints? Parse (tutWrite.Text)) * g.Dranbine(Pens.DarkBlue, space, y, width - space, y)i g.Dispose(); uw KY THUAT GIAO TIEP MAY TINH private fl , private a , private n , private fi , private i ’ privace a ’ void void void void void void panell_LocationChanged (object sender, Sventargs e) paneli_Paint (object sender, a1 text#ox13_TextChanged (object sender, EventArgs e} textBox1§_TextChanged (object sender, EventArgs e) labell_CLick (object sender, SventAras e) label2_Click (object sender, Hventarss e) HE KY THUAT GIAO TIEP MAY TINH

You might also like