' ******************************************************************* ' * Programma: ENC8254.BAS Versione: 1.1 - 02 Settembre 1999 * ' * Compilatore: BASCOM 8051 (IDE V.1.0.0.16, LIB V.1.10) * ' * Ditta: Grifo(r) Via Dell'Artigiano 8/6 * ' * 40016 San Giorgio di Piano * ' * Tel. +39-051-892052 Fax +39-051-893661 * ' * http://www.grifo.com http://www.grifo.it * ' * sales@grifo.it tech@grifo.it grifo@grifo.it * ' * * ' * Realizzato da: Graziano Gaiba * ' ******************************************************************* ' ' Programma realizzato per la scheda GPC(r) F2, con il BASCOM 8051, ' collegata alla EMI 01. ' Vengono acquisiti due encoder collegati ai connettori CN3 e CN4 della ' EMI 01 ' ' '****************** Direttive del compilatore ************************** ' $romstart = &H8050 ' Indirizzo di partenza del codice macchina $ramstart = &HD000 ' Indirizzo di partenza della area dati $ramsize = &H2800 ' Pongo a 10K la area dati $crystal = 11059200 ' Frequenza di clock del microcontrollore $baud = 9600 ' Velocità di comunicazione RS-232 $large ' Indirizzamento a 16 bit ' '****************** Dichiarazione delle costanti ********************** ' ' Indirizzi Counter 8254 A Dim Cwa As Const &HFA43 ' Indirizzo reg. di stato 8254 A Dim Acnt0 As Const &HFA40 ' Indirizzo reg. dati cont. 0 8254 A Dim Acnt1 As Const &HFA41 ' Indirizzo reg. dati cont. 1 8254 A ' Indirizzi Counter 8254 B Dim Cwb As Const &HFA83 ' Indirizzo reg. di stato 8254 B Dim Bcnt0 As Const &HFA80 ' Indirizzo reg. dati cont. 0 8254 B Dim Bcnt1 As Const &HFA81 ' Indirizzo reg. dati cont. 1 8254 B ' Costanti Dim Cmdw0 As Const &H30 ' Parola di comando per contatore 0: modo 0 Dim Cmdw1 As Const &H70 ' Parola di comando per contatore 1: modo 0 ' '****************** Dichiarazione delle variabili ********************** ' Dim V As Byte , L As Long ' Variabili di uso generico Dim Ea As Long , Eb As Long ' Valore assunto dall'encoder Dim Apc0 As Long , Apc1 As Long ' Variabili usate per calcolare Dim Bpc0 As Long , Bpc1 As Long ' Il valore dell'encoder Dim Ac0 As Long , Ac1 As Long Dim Bc0 As Long , Bc1 As Long Dim Diff_a As Long , Diff_b As Long ' '************************* Programma main ****************************** ' Waitms 1 ' Ritardo per assestamento dei segnali ' Inizializzazione Counters 8254 Out Cwa , Cmdw0 ' Inizializzazione contatore 0 8254 A Out Cwa , Cmdw1 ' Inizializzazione contatore 1 8254 A Out Cwb , Cmdw0 ' Inizializzazione contatore 0 8254 B Out Cwb , Cmdw1 ' Inizializzazione contatore 1 8254 B ' Inizio programma Ea = 0 : Eb = 0 : ' Variabili per valore encoders Print Chr(12) : ' Pulizia video. Print "PROGRAMMA DI LETTURA DI 2 ENCODER NEL RANGE -65536<->+65536" Print "TRAMITE LA EMI01 COLLEGATA ALLA GPC(r) F2" Print : Print ' spaziatura ' Reset dei registri del 8254 A Out Acnt0 , 255 ' Contatore 0 H e Out Acnt0 , 255 ' L a 0. Out Acnt1 , 255 ' Contatore 1 H e Out Acnt1 , 255 ' L a 0. ' Reset dei registri del 8254 B Out Bcnt0 , 255 ' contatore 0 H e Out Bcnt0 , 255 ' L a 0. Out Bcnt1 , 255 ' contatore 1 H e Out Bcnt1 , 255 ' L a 0. Print : Print "PREMERE PER INIZIARE LA LETTURA ENCODER" V = Waitkey ' Attende un tasto Print Chr(12) ' Pulizia video. ' Lettura combinazioni iniziali dei contatori 0 e 1 delle sezioni A e B ' Apc0 = Inp(anct0) + Inp(acnt0) * 256 V = Inp(acnt0) : Apc0 = V V = Inp(acnt0) : L = V * 256 Apc0 = Apc0 + L ' Apc1 = Inp(anct1) + Inp(acnt1) * 256 V = Inp(acnt1) : Apc1 = V V = Inp(acnt1) : L = V * 256 Apc1 = Apc1 + L ' Bpc0 = Inp(bnct0) + Inp(bcnt0) * 256 V = Inp(bcnt0) : Bpc0 = V V = Inp(bcnt0) : L = V * 256 Bpc0 = Bpc0 + L ' Bpc1 = Inp(bnct1) + Inp(bcnt1) * 256 V = Inp(bcnt1) : Bpc1 = V V = Inp(bcnt1) : L = V * 256 Bpc1 = Bpc1 + L Ciclo: ' Inizio ciclo di lettura encoder ' Lettura cont. 0 8254 A ' Ac0 = Inp(anct0) + Inp(acnt0) * 256 V = Inp(acnt0) : Ac0 = V V = Inp(acnt0) : L = V * 256 Ac0 = Ac0 + L ' Lettura cont. 1 8254 A ' Ac1 = Inp(anct1) + Inp(acnt1) * 256 V = Inp(acnt1) : Ac1 = V V = Inp(acnt1) : L = V * 256 Ac1 = Ac1 + L ' Lettura cont. 0 8254 B ' Bc0 = Inp(bnct0) + Inp(bcnt0) * 256 V = Inp(bcnt0) : Bc0 = V V = Inp(bcnt0) : L = V * 256 Bc0 = Bc0 + L ' Lettura cont. 1 8254 B ' Bc1 = Inp(bnct1) + Inp(bcnt1) * 256 V = Inp(bcnt1) : Bc1 = V V = Inp(bcnt1) : L = V * 256 Bc1 = Bc1 + L ' Calcolo variazione rispetto alla condizione precedente ' Diff_a = (apc0 - ac0) - (apc1 - ac1) Diff_a = Apc0 - Ac0 L = Apc1 - Ac1 Diff_a = Diff_a - L 'Diff_b = (bpc0 - bc0) - (bpc1 - bc1) Diff_b = Bpc0 - Bc0 L = Bpc1 - Bc1 Diff_b = Diff_b - L ' Sezione A ************************************************************ ' Se azzeramento di un contatore, calcolo nuova variazione If Abs(diff_a) > 65000 Then Goto Gestreset_a Else Goto Skip_a Gestreset_a: If Diff_a > 0 Then Goto Reset0_a Else Goto Reset1_a ' Determinazione del cont. azzerato Reset0_a: ' Diff_a = -(Apc1 + (65536 -Ac1)) L = Ac1 - 65536 Diff_a = L - Apc1 ' Azzeramento contatore 1 Goto Skip_a Reset1_a: ' Diff_a = Apc0 + (65536 -ac0) L = 65536 - Ac0 Diff_a = Apc0 + L ' Azzeramento contatore 0 Skip_a: Ea = Ea + Diff_a ' Aggiornamento valorer Encoder ' Se valore encoder fuori dal range -65536<>+65536 If Abs(ea) > 65536 Then Goto Gestoverflow_a Else Goto Prosegui Gestoverflow_a: ' Determinazione del limite superato If Ea > 0 Then Goto Sup_a Else Goto Inf_a Sup_a: Ea = Ea - 65536 ' Limite Superiore Goto Prosegui Inf_a: Ea = Ea + 65536 ' Limite inferiore Prosegui: ' Sezione B ************************************************************ ' Se azzeramento di un contatore, calcolo nuova variazione If Abs(diff_b) > 65000 Then Goto Gestreset_b Else Goto Skip_b Gestreset_b: If Diff_b > 0 Then Goto Reset0_b Else Goto Reset1_b ' Determinazione del cont. azzerato Reset0_b: ' Diff_b = -(Bpc1 + (65536 -Bc1)) L = Bc1 - 65536 Diff_b = L - Bpc1 ' Azzeramento contatore 1 Goto Skip_b Reset1_b: ' Diff_b = Bpc0 + (65536 -Bc0) L = 65536 - Bc0 Diff_b = Bpc0 + L ' Azzeramento contatore 0 Skip_b: Eb = Eb + Diff_b ' Aggiornamento valorer Encoder ' Se valore encoder fuori dal range -65536<>+65536 If Abs(eb) > 65536 Then Goto Gestoverflow_b Else Goto Aggiorna Gestoverflow_b: ' Determinazione del limite superato If Eb > 0 Then Goto Sup_b Else Goto Inf_b Sup_b: Eb = Eb - 65536 ' Limite Superiore Goto Aggiorna Inf_b: Eb = Eb + 65536 ' Limite inferiore Aggiorna: ' ********************************************************************** ' Aggiornamento valori precedenti Apc0 = Ac0 : Apc1 = Ac1 Bpc0 = Bc0 : Bpc1 = Bc1 'Visualizzazione valori Print "ENCODER su CN3 = " ; Ea ; " ENCODER su CN4 = " ; Eb ; " " ; Chr(13) ; Waitms 200 ' Ritardo per avere solo 5 cicli al secondo Goto Ciclo ' Esce dal programma con ^C End ' '************************ Fine del programma *************************** '