' ********************************************************************** ' * File: uk_BASAVR_076.BAS * ' * Version: 1.1 * ' * Date: 18.04.15 * ' * 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 076 of BASCOM AVR course. ' 32 bits Counters from optocoupled inputs. ' The program counts the incoming transictions of all the optocoupled inputs, ' connected to dedicated hardware peripheral devices (TIMER 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 inputs, 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 AVR (see IDE Configuration). ' The program works only when the GMM AM08 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 TIMER1; ON OVF1; ENABLE OVF1; START COUNTER1; ' STOP COUNTER1. ' ' 18/04/15: uk_BASAVR_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 M8DEF.DAT file into the directory where the ' BASCOM AVR is installed, copy it if not present and then restart the IDE. ' 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 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 AM08 GMM AM08 Used uP ' signal resource Z2 pin pin signal signal ' Opto-In 3 CN4.15 25 19 PD2 INT0 INT0 ' Opto-In 4 CN4.16 24 18 PD3 INT1 INT1 ' Opto-In 5 CN4.13 23 17 PD4 T0 XCK T0 ' Opto-In 6 CN4.14 22 16 PD5 T1 T1 ' GND CN4.17 20 14 GND - ' +5 Vdc CN4.18 34 28 +5 Vdc - ' ' PC COMx GMM TST3 GMM TST3 GMM AM08 GMM AM08 Used uP ' signal DB9 pin CN5 pin Z2 pin pin signal signal ' 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 a direct cable. Grifo(r) can supply ' it by requesting the CCR 9+9E code. Pinopto3 Alias Pind.2 ' Bit with input signal connected to Optocoupled input 3 Pinopto4 Alias Pind.3 ' Bit with input signal connected to Optocoupled input 4 Pinopto5 Alias Pind.4 ' Bit with input signal connected to Optocoupled input 5 Pinopto6 Alias Pind.5 ' Bit with input signal connected to Optocoupled input 6 Pupopto3 Alias Portd.2 ' Bit for pull up enable on signal connected to Optocoupled input 3 Pupopto4 Alias Portd.3 ' Bit for pull up enable on signal connected to Optocoupled input 4 Pupopto5 Alias Portd.4 ' Bit for pull up enable on signal connected to Optocoupled input 5 Pupopto6 Alias Portd.5 ' Bit for pull up enable on signal connected to Optocoupled input 6 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 Cret = &H0D ' Carriage Return ASCII code Const Tab = &H09 ' Horrizontal Tab ASCII code '************************* Variables declaration ******************************* Dim Count0l As Byte ' 8 bits variable with transictions counted by TIMER0 Dim Count0h As Long ' 32 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 countings number acquisition Dim Cntl As Long ' Long for countings 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 = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs ' Initialize the signals used as digital inputs in order to use the alternative ' function as counters. Moreover they are enabled the internal pull up on the same ' signals to avoid possible coupling when they are not connected. Pupopto3 = 1 ' Initialize signal connected to Opto-In 3 as digital input with pull up Config Pinopto3 = Input Pupopto4 = 1 ' Initialize signal connected to Opto-In 4 as digital input with pull up Config Pinopto4 = Input Pupopto5 = 1 ' Initialize signal connected to Opto-In 5 as digital input with pull up Config Pinopto5 = Input Pupopto6 = 1 ' Initialize signal connected to Opto-In 6 as digital input with pull up Config Pinopto6 = Input 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 Config Int0 = Falling ' INT0 active on falling edge 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 Config Int1 = Falling ' INT1 active on falling edge Enable Int1 ' Enable INT1 interrupt ' Enable TIMER0 as 8 bits counter, with interrupt on overflow Count0h = 0 ' Reset TIMER0 overflow counter Config Timer0 = Counter , Edge = Falling ' TIMER1 as 8 bits counter on falling edge Counter0 = 0 ' Reset TIMER0 counter Start Counter0 ' Start TIMER0 counter On Ovf0 Counter0_irq ' Define interrupt service routine for TIMER0 overflow Enable Ovf0 ' Enable TIMER0 overflow interrupt ' Enable TIMER1 as 16 bits counter, with interrupt on overflow Count1h = 0 ' Reset TIMER1 overflow counter Config Timer1 = Counter , Edge = Falling ' TIMER1 as 16 bits counter on falling edge Counter1 = 0 ' Reset TIMER1 counter Start Counter1 ' Start TIMER1 counter On Ovf1 Counter1_irq ' Define interrupt service routine for TIMER1 overflow Enable Ovf1 ' 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 100 ' 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. This device has only 8 bits and the read ' value has no aliasing problems caused by overflow. ' This subroutine coincides with Counter0 instructions of BASCOM AVR and it has ' been developed for compatibility and code exchange reasons. ' Input: None ' Output: Count0l = Current 8 bits value of TIMER0 Sub Get_cnt0() Count0l = Counter0 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 coincides with Counter1 instructions of BASCOM AVR and it has ' been developed for compatibility and code exchange reasons. ' Input: None ' Output: Count1l = Current 16 bits value of TIMER1 Sub Get_cnt1() Count1l = Counter1 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() Cntl = Count0h ' Save overflow number that is the 24 bits most significative of total count Call Get_cnt0() ' Acquire valid counter from TIMER0 If Cntl <> Count0h Then ' If TIMER0 overflow occured Call Get_cnt0() ' Acquire again valid counter from TIMER0 Cntl = Count0h ' Save again overflow number End If Cntopto5 = Cntl ' Obtain 32 bits total count number Shift Cntopto5 , Left , 8 ' 24 bits most significative from overflow number Cntopto5 = Cntopto5 Or Count0l ' 8 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 ************************