trịnh phi hùng

  • Published on
    29-Dec-2014

  • View
    67

  • Download
    4

Embed Size (px)

Transcript

<p>Hc Vin Cng Ngh Bu Chnh Vin Thng C s ti thnh ph H Ch Minh</p> <p> N MN HC Mn hc: K thut ghp ni v thit b ngoi vi</p> <p> ti : o nhit v t gi ti nc cho vn Gio vin hng dn : Ths. Phm Th Duy H v tn sinh vin: 1. Trnh Phi Hng MSSV : 409190009 2. Nguyn ng Khoa MSSV : 409190011</p> <p>H ch minh nm 2013 ----------</p> <p>-1-</p> <p>MC LC trang Li ni u M t cng vic v l do chn ti: 1. L do chn ti 2. M t ti Tm hiu linh kin v cc thit b trong ti 1. MCU Atmega32 a. c im b. Cc I/O c. Chc nng ADC d. Giao tip s dng I2C e. Giao tip USART 2. IC thi gian thc DS1307 a. Cu to ngoi v c im b. Cc thanh ghi ca IC c. Giao tip I2C vi AVR 3. Cm bin m DHT-11 a. c im k thut v cu to chn linh kin b. Nguyn l hot ng 4. Cm bin nhit LM35 a. c im k thut v cu to chn linh kin b. Nguyn l hot ng 5. LCD 1602 a. c im v cu to ngoi LCD 1602 b. Cc hm iu khin LCD 6. Truyn nhn d liu qua cng ni tip a. Cu trc cng ni tip b. Cng RS-232 trn PC c. Truyn d liu d. Nhn d liu 7. IC chuyn i tn hiu USART-RS-232 MAX 232 a. c im v s chn IC MAX 232 b. Nguyn l hot ng 8. USB-COM vi PL2303 a. c im v s chn IC PL2303 b. Nguyn l hot ng 9. Kt ni mng RS-485 a. Khi nim a mng b. Chun RS-485 c. Mt s vn lin quan n RS-485 d. Cc kiu truyn nhn trong mng RS-485 Thit k phn cng 1. Khi x l v hin th 2. Khi thi gian thc 3. Khi giao tip my tnh -2-</p> <p>Thit k phn mm 1. Gii thut cho my tnh 2. Gii thut cho vi iu khin a. Lu chng trnh chnh b. Chng trnh hin th thi gian v nhit c. Gii thut hin th gi ti hng ngy d. Gii thut chnh thi gian thc Kt qu thi cng 1. Hnh nh 2. Hn ch 3. Hng gii quyt tip theo</p> <p>-3-</p> <p>Li ni u Vi mt lng ln dn s trong nc l nng dn v nhng nm gn y cc m hnh nh VAC, VACR, trang tri, nh knh, c cc h dn quy hoch vi din tch c th ln n hng trm hecta, ngoi ra cc nhn lc lm trong ngnh nng nghip thng cao tui. Khi gnh nng cng vic t ra lm sao c mt m hnh ti cy, rau, vn m mt cch hon ton t ng hoc bn t ng l mc tiu cho chng em chn ti ny. Hi vng vi m hnh ti cy s gim c phn no cng vic cho cc h gia nh v nng cao nng sut cy trng, t c gi tr thng phm cao hn. Tuy nhin do hn ch ca ti ch dng li mc nghin cu n gin v tm hiu nn nhm em mong c s hng dn v gp thm chng em hoang thnh ti trong tng lai. Khng thy my lm nn . Em xin cm n tt c cc thy, c trong khoa in t 2 gip chng em trong thi gian hc tp ti trng v gp cho em nhng kin qu bu. c bit em xin cm n thy Phm Th Duy v thy Tn Tht Bo t l gio vin b mn v giao vin hng dn khuyn khch v gip chng em trong qu trnh thit k v thi cng ti.</p> <p>-4-</p> <p>I.</p> <p>Cm bin m</p> <p>analog I/O</p> <p>ADC</p> <p>M T L DO V MC TIU CNG VIC: L do chn ti: Nhm em chn ti m hnh ti cy t ng v cm bin nhit v m vi l do: Cng vic nh nng rt vt v v nhiu cng on Xu hng trng ra oan ton trn nhng cnh ng ln ang c trin khai v nhn rng khp cc tnh thnh - i tng lm nng ch yu l ngi ln tui v vy mun gim gnh nng cng vic cho khi lm nng - M hnh c th p dng cc cnh ng rau, cc vn cy n qu hoc cc vn cy cng nghip. 2. M t ti: - M hnh hot ng da trn ng h thi gian thc, chnh xc cao, t t gi ti v cc thng s khc (ln ti, thi lng ti) mt cch chnh xc. - Hot ng hon ton t ng, ch cn t mt ln v hot ng cho cc ln k tip. - Cc cm bin m v nhit gip thu thp thng tin vn cy c cc phng n ti ph hp. - C giao tip my tnh c th cp nhp thng tin v vn cy, rau qua giao din my tnh. - S khi: 1/0 LCD 1602 I/O analog LM35 1/0 DS1307 I2C 1. -</p> <p>1/0</p> <p>p ng(vi ti)</p> <p>MCU (ATmega8 hoc 32) RS-232 USART Cc phm bm 1/0 I/O My tnh</p> <p>Vcc,Vref,GND</p> <p>5V DC</p> <p>Ngun</p> <p>M t: MCU (atmega8 hoc atmega32): lm b x l trung tm cc tn hiu s, tng t hin th v thc thi chng trnh chnh v lm mt s giao tip vi IC khc hoc vi my tnh c nhn. IC DS1307: IC thi gian thc c giao tip vi MCU qua giao tip I2C. -5-</p> <p>-</p> <p>Cc sensor: dng lm chc nng o cc thng s mi trng ri gi cc tn hiu tng t ti b ADC ca MCU chuyn i v hin th n mc ch kim sot v iu khin. Cc phm bm: dng khi ta c thao tc iu khin v ci t cc thng s bng tay trn mch. Lcd 1602 : hin th cc thng s(thi gian, ci t ti, nhit ) khi thc hin t thng s bng tay. Vi ti: c dng ng m ci relay khi n thi gian ti nh sn. My tnh: giao tip vi cc mch bn ngoi v iu khin cc mch. N cn c dng qun l cc vn khc nhau. Ngun: (ngun cho mch): s dng in p DC5V cc IC, MCU, lcd hot ng.</p> <p>II.</p> <p>TM HIU CC LINH KIN THIT B TRONG TI :</p> <p> ti c s dng mt s linh kin in t l cc IC v MCU cng mt s chun giao tip c bn nh USART, I2C, cm bin c s dng nh LM35, cm bin m, hin th bng LCD 1602L, giao tip my tnh c IC MAX232, IC chuyn i USB-COM hoc cable chuyn dng. 1. a. MCU ATmega32: c im: y l dng vi iu khin ca hng Atmel c nhiu tnh nng ni tri Cu trc tp lnh thu gn RISC, n gin, d lp trnh Tit kim nng lng C 32 chn I/O, 4 port xut nhp d liu a chc nng 32 thanh ghi a dng B nh 32Kb b nh Flash lp trnh c, 1024Byte EEPROM Tc ti a ln ti 16MHz, c th s dng c dao ng ni chip v thch anh ngoi 2 b Timer/counter 8 bit, mt b so snh 1 b timer/counter 16 bit 4 knh PWM 8 knh ADC h tr phn gii 10 bit v 8 bit Cc giao tip h tr nh: I2C, SPI, USART in p hot ng 4,5-5 V S chn v ng gi: loi 40 chn PDIP v loi TQFP/MLF (linh kin dn)</p> <p>-6-</p> <p>Chn s 1-8 9 10, 11 12, 13</p> <p>Tn chn PORT B RESET Ngun Chn gn dao ng ngoi</p> <p>Chc nng Port xut nhp B, ngoi ra cn c chc nng l giao tip SPI Chn reset ca atmega 32, chn ny c kch khi c mt tch cc mc thp tc ng ln, khi atmega32 s hot ng li VCC, chn cp ngun dng VCC v GND Hai chn gn thch anh dao ng ngoi ca chip khi s dng, mun s dng c dao ng ngoi ta cn fuse bt chip c set s dng thch anh -7-</p> <p>14-21</p> <p>PORT D</p> <p>22-29</p> <p>Port C Chn ngun Vref Port A</p> <p>30-31 32 33-40</p> <p>Port truy xut I/O, ngoi ra cn c chc nng khc nh: - Pd0-pd1: chn rxd v txd s dng giao tip USART - Pd2-pd3: chn ngt ngoi - Pd4-pd5: ng ra so snh Port truy xut I/O, ngoi ra cn c chc nng khc nh: - Pc0-pc1: chn giao tip i2c Chn acvv v gnd Chn in p so snh (dng trong ch adc) Port truy xut I/O, ngoi ra cn c chc nng l 8 knh adc.</p> <p>b. Cc I/O: - Ng ra cc mng h.</p> <p>DDxn 0 0 0 1 1</p> <p>ATmega32 c 4 PORT xut nhp d liu l: A, B,C,D C 3 thanh ghi chc nng lp trnh vo ra cho cc chn I/O l: DDR, PIN, PORT. Cu hnh I/O theo bng sau: PORTxn 0 1 1 0 1 I/O Input Input Input Output Output Pull-up khng C Khng Khng Khng Chc nng Trng thi Hi-z tr khng cao Ng vo c in tr ko ln Trng thi Hi-z tr khng cao Ng ra mc thp Ng ra mc cao</p> <p>- Thanh ghi PINx nhn cc tn hiu vo. c. Chc nng ADC: - PORT A ca ATmega32 c chc nng ADC kim cng vo ra s c im : phn gii 10 bit hoc 8 bit ty chc nng -8-</p> <p>-</p> <p>8 knh ADC n Tc chuyn i 13s - 260s. 0 Vcc ADC di in p ng vo. Ngt ADC khi chuyn i xong</p> <p>C 4 thanh ghi trong AVR dng chc nng ADC ADMUX: thanh ghi 8 bit dng chn in p tham chiu cho ADC, 1 trong 3 ngun in p tham chiu c th chn l : in p t chn VREF, in p tham chiu ni 2,56V hoc in p VCC theo bng sau:</p> <p>-</p> <p>ADCSRA (ADC Control and Status Register A): l thanh ghi iu khin hot ng v cha trng thi ca module ADC.</p> <p>Bit 7 - ADEN(ADC Enable): vit gi tr 1 vo bit ny tc bn cho php module ADC c s dng. Tuy nhin khi ADEN=1 khng c ngha l ADC hot ng ngay, bn cn set mt bit khc ln 1 bt u qu trnh chuyn i, l bit ADSC. Bit 6 - ADSC(ADC Start Conversion): set bit ny ln 1 l bt u khi ng qu trnh chuyn i. Trong sut qu trnh chuyn i, bit ADSC s c gi nguyn gi tr 1, khi qu trnh chuyn i kt thc (t ng), bit ny s c tr v 0. V vy bn khng cn v cng khng nn vit gi tr 0 vo bit ny bt k tnh hung no. thc hin mt chuyn i, thng thng chng ta s set bit ADEN=1 trc v sau set ADSC=1. Bit 4 ADIF(ADC Interrupt Flag): c bo ngt. Khi mt chuyn i kt thc, bit ny t ng c set ln 1, v th ngi dng cn kim tra gi tr bit ny trc khi thc hin c gi tr chuyn i m bo qu trnh chuyn i thc s hon tt. Bit 3 ADIE(ADC Interrupt Enable): bit cho php ngt, nu bit ny c set bng 1 v bit cho php ngt ton cc (bit I trong thanh ghi trng thi ca chip) c set, mt ngt s xy ra khi mt qu trnh chuyn i ADC kt thc v cc gi tr chuyn i c cp nht (cc gi tr chuyn i cha trong 2 thanh ghi ADCL v ADCH). Bit 2:0 ADPS2:0(ADC Prescaler Select Bits): cc bit chn h s chia xung nhp cho ADC. ADC, cng nh tt c cc module khc trn AVR, cn c gi nhp bng mt ngun xung clock. Xung nhp ny c ly t ngun xung chnh ca chip thng qua mt h s chia. Cc bit -9-</p> <p>-</p> <p>ADPS cho php ngi dng chn h s chia t ngun clock chnh n ADC. Tham kho bng 4 bit cch chn h s chia. ADCL v ADCH (ADC Data Register): hai thanh ghi cha gi tr chuyn i. Do phn gii ca module ADC trn AVR l 10 bit nn cn hai thanh ghi cha gi tr chuyn i. Do tng hai thanh ghi l 16 bit nn bt ADLAR trong thanh ghi ADMUX quy nh cch ghi kt qu vo hai thanh ghi ny:</p> <p>ADLAR =0:</p> <p>ADLAR =1:</p> <p>d. -</p> <p>Giao tip s dng I2C: Giao thc truyn thng n gin, ng bus ch cn hai dy scl v sda 7 bit a ch cho php ln ti 128 thit b slave. Tc truyn d liu ln ti 400kHz.</p> <p>Giao tip I2C l mt ng dng h tr trong nhiu dng vi x l, n cho php thit k h thng kt ni ln n 128 thit b m ch cn hai dy bus, mt dy clock (scl) v mt dy data (sda).</p> <p>- 10 -</p> <p>-</p> <p>-</p> <p>Master: l chip khi ng qu trnh truyn nhn, pht i a ch ca thit b cn giao tip v to xung gi nhp trn ng SCL. Slave : l chip c a ch c nh, c gi bi master v phc v yu cu ca master. Cc thanh ghi s dng giao tip I2C trn AVR gm 5 thanh ghi gm c thanh ghi tc gi nhp TWBR, thanh ghi iu khin TWCR, thanh ghi trng thi TWSR, thanh ghi a ch TWAR, thanh ghi d liu TWDR. TWBR: quy nh tc pht xung gi nhp trn ng SCL ca chip master.</p> <p>Tc xung gi nhp tnh theo cng thc:</p> <p>TWBR : gi tr trong thanh ghi TWBR TWPS : gi tr hai bit TWPS1 v TWPS0 nm trong thanh thi trng thi TWSR TWCR: thanh ghi iu khin hot ng ca I2C:</p> <p>Thanh ghi ny gm 8 bt iu khin cc hot ng ca I2C: TWSR: thanh ghi trng thi 8 bit, trong c 5 bt cha code trng thi ca TWI v 3 bit la chn h s prescaler.</p> <p>- 11 -</p> <p>-</p> <p>TWDR: thanh ghi d liu chnh ca TWI. Trong qu trnh nhn d liu c lu trong TWDR, trong qu trnh gi d liu s c ly v truyn trn ng SDA.</p> <p>-</p> <p>TWAR: thanh ghi cha cc a ch ca cc chip slave.</p> <p>e. -</p> <p>Giao tip USART: Ch truyn song cng (va truyn va nhn) Ch ng b v cn ng b. H tr khung truyn ni tip vi 5,6,7,8 hoc 9 bit d liu v 1,2 stop bit. Bo trn d liu</p> <p>C 5 thanh ghi c dng trong giao tip USART l: UDR: thanh ghi d liu, cha cc d liu nhn v pht i ca USART, thc cht n l hai thanh ghi TXB v RXB c chung a ch.</p> <p>-</p> <p>UCSRA: y l thanh ghi quan trng iu khin USART</p> <p>- 12 -</p> <p>Thanh ghi UCSRA gm cc bit trng thi nh bit bo qu trnh nhn kt thc RXC, truyn kt thc TXC, bo thanh ghi d liu trng UDRE, khung truyn c li FE, d liu trn DOR, kim tra parity c li PE. UCSRB: cng l mt thanh ghi iu khin USART quan trng:</p> <p>-</p> <p>RXCIE (Receive Complete Interrupt Enable) l bit cho php ngt khi qu trnh nhn kt thc. Vic nhn d liu truyn bng phng php ni tip khng ng b thng c thc hin thng qua ngt, v th bit ny thng c set bng 1 khi USART c dung nhn d liu. TXCIE (Transmit Complete Interrupt Enable) bit cho php ngt khi qu trnh truyn kt thc. UDRIE (USART Data Register Empty Interrupt Enable) l bit cho php ngt khi thanh ghi d liu UDR trng. RXEN (Receiver Enable) l mt bit quan trng iu khin b nhn ca USART, kch hot chc nng nhn d liu bn phi set bit ny ln 1. TXEN (Transmitter Enable) l bit iu khin b pht. Set bit ny ln 1 bn s khi ng b pht ca USART. UCSZ2 (Chracter size) bit ny kt hp vi 2 bit khc trong thanh ghi UCSRC quy nh di ca d liu truyn/nhn. Chng ta s kho st chi tit khi tm hiu thanh ghi UCSRC. RXB8 (Receive Data Bit 8) gi l bit d liu 8. Bn nh li rng USART trong AVR c h tr truyn d liu c di ti a 9 bit, trong khi thanh ghi d liu l thanh ghi 8 bit. Do , khi c gi d liu 9 bit c nhn, 8 bit u s cha trong thanh ghi UDR, cn c 1 bit khc ng vai tr bit th chn, RXD8 l bit th chn ny. Bn ch l cc bit c nh s t 0, v th bit th chn s c ch s l 8, v l m bit ny c tn l RXD8 (khng phi RXD9). TXB8 (Transmit Data Bit 8), tng t nh bit RXD8, bit TXB8 cng ng vai tr bit th 9 truyn thng, nhng bit ny c dung trong lc truyn d liu. UCSRC: thanh ghi ny dng quy nh cc thng s v khung truyn v ch truyn</p> <p>UMSEL (USART Mode Select) l bit la chn gia 2 ch truyn thng ng b v khng ng b. Nu UMSEL=0, ch khng ng b c chn, ngc li nu UMSEL=1, ch ng b c kch hot. Xem bng sau:</p> <p>- 13 -</p> <p>Hai bit UPM1 v UPM0( Parity Mode) c dng quy nh kim tra pariry. Nu UPM1:0=00, parity khng c s dng (mode ny kh thng dng), UPM1:0=01 khng c s dng, UPM1:0=10 th parity chn c dng, UPM1:0=11 parity l c s dng xem bng sau:</p> <p>USBS (Stop bit Select), bit Stop trong khung truyn bng AVR USART c th l 1 hoc 2 bit, nu USBS=0 th Stop bit ch l 1 bit trong khi USBS=1 s c 2 Stop bit c dng.</p> <p>Hai bit UCSZ1 v UCSZ2 (Character Size) kt hp vi bit UCSZ2 trong thanh ghi UCSRB to thnh 3 bit quy nh di d liu truyn. Bng 2 tm tt cc gi tr c th c ca t hp 3 bit ny v di d liu truyn tng ng.</p> <p>UCPOL (Clock Pority) l bit ch cc ca xung kch trong ch truyn thng ng b. nu UCPOL=0, d liu s thay i thay i cnh ln ca xung nhp, nu UCPOL=1, d liu thay i cnh xung xung nhp. Nu bn s dng ch truyn thng khng ng b, hy set bit ny bng 0.</p> <p>- 14 -</p> <p>-</p> <p>UBRRL v UBRRH: hai thanh ghi thp v cao quy nh tc baud.</p> <p>Bng tnh tc Baud:</p> <p>Vi mi thch anh gn ngoi c mt sai s truyn tnh bng cng thc:</p> <p>2. IC thi gian thc DS1307: a. Cu to ngoi v c im:</p> <p>Th t chn</p> <p>Chc nng - 15 -</p> <p>1,2 3 4,8 5,6 7 -</p> <p>Chn dao ng, gn thch anh dao ng. Gn ngun PIN ngoi phng khi mt ngun nui Chn ngun Vcc v GND Chn gi xung nhp v d liu trong giao tip I2C Chn cp xung ra ngoi</p> <p>IC c hng DALLAS sn xut v gii thiu. IC thi gian thc (Real Timer Clock) c th s dng m thi gian (gi, pht, giy), lch (th, ngy, thng, nm). - c thit k ti lch ca nm 2100. - 56-byte, dng ngun PIN ngoi phng khi mt in. - Giao tip I2C vi vi iu khin. b. Cc thanh ghi ca IC: - Ds1307 c 64 thanh ghi 8 bit ng du t a ch 00H-3FH, ch 8 thanh ghi c chc nng ng h cn li 56 thanh ghi cn li c chc nng nh b nh RAM ca IC</p> <p>-</p> <p>Ba thanh ghi u t a ch 00H- 02H c chc nng l ng h gm c : SECONDS, MINUTES, HOURS Bn thanh ghi t a ch 03H-06H c chc nng ghi li lch gm: DAY, DATE, MONTH, YEAR Thanh ghi th 8 (CONTROL) c chc nng cp mt xung ra chn s 7.</p> <p>- 16 -</p> <p>T chc cc thanh ghi trong IC ds1307: Thanh ghi giy SECONDS: thanh ghi ny l thanh ghi u tin trong b nh ca DS1307, a ch ca n l 0x00. Bn bit thp ca thanh ghi ny cha m BCD 4-bit ca ch s hng n v ca gi tr giy. Do gi tr cao nht ca ch s hng chc l 5 (khng c giy 60) nn ch cn 3 bit (cc bit SECONDS 6:4) l c th m ha c (s 5 =101, 3 bit). Bit cao nht bit 7 trong thanh ghi ny l 1 bit iu khin c tn CH (Clock halt treo ng h), nu bit ny c set bng 1 b dao ng trong chip b v hiu ha, ng h khng hot ng. V vy, nht thit phi reset bit ny xung 0 ngay t u. - Thanh ghi pht MINUTES: c a ch 0x01, cha gi tr pht ca ng h. Tng t thanh ghi SECONDS, ch c 7 bit ca thanh ghi ny c dng lu m BCD ca pht, bit 7 lun lun bng 0 - Thanh ghi gi HOURS: c th ni y l thanh ghi phc tp nht trong DS1307. Thanh ghi ny c a ch 0x02. Trc ht 4-bits thp ca thanh ghi ny c dng cho ch s hng n v ca gi. Do DS1307 h tr 2 loi h thng hin th gi (gi l mode) l 12h (1h n 12h) v 24h (1h n 24h) gi, bit6 xc lp h thng gi. Nu bit6=0 th h thng 24h c chn, khi 2 bit cao 5 v 4 dng m ha ch s hng chc ca gi tr gi. Do gi tr ln nht ca ch s hng chc trong trng hp ny l 2 (=10, nh phn) nn 2 bit 5 v 4 l m ha. Nu bit6=1 th h thng 12h c chn, vi trng hp ny ch c bit 4 dng m ha ch s hng chc ca gi, bit 5 ch bui trong ngy, AM hoc PM. Bit5 =0 l AM v bit5=1 l PM. Bit 7 lun bng 0. - Thanh ghi th (DAY ngy trong tun): nm a ch 0x03. Thanh ghi DAY ch mang gi tr t 1 n 7 tng ng t Ch nht n th 7 trong 1 tun. V th, ch c 3 bit...</p>