' ********************************************************************** ' * File: uk_BAS51_061.BAS * ' * Version: 1.1 * ' * Date: 25.03.12 * ' * 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 061 of BASCOM 8051 course. ' A/D converter: through a menu the user can choose the used conversion modality ' among the following ones: ' - single conversion of all the inputs for each key pressure; ' - averaged conversion of all the inputs for each key pressure; ' - single conversion of all the inputs automatically repeated; ' - averaged conversion of all the inputs automatically repeated; ' When averaged conversion is selected, the user has the possibility to define ' the used number of conversions, too. ' The GMM 5115 Mini Module provides only 7 analog inputs, of the 8 available on ' microcontroller, that are acquired and displayed in every modalities. ' The program describes its functionalities and shows conversions on 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!! ' ' Added instructions: None. ' ' 25/03/12: uk_BAS51_061.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 jumpers must be set in following positions: ' 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!!! ' ' GMM TST3 pin Z2 pin Signal Used uP ' resource GMM TST3 GMM 5115 GMM 5115 signal ' CN4.1 32 26 P1.1 ADC1 T2EX ADC1 ' CN4.4 31 25 P1.2 ADC2 ECI ADC2 ' CN4.3 30 24 P1.3 ADC3 CEX0 ADC3 ' CN4.6 29 23 P1.4 ADC4 CEX1 ADC4 ' CN4.5 28 22 P1.5 ADC5 ADC5 ' CN4.8 27 21 P1.6 ADC6 ADC6 ' CN4.7 26 20 P1.7 ADC7 ADC7 ' R4+R5 7 1 Vref Vref ' CN4.17 20 14 GND - ' ' 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 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. Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* Const Cret = 13 ' Carriage return ASCII code Const Qadc = 2.48 / 1023 ' A/D converter resolution with full scale voltage=Vref=2.48 V '************************* Variables declaration ******************************* Dim Choice As Byte ' Operation selected on console Dim Chadc As Byte ' Channel of A/D converter section Dim Cmbadc As Word ' Combination obtained from A/D converter Dim Ncnv As Byte ' A/D conversions number for average Dim Idxadc As Byte ' A/D conversions index for average Dim Avgshift As Byte ' Number of bits to shift in order to obtain conversions average Dim Sumadc As Word ' A/D conversions sum for average Dim Avgadc As Word ' Averaged combination obtained from A/D converter Dim Voltadc As Single ' Voltage acquired from A/D converter Dim Gstr As String * 6 ' String used to visualize formatted voltage Dim Hlpb As Byte ' General purpose byte variabile '************************ Subroutines declaration ***************************** Declare Sub Ini_adc() ' Initialize A/D converter section Declare Sub Get_adc(chadc As Byte) ' Convert analog input channel of A/D converter Declare Sub Getavg_adc(chadc As Byte , Ncnv As Byte) ' Acquire A/D converter input with average Declare Sub Shwconn_adc() ' Show analog inputs connections legend Declare Sub Shwonecnv_adc() ' Acquire and show analog inputs with single conversion Declare Sub Shwavgcnv_adc() ' Acquire and shows analog inputs with averaged conversion '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Ncnv = 8 ' Number of conversion for A/D average (multiple of 2, <=64) Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Conversions of GMM 5115 analog inputs" Print "Mount Mini Module on Z2 of GMM TST3, connect analog inputs to convert" Print "to GMM TST3, as described in following table." Print "For each inputs it is displayed the relative voltage on the console." Call Ini_adc() ' Initialize lines and A/D converter section, used by program Do ' Begin endless loop Print ' Show menu with possible operations Print " S) Single conversions when key pressed" Print " A) Averaged conversions when key pressed" Print " s) Single conversions repeated" Print " a) Averaged conversions repeated" Print "Perform the choice by typing the associated key: "; Choice = Waitkey() ' Wait selection of operation to perform Printbin Choice ' Show performed choice Print Print ' Separate menu from following visualizations Select Case Choice ' Check converted choice Case "S": ' Single conversions when key pressed has been selected Print "Press F to stop, other keys to convert." Call Shwconn_adc() ' Display legend with analog inputs connections Do ' Begin conversion loop Call Shwonecnv_adc() ' Acquire and show analog inputs with single conversions Choice = Waitkey() ' Wait key pressed on console Loop Until Choice = "F" ' Repeat until "F" key is pressed Case "A": ' Averaged conversions when key pressed has been selected Input "Insert conversions number for average (multiple of 2, <=64): " , Ncnv Print "Press F to stop, other keys to convert." Call Shwconn_adc() ' Display legend with analog inputs connections Do ' Begin conversion loop Call Shwavgcnv_adc() ' Acquire and show analog inputs with averaged conversions Choice = Waitkey() ' Wait key pressed on console Loop Until Choice = "F" ' Repeat until "F" key is pressed Case "s": ' Single conversions repeated has been selected Print "Conversions under execution; press F to stop." Call Shwconn_adc() ' Display legend with analog inputs connections Do ' Begin conversion loop Call Shwonecnv_adc() ' Acquire and show analog inputs with single conversions Waitms 50 ' Delay between conversions Choice = Inkey() ' Check key pressed on console Loop Until Choice = "F" ' Repeat until "F" key is pressed Case "a": ' Averaged conversions repeated has been selected Input "Insert conversions number for average (multiple of 2, <=64): " , Ncnv Print "Conversions under execution; press F to stop." Call Shwconn_adc() ' Display legend with analog inputs connections Do ' Begin conversion loop Call Shwavgcnv_adc() ' Acquire and show analog inputs with averaged conversions Waitms 50 ' Delay between conversions Choice = Inkey() ' Check key pressed on console Loop Until Choice = "F" ' Repeat until "F" key is pressed Case Else: ' Not valid selection Printbin &H07 ' Generate an audible advise BEL on console End Select Print ' Separate from following visualization Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Initialize resources, variables and peripheral devices used for A/D conversion. ' Input: None ' Output: None Sub Ini_adc() ' Define multifunctions signals connected to used A/D inputs P1 = &HFF ' Set signals connected to ADC1÷ADC7 as digital input Hlpb = P1 Adcf = &HFE ' Set signals connected to ADC1÷ADC7 as analog input Adclk = &H10 ' Set A/D clock to 50% End Sub ' Convert channel of A/D converter in polling and it returns the obtained ' 10 bits combination ' Input: Chadc = channel to convert ' Output: Cmbadc = combination obtained from conversion Sub Get_adc(chadc As Byte) ' Perform conversion of passed channel Adcon = Chadc Or &H20 ' Enable A/D and select channel NOP ' 4 usec delay as Tsetup settling time of Sample & Hold NOP NOP NOP NOP Adcon = Chadc Or &H28 ' Start conversion Do ' Loop that wait end of conversion Hlpb = Adcon And &H10 ' Acquire A/D control register and mantain bit End Of Conversion Loop Until Hlpb = &H10 ' Repeat until bit End Of Conversion is active ' Obtain 10 bits complete combination from performed conversion Hlpb = Addl ' Acquire byte L of combination Cmbadc = Addh ' Acquire byte H of combination Adcon = &H00 ' Disable A/D by setting standby mode Hlpb = Hlpb And &H03 ' Transform byte L of combination by mantaining 2 Bits Shift Cmbadc , Left , 2 ' Transform byte H of combination by mantaining 8 Bits Cmbadc = Cmbadc Or Hlpb ' Obtain complete 10 bits combination Cmbadc = Cmbadc And &H03FF ' Mask only the 10 Bits meaningfull End Sub ' Perform a serie of conversions on one A/D converter input, in polling modality ' and it returns the obtained 10 bits averaged combination. ' Input: Chadc = channel to convert ' Ncnv = number of conversion for average (multiple of 2, <=64) ' Output: Avgadc = combination obtained from conversion Sub Getavg_adc(chadc As Byte , Ncnv As Byte) ' Calculate number of bits to shift, in order to obtain averaged conversions Avgshift = 0 ' Reset number of bits to shift Idxadc = Ncnv ' Copy number of conversions for average Do Incr Avgshift ' Increase number of bits to shift Shift Idxadc , Right , 1 ' Shift copy number of conversion for average Loop Until Idxadc <= 1 ' Repeat until conversions completed Sumadc = 0 ' Clear sum of A/D converter combinations for average For Idxadc = 1 To Ncnv ' Repeat cycle for average conversion number Call Get_adc(chadc) ' Convert A/D converter channel for average Sumadc = Sumadc + Cmbadc ' Update sum of A/D converter combinations for average Next Idxadc Avgadc = Sumadc ' Obtain average from sum of A/D converter combinations Shift Avgadc , Right , Avgshift ' by shifting the sum of the calculated number of bits (=divide End Sub ' for the average conversions number) ' Display on console the legend with the analog inputs connections of Mini Module ' mounted on GMM TST3 ' Input: None ' Output: None Sub Shwconn_adc() Print " ADC1 ADC2 ADC3 ADC4 ADC5 ADC6 ADC7" Print "CN4.1 CN4.4 CN4.3 CN4.6 CN4.5 CN4.8 CN4.7" Print " Volt Volt Volt Volt Volt Volt Volt" End Sub ' Acquire all the available analog inputs with a single A/D conversion and ' it displays the obtained voltages on console ' Input: None ' Output: None Sub Shwonecnv_adc() For Chadc = 1 To 7 ' Loop for the 8 channels available on A/D converter Call Get_adc(chadc) ' Acquire A/D converter channel Voltadc = Cmbadc ' Obtain voltage from combination Voltadc = Voltadc * Qadc ' Through resolution If Voltadc = 0.0 Then ' If voltage to show is equal to 0 Gstr = "0.000" ' Ensure format also for value 0 Else ' Voltage different from 0 Gstr = Fusing(voltadc , 0.###) ' Format voltage with one integer digit and three decimals End If Print Gstr ; " "; ' Display formatted voltage Next Chadc Printbin Cret ' Mantain visualization on the same row End Sub ' Acquire all the available analog inputs with an averaged A/D conversion and ' it displays the obtained voltages on console ' Input: Ncnv = conversions number for average (multiple of 2, <=64) ' Output: None Sub Shwavgcnv_adc() For Chadc = 1 To 7 ' Loop for the 8 channels available on A/D converter Call Getavg_adc(chadc , Ncnv) ' Acquire A/D converter channelwith average on Ncnv conversions Voltadc = Cmbadc ' Obtain voltage from combination Voltadc = Voltadc * Qadc ' Through resolution If Voltadc = 0.0 Then ' If voltage to show is equal to 0 Gstr = "0.000" ' Ensure format also for value 0 Else ' Voltage different from 0 Gstr = Fusing(voltadc , 0.###) ' Format voltage with one integer digit and three decimals End If Print Gstr ; " "; ' Display formatted voltage Next Chadc Printbin Cret ' Mantain visualization on the same row End Sub '******************* End of subroutines used by program ************************