' ********************************************************************** ' * File: uk_BAS51_055.BAS * ' * Version: 1.1 * ' * Date: 06.12.11 * ' * 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 055 of BASCOM 8051 course. ' It manages an operator panel QTP 03 through additional asynchronous serial ' line, with interrupts. ' The program communicate with QTP 03 by using a physical protocol 4800 Baud, ' 8 Bit x chr, 1 Stop bit, No parity set on sw serial line. ' In order to simplify the program, the sw serial line is managed through the ' high level instructions of BASCOM dedicated to console; these are redirected ' on sw serial line in place of the hw one, thanks to a suited selector defined ' in the source. ' About electric protocol the serial line of QTP 03 can be either RS 232 or ' TTL and thus it can be connected to Mini Module both with an electric driver ' (i.e. MSI 01) or directly. ' The program supports a small subset of the numerous functionalities offered ' by QTP 03 and they are selectable by user through a console provided of ' monitor and keyboard, connected to hw serial line, with a fixed physical ' protocol at 19200 Baud, 8 Bits 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 works only when the GMM 5115 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: $SERIALINPUT, $SERIALOUTPUT. ' ' 06/12/11: uk_BAS51_055.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 $serialinput = Handle_consoleinp ' Redirect console input $serialoutput = Handle_consoleout ' Redirect console output '******************************* Definitions *********************************** ' The resources used by program are connected as described in following table. ' !!! 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!!! ' ' Software GMM TST3 pin Z2 pin Signal uP used ' serial line resource GMM TST3 GMM 5115 GMM 5115 signal ' RX SW CN4.16 24 18 P3.3 INT1 P3.3 ' TX SW CN4.13 23 17 P3.4 T0 P3.4 ' ' 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. Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD ' Status flags for hw serial line Ri Alias Scon.0 ' Flag for chr received from hw serial line Ti Alias Scon.1 ' Flag for transmission possible on hw serial line '************************* Constants declaration ******************************* Const Cret = 13 ' ASCII code for carriage return Const Lf = 10 ' ASCII code for line feed Const Bell = 7 ' ASCII code for bell Const Esc = 27 ' ASCII code for escape '************************* Variables declaration ******************************* Dim Crxsw As Byte ' Character received from sw serial line Dim Ctxsw As Byte ' Character to transmit on sw serial line Dim Rxcsw As Byte ' Character received by interrupt Dim Bufrx(5) As Byte ' Receive buffer for 5 characters Dim Wpntrx As Byte ' Write pointer to receive buffer Dim Rpntrx As Byte ' Read pointer to receive buffer Dim Isrxsw As Bit ' Flag for character received from sw serial line Dim Conssw As Bit ' Flag che selezione il dispositivo di console (0=ser.hw, 1=ser.sw) Dim Ischr As Bit ' Flag dalla console status per chr disponibile Dim Chrx As Byte ' Character received from console status Dim Cur_qtp As Byte ' Parameter for QTP cursor type Dim Buz_qtp As Byte ' QTP buzzer status Dim Col_qtp As Byte ' Parameter with cursor position of QTP Dim Row_qtp As Byte Dim Tout As Byte ' Timeout counter Dim Gstr As String * 22 ' Strings for value and messages visualization Dim G As Single ' Variable for meausure to visualize Dim Choice As Byte ' Selected operation Dim Hlpb As Byte ' General purpose help variable '************************ Subroutines declaration ****************************** On Int1 Ricev_sw ' INT1 service routine Declare Sub Iniser_sw() ' Initialize and arrange the sw serial line use Declare Sub Closeser_sw() ' Close and finish sw serial line use Declare Sub Isrxser_sw() ' Manage sw serial line reception Declare Sub Txser_sw() ' Transmit character on sw serial line Declare Sub Consolesta() ' Check console status ' QTP 03 subroutines Declare Sub Clear_qtp() ' Clear all display of QTP Declare Sub Cur_type_qtp(cur_qtp As Byte) ' Define QTP cursor type Declare Sub Buz_attr_qtp(buz_qtp As Byte) ' Define QTP Buzzer status Declare Sub Pos_cur_qtp(col_qtp As Byte , Row_qtp As Byte) ' Position QTP cursor Declare Sub Wt_eerdy_qtp() ' Wait QTP EEPROM ready Declare Sub Keyp_qtp() ' Manage QTP 03 keys pressure '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Conssw = 0 ' Console on hw seriale line Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " QTP 03 management with GMM 5115 + GMM TST3" ' The instruction use are not showed on console in order to reduce the code of ' the program and thus it can be compiled with demo version of compiler. ' Perform the following operations before using program: ' - mount Mini Module on Z2 of GMM TST3; ' - configure QTP 03 at 4800 Baud, 1 Stop by using its LOCAL SETUP; ' - connect sw serial line from CN4 GMM TST3 to CN3 QTP 03; ' - configure J1, J2 of QTP 03 according with performed connection (TTL or RS 232 line) ' - connect a suitable power supply voltage to QTP 03. ' Initializes the sw serial line for 4800 Baud, 8 Bits x chr, 1 stop Bit, No parity ' on the I/O signals defined in previous table. ' In the program source the following two instructions must be used before each ' other instructions that use the sw serial line. Open "com3.4:4800" For Output As #1 ' Associate signal used as transmission line, to channel 1 Open "com3.3:4800" For Input As #2 ' Associate signal used as reception line, to channel 2 Call Iniser_sw() ' Initialize and arrange the sw serial line use Do ' Begin endless loop ' The program checks the keys of QTP 03 and when they are pressed it produces an ' audible feddback and it shows a message on display. Moreover it allows the ' following operations: Conssw = 0 ' Console on hw serial line Print Print "See source for use." Print Print "Enable/Disable power on visualization" Print "Clear" Print "Measure" Print "Buzzer" Print "Choice:"; Do Conssw = 1 ' Console on sw serial line Call Keyp_qtp() ' Manage keys pressed on QTP 03 Conssw = 0 ' Console on hw serial line Call Consolesta ' Verify if key pressed on console and acquire it Loop Until Ischr = 1 ' Repeat until key pressed on console Choice = Chrx And &HDF ' Save received chr=performed choice in upper case Printbin Choice ' Shows performed choice Print Select Case Choice ' Check performed choice Case "D": ' Selected Disable power on visualization Conssw = 1 ' Console on sw serial line Printbin Esc ; 150 ; 8 ; 255 ; 1 ; 0 ; 0 ; 0 ' Disable power on visualization: ESC 150 cmb nmsg length shift r c Conssw = 0 ' Console on hw serial line Case "E": ' Selected Enable power on visualization Input "Insert msg.(max. 20 chrs): " , Gstr Conssw = 1 ' Console on sw serial line ' Define power on visualization of message 0 only, static (with no sliding), ' at beginning of display Hlpb = Len(gstr) ' Force maximum message length to 20 characters If Hlpb >= 20 Then ' If length equal or higher than Gstr = Left(gstr , 20) ' Mantain the first 20 characters Else ' Length lower than Hlpb = 20 - Hlpb ' Adds spaces up to 20 characters Gstr = Gstr + Space(hlpb) End If Printbin Esc ; 33 ; 67 ; 0 ' Save messagge 0: ESC ! C n.msg chr.0śchr.19 Print Gstr; Call Wt_eerdy_qtp() ' Wait QTP EEPROM ready Printbin Esc ; 150 ; 8 ; 0 ; 1 ; 0 ; 0 ; 0 ' Enable power on visualization: ESC 150 cmb nmsg length shift r c Conssw = 0 ' Console on hw serial line ' Setting performed. Turn off and on the QTP in order to see the effects Case "C": ' Selected Clear display Conssw = 1 ' Console on sw serial line Call Clear_qtp() ' Clear QTP display Conssw = 0 ' Console on hw serial line Case "M": ' Selected numeric Measure visualization on display Print "Vis. under execution; press QTP key to stop.."; Conssw = 1 ' Console on sw serial line Call Cur_type_qtp(0) ' Disable cursor Call Pos_cur_qtp(1 , 1) ' Position QTP cursor at beginning of first row Print "Measure =" ; ' Shows label on QTP Do ' Cycle that show numeric value Call Pos_cur_qtp(13 , 1) ' Position cursor at label end Hlpb = Rnd(255) ' Simulate measure acquisition G = Hlpb / 10 ' Simulate one decimal digit on measure Gstr = Fusing(g , "00.#") ' Shows formatted measure Print Gstr ; Waitms 500 Call Consolesta ' Verify if key pressed on console and acquire it Loop Until Ischr = 1 ' Repeat until key pressed on console Call Cur_type_qtp(85) ' Enable blinking block cursor Conssw = 0 ' Console on hw serial line Case "B": ' Selected QTP Buzzer activation Print "Buzzer Active,Deactive,Intermittent:"; Do Choice = Waitkey() ' Wait valid user choice Choice = Choice And &HDF ' Convert choice in upper case Loop Until Choice = "A" Or Choice = "D" Or Choice = "I" Printbin Choice ' Shows converted choice Print Buz_qtp = Choice ' Save choice=QTP Buzzer status Conssw = 1 ' Console on sw seriale line Call Buz_attr_qtp(buz_qtp) ' Define QTP Buzzer status Conssw = 0 ' Console on hw seriale line Case Else: ' Invalid selection Printbin &H07 ' Produce an advise BEL on console End Select Loop ' End endless loop Disable Interrupts ' Interrupt general disable Call Closeser_sw() ' Close and finish sw serial line use End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' INT1 interrupt service routine, activated by Start bit of received character. ' This method can be used only when the receive signal of the sw serial line ' (RX SW) is capable to generate interrupt on falling edge. Moreover this method ' makes the reception from the same serial line not suspensive and it frees up ' the microcontroller from useless work. ' Input: Wpntrx = current write pointer to receive buffer ' Output: Wpntrx = updated write pointer to receive buffer ' Bufrx() = receive buffer with received character Ricev_sw: Get #2 , Rxcsw ' Receive chr just started with sw serial, associated to channel 2 Bufrx(wpntrx) = Rxcsw ' Save received chr into the receive buffer Incr Wpntrx ' Increase write pointer in circular manner If Wpntrx > 5 Then Wpntrx = 1 End If Tcon.3 = 0 ' Clear INT1 flag to erase all the chr's transitions Return ' Initialize and arrange sw serial line use ' Input: None ' Output: Wpntrx = write pointer to receive buffer initialized ' Rpntrx = read pointer to receive buffer initialized Sub Iniser_sw() Wpntrx = 1 ' Initialize write pointer to receive buffer Rpntrx = 1 ' Initialize read pointer to receive buffer Tcon.2 = 1 ' INT1 active on falling edge Enable Int1 ' Enable INT1 interrupt Enable Interrupts ' General interrupt enable End Sub ' Manage sw serial reception ' Input: Rpntrx = current read pointer to receive buffer ' Output: Rpntrx = updated read pointer to receive buffer ' Isrxsw = flag for character received from sw serial line ' Crxsw = possible received character Sub Isrxser_sw() If Wpntrx <> Rpntrx Then ' Verify if character received through INT1 and saved in circular buffer Crxsw = Bufrx(rpntrx) ' Acquire received character from receive buffer Incr Rpntrx ' Increase read pointer in circular manner If Rpntrx > 5 Then Rpntrx = 1 End If Isrxsw = 1 ' Signalize character received and returned Else Isrxsw = 0 ' Signalize character not received End If End Sub ' Transmit character on sw serial line, by ensuring all interrupts disabled in ' order to mantain timings and correct framing of character under transmission. ' Input: Ctxsw = character to transmit ' Output: None Sub Txser_sw() Disable Interrupts ' General interrupt disable Put #1 , Ctxsw ' Transmit character on sw serial line, associated to channel 1 Enable Interrupts ' General interrupt enable End Sub ' Close and finish sw serial line use ' In the program source the following two instructions must be used after each ' other instructions that use the sw serial line. ' Input: None ' Output: None Sub Closeser_sw() Close #1 ' Close channels associated to sw serial line Close #2 End Sub '********************** Console management subroutines ************************* ' Subroutine that returns the status of the device currently selected by ' variable: Conssw = 0 -> hw serial line ' = 1 -> sw serial line ' into Ischr variable, plus the possible received chr, into variable Chrx Sub Consolesta $asm push PSW ' Save PSW register, always used Jb {conssw} , Serswsta ' Check selected device mov C,{Ri} ' Device = hw serial line mov {Ischr},C ' Return variable with chr received status jnc Sta99 ' Character not available clr {Ri} ' Clear chr received flag mov {Chrx},SBUF ' Get and return received chr from hw serial line sjmp Sta99 Serswsta: ' Device = sw serial line push B ' Save all registers, except PSW push ACC push DPL push DPH push R0 push R1 push R2 push R3 push R4 push R5 push R6 push R7 $end Asm Call Isrxser_sw() ' Verify and receive chr from sw serial line Ischr = Isrxsw ' Save variables for sw serial line and console Chrx = Crxsw $asm pop R7 ' Restore all registers, except PSW pop R6 pop R5 pop R4 pop R3 pop R2 pop R1 pop R0 pop DPH pop DPL pop B pop ACC Sta99: pop PSW ' Restore PSW register, always used $end Asm End Sub ' Subroutine that manages the console input for one chr from the device currently ' selected by variable: Conssw = 0 -> hw serial line ' = 1 -> sw serial line !Handle_consoleinp: $asm Jb {conssw} , Serswinp ' Check selected device Wrxchr: ' Device = hw serial line jnb {Ri},Wrxchr ' Wait chr reception from hw serial line clr {Ri} ' Clear chr received flag mov a,SBUF ' Get received chr from hw serial line ret Serswinp: ' Device = sw serial line push PSW ' Save all registers, except ACC push B push DPL push DPH push R0 push R1 push R2 push R3 push R4 push R5 push R6 push R7 $end Asm Do Call Isrxser_sw() ' Verify and receive chr from sw serial line Loop Until Ischr = 1 ' Wait chr reception from sw serial line $asm mov a,{Crxsw} ' Return received chr, saved in Crxsw pop R7 ' Restore all registers, except ACC pop R6 pop R5 pop R4 pop R3 pop R2 pop R1 pop R0 pop DPH pop DPL pop B pop PSW ret $end Asm ' Subroutine that manages the console output of one chr on the device currently ' selected by variable: Conssw = 0 -> hw serial line ' = 1 -> sw serial line !Handle_consoleout: $asm Jb {conssw} , Serswout ' Check selected device Wtxfree: ' Device = hw serial line jnb {Ti},Wtxfree ' Wait hw transmitter free clr {Ti} ' Clear end of transmission flag mov SBUF,a ' Transmit chr on hw serial line ret Serswout: ' Device = sw serial line push PSW ' Saves all registers push ACC push B push DPL push DPH push R0 push R1 push R2 push R3 push R4 push R5 push R6 push R7 mov {Ctxsw},a ' Save chr to transmit in Ctxsw $end Asm Call Txser_sw() ' Transmit chr on sw serial line $asm pop R7 ' Restore all registers pop R6 pop R5 pop R4 pop R3 pop R2 pop R1 pop R0 pop DPH pop DPL pop B pop ACC pop PSW ret $end Asm '********************** QTP 03 management subroutines ************************** ' Clear all QTP display and place cursor in HOME position ' Input: None ' Output: None Sub Clear_qtp() Printbin 12 ' Send clear page command: FF End Sub ' Set cursor type of QTP according with passed parameter ' Input: Cur_qtp = cursor type (0->disabled, 85->blinking, 255->fixed) ' Output: None Sub Cur_type_qtp(cur_qtp As Byte) Select Case Cur_qtp Case 0 : Printbin Esc ; 80 ' Send disable cursor command: ESC P Case 85 : Printbin Esc ; 81 ' Send enable blinking cursor command: ESC Q Case 255 : Printbin Esc ; 79 ' Send enable fixed cursor command: ESC O End Select End Sub ' Set QTP Buzzer according with passed parameter ' Input: Buz_qtp = buzzer status ("D"->deactive, "I"->intermittent, "A"->active) ' Output: None Sub Buz_attr_qtp(buz_qtp As Byte) Printbin Esc ; 50 ; 255 Select Case Buz_qtp Case "D" : Printbin 0 ' Send disable buzzer command: ESC 2 255 0 Case "I" : Printbin 85 ' Send enable intermittent buzzer command: ESC 2 255 85 Case "A" : Printbin 255 ' Send enable buzzer command: ESC 2 255 255 End Select End Sub ' Place QTP cursor on the position defined by passed parameters, with a base 1 ' value ' Input: Col_qtp = Cursor column ' Row_qtp = Cursor row ' Output: None Sub Pos_cur_qtp(col_qtp As Byte , Row_qtp As Byte) Printbin Esc ; 89 ' Send cursor position command: Hlpb = Row_qtp + 31 ' ESC Y r+32 c+32 Printbin Hlpb Hlpb = Col_qtp + 31 Printbin Hlpb End Sub ' Wait QTP EEPROM ready, with a security timeout of about 500 milliseconds ' Input: None ' Output: None Sub Wt_eerdy_qtp() Tout = 0 ' Reset timeout counter Do Printbin Esc ; 51 ' Send request EEPROM availability command: ESC 3 Waitms 100 ' Wait time for answer reception Call Consolesta ' Verify if received answer to command Incr Tout ' Increase timeout counter If Ischr = 1 Then ' If answer received: check it If Chrx = 6 Then ' If QTP EEPROM ready Tout = 6 ' Set for immediate exit End If End If Loop Until Tout >= 5 ' Repeat until EEPROM ready or timeout elapsed End Sub ' Manage QTP 03 keys pressure. Verify if there are key pressed on QTP 03 and when ' they are available it produces an audible feedback and it shows a proper ' indication on second row of display. ' Input: None ' Output: None Sub Keyp_qtp() Call Consolesta ' Verify if key pressed on QTP 03 and get it If Ischr = 1 Then ' If key pressed Call Pos_cur_qtp(1 , 2) ' Position QTP cursor at the beginning of second row Print "QTP key pressed:"; ' Visualize indication on display Printbin Chrx ' Add key pressed on display Select Case Chrx ' Check received chr=key pressed code Case "1": ' QTP key 1 pressed Printbin Bell ' Perform 1 beep with QTP Buzzer Case "2": ' QTP key 2 pressed Printbin Bell ' Perform 2 beeps with QTP Buzzer Waitms 200 ' Delay for beeps separation Printbin Bell Case "3": ' QTP key 3 pressed Printbin Bell ' Perform 3 beeps with QTP Buzzer Waitms 200 ' Delay for beeps separation Printbin Bell Waitms 200 Printbin Bell End Select End If End Sub '******************* End of subroutines used by program ************************