' ********************************************************************** ' * File: uk_BASAVR_075.BAS * ' * Version: 1.1 * ' * Date: 27.03.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 075 of BASCOM AVR 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 TIMER1 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 6 input, connected to CN4 connector of GMM TST3 ' as described in electric diagram, the T1 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 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: none. ' ' 23.03.15: uk_BASAVR_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 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 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. Pinopto6 Alias Pind.5 ' Bit with input signal connected to Optocoupled input 6 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 '************************* Variables declaration ******************************* 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 Cntopto6 As Long ' 32 bits variable with total transictions (union of previous 2) '************************ Subroutines declaration ****************************** Declare Sub Get_cnt1() ' Get valid counter from TIMER1 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 signal used as digital input in order to use the alternative ' function as counter. Moreover it is enabled the internal pull up on the same ' signal to avoid possible coupling when it is not connected. Pupopto6 = 1 ' Enable pull up on signal connected to Opto-In 6 input Config Pinopto6 = Input ' Initialize signal connected to Opto-In 6 as digital input 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 TIMER1 as 16 bits counter, with interrupt on overflow Count1h = 0 ' Reset TIMER1 overflow counter Config Timer1 = Counter , Edge = Falling , Noise Cancel = 0 ' TIMER1 as 16 bits counter on falling edge, without noises filter 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 Call Count_optoin6() ' Acquire total counts number from Opto-In 6 input Print Cntopto6 ; " "; ' Show total counts on console Printbin Cret ' Mantain visualization on the same row Waitms 50 ' Delay between acquisitions Loop ' End endless loop Stop Counter1 ' Stop TIMER1 counter Disable Interrupts ' General interrupts disable End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' 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 overflow counter Return ' 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 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 ************************