' ******************************************************************* ' * Program: ENC8254.BAS Version: 1.1 - 02 September 1999 * ' * Compiler: BASCOM 8051 (IDE V.1.0.0.16, LIB V.1.10) * ' * Firm: 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 * ' * * ' * Written by: Graziano Gaiba * ' ******************************************************************* ' ' Program designed to work with BASCOM 8051 and a GPC(r) F2 board ' connected to an EMI 01 board. ' The value output by the two encoders connected to the CN3 and CN4 ' Connectors of EMI 01 board are read and pronted to the screen: ' '************************* Compiler Directives ******************************* ' $romstart = &H8050 ' Start address of machine code $ramstart = &HD000 ' Start address of data area $ramsize = &H2800 ' 10k of data area $crystal = 11059200 ' Clock of microcontroller $baud = 9600 ' RS-232 baud rate $large ' 16 bit addressing mode ' '************************** Constant Declaration ***************************** ' ' Addresses of Counter 8254 A Dim Cwa As Const &HFA43 ' Address of 8254 A status register Dim Acnt0 As Const &HFA40 ' Address of 8254 A counter 0 data register Dim Acnt1 As Const &HFA41 ' Address of 8254 A counter 1 data register ' Addresses of Counter 8254 B Dim Cwb As Const &HFA83 ' Address of 8254 B status register Dim Bcnt0 As Const &HFA80 ' Address of 8254 B counter 0 data register Dim Bcnt1 As Const &HFA81 ' Address of 8254 B counter 1 data register ' Command words Dim Cmdw0 As Const &H30 ' Counter 0: mode 0 command word Dim Cmdw1 As Const &H70 ' Counter 1: mode 0 command word ' '************************** Variables Declaration ***************************** ' Dim V As Byte , L As Long ' General purpose variables Dim Ea As Long , Eb As Long ' Value returned by encoders Dim Apc0 As Long , Apc1 As Long ' Variables used to calculate Dim Bpc0 As Long , Bpc1 As Long ' The value returned by encoders Dim Ac0 As Long , Ac1 As Long Dim Bc0 As Long , Bc1 As Long Dim Diff_a As Long , Diff_b As Long ' '****************************** Main Program ********************************** ' Waitms 1 ' Delay for signals settling ' 8254 counters initialization Out Cwa , Cmdw0 ' Initialization counter 0 8254 A Out Cwa , Cmdw1 ' Initialization counter 1 8254 A Out Cwb , Cmdw0 ' Initialization counter 0 8254 B Out Cwb , Cmdw1 ' Initialization counter 1 8254 B ' Start of program Ea = 0 : Eb = 0 : ' Variables for encoders Print Chr(12) ' Clears the screen Print "PROGRAM TO ACQUIRE 2 ENCODERS IN THE RANGE -65536<->+65536" Print "BY THE EMI01 BOARD CONNECTED TO THE GPC(r) F2 BOARD" Print : Print ' Spacing ' Reset 8254 A registers Out Acnt0 , 255 ' Counter 0 H and Out Acnt0 , 255 ' L set to 0. Out Acnt1 , 255 ' Counter 1 H and Out Acnt1 , 255 ' L set to 0. ' Reset 8254 B registers Out Bcnt0 , 255 ' Counter 0 H and Out Bcnt0 , 255 ' L set to 0 Out Bcnt1 , 255 ' Counter 1 H and Out Bcnt1 , 255 ' L set to 00. Print : Print "PRESS TO BEGIN TO ACQUIRE THE ENCODER" V = Waitkey ' Waits a key Print Chr(12) ' Clears the screen ' Reads the initial combinations of counters 0 and 1 in the A and B sections ' 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 Cycle: ' Starts the encoder read cycle ' Read counter 0 8254 A ' Ac0 = Inp(anct0) + Inp(acnt0) * 256 V = Inp(acnt0) : Ac0 = V V = Inp(acnt0) : L = V * 256 Ac0 = Ac0 + L ' Read counter 1 8254 A ' Ac1 = Inp(anct1) + Inp(acnt1) * 256 V = Inp(acnt1) : Ac1 = V V = Inp(acnt1) : L = V * 256 Ac1 = Ac1 + L ' Read counter 0 8254 B ' Bc0 = Inp(bnct0) + Inp(bcnt0) * 256 V = Inp(bcnt0) : Bc0 = V V = Inp(bcnt0) : L = V * 256 Bc0 = Bc0 + L ' Read counter 1 8254 B ' Bc1 = Inp(bnct1) + Inp(bcnt1) * 256 V = Inp(bcnt1) : Bc1 = V V = Inp(bcnt1) : L = V * 256 Bc1 = Bc1 + L ' Calculates variation from previous values ' 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 ' Section A ************************************************************ ' If counter resets, calculate new variation If Abs(diff_a) > 65000 Then Goto Handlereset_a Else Goto Skip_a Handlereset_a: If Diff_a > 0 Then Goto Reset0_a Else Goto Reset1_a ' Identifies which counter reset Reset0_a: ' Diff_a = -(Apc1 + (65536 -Ac1)) L = Ac1 - 65536 Diff_a = L - Apc1 ' Counter 1 reset Goto Skip_a Reset1_a: ' Diff_a = Apc0 + (65536 -ac0) L = 65536 - Ac0 Diff_a = Apc0 + L ' Counter 0 reset Skip_a: Ea = Ea + Diff_a ' Encoder values update ' If encoder value out of range -65536<>+65536 If Abs(ea) > 65536 Then Goto Handleoverflow_a Else Goto Continue Handleoverflow_a: ' Determines which limit has been overridden If Ea > 0 Then Goto Sup_a Else Goto Inf_a Sup_a: Ea = Ea - 65536 ' Higher limit Goto Continue Inf_a: Ea = Ea + 65536 ' Lower limit Continue: ' Section B ************************************************************ ' If counter resets, calculate new variation If Abs(diff_b) > 65000 Then Goto Handlereset_b Else Goto Skip_b Handlereset_b: If Diff_b > 0 Then Goto Reset0_b Else Goto Reset1_b ' Identifies which counter reset Reset0_b: ' Diff_b = -(Bpc1 + (65536 -Bc1)) L = Bc1 - 65536 Diff_b = L - Bpc1 ' Counter 1 reset Goto Skip_b Reset1_b: ' Diff_b = Bpc0 + (65536 -Bc0) L = 65536 - Bc0 Diff_b = Bpc0 + L ' Counter 0 reset Skip_b: Eb = Eb + Diff_b ' Encoder values update ' If encoder value out of range -65536<>+65536 If Abs(eb) > 65536 Then Goto Handleoverflow_b Else Goto Update Handleoverflow_b: ' Determines which limit has been overridden If Eb > 0 Then Goto Sup_b Else Goto Inf_b Sup_b: Eb = Eb - 65536 ' Higher limit Goto Update Inf_b: Eb = Eb + 65536 ' Lower limit Update: ' ********************************************************************** ' Update previous values Apc0 = Ac0 : Apc1 = Ac1 Bpc0 = Bc0 : Bpc1 = Bc1 ' Output Print "ENCODER on CN3 = " ; Ea ; " ENCODER on CN4 = " ; Eb ; " " ; Chr(13) ; Waitms 200 ' Delay to have only 5 cycles per second Goto Cycle End ' '********************************* Program End ******************************** '