' ********************************************************************** ' * File: uk_BASAVR_062.BAS * ' * Version: 1.1 * ' * Date: 11.03.12 * ' * 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 062 of BASCOM AVR course. ' A/D converter: analog input management with control band, hysteresys and ' output driving. ' Each second the program acquires the analog input and then it compare the ' current value with a limit (set point) with hysteresy, both presettable. ' The input, acquired by using one A/D converter channel with 8 conversion ' average, is displayed on console, together with the other control parameters. ' Moreover through the console it is possible to set the current value for ' set point and hysteresys. The control status is reported on a digital output ' that, when connected to proper actuator, can change the acquired measure. ' The program describes its functionalities and uses 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 AVR (see IDE Configuration). ' The program works only when the GMM AM08 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: None. ' ' 11.03.12: uk_BASAVR_062.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to coorectly 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 table. ' !!! Note: On GMM TST3 the jumpers must be configured as below described: ' J1 N.C. ; 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 AM08 GMM AM08 signal ' CN4.6 29 23 PBO ICP PB0 ' CN4.2 33 27 ADC7 ADC7 ' CN4.17 20 14 GND - ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Signal ' PC DB9 GMM TST3 GMM TST3 GMM AM08 GMM AM08 uP ' 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 direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pincntr Alias Portb.0 ' Bit with output signal connected to control actuator 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 = 13 ' Carriage return ASCII code Const Qadc = 2.56 / 1023 ' A/D converter resolution with full scale voltage=Vref=2.56 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 Setpoint As Single ' Set point for control Dim Hyster As Single ' Hysteresys for control Dim Inihyst As Single ' Start hysteresys value Dim Cntron As Bit ' Flag for control status Dim Onhyst As Bit ' Flag for control inside hysteresys band '************************ Subroutines declaration ****************************** Declare Sub Ini_adc() ' Initialize A/D converter section Declare Sub Getavg_adc(byval Chadc As Byte , Byval Ncnv As Byte) ' Acquire A/D converter input with average '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Pincntr = 1 ' Initialize signal connected to control actuator as Ddrb.0 = 1 ' digital output high, equal to disabled Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Control of GMM AM08 analog inputs" Print "Mount Mini Module on Z2 of GMM TST3, connect analog input to convert" Print "between pin 17 (GND) and 2 (ADC7) of CN4 on GMM TST3; connect digital" Print "output for control to pin 6 of CN4 on GMM TST3 (see proper electric" Print "diagram for details)." Print "Each second the program acquires, controls, drive the output and it" Print "shows the envolved values, on console." Print Call Ini_adc() ' Initialize lines and A/D converter section, used by program Setpoint = 2.0 ' Initial set point at 3 Volts Hyster = 0.2 ' Initial hysteresys at 0.2 volts Onhyst = 0 ' Control not inside hysteresys Do ' Begin endless loop Print "S change set point, H change hysteresys" Print "ADC7(V) S.P.(V) Ist.(V) Actuator" Inihyst = Setpoint - Hyster ' Obtain start hysteresys temperature Do ' Begin control loop Call Getavg_adc(7 , 8) ' Acquire ADC7 channel of A/D converter with average on 8 conversions Voltadc = Cmbadc ' Obtain voltage from combination Voltadc = Voltadc * Qadc ' Through resolution If Voltadc < Inihyst Then ' If voltage under Hysteresys band Cntron = 1 ' Actuator output enabled Onhyst = 0 ' Control not in Hysteresys End If If Voltadc >= Inihyst And Onhyst = 0 Then ' If voltage inside Hysteresys band and control not in Hysteresys Cntron = 1 ' Actuator output enabled End If If Voltadc >= Setpoint Then ' If voltage over Hysteresys band Cntron = 0 ' Actuator output disabled Onhyst = 1 ' Control in Hysteresys End If ' Shows values involved in voltage control, aligned on previously displayed legend Print " "; Gstr = Fusing(voltadc , "0.###") ' Format temperature with three integer digits Print Gstr ; " "; ' Shows valid, acquired, formatted temperature Gstr = Fusing(setpoint , "0.###") ' Format set point with three integer digits Print Gstr ; " "; ' Show formatted set point Gstr = Fusing(hyster , "0.###") ' Format Hysteresys with three integer digits Print Gstr ; " "; ' Shows formatted Hysteresys If Cntron = 1 Then ' Shows heater status Print "ON "; Pincntr = 0 ' Set control digital output low, in order to enable actuator Else Print "OFF"; Pincntr = 1 ' Set control digital output high, in order to disablee actuator End If Print " "; ' Clear possible previous visualizations Printbin Cret ' Mantiene rappresentazione su stessa riga Wait 1 ' 1 sec delay: it defines frequency of control ' Check if the user has pressed one of the key that change control parameters ' and when available, acquire them Choice = Inkey() ' Verify if key is pressed on console If Choice >= "a" Then ' When key is lower case Choice = Choice And &HDF ' Convert it in upper case End If Loop Until Choice = "S" Or Choice = "H" ' Exit with a valid choice from user Print ' Separate following visualizations Print Select Case Choice ' Check converted choice Case "S": ' Selected Set point change: acquire it from console, in celsius degrees Input "Set point (C): " , Setpoint Case "H": ' Selected Hysteresys change: acquire it from console, in celsius degrees Input "Hysteresis (C): " , Hyster End Select Print ' Separate following visualizations 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() ' The used analog input (ADC7) doesn't have multifunctions and thus it must ' not be set as input without pull up ' Define conversion on request, Prescaler=64 (50% of clock A/D), internal Vref=2,56V Config Adc = Single , Prescaler = 64 , Reference = Internal_2.56 Start Adc ' Enable A/D converter section 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(byval Chadc As Byte , Byval 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 Cmbadc = Getadc(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) '******************* End of subroutines used by program ************************