' ********************************************************************** ' * File: uk_BAS51_018.BAS * ' * Version: 1.1 * ' * Date: 19.07.10 * ' * 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 018 of BASCOM 8051 course. ' It manages all the keys of a 4x4 matrix keyboard. ' The program continuosly acquires the state of the 16 keys connected to matrix ' keyboard available on GMM TST3 and it transmits them on serial line. The ' acquisition is performed with debouncing, without autorepeat and without ' times controls. ' The visualization of pressed keys is performed on a serial console provided ' of monitor and it must communicate 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 works only when the GMM 5115 is mounted on Z1 socket of GMM TST3!! ' Inside the program the row and column terms refers to electric diagram of ' matrix keyboard, not to its phisical format!! ' ' Added instructions: None. ' ' 19/07/10: uk_BAS51_018.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to coorectly 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 table: ' !!! Note: On GMM TST3 the following jumpers must be properly configured: ' J1 in 2-3 ; J3 in 1-2 ; J10 in 1-2 ; J11 in 1-2 !!! ' ' hardware pin Z1 pin Signal Used uP ' resource GMM TST3 GMM 5115 GMM 5115 signal ' TST1.Row0 33 27 P1.0 DSW1.1 T2 DL1 P1.0 ' TST1.Row1 32 26 P1.1 ADC1 T2EX P1.1 ' TST1.Row2 31 25 P1.2 ADC2 ECI P1.2 ' TST1.Row3 30 24 P1.3 ADC3 CEX0 P1.3 ' TST1.Col0 29 23 P1.4 ADC4 CEX1 P1.4 ' TST1.Col1 28 22 P1.5 ADC5 P1.5 ' TST1.Col2 27 21 P1.6 ADC6 P1.6 ' TST1.Col3 26 20 P1.7 ADC7 P1.7 ' ' 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 - ' 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. Pinrow0 Alias P1.0 ' Signal connected to row 0 of GMM TST3 keyboard TST1 Pinrow1 Alias P1.1 ' Signal connected to row 0 of GMM TST3 keyboard TST1 Pinrow2 Alias P1.2 ' Signal connected to row 0 of GMM TST3 keyboard TST1 Pinrow3 Alias P1.3 ' Signal connected to row 0 of GMM TST3 keyboard TST1 Pincol0 Alias P1.4 ' Signal connected to column 0 of GMM TST3 keyboard TST1 Pincol1 Alias P1.5 ' Signal connected to column 1 of GMM TST3 keyboard TST1 Pincol2 Alias P1.6 ' Signal connected to column 2 of GMM TST3 keyboard TST1 Pincol3 Alias P1.7 ' Signal connected to column 3 of GMM TST3 keyboard TST1 Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* Const Nokey = &HFF ' Value that indicate no key pressed Const Debdt = 5 ' Debouncing execution time in milliseconds Const Debtime = 20 ' Debouncing time for real key in milliseconds Const Debcycle = Debtime / Debdt ' Debouncing number obtained by defined times '************************* Variables declaration ******************************* Dim Keypos As Byte ' Position of key pressed on matrix keyboard Dim Key As Byte ' Code of key pressed on matrix keyboard Dim Krow As Byte ' Rows states of matrix keyborad Dim Kcol As Byte ' Column states of matrix keyboard Dim Kcode(16) As Byte ' Array with keys codes of matrix keyboard Dim Debpos As Byte ' Position of key pressed under debouncing Dim Debcnt As Byte ' Debouncing counter '************************ Subroutines declaration ****************************** Declare Sub Ini_key() ' Initialize lines of matrix keyboard Declare Sub Set_col() ' Set column state of matrix keyboard Declare Sub Get_row() ' Acquire rows states of matrix keyboard Declare Sub Scan_keypos() ' Acquire state of matrix keyboard Declare Sub Deb_key() ' Acquire state of matrix keyboard with debouncing '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Call Ini_key() ' Initialize line of matrix keyboard Print ' Separate from previous visualization by showing an empty new line Print " Acquire 16 keys from a 4x4 matrix keyboard" Print "The program show the keys pressed on TST1 keyboard of GMM TST3, by using" Print "debouncing modality." Print Do ' Begin endless loop Waitms Debdt ' Delay equal to debouncing time Call Deb_key() ' Acquire state of matrix keyboard with debouncing If Key <> Nokey Then ' Check if there is a key pressed Printbin Key ' Show key pressed on console End If Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Initialize resources, variables and peripheral devices used for matrix ' keyboard management. ' Input: None ' Output: Kcode() = array with initialized keys codes ' Debpos, Debcnt = variables for debouncing initialized Sub Ini_key() Adcf = &H00 ' Set signals for matrix keyboard as digital I/Os Pinrow0 = 1 ' Initialize signals connected to rows of Krow = Pinrow0 ' matrix keyboard as digital inputs Pinrow1 = 1 Krow = Pinrow1 Pinrow2 = 1 Krow = Pinrow2 Pinrow3 = 1 Krow = Pinrow3 Pincol0 = 1 ' Initialize signals connected to column of Pincol1 = 1 ' matrix keyboard as digital outputs at high level Pincol2 = 1 Pincol3 = 1 Kcode(1) = "D" ' Set keys codes of matrix keyboard in Kcode(2) = "#" ' proper array Kcode(3) = "0" Kcode(4) = "*" Kcode(5) = "C" Kcode(6) = "9" Kcode(7) = "8" Kcode(8) = "7" Kcode(9) = "B" Kcode(10) = "6" Kcode(11) = "5" Kcode(12) = "4" Kcode(13) = "A" Kcode(14) = "3" Kcode(15) = "2" Kcode(16) = "1" Debpos = Nokey ' No key pressed under debouncing Debcnt = 0 ' Initialize debouncing counter End Sub ' Set a low level on a single column of the 4 available in the matrix keyboard ' of GMM TST3. ' Input: Kcol = Column number to set low (0..3) ' Output: None Sub Set_col() If Kcol = 0 Then ' Set column 0 state Pincol0 = 0 Else Pincol0 = 1 End If If Kcol = 1 Then ' Set column 1 state Pincol1 = 0 Else Pincol1 = 1 End If If Kcol = 2 Then ' Set column 2 state Pincol2 = 0 Else Pincol2 = 1 End If If Kcol = 3 Then ' Set column 3 state Pincol3 = 0 Else Pincol3 = 1 End If End Sub ' Acquire and check the states of the 4 rows of GMM TST3 matrix keyboard ' Input: None ' Output: Krow = Number of the row at low level (0..3, Nokey if none is low) Sub Get_row() Krow = Nokey ' Set rows state for no key pressed If Pinrow0 = 0 Then ' Check and set row 0 state Krow = 0 End If If Pinrow1 = 0 Then ' Check and set row 1 state Krow = 1 End If If Pinrow2 = 0 Then ' Check and set row 2 state Krow = 2 End If If Pinrow3 = 0 Then ' Check and set row 3 state Krow = 3 End If End Sub ' Verify state of matrix keyboard on GMM TST3, by setting low the 4 columns and ' checking the states of the 4 rows that compose the same keyboard. When more ' keys are contemporaneously pressed, it is returned the position of those ' connected to higher column. The position is calculated with locical operations ' in place of arithmetic ones, in order to optimize execution times. ' Input: None ' Output: Keypos = Position of key pressed (Nokey if none is pressed) Sub Scan_keypos() Keypos = Nokey ' Initialize code of no key pressed Kcol = 0 ' Set current column=the first Do Call Set_col() ' Set low current column Call Get_row() ' Get rows state If Krow <> Nokey Then ' Check if there is a key pressed Keypos = Kcol ' Obtain key pressed position from column Shift Keypos , Left , 2 ' and row starting from 0: multiply column by 2 Keypos = Keypos Or Krow ' and add row Incr Keypos ' Obtain key position starting from 1 End If Incr Kcol ' Increase current column Loop Until Kcol > 3 ' Repeat up to last column End Sub ' Verify state of matrix keyboard on GMM TST3, by performing the scanning and ' managing debouncing. This debounce avoids to recognize the typical rebounds ' of each electric button as pressures and releases of the same button, by ' obtaining a real state of pressed keys. ' Input: Debpos = Position of key pressed under debouncing ' Debcnt = Debouncing counter ' Output: Key = ASCII code of key pressed (Nokey if none is pressed) ' Debpos = Updated position of key pressed under debouncing ' Debcnt = Updated debouncing counter Sub Deb_key() Call Scan_keypos() ' Perform matrix keyboard scanning ' Debouncing management: verifies if the possible pressed key has been pushed continuosly for all ' the debouncing time and if this condition is satisfied returns its code. This verify is performed ' with two variables: the Debpos memorizes the position of the previously pressed key and the ' Debcnt counts the number of consecutive scans with key pressed. In order to obtain a Debouncing ' time equal to value defined in Debtime, the Deb_key() subroutines must be called at regular ' time interval long Debdt milliseconds. Key = Nokey ' No real key pressed If Keypos <> Nokey Then ' If there is a pressed key If Keypos = Debpos Then ' If a key was already pressed, that is under debouncing Incr Debcnt ' Increase debouncing counter Else ' Key was not already pressed Debpos = Keypos ' Saves position of new key under debouncing Debcnt = 0 ' Reset debouncing counter End If If Debcnt >= Debcycle Then ' If debouncing time is elapsed Debcnt = 0 ' Reset counter to restart with next debouncing Key = Kcode(keypos) ' Get pressed key code from dedicated array End If Else ' Nessun tasto premuto Debpos = Nokey ' Set no key under debouncing Debcnt = 0 ' Reset debouncing counter End If End Sub '******************* End of subroutines used by program ************************