' ********************************************************************** ' * File: uk_BAS51_076.BAS * ' * Version: 1.1 * ' * Date: 27.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 076 of BASCOM 8051 course. ' 32 bits Counters from optocoupled inputs. ' The program counts the incoming transictions of all the optocoupled inputs, ' connected to dedicated hardware peripheral devices (TIMER1 and interupt). ' The counts are always managed with 32 bits, equal to 4294967296 transictions, ' in order to obtain an high resolution on the maximum number of available ' channels. ' The transictions numbers are continuosly displayed on serial console. By ' changing the status of Opto-In 3,4,5,6 input, connected to CN4 connector of ' GMM TST3 as described in electric diagram, the proper counters are increased. ' The typical usage of this program are for example, counting of the pulses ' generated by phonic wheels, by generic pieces counters, manage the position ' of some monodirectional encoders, 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: CONFIG TIMER2; CONFIG TIMER1; ON TIMER1; ENABLE TIMER1; ' START COUNTER1; STOP COUNTER1; PRIORITY SET TIMER1. ' ' 27/04/15: uk_BAS51_076.BAS - Ver 1.1 - By G.A. ' Prima versione. ' ' '**************************** 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 Config Timer2 = Timer , Gate = Internal , Mode = 2 ' Use TIMER2 as Baud rate generator in place of TIMER1 used for counting '******************************* 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 3 CN4.15 25 19 P3.2 INT0 INT0 ' Opto-In 4 CN4.16 24 18 P3.3 INT1 INT1 ' Opto-In 5 CN4.13 23 17 P3.4 T0 T0 ' Opto-In 6 CN4.14 22 16 P3.5 T1 T1 ' 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 - ' The last 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. Pinopto3 Alias P3.2 ' Bit with signal connected to Optocoupled input 3 Pinopto4 Alias P3.3 ' Bit with signal connected to Optocoupled input 4 Pinopto5 Alias P3.4 ' Bit with signal connected to Optocoupled input 5 Pinopto6 Alias P3.5 ' Bit with signal connected to Optocoupled input 6 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 Tab = &H09 ' Horrizontal Tab 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 Count1l As Word ' 16 bits variable with transictions counted by TIMER1 Dim Count1h As Word ' 16 bits variable with TIMER1 overflows number Dim Cntw As Word ' Word for counts number acquisition Dim Cntopto3 As Long ' 32 bits variable with total transictions of Opto-In 3 Dim Cntopto4 As Long ' 32 bits variable with total transictions of Opto-In 4 Dim Cntopto5 As Long ' 32 bits variable with total transictions of Opto-In 5 Dim Cntopto6 As Long ' 32 bits variable with total transictions of Opto-In 6 '************************ Subroutines declaration ****************************** Declare Sub Get_cnt0() ' Get valid counter from TIMER0 Declare Sub Get_cnt1() ' Get valid counter from TIMER1 Declare Sub Count_optoin5() ' Get total counting number from Opto-In 5 input Declare Sub Count_optoin6() ' Get total counting number from Opto-In 6 input '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Pinopto3 = 1 ' Initialize signal connected to Opto-In 3 as digital input Hlpbit = Pinopto3 Pinopto4 = 1 ' Initialize signal connected to Opto-In 4 as digital input Hlpbit = Pinopto4 Pinopto5 = 1 ' Initialize signal connected to Opto-In 5 as digital input Hlpbit = Pinopto5 Pinopto6 = 1 ' Initialize signal connected to Opto-In 6 as digital input Hlpbit = Pinopto6 Print ' Separate from previous visualization by showing 2 empty new lines on console Print Print " Counts transictions of 4 Optocoupled inputs 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:" Print "Opto-In 3 Opto-In 4 Opto-In 5 Opto-In 6" ' Enable INT0 interrupt used for Opto-In 3 countings Cntopto3 = 0 ' Reset total transictions of Opto-In 3 On Int0 Countint0_irq ' Define interrupt service routine for INT0 Tcon.0 = 1 ' INT0 active on falling edge Priority Set Int0 ' Higher priority for external interrupt INT0 Enable Int0 ' Enable INT0 interrupt ' Enable INT1 interrupt used for Opto-In 4 countings Cntopto4 = 0 ' Reset total transictions of Opto-In 4 On Int1 Countint1_irq ' Define interrupt service routine for INT1 Tcon.2 = 1 ' INT1 active on falling edge Priority Set Int1 ' Higher priority for external interrupt INT1 Enable Int1 ' Enable INT1 interrupt ' Enable TIMER0 as 16 bits counter, with interrupt on overflow 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 Priority Set Timer0 ' Higher priority for interrupt from TIMER0 Enable Timer0 ' Enable TIMER0 overflow interrupt ' Enable TIMER1 as 16 bits counter, with interrupt on overflow Count1h = 0 ' Reset TIMER1 overflow counter Config Timer1 = Counter , Gate = Internal , Mode = 1 ' TIMER1 as 16 bits counter and internal activation Counter1 = 0 ' Reset TIMER1 counter Start Counter1 ' Start TIMER1 counter On Timer1 Counter1_irq ' Define interrupt service routine for TIMER1 overflow Priority Set Timer1 ' Higher priority for interrupt from TIMER1 Enable Timer0 ' Enable TIMER1 overflow interrupt Enable Interrupts ' General interrupts enable Do ' Begin endless loop Print Cntopto3 ; ' Show total counts from Opto-In 3 input Printbin Tab ; Tab ' Align visualizations Print Cntopto4 ; ' Show total counts from Opto-In 4 input Printbin Tab ; Tab Call Count_optoin5() ' Acquire and show total counts number from Opto-In 5 input Print Cntopto5 ; Printbin Tab ; Tab Call Count_optoin6() ' Acquire and show total counts number from Opto-In 6 input Print Cntopto6 ; Printbin Cret ' Mantain visualization on the same row Waitms 50 ' Delay between acquisitions Loop ' End endless loop Stop Counter0 ' Stop TIMER0 counter Stop Counter1 ' Stop TIMER1 counter Disable Interrupts ' General interrupts disable End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Service routine for INT0 external interrupt: it increases a specific variable ' that counts the occured transictions. ' The subroutine changes the microprocessor's registers and so it has been ' declared without the Nosave directive, in order to let them uncanged. ' Input: Cntopto3 = INTO variations number ' Output: Cntopto3 = increased INTO variations number Countint0_irq: Incr Cntopto3 ' Increase INT0 variations counter Return ' Service routine for INT1 external interrupt: it increases a specific variable ' that counts the occured transictions. ' The subroutine changes the microprocessor's registers and so it has been ' declared without the Nosave directive, in order to let them uncanged. ' Input: Cntopto4 = INT1 variations number ' Output: Cntopto4 = increased INT1 variations number Countint1_irq: Incr Cntopto4 ' Increase INT1 variations counter Return ' 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 TIMER0 overflow counter Return ' Interrupt service routine associated to TIMER1 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: Count1h = TIMER1 overflows number ' Output: Count1h = increased TIMER1 overflows number Counter1_irq: Incr Count1h ' Increase TIMER1 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 current counter from TIMER1 by ensuring that read value has no aliasing ' problems caused by overflow of less significative byte. ' This subroutine replaces Counter1 instructions of BASCOM 8051 that can't be ' used as it stops the counter of TIMER1. ' Input: None ' Output: Count1l = Current 16 bits value of TIMER1 Sub Get_cnt1() Cnth = Th1 ' Acquire byte H,L from TIMER1 Cntl = Tl1 If Cnth <> Th1 Then ' If byte L overflow occured Cnth = Th1 ' Acquire again byte H,L from TIMER1 Cntl = Tl1 End If Count1l = Cnth ' Obtain 16 bits counter from acquired byte H,L Shift Count1l , Left , 8 Count1l = Count1l 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 ' Acquire 32 bits total counts number of Opto-In 6 transictions, by ensuring ' that read value has no aliasing problems caused by overflows occured during ' execution. ' Input: None ' Output: Cntopto6 = Current 32 bits count Sub Count_optoin6() Cntw = Count1h ' Save overflow number that is the 16 bit most significative of total count Call Get_cnt1() ' Acquire valid counter from TIMER1 If Cntw <> Count1h Then ' If TIMER1 overflow occured Call Get_cnt1() ' Acquire again valid counter from TIMER1 Cntw = Count1h ' Save again overflow number End If Cntopto6 = Cntw ' Obtain 32 bits total count number Shift Cntopto6 , Left , 16 ' 16 bits most significative from overflow number Cntopto6 = Cntopto6 Or Count1l ' 16 bits less significative from TIMER1 counter End Sub '******************* End of subroutines used by program ************************