' ********************************************************************** ' * File: uk_BAS51_074.BAS * ' * Version: 1.1 * ' * Date: 18.08.13 * ' * Development Tools: Bascom 8051 COMP.,IDE 2.0.14.0 + FLIP 2.4.6 * ' * Cards: GMM 5115 + GMM TST3 * ' * Developed by: GRIFO(r) Italian Technology * ' * via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * Author: Gianluca Angelini * ' ********************************************************************** ' Example program 074 of BASCOM 8051 course. ' It emulates a parallel printer provided of CENTRONICS interface, by using ' GMM 5115 Mini Module. ' The data sent to emulated printer are saved into a circular buffer and ' displayed on serial console, in ASCII format. The CENTRONICS interface of ' the sistem that should drive the printer (ie. PC) must be connected to I/O ' lines of Mini Module; this connection must be performed with the relationship ' described below in the definitions, and it can be easily performed with ' the card GMM TST3 and DEB 01. In details the program requires the following ' connections: ' - GMM 5115 mounted on socket Z2 of GMM TST3; ' - CN5 of GMM TST3 connected to serial line of console; ' - CN4 of GMM TST3 connected to CN14 of DEB 01, through a 20 wires flat cable; ' - CN13 of DEB 01 connected to CENTRONICS interface, by using a proper ' DB25M+DB25M direct cable (1:1). ' In order to ensure program execution it is necessary to supply power to Mini ' Module, or reset it, when the CENTRONICS interface is not connected and link ' it later; in fact whenever the last printed data has the less significant bit ' at low level (D1=0) the correspondent line (P1.0 DSW1.1 T2 DL1) is low too and ' so it selects the Debug modality of GMM 5115!! ' The program describes its functionalities and shows the data sent to printer ' on a serial console provided of monitor, with a fixed physical protocol at ' 19200 Baud, 8 Bit x chr, 1 Stop bit, No parity. ' This console can be another system capable to support a serial RS 232 ' communication. In order to simplify the use it can be used a PC provided of ' one COMx line, that execute a terminal emulation program as HYPERTERMINAL or ' the homonym modality provided by BASCOM 8051 (see IDE Configuration). ' The program uses some names in order to identify the used signals, that are ' those already used in electric diagrams and technical manuals of GMM TST3 and ' DEB 01 cards!! ' The program works only when the GMM 5115 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: On Int0; Enable Int0. ' ' 18/08/13: uk_BAS51_074.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to correctly use this demo program, please execute the following ' steps: ' 1) Check the availability of 89C5115.DAT file into the directory where the ' BASCOM 8051 is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Misc" set: ' Register File = 89C5115.DAT ' Byte End(Hex) = A0 ' Size warning = selected at 16384 (=4000H) ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM 5115, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' Run emulator modal = not selected ' 4) At the end of compilation, after the code is programmed on GMM 5115, select ' RUN mode and open the terminal emulation window of BASCOM 8051 with the ' option: Tools | Terminal emulator (Ctrl+T) and then reset or powen on the ' Mini Module. '************************* Compiler directives ********************************* $regfile "89C5115.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $iramstart = &H0 ' Data start address on internal RAM $ramstart = &H0 ' Data start address on external RAM $ramsize = &H100 ' External RAM size $crystal = 14745600 ' Microcontroller crystal frequency $large ' Code size > 2K $map ' Generate debug information $baud = 19200 ' Serial communication speed: 19200 Baud ' Other parameters fixed to: 8 bit x chr ' 1 Stop bit ' No parity '******************************* Definitions *********************************** ' The resources used by program are connected as described in following tables. ' !!! Note: On GMM TST3 the jumpers must be set in following positions: ' J1 in 2-3 ; J2 in 1-2 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3!!! ' ' GMM TST3 'CENTRONICS CENTRONICS -DEB 01 pin Z2 pin Signal Used uP ' signal DB 25 resource GMM TST3 GMM 5115 GMM 5115 signal ' D1 2 CN4-CN14.2 33 27 P1.0 DSW1.1 T2 DL1 P1.0 ' D2 3 CN4-CN14.1 32 26 P1.1 ADC1 T2EX P1.1 ' D3 4 CN4-CN14.4 31 25 P1.2 ADC2 ECI P1.2 ' D4 5 CN4-CN14.3 30 24 P1.3 ADC3 CEX0 P1.3 ' D5 6 CN4-CN14.6 29 23 P1.4 ADC4 CEX1 P1.4 ' D6 7 CN4-CN14.5 28 22 P1.5 ADC5 P1.5 ' D7 8 CN4-CN14.8 27 21 P1.6 ADC6 P1.6 ' D8 9 CN4-CN14.7 26 20 P1.7 ADC7 P1.7 ' /STROBE 1 CN4-CN14.15 25 19 P3.2 INT0 P3.2 ' /AUTOLF 14 CN4-CN14.16 24 18 P3.3 INT1 P3.3 ' /RESET 16 CN4-CN14.13 23 17 P3.4 T0 P3.4 ' MODE 17 CN4-CN14.14 22 16 P3.5 T1 P3.5 ' PE 12 CN4-CN14.11 21 15 P3.6 P3.6 ' /ACK 10 CN4-CN14.12 19 13 P3.7 P3.7 ' SELECT 13 CN4-CN14.9 14 8 P4.0 P4.0 ' BUSY 11 CN4-CN14.10 15 9 P4.1 P4.1 ' GND 19,20,..,29 CN4-CN14.17 20 14 GND -- ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Used up ' PC DB9 GMM TST3 GMM TST3 GMM 5115 GMM 5115 signal ' TX 3 3 9 3 RxD RS232 P3.0 ' RX 2 2 10 4 TxD RS232 P3.1 ' GND 5 5 20 14 GND GND ' This table shows that the connection cable between PC COM line and CN5 of ' GMM TST3 is a normal pin to pin cable or direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pinstb Alias P3.2 ' Signal connected to /STROBE CENTRONICS Pinmode Alias P3.5 ' Signal connected to MODE CENTRONICS Pinack Alias P3.7 ' Signal connected to /ACK CENTRONICS Pinpempty Alias P3.6 ' Signal connected to PE CENTRONICS Pinselect Alias P4.0 ' Signal connected to SELECT CENTRONICS Pinbusy Alias P4.1 ' Signal connected to BUSY CENTRONICS Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD Lenbufprn Alias 255 ' Circular buffer length, used for printer data Maxfilprn Alias 185 ' Data number to avoid printer buffer overflow Minfilprn Alias 135 ' Data number to restart printer buffer filling '************************* Constants declaration ******************************* '************************* Variables declaration ******************************* Dim Wpntprn As Byte ' Write pointer to printer data circular buffer Dim Rpntprn As Byte ' Read pointer to printer data circular buffer Dim Datprn As Byte ' Data sent to printer Dim Bufprn(lenbufprn) As Xram Byte ' Circular buffer for printer data Dim Hlpf As Bit ' General purpose bit variable Dim Hlpb As Byte ' General purpose byte variable '************************ Subroutines declaration ****************************** Declare Sub Inisim_prn() ' Inizialize lines for CENTRONICS printer emulation Declare Sub Genack_prn() ' Generate a pulse on /ACK signal of CENTRONICS interface '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs ' In order to correctly receive all the data sent to emulated printer, it must ' be obligatory used an X2 clock on CPU; this reduce the execution time of the ' service interrupt routine that receives and saves the printed data. Ckcon = &H7F ' Set 6 clock machine cycle (X2) onlly for CPU Call Inisim_prn() ' Initialize lines connected to CENTRONICS interface as printer Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Emulate CENTRONICS parallel printer" Print "Mount Mini Module on Z2 of GMM TST3, connect GMM TST3: CN4 to DEB 01:CN14 (20" Print "wires flat cable) and DEB 01:CN13 to CENTRONICS interface (DB25M+DB25M cable)." Print "The data sent to printer through thr CENTRONICS interface are displayed on" Print "console in ASCII format." Do ' Begin endless loop If Wpntprn <> Rpntprn Or Pinbusy = 1 Then ' If printer circular buffer has new data or it is full ' Get and show first new data saved into printer buffer Datprn = Bufprn(rpntprn) ' Get new data from printer buffer Printbin Datprn ' Show new data in ASCII format ' Update read pointer to printer buffer in circular manner If Rpntprn = Lenbufprn Then ' If reached end of circular buffer Rpntprn = 1 ' Read pointer to printer buffer at beginning Else Incr Rpntprn ' Increase read pointer to printer buffer End If ' If printer buffer was quite full (BUSY enabled) and it has been sufficiently ' emptied, it complete the reception of the last data by sending an /ACK pulse ' and by disabling the BUSY If Pinbusy = 1 Then ' If BUSY signal of CENTRONICS interface is active If Wpntprn >= Rpntprn Then ' Obtain number of data into printer buffer Datprn = Wpntprn - Rpntprn ' Data number into buffer = write-read pointer Else Datprn = Lenbufprn - Rpntprn ' Data number into buffer = (Lenbufprn-read)+write pointer Datprn = Datprn + Wpntprn End If If Datprn < Minfilprn Then ' If printer buffer is sufficiently empty Call Genack_prn() ' Generate pulse on /ACK signal of CENTRONICS interface Pinbusy = 0 ' Disable BUSY signal of CENTRONICS interface End If End If ' Perform a delay that emulate the processing time of main program for data ' into printer buffer and ensures that serial console doesn't overflow Waitms 10 ' 10 ms delay End If Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Initialize resource, variable and peripherals used for the emulation of a ' parallel printer with CENTRONICS interface. ' Input: None ' Output: Wpntprn = write pointer to printer buffer initialized ' Rpntprn = read pointer to printer buffer initialized Sub Inisim_prn() Adcf = &H00 ' Set signals for printer as digital I/O P1 = &HFF ' Initialize signals connected to Dx lines of the Hlpb = P1 ' CENTRONICS printer as digital input Pinstb = 1 ' Initialize signal connected to /STROBE line of the Hlpf = Pinstb ' CENTRONICS printer as digital input Pinmode = 1 ' Initialize signal connected to MODE line of the Hlpf = Pinmode ' CENTRONICS printer as digital input Pinack = 1 ' Initialize signal connected to /ACK line of the ' CENTRONICS printer as digital output at high level = disabled Pinpempty = 0 ' Initialize signal connected to PE line of the ' CENTRONICS printer as digital output at low level = disabled Pinselect = 1 ' Initialize signal connected to SELECT line of the ' CENTRONICS printer as digital output at high level = enabled Pinbusy = 0 ' Initialize signal connected to BUSY of the ' CENTRONICS printer as digital output at low level = disabled Wpntprn = 1 ' Inizialize write pointer to printer buffer at beginning Rpntprn = 1 ' Inizialize read pointer to receive buffer at beginning On Int0 Getdat_prn Nosave ' Service routine for INT0 interrupt: receive data for printer Tcon.0 = 1 ' Set INT0 active on falling edge Enable Int0 ' Enable INT0 interrupt Enable Interrupts ' General interrupt enable End Sub ' Generate a pulse on /ACK signal of CENTRONICS interface. ' Input: None ' Output: None Sub Genack_prn() Pinack = 0 ' Generate low pulse on /ACK signal of CENTRONICS printer NOP ' with a duration of about 5 usec NOP NOP NOP Pinack = 1 End Sub ' Service routine for INT0 interrupt of microcontroller, activated by /STROBE ' signal of CENTRONICS interface, for each data sent to printer. It get the ' printed data and save it into the printer circular buffer. ' The described operations are coded in assembly language in order to use as ' few microcontroller's registers as possible and to obtain the maximum execution ' speed. ' The subroutine changes the microprocessor's registers and so it has been ' declared without the Nosave directive. ' Input: Wpntprn = current write pointer to printer buffer ' Rpntprn = current read pointer to printer buffer ' Output: Wpntprn = currentwrite pointer to printer buffer updated ' Bufprn() = printer buffer with printed data ' Pinbusy = Active when printer buffer quite full Getdat_prn: $asm SETB Pinbusy ' Slow printing by activating the BUSY signal of CENTRONICS interface ' Save used registers PUSH PSW ' Save Program Status Word PUSH ACC ' Save accumulator PUSH DPH ' Save data pointer DPTR PUSH DPL ' Get data to print and save it into printer buffer MOV DPL,{wpntprn} ' Obtain external RAM location address of the printer buffer DEC DPL ' from write pointer and store it in DPTR (pointer starts MOV DPH,#0 ' from 1 while address from 0) MOV A,P1 ' Get data sent to printer MOVX @DPTR,A ' Save data into printer buffer ' Update write pointer to printer buffer in circular manner MOV A,{wpntprn} ' Get write pointer to printer buffer CJNE A,#255,Gdat00 ' If not reached end of circular buffer: Lenbufprn MOV A,#1 ' Reached end of circular buffer: set write pointer at beginning SJMP Gdat10 Gdat00: ' End of circular buffer not reached INC A ' Increase write pointer to printer buffer Gdat10: MOV {wpntprn},A ' Save updated write pointer to printer buffer ' Obtain number of data currently saved into printer circular buffer, by using ' the two pointer to same buffer CJNE A,{rpntprn},Gdat30 ' Compare write and read pointer to printer buffer Gdat20: ' Write pointer >= read pointer CLR C ' Data number into buffer = write-read pointer SUBB A,{rpntprn} SJMP GDAT40 Gdat30: ' Write pointer <> read pointer JNC Gdat20 ' if write pointer > read pointer CLR C ' Write pointer < read pointer MOV A,#255 ' Data number into buffer = (Lenbufprn-read)+write pointer SUBB A,{rpntprn} ADD A,{wpntprn} Gdat40: ' A contains data number into buffer ' Check if printer buffer is quite full, through the data number just obtained. ' When buffer is not full it generates a pulse on /ACK and it disables BUSY, ' viceversa it exits by leaving BUSY enabled CJNE A,#195,Gdat50 ' If printer buffer has not reached the maximum filling level (data number into buffer <> Maxfilprn) SETB Pinbusy ' Printer buffer quite full: enable BUSY signal of CENTRONICS interface SJMP Gdat60 Gdat50: ' Printer buffer not full LCALL Genack_prn ' Generate pulse on /ACK signal of CENTRONICS interface CLR Pinbusy ' Disable BUSY signal of CENTRONICS interface in order to restart printing Gdat60: ' Restore used registers POP DPL ' Restore data pointer DPTR POP DPH POP ACC ' Restore accumulator POP PSW ' Restore Program Status Word $end Asm Return '******************* End of subroutines used by program ************************