' ********************************************************************** ' * File: uk_BASAVR_016.BAS * ' * Version: 1.1 * ' * Date: 05.07.10 * ' * Development Tools: Bascom-AVR Demo Ver. 1.11.9.1 + * ' * + AVR bootloader grifo(r) Ver. 1.2 * ' * Cards: GMM AM08 + 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 16 of BASCOM AVR course. ' It manages 4 keys of a 4x4 matrix keyboard. ' The program continuosly acquires the state of 4 keys connected to first ' column of matrix keyboard available on GMM TST3 and it transmits them on ' serial line. The acquisition is performed without 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 AVR (see IDE Configuration). ' The program works only when the GMM AM08 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: Config Adc, Start Adc, Getadc, Local. ' 05/07/10: uk_BASAVR_016.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 M8DEF.DAT file into the directory where the ' BASCOM AVR is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Chip" set: ' Chip: m8def.dat ' XRAM: None ' HW Stack: 64 ' Soft Stack: 32 ' Framesize: 64 ' XRAM waitstate: disabled ' External Access Enable: disabled ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM AM08, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' 4) At the end of compilation, after the code is programmed on GMM AM08, open ' the terminal emulation window of BASCOM AVR with the option: Tools | ' Terminal emulator (Ctrl+T) and then reset or powen on the Mini Module. '************************* Compiler directives ********************************* $regfile "M8DEF.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $crystal = 7372800 ' Microcontroller crystal frequency $hwstack = 64 ' Hardware stack space $swstack = 32 ' Software stack space $framesize = 64 ' Frame space $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 jumpers must be set in following positions: ' 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 AM08 GMM AM08 signal ' TST1.Row0 33 27 ADC7 ADC7 ' TST1.Row1 32 26 PC0 ADC0 PC0 ' TST1.Row2 31 25 PC1 ADC1 PC1 ' TST1.Row3 30 24 PB1 OC1A PB1 ' TST1.Col0 29 23 PB0 ICP PB0 ' TST1.Col1 28 22 PB2 OC1B /SS PB2 ' TST1.Col2 27 21 PD6 AIN0 PD6 ' TST1.Col3 26 20 PD7 AIN1 PD7 ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Signal ' PC DB9 GMM TST3 GMM TST3 GMM AM08 GMM AM08 uP ' TX 3 3 9 3 RxD RS232 PD0 ' RX 2 2 10 4 TxD RS232 PD1 ' 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 7 ' A/D input connected to row 0 of GMM TST3 keyboard TST1 Pinrow1 Alias Pinc.0 ' Bit with input signal connected to row 1 of GMM TST3 keyboard TST1 Pinrow2 Alias Pinc.1 ' Bit with input signal connected to row 2 of GMM TST3 keyboard TST1 Pinrow3 Alias Pinb.1 ' Bit with input signal connected to row 3 of GMM TST3 keyboard TST1 Pincol0 Alias Portb.0 ' Bit with output signal connected to column 0 of GMM TST3 keyboard TST1 Pincol1 Alias Portb.2 ' Bit with output signal connected to column 1 of GMM TST3 keyboard TST1 Pincol2 Alias Portd.6 ' Bit with output signal connected to column 2 of GMM TST3 keyboard TST1 Pincol3 Alias Portd.7 ' Bit with output signal connected to column 3 of GMM TST3 keyboard TST1 Pinrx Alias Ddrd.0 ' Bit with direction signal connected to GMM AM08 RxD Pintx Alias Ddrd.1 ' Bit with direction signal connected to GMM AM08 TxD '************************* Constants declaration ******************************* Const Nokey = &HFF ' Value that indicate no key pressed '************************* 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 '************************ 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 '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Call Ini_key() ' Initialize line of matrix keyboard Print ' Separate from previous visualization by showing an empty new line Print " Acquire 4 keys from a 4x4 matrix keyboard" Print "The program show the keys pressed on the first column of TST1 keyboard," Print "of GMM TST3 (* 0 # D)." Print Do ' Begin endless loop Kcol = 0 ' Set current column=the first 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 * 4 ' Obtain key pressed position from column Keypos = Keypos + Krow ' and row starting from 0 Incr Keypos ' Obtain key position starting from 1 Key = Kcode(keypos) ' Get pressed key code from dedicated array Printbin Key ' Visualize 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 Sub Ini_key() ' One of the 8 lines required to manage the 4x4 matrix keyboard, is connected ' to a microcontroller analog signal and thus it is managed through the ' relative A/D converter section Config Adc = Single , Prescaler = Auto , Reference = Avcc ' Initialize A/D section with conversion on request, Start Adc ' best prescaler, Vref=5 V and enable it ' Initialize remaining 7 lines of matrix keyboard connected to digital signals ' of microcontroller Ddrc.0 = 0 ' Initialize signals connected to rows of matrix keyboard Ddrc.1 = 0 ' as digital inputs Ddrb.1 = 0 Pincol0 = 1 ' Initialize signals connected to column of matrix keyboard Ddrb.0 = 1 ' as digital outputs at high level Pincol1 = 1 Ddrb.2 = 1 Pincol2 = 1 Ddrd.6 = 1 Pincol3 = 1 Ddrd.7 = 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" 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() Local Adres As Word ' Local variable for A/D conversion result Krow = Nokey ' Set rows state for no key pressed ' One of the 4 lines connected to rows of 4x4 matrix keyboard, is connected ' to a microcontroller analog signal and thus it is acquired through the ' relative channel of A/D converter section Adres = Getadc(pinrow0) ' Acquire state of A/D input connected to row 0 If Adres < 100 Then ' Check and set row 0 state Krow = 0 End If ' Acquire remaining 3 rows of matrix keyboard connected to digital signals ' of microcontroller 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 '******************* End of subroutines used by program ************************