unit las_level_2; { Unit Delphi 2.0 per comunicazione tramite link hardware tipo LAS comandato via LPT1. Program by Claudio Fin 1-10-2000 !------------------------------------------------! ! Utilizzo bit porta LPT: ! ! ! ! Pin Segnale Collegato con ! ! --- ------------ --------------------------! ! 2 D0 (out) TX ! ! 3 D1 (out) CLOCK (normalmente a 0) ! ! 4 D2 (out) STROBE (normalmente a 0) ! ! 10 ACK (in) BR2 ! ! 11 /BUSY (in) BR1 ! ! 12 PO (in) RX ! ! 18 GND GND ! !------------------------------------------------! USO COME MASTER: ---------------- Istanziare l'oggetto: var L:Tlas; L.init(true); Inviare un reset: L.resetchannel; Controllare lo stato: L.stat; ritorna: R=stato di reset O=canale occupato L=canale libero P=dato pronto Inviare i dati: L.tx(b1,b2); con b1 da 0 a 255 e b2 da 0 a 63 i 2 bit alti di b2 sono usati per la segnalazione (br1=bit D7 di b2 e br2=bit D6 di b2) Ricevere i dati: L.rx(b1,b2); USO COME SLAVE: --------------- Istanziare l'oggetto: var L:Tlas; L.init(false); Controllare lo stato: L.stat; Rispondere al reset: L.freechannel; Ricevere i dati: L.rx(b1,b2); Inviare i dati: L.tx(b1,b2); } interface const outport=888; //indirizzo output LPT inport=889; //indirizzo input LPT type Tlas = object m:boolean; //true se master, false se slave d:integer; //byte in scrittura/lettura su lpt function inp(ADDR:word):byte; procedure out(ADDR:word; B:byte); procedure clockpulse; procedure strobepulse; procedure resetchannel; procedure freechannel; procedure init(a:boolean); function stat:char; //L(ibero) O(ccupato) R(eset) P(ronto) procedure tx(b1,b2:integer); procedure rx(var b1,b2:integer); end; implementation {----------------------------------------------------------------------------} { Lettura valore da porta {----------------------------------------------------------------------------} function Tlas.inp(ADDR:word):byte; var B:byte; begin asm mov dx,ADDR in al,dx mov B,al end; result:=B; end; {----------------------------------------------------------------------------} { Scrittura valore su porta {----------------------------------------------------------------------------} procedure Tlas.out(ADDR:word; B:byte); begin asm mov dx,ADDR mov al,B out dx,al end; end; {----------------------------------------------------------------------------} { Emissione impulso di clock {----------------------------------------------------------------------------} procedure Tlas.clockpulse; begin d:=d or 2; out(outport,d); d:=d and 1; out(outport,d); end; {----------------------------------------------------------------------------} { Emissione impulso di strobe {----------------------------------------------------------------------------} procedure Tlas.strobepulse; begin d:=d or 4; out(outport,d); d:=d and 1; out(outport,d); end; {----------------------------------------------------------------------------} { Libera il canale scrivendo 00 nei bit br1 br2 {----------------------------------------------------------------------------} procedure Tlas.freechannel; var h:integer; begin d:=0; out(outport,d); for h:=1 to 8 do clockpulse; strobepulse; end; {----------------------------------------------------------------------------} { Comanda un reset scrivendo 11 nei bit br1 br2 {----------------------------------------------------------------------------} procedure Tlas.resetchannel; var h:integer; begin d:=1; out(outport,d); clockpulse; clockpulse; d:=0; out(outport,d); for h:=1 to 6 do clockpulse; strobepulse; end; {----------------------------------------------------------------------------} { Inizializza l'oggetto {----------------------------------------------------------------------------} procedure Tlas.init(a:boolean); begin m:=a; end; {----------------------------------------------------------------------------} { Controlla lo stato del canale. Il master considera br1 a 1 come indicazione { di occupato e br2 come dato pronto. Lo slave considera br1 a 1 come dato { pronto e br2 come occupato. 11 č un reset, 00 č canale libero. {----------------------------------------------------------------------------} function Tlas.stat:char; begin d:=(inp(inport) and 192 xor 128) div 64; case d of 0 : result:='L'; 3 : result:='R'; 2 : if m then result:='O' else result:='P'; 1 : if m then result:='P' else result:='O'; end; end; {----------------------------------------------------------------------------} { Trasmette i bytes b1 e b2 su link. Il primo bit trasmesso č il bit D7 di b1, { l'ultimo il bit D0 di b2. I 2 bit alti di b2 sono br1 br2, perciņ i bit { usabili in b2 per trasportare l'informazione sono solo 6 (valore da 0 a 63). {----------------------------------------------------------------------------} procedure Tlas.tx(b1,b2:integer); var h:integer; begin if m then b2:=b2 and 63 or 128 else b2:=b2 and 63 or 64; //imposta br1 br2 for h:=1 to 8 do begin d:=b1 and 128; if d=128 then d:=1; out(outport,d); clockpulse; b1 := b1 * 2; end; for h:=1 to 8 do begin d:=b2 and 128; if d=128 then d:=1; out(outport,d); clockpulse; b2:=b2 * 2; end; strobepulse; end; {----------------------------------------------------------------------------} { Legge i bytes b1 e b2 dal link. Il primo bit ricevuto č il bit D7 di b1, { l'ultimo il bit D0 di b2. {----------------------------------------------------------------------------} procedure Tlas.rx(var b1,b2:integer); var h:integer; begin b1:=0; for h:=1 to 8 do begin b1:=b1 * 2; d:=inp(inport) and 32; if d<>0 then b1:=b1 or 1; d:=0; clockpulse; end; b2:=0; for h:=1 to 8 do begin b2:=b2 * 2; d:=inp(inport) and 32; if d<>0 then b2:=b2 or 1; d:=0; clockpulse; end; b2:=b2 and 63; //maschera br1 br2 freechannel; end; {----------------------------------------------------------------------------} end.