' ********************************************************************** ' * File: uk_BAS51_075.BAS * ' * Version: 1.1 * ' * Date: 26.04.15 * ' * 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 075 of BASCOM 8051 course. ' 32 bits Counter from an optocoupled input. ' The program counts the incoming transictions of the optocoupled inputs, ' connected to TIMER1. The number of managed counts is not limited to maximum ' 16 bits of TIMER0 but it is doubled up to 32 bits, equal to 4294967296 ' transictions. The program describes the modality used to increase the counter ' resolution and the user can adapt it according with his requirements. ' The transictions number is continuosly displayed on serial console. By ' changing the status of Opto-In 5 input, connected to CN4 connector of GMM TST3 ' as described in electric diagram, the T0 signal is modified and counted as it ' is the counter input signal. The overflows of the counter are recognized by ' proper interrupt routine and then counted too, in order to increase the counts ' resolution. ' The typical usage of this program are for example, counting of the pulses ' generated by a phonic wheel, a generic pieces counter, manage the position ' of a monodirectional encoder, etc. ' The program describes its functionalities and uses a serial console provided ' of monitor and keyboard 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!! ' In the program source the names that identifies the used signals refers to ' electric diagram and technical manual of GMM TST3!! ' ' Added instructions: none. ' ' 26/04/15: uk_BAS51_075.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 tables. ' !!! Note: On GMM TST3 the jumpers must be configured as below described: ' 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 !!! ' ' External GMM TST3 GMM TST3 GMM 5115 GMM 5115 Used uP ' signal resource Z2 pin pin signal signal ' Opto-In 5 CN4.13 23 17 P3.4 T0 T0 ' GND CN4.17 20 14 GND - ' +5 Vdc CN4.18 34 28 +5 Vdc - ' ' PC COMx GMM TST3 GMM TST3 GMM 5115 GMM 5115 Used uP ' signal DB9 pin CN5 pin Z2 pin pin signal 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 a direct cable. Grifo(r) can supply ' it by requesting the CCR 9+9E code. Pinopto5 Alias P3.4 ' Bit with input signal connected to Optocoupled input 5 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 '************************* Variables declaration ******************************* Dim Hlpbit As Bit ' General purpose bit variable Dim Cnth As Byte ' Byte H for counter acquisition Dim Cntl As Byte ' Byte L for counter acquisition Dim Count0l As Word ' 16 bits variable with transictions counted by TIMER0 Dim Count0h As Word ' 16 bits variable with TIMER0 overflows number Dim Cntw As Word ' Word for counts number acquisition Dim Cntopto5 As Long ' 32 bits variable with total transictions (union of previous 2) '************************ Subroutines declaration ****************************** Declare Sub Get_cnt0() ' Get valid counter from TIMER0 Declare Sub Count_optoin5() ' Get total counting number from Opto-In 5 input '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Pinopto5 = 1 ' Initialize signal connected to Opto-In 5 as digital input Hlpbit = Pinopto5 Print ' Separate from previous visualization by showing 2 empty new lines on console Print Print " Counts transictions of an Optocoupled input with 32 bits" Print "Mount Mini Module on Z2 of GMM TST3, connect circuit for buffered Optocoupled" Print "inputs to CN4, as described in electric diagram." Print "By taking advantages from adopted solutions, the program counts also electric" Print "spikes, normally generated by transictions of mechanical contacts." Print "Number of counted transictions:" ' Enable TIMER0 as 16 bits counter, with interrupt on overflow, used for Opto-In 5 counts Count0h = 0 ' Reset TIMER0 overflow counter Config Timer0 = Counter , Gate = Internal , Mode = 1 ' TIMER0 as 16 bits counter and internal activation Counter0 = 0 ' Reset TIMER0 counter Start Counter0 ' Start TIMER0 counter On Timer0 Counter0_irq ' Define interrupt service routine for TIMER0 overflow Enable Timer0 ' Enable TIMER0 overflow interrupt Enable Interrupts ' General interrupts enable Do ' Begin endless loop Call Count_optoin5() ' Acquire total counts number from Opto-In 5 input Print Cntopto5 ; " "; ' Show total counts on console Printbin Cret ' Mantain visualization on the same row Waitms 50 ' Delay between acquisitions Loop ' End endless loop Stop Counter0 ' Stop TIMER0 counter Disable Interrupts ' General interrupts disable End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Interrupt service routine associated to TIMER0 overflow: it increases ' specific variable that counts the occured overflows. ' The interrupt service routine uses microprocessor registers and it has been ' declared without Nosave directive, in order to mantain them unchanged. ' Input: Count0h = TIMER0 overflows number ' Output: Count0h = increased TIMER0 overflows number Counter0_irq: Incr Count0h ' Increase overflow counter Return ' Acquire current counter from TIMER0 by ensuring that read value has no aliasing ' problems caused by overflow of less significative byte. ' This subroutine replaces Counter0 instructions of BASCOM 8051 that can't be ' used as it stops the counter of TIMER0. ' Input: None ' Output: Count0l = 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 occured Cnth = Th0 ' Acquire again byte H,L from TIMER0 Cntl = Tl0 End If Count0l = Cnth ' Obtain 16 bits counter from acquired byte H,L Shift Count0l , Left , 8 Count0l = Count0l Or Cntl End Sub ' Acquire 32 bits total counts number of Opto-In 5 transictions, by ensuring ' that read value has no aliasing problems caused by overflows occured during ' execution. ' Input: None ' Output: Cntopto5 = Current 32 bits count Sub Count_optoin5() Cntw = Count0h ' Save overflow number that is the 16 bit most significative of total count Call Get_cnt0() ' Acquire valid counter from TIMER0 If Cntw <> Count0h Then ' If TIMER0 overflow occured Call Get_cnt0() ' Acquire again valid counter from TIMER0 Cntw = Count0h ' Save again overflow number End If Cntopto5 = Cntw ' Obtain 32 bits total count number Shift Cntopto5 , Left , 16 ' 16 bits most significative from overflow number Cntopto5 = Cntopto5 Or Count0l ' 16 bits less significative from TIMER0 counter End Sub '******************* End of subroutines used by program ************************