' ********************************************************************** ' * File: uk_BASAVR_038.BAS * ' * Version: 1.1 * ' * Date: 10.01.11 * ' * 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 38 of BASCOM AVR course. ' Reflexes test. ' The program pre-notice the user with two beeps, then it activates one of ' the two LEDs of GMM TST3 (L2,L3) after a randomic delay time and it waits ' the pressure of corresponding button (T1,T2), by measuring the time elapsed ' between LED deactivation and button pressure, through TIMER1. A third audible ' beep indicates the button pressure or a 2 seconds maximum timeout. Finally ' it is shown the test result (measured time) on the console. ' The program describe its functionalities and uses a serial console provided ' of monitor and keyboard 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 terms that identify the used signals refers to electric ' diagram and technical manual of GMM TST3!! ' ' Added instructions: Rnd, Fusing. ' ' 10/01/11: uk_BASAVR_038.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 ; J2 in 2-3 ; J3 in 1-2 ; J8 in 2-3 !!! ' GMM TST3 pin Z1 pin Signal Signal ' resource GMM TST3 GMM AM08 GMM AM08 uP ' LED L2,Button T1 12 6 PC5 ADC5 SCL PC5 ' LED L3,Button T2 13 7 PC4 ADC4 SDA PC4 ' BZ1 Buzzer 15 9 PB4 MISO PB4 ' ' 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. Pinl2 Alias Portc.5 ' Bit with output signal connected to red LED L2 Pint1 Alias Pinc.5 ' Bit with input signal connected to red button T1 Pinl3 Alias Portc.4 ' Bit with output signal connected to green LED L3 Pint2 Alias Pinc.4 ' Bit with input signal connected to green button T2 Pinbz1 Alias Portb.4 ' Bit with output signal connected to buzzer BZ1 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 Tmrper = 1000 /(7372800 / 8) ' Counting period of TIMER1 in msec '************************* Variables declaration ******************************* Dim T1stat As Bit ' Boolean variable with red button T1 status Dim T2stat As Bit ' Boolean variable with green button T2 status Dim Pressok As Bit ' Boolean variable with correct button pressure Dim Dtbeep As Word ' Beep duration generated by buzzer Dim Deldt As Word ' Random value for delayed LED activation Dim Dtpressl As Byte ' Passed time up to button pressure: byte L Dim Dtpressm As Byte ' Passed time up to button pressure: byte M Dim Dtpressh As Byte ' Passed time up to button pressure: byte H Dim Dtpress As Long ' Passed time up to button pressure: byte H,M,L Dim Dttime As Single ' Passed time up to button pressure in msec Dim Dtstr As String * 10 ' String with formatted passed time Dim Hlpb As Byte ' General purpose byte help variable Dim Hlpl As Long ' General purpose long help variable '************************ Subroutines declaration ****************************** Declare Sub Beep(byval Dtbeep As Word) ' Generate beep with GMM TST3 buzzer '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Pinbz1 = 1 ' Initialize signal connected to BZ1 as digital output at high level Ddrb.4 = 1 Pinl2 = 1 ' Initialize signal connected to L2 at high level, equal to disabled Pinl3 = 1 ' Initialize signal connected to L3 at high level, equal to disabled Print ' Separate from previous visualization by showing an empty new line Print Print " Test reflexes with LEDs and buttons of GMM TST3" Print "Mount Mini Module on Z1 of GMM TST3." Print "Tests under execution..." Config Timer1 = Timer , Prescale = 8 ' TIMER1 as 16 bit timer with internal activation Do ' Begin endless loop Print "Test started" ' Generate two beep with GMM TST3 buzzer Call Beep(200) ' Generate first long beep (200 msec) Waitms 500 ' Delay between two beeps Call Beep(100) ' Generate second short beep (100 msec) ' Perform random delay up to 5 sec Deldt = Rnd(5000) ' Obtain randomic number from 0 to 5000 Waitms Deldt ' Activate randomly one of the two LEDs L2,L3 for 100 msec Ddrc.5 = 1 ' Initialize signal connected to L2 as digital output at high level Ddrc.4 = 1 ' Initialize signal connected to L3 as digital output at high level Deldt = Deldt And &H0408 ' Mantains some bits of randomic delay If Deldt = 0 Then ' Determine which LED must be enabled Pinl2 = 0 ' Enable red L2 Else Pinl3 = 0 ' Enable green L3 End If Waitms 100 ' Mantain LED enabled for 100 msec Pinl2 = 1 ' Disable LEDs and set lines as inputs Pinl3 = 1 Ddrc.5 = 0 ' Initialize signal connected to T1 as digital input Ddrc.4 = 0 ' Initialize signal connected to T2 as digital input ' Enable TIMER1 in order to measure the time passed up to pressure of T1,T2 ' button or timeout of about 2 secs. The TIMER1 is updated with a frequency ' equal to Clock frequency/8 and its 16 bits overflow happens about every ' 71 msec = 65536*(1/(7372800/8)). ' For this reason in 2 secs occour 28 overflows = 2/0.071. Timer1 = 0 ' Reset TIMER1 Start Timer1 ' Enable TIMER1 Dtpressh = 0 ' Clear byte H of passed time up to button pressure Dtpressm = 0 ' Clear byte M of passed time up to button pressure Do Dtpressl = Tcnt1l ' Acquire current TIMER1 value and save it in byte L Hlpb = Tcnt1h ' and on help variable in order to avoid partial variations If Hlpb < Dtpressm Then ' If occured overflow on TIMER1 Incr Dtpressh ' Increase byte H of passed time End If Dtpressm = Hlpb ' Complete saving byte M Loop Until Pint1 = 0 Or Pint2 = 0 Or Dtpressh > 28 ' Wait pressure of T1,T2 or 2 secs timeout Stop Timer1 ' Stop TIMER1 T1stat = Pint1 ' Save red button T1 status T2stat = Pint2 ' Save green button T2 status ' Obtain passed time in msec Hlpl = Dtpressh ' Shift byte H of passed time 16 bits to the left Shift Hlpl , Left , 16 Dtpress = Hlpl ' Save shifted byte H Hlpl = Dtpressm ' Shift byte M of passed time 8 bits to the left Shift Hlpl , Left , 8 Dtpress = Dtpress Or Hlpl ' Save shifted byte M Hlpl = Dtpressl ' Save byte L without shift Dtpress = Dtpress Or Hlpl ' Dttime contain passed time counter with 3 bytes H,M,L Dttime = Dtpress * Tmrper ' Obtain passed time in msec Dtstr = Fusing(dttime , "0000.###") ' Mantains significant digits of passed time ' Show on console the result of the just performed reflexes test Pressok = 0 ' Wrong button pressure If Deldt = 0 Then ' Check wich LED has been turned on Print "Activated red L2, "; If T1stat = 0 Then ' If red T1 pressed Print "pressed red T1 in " ; Dtstr ; " msec: OK"; Pressok = 1 ' Correct button pressure Else If T2stat = 0 Then ' If green T2 pressed Print "pressed green T2 in " ; Dtstr ; " msec: ERROR"; Else Print "no button pressed: ERROR"; End If End If Else Print "Activated green L3, "; If T2stat = 0 Then ' If green T2 pressed Print "pressed green T2 in " ; Dtstr ; " msec: OK"; Pressok = 1 ' Correct button pressure Else If T1stat = 0 Then ' If red T1 pressed Print "pressed red T1 in " ; Dtstr ; " msec: ERROR"; Else Print "no button pressed: ERROR"; End If End If End If Print If Pressok = 1 Then ' If correct button pressure Call Beep(100) ' Signal end of test with short beep Else ' Wrong button pressure Call Beep(500) ' Signal end of test with long beep End If Waitms 3000 ' 3 secs delay before to restart new test Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Generate beep with buzzer of GMM TST3 with passed duration in msec, into ' dtbeep argument parameter Sub Beep(byval Dtbeep As Word) Pinbz1 = 0 ' Enable buzzer for beep Waitms Dtbeep ' Perform delay for beep duration Pinbz1 = 1 ' Disable buzzer End Sub '******************* End of subroutines used by program ************************