' ********************************************************************** ' * File: uk_BASAVR_058.BAS * ' * Version: 1.1 * ' * Date: 13.01.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 058 of BASCOM AVR course. ' Temperature acquisition from DS18S20 sensor through 1-WIRE BUS, with control ' band and hysteresys. ' Each second the program acquires the temperature from sensor and then it ' performs an heating thermostat function or, in other words, it checks if ' the temperature with a presettable limit (set point) and hysteresys. The ' temperature is acquired by using a three wires (DQ, GND, Vdd) 1-WIRE ' interface, with one celsius degree resolution, and it is displayed on ' console, together with the other thermostat parameters. Moreover through ' the console it is possible to set the current value for set point and ' hysteresys. ' When on 1-WIRE BUS there are several devices, the program searchs for and ' manages only the first DS18S20 sensor, with no intervents on the other ones. ' 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. ' ' 13/01/12: uk_BASAVR_058.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 | Compiler | I2C, SPI, 1WIRE" set: ' I2C ' SCL port = don't care ' SDA port = don't care ' 1Wire ' 1wire = PORTD.3 ' SPI ' Clock = don't care ' MOSI = don't care ' MISO = don't care ' SS = don't care ' Use Hardware SPI = disabled ' 4) 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 ' 5) 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 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 !!! ' ' 1-WIRE GMM TST3 pin Z2 pin Signal Used uP ' signal resource GMM TST3 GMM AM08 GMM AM08 signal ' DQ CN4.16 24 18 PD3 INT1 PD3 ' Vdd CN4.18 34 28 +5 Vdc - ' GND 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. 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 ' ASCII code for carriage return '************************* Variables declaration ******************************* Dim Ndev_ow As Word ' Devices number on 1-WIRE BUS Dim Romcode_ow(8) As Byte ' Array for 8 bytes of 1-WIRE ROM code Dim Scrpad_ow(9) As Byte ' Array for 9 bytes of DS18S20 sensor Scratchpad area Dim Err_ow As Byte ' Copy of errors on 1-WIRE communication Dim Crc_ow As Byte ' Calculated CRC on 1-WIRE communication Dim Tint_ow As Integer ' Internal temperature (in 0,5 °C), acquired from DS18S20 sensor Dim Tdec_ow As Single ' Temperature in decimal format (in °C), acquired from DS18S20 sensor Dim Tcur As Integer ' Acquired temperature, without decimals (in °C), to be checked Dim Setpoint As Integer ' Set point for thermostat function Dim Hyster As Integer ' Hysteresys for thermostat function Dim Inihyst As Integer ' Start hysteresystTemperature Dim Heaton As Bit ' Flag for heater status Dim Onhyst As Bit ' Flag for control inside hysteresys band Dim Gstr As String * 6 ' String for visualization of formatted temperature Dim Hlpw As Word ' General purpose word help variable Dim Choice As Byte ' Operation selected on console '************************ Subroutines declaration ****************************** Declare Sub Gettemp_ow() ' Acquire current temperature of DS16S20 1-WIRE sensor '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Temperature control as thermostat with GMM AM08 + GMM TST3" Print "Mount Mini Module on Z2 of GMM TST3, connect 1-WIRE line and sensor" Print "as described in electric diagram." Print "The program checks the sensor presence and then each second, it acquires," Print "controls and shows the envolved values, on console." Print Config 1wire = Portd.3 ' Initialize 1-WIRE interface ' Check presence of devices on 1-WIRE BUS and when available, it acquires ' the ROM codes from these component until a DS18S20 sensor is found, that ' is a ROM code with "family code" = 10H. Ndev_ow = 1wirecount() ' Obtain number of connected 1-WIRE device If Ndev_ow > 0 Then ' If at least one 1-WIRE device is connected Hlpw = 0 ' Initialize 1-WIRE devices counter Do ' Loop that check presence of DS18S20 sensor Incr Hlpw ' Increase 1-WIRE device counter If Hlpw = 1 Then Romcode_ow(1) = 1wsearchfirst() ' Obtain ROM code of first 1-WIRE device Else Romcode_ow(1) = 1wsearchnext() ' Obtain ROM code of other 1-WIRE devices End If Loop Until Romcode_ow(1) = &H10 Or Hlpw = Ndev_ow ' Repeat until a DS18S20 is found or reached last device Else Romcode_ow(1) = 0 ' Force condition for DS18S20 not found End If If Romcode_ow(1) = &H10 Then ' If DS18S20 found Print "DS18S20 sensor found." Err_ow = 0 ' Clear errors recognized on 1-WIRE communication Setpoint = 50.0 ' Initial set point at 50 celsius degrees Hyster = 2.0 ' Initial hysteresys at 2 celsius degrees Onhyst = 0 ' Control not inside hysteresys Do ' Begin endless loop Print "S change set point, H change hysteresys" Print "T.(C) S.P.(C) Hyst.(C) Heater" Inihyst = Setpoint - Hyster ' Obtain start hysteresys temperature Do ' Begin control loop Call Gettemp_ow() ' Acquire temperature from 1-WIRE sensor DS16S20 (it belong 1 seconds) If Err_ow = 0 Then ' If no error recognized ' Perform temperature control as heating thermostat and it obtains the heater ' status in Heaton variable Tcur = Tdec_ow ' Obtain temperature to control, with no decimals If Tcur < Inihyst Then ' If temperature under Hysteresys band Heaton = 1 ' Heater enabled Onhyst = 0 ' Control not in Hysteresys End If If Tcur >= Inihyst And Onhyst = 0 Then ' If temperature inside Hysteresys band and control not in Hysteresys Heaton = 1 ' Heater enabled End If If Tcur >= Setpoint Then ' If temperature over Hysteresys band Heaton = 0 ' Heater disabled Onhyst = 1 ' Control in Hysteresys End If ' Shows values involved in thermostat control, aligned on previously displayed legend. ' These values aren't formatted in order to compile the program with demo version of ' BASCOM AVR; the instructions that display formatted values, always aligned, are ' anyway listed in the source, but they are remarked. Print " "; Print Tcur ; " "; ' Show acquired temperature ' Gstr = Fusing(tcur , "000") ' Format temperature with three integer digits ' Print Gstr ; " "; ' Shows valid, acquired, formatted temperature Print Setpoint ; " "; ' Show set point ' Gstr = Fusing(setpoint , "000") ' Format set point with three integer digits ' Print Gstr ; " "; ' Show formatted set point Print Hyster ; " "; ' Shows hysteresys ' Gstr = Fusing(hyster , "000") ' Format Hysteresys with three integer digits ' Print Gstr ; " "; ' Shows formatted Hysteresys If Heaton = 1 Then ' Shows heater status Print "ON "; Else Print "OFF"; End If Print " "; ' Clear possible previous visualizations Else Print "Errors: " ; Err_ow; ' Shows errors recognized on 1-WIRE communication Err_ow = 0 ' Clear errors recognized on 1-WIRE communication End If Printbin Cret ' Mantiene rappresentazione su stessa riga ' Check if the user has pressed one of the key that change control parameters ' as thermostat 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 Else Print "DS18S20 sensor not found!" Print "Verify connections and re-execute the program." End If End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Acquire current temperature from 1-WIRE sensor DS16S20. ' The subroutine first send the conversion command and then the read command to ' acquire the result of last conversion (Scratchpad area), with an execution ' time of about 1 second. The validity of the read result is also checked through ' proper CRC available into the Scratchpad area. ' Input: Romcode_ow() = Array for 8 bytes of 1-WIRE ROM code ' Err_ow = Errors recognized on 1-WIRE communication ' Output: Tdec_ow = Decimal temperature (in °C) acquired from sensor ' Err_ow = Updated errors recognized on 1-WIRE communication Sub Gettemp_ow() 1wverify Romcode_ow(1) ' Send ROM command "Match ROM" to founded DS18S20 sensor Err_ow = Err_ow Or Err ' Update error recognized on 1-WIRE sensor 1wwrite &H44 ' Send function command "Convert T" to DS18S20 sensor Wait 1 ' 1 second delay used by sensor to convert the temperature 1wverify Romcode_ow(1) ' Send ROM command "Match ROM" to founded DS18S20 sensor Err_ow = Err_ow Or Err ' Update error recognized on 1-WIRE sensor 1wwrite &HBE ' Send function command "Read Scratchpad" to DS18S20 sensor Scrpad_ow(1) = 1wread(9) ' Read 9 bytes of Scratchpad area from DS18S20 sensor Crc_ow = Crc8(scrpad_ow(1) , 8) ' Calculate polinomial CRC with base 8 on 8 bytes of Scratchpad area If Crc_ow <> Scrpad_ow(9) Then ' When CRC calculated and received (on nineth byte of Scratchpad area) are different Err_ow = Err_ow Or &H02 ' Adds CRC error recognized on 1-WIRE communication End If ' Obtain integer temperature (in 0,5 °C) from two dedicated bytes of Scratchpad area Tint_ow = Scrpad_ow(2) ' Acquire MSB Shift Tint_ow , Left , 8 ' Move MSB Tint_ow = Tint_ow Or Scrpad_ow(1) ' Add LSB ' Transform integer temperature (in 0,5 °C) in decimal format (in °C) Tdec_ow = Tint_ow ' Save obtained integer temperature on single type variable Tdec_ow = Tdec_ow / 2.0 ' Obtain temperature in °C End Sub '******************* End of subroutines used by program ************************