' ********************************************************************** ' * File: uk_BAS51_040.BAS * ' * Version: 1.1 * ' * Date: 30.01.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 040 of BASCOM 8051 course. ' Frequency meter. ' The program measures the frequency of the input signal of TIMER0 used as ' 16 bits counter. The frequency is calculated as number of occurred transitions ' in a time unit equal to 0.25 seconds, and it is continuously displayed on ' serial console. By pressing T11 button of a TIO 16, connected to CN4 connector ' of GMM TST3, it is changed the measured signal. ' 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 8051 (see IDE Configuration). ' The program works only when the GMM 5115 is mounted on Z2 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: Counter0, Start Counter0, Stop Counter0. ' ' 10/01/11: uk_BAS51_039.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 table: ' !!! Note: On GMM TST3 the following jumpers must be properly configured: ' 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!!! ' ' TIO 16 GMM TST3 pin Z2 pin Signal Used up ' button resource GMM TST3 GMM 5115 GMM 5115 signal ' T11 CN4.13 23 17 P3.4 T0 T0 ' ' 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. Pint11 Alias P3.4 ' Signal connected to yellow button T11 Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* Const Cret = &H0D ' Carriage Return ASCII code Const Dtfreq = 0.25 ' Time unit for frequency measure, in seconds Const Delfreq =(dtfreq * 1000) - 12 ' Delay for frequency measure, in milliseconds '************************* Variables declaration ******************************* Dim Hlpbit As Bit ' General purpose help bit variable Dim Cnth As Byte ' Byte H for counter acquisition Dim Cntl As Byte ' Byte L for counter acquisition Dim Count0 As Word ' Variable with variations counted by TIMER0 Dim Oldcount0 As Word ' Previous variations counted by TIMER0 Dim Dtcount0 As Word ' Variations number in the time unit Dim Freq As Single ' Variable for measured frequency '************************ Subroutines declaration ****************************** Declare Sub Get_cnt0() ' Acquire valid counter from TIMER0 '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Pint11 = 1 ' Initialize signal connected to T11 of TIO 16 as digital input Hlpbit = Pint11 Print ' Separate from previous visualization by showing an empty new line Print Print " Measure frequency of T0 input signal of TIMER0" Print "Mount Mini Module on Z2 of GMM TST3, connect TIO 16 to CN4 and press T11 button." ' Obtain and show on console the features of performed measure Freq = 1 / Dtfreq ' Obtain resolution of the frequency measure Print "The measure has a resolution=" ; Freq ; " Hz and F.max="; Freq = 65535 * Freq ' Obtain maximum measurable frequency Print Freq ; " Hz" Print "Measured frequency (Hz):" ' Arrange TIMER0 for the frequency measure Config Timer0 = Counter , Gate = Internal , Mode = 1 ' TIMER0 as 16 bits counter and internal enable Counter0 = 0 ' Reset TIMER0 counter Oldcount0 = 0 ' Reset previous counter of TIMER0 Start Counter0 ' Start TIMER0 counter Do ' Begin endless loop ' The frequency measure is performed by counting the variations number of ' the signal occurred in a time unit, related to one second. When, for ' example, the time unit is 1/4=0,25 seconds and in this time the timer ' counts 100 variations, then the frequency will be 100/0,25=400 Hz. ' It is advantageous select a low time unit, in order to increase the ' maximum measurable frequency; this coincides with the 65.535 maximum ' variations of 16 bits counter in the same time unit. The previous ' instructions calculate and visualize either the resolution and maximum ' frequency, just obtained from the selected time unit, defined by the ' program constant Dtfreq. ' The time unit is performed with a delay that take into account even the ' processing time required to calculate the frequency, experimentally ' calibrated. Call Get_cnt0() ' Acquire valid counter from TIMER0 Dtcount0 = Count0 - Oldcount0 ' Obtain variation number in the time unit Oldcount0 = Count0 ' Update previous counter of TIMER0 Freq = Dtcount0 ' Obtain frequency from variation number Freq = Freq / Dtfreq ' in the time unit Print Freq ; " "; ' Show obtained frequency on console Printbin Cret ' Mantain visualization on same row Waitms 50 ' Delay between acquisition Loop ' End endless loop Stop Counter0 ' Stop TIMER0 counter End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Acuire current counter from TIMER0 by ensuring that read value has no aliasing ' problem caused by less significant byte overflow. ' This subroutine replaces the instruction Counter0 of BASCOM 8051 that can't ' be used as it stops TIMER0. ' Input: None ' Output: Count0 = Current 16 bits value of TIMER0 Sub Get_cnt0() Cnth = Th0 ' Acquire byte H,L from TIMER0 Cntl = Tl0 If Cnth <> Th0 Then ' If byte L overflow happened Cnth = Th0 ' Re acquire byte H,L from TIMER0 Cntl = Tl0 End If Count0 = Cnth ' Obtain 16 bits counter from acquired byte H,L Shift Count0 , Left , 8 Count0 = Count0 Or Cntl End Sub '******************* End of subroutines used by program ************************