' ********************************************************************** ' ** Program name: TERMO_AU.BAS - Version : 1.2 - 01 february 2001 ** ' ** Compiler : BASCOM AVR, (IDE V.1.11.4.0, LIB 1.11.4.0) ** ' ** Board : K51-AVR ** ' ** Firm: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** sales@grifo.it tech@grifo.it grifo@grifo.it ** ' ** ** ' ** Written by: Graziano GAIBA ** ' ********************************************************************** ' ' This program reads the temperature measured by IC3 (DS1621) and shows ' it in centigrad degreeses with values ranging from -55 to +125. ' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' ' $regfile = "8515def.dat" ' AT90s8515 $romstart = &H0 ' code start address $crystal = 5529600 ' micro clock frequence $baud = 19200 ' baud rate 'Config Debounce = 50 ' time to wait to convalidate ' a key pressed ' Es: 10= 10ms, 50= 50ms ' Config Sda = Portd.6 ' Pin 19 DATA signal for I2CBUS Config Scl = Portd.7 ' Pin 18 Clock signal for I2CBUS ' '********************* Constants declaration *************************** ' Const Saa1064 = &H38 ' Slave address SAA1064 Const Wsaa1064 = &H70 ' Slave address SAA1064 in Write Const Rsaa1064 = &H71 ' Slave address SAA1064 in Read ' ********************* Addresses list for Saa1064 *********************** Const Ctb = 0 ' Ind. Control byte Const Dig1 = 1 ' Ind. Digit 1 Const Dig2 = 2 ' Ind. Digit 2 Const Dig3 = 3 ' Ind. Digit 3 Const Dig4 = 4 ' Ind. Digit 4 ' ' ********************************************************************** ' Const Ds1621 = &H4C ' Slave address DS1621 Const Wds1621 = &H99 ' Slave address DS1621 in Write Const Rds1621 = &H98 ' Slave address DS1621 in Read ' ***************** Commands list of DS1621 **************************** Const Cfg = &HAC ' R/W Config register of DS1621 Const Rtemp = &HAA ' read temperature 2 bytes Const Strct = &HEE ' Start temperature conversion Const Stpct = &H22 ' Stop temperature conversion ' '*********************** Variables declaration ************************* ' Dim Valore As Byte ' value to write to or read from I2CBUS etc. Dim Dig As Byte ' number in 7 segments format Dim Th As Byte ' temperature high byte Dim Tl As Byte ' temperature low byte Dim Cifc As Byte ' figure of hundreds Dim Cifd As Byte ' figure of decines Dim Cifu As Byte ' figure of units Dim Getdata(2) As Byte ' 3 byte vector Dim Cifra As Byte ' Generic use Dim Numero As Byte ' Generic use ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization Declare Sub Temperatura ' reads temperature Declare Sub Cifre (numero As Byte ) ' Converts TH in degreeses into figures Declare Function Digit (byval Dig As Byte ) As Byte ' Converts a number from 0 to 9 in 7 segments Declare Sub Vis_temp (th As Byte , Tl As Byte) ' visualizes temperature ' '*************************** Main program ****************************** ' Main: ' Delay for signals settling Waitms 1 Call Iniz ' initializations Do Call Temperatura ' read temperature ' get high byte ' get low byte Call Vis_temp(getdata(1) , Getdata(0)) ' visualize temperature Loop End ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz ' Peripherals initialization Do I2creceive Rsaa1064 , Valore ' Read the status register Loop Until Valore = 0 ' Wait for SAA1064 to turn on I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' Communicate the Slave address I2cwbyte Ctb ' Point to control register I2cwbyte &B00100111 ' bit0 =1 dynamic mode ' bit1 =1 digit 1+3 not blanked ' bit2 =1 digit 2+4 not blanked ' bit3 =0 no test segment ' bit4 =0 no 3mA segment current ' bit5 =1 6mA segment current ' bit6 =0 no 12mA segment current ' bit7 =0 indifferent I2cwbyte 0 ' set DY1 off I2cwbyte 0 ' set DY2 off I2cwbyte 0 ' set DY3 off I2cwbyte 0 ' set DY4 off I2cstop I2csend Wds1621 , Cfg ' point to Configuration register I2creceive Rds1621 , Valore ' read Configuration register Valore = Valore And 1 ' mask Bit 0 If Valore = 1 Then ' if temperature conversion not actived I2cstart ' Start sequence for I2CBUS I2cwbyte Wds1621 ' Communicate the Slave address I2cwbyte Cfg ' point to Configuration register I2cwbyte &B00001010 ' write Configuration register ' bit1= 1 polarity "1" ' bit0= 0 continuous conversion I2cstop Waitms 50 ' delay End If I2csend Wds1621 , Strct ' start temperature conversion End Sub ' ' *************************** read temperature *************************** ' This procedure reads the value of the temperature. ' Parameters: ' Input : nothing ' Output : Getdata(0), contains TH, high byte of temperature ' Getdata(1), contains TL, low byte of temperature ' ************************************************************************ ' Sub Temperatura ' read temperature I2csend Wds1621 , Rtemp ' ask to read temperature I2creceive Rds1621 , Getdata() , 0 , 2 ' read TH and TL End Sub ' ' ' *********** Converts a figure in range from 0 to F in 7 segments ***** ' This procedure converts a figure in range from 0 to 9 in 7 segments format, if ' value is equal to or greater than 11 the display is off, if equal to 10 a ' negative sign is displayed. ' Parameters: ' Input : dig as byte, value from 0 to 9 ' Output : dig as byte, value in 7 segments format. ' ************************************************************************ ' Function Digit (dig As Byte ) As Byte ' Converts a figure in range from 0 to F in 7 segments If Dig < 10 Then ' if number is lower thawn 16 Digit = Lookup(dig , Tab_7seg) ' read value from table Else If Dig > 10 Then ' if true trun off dispaly Digit = 0 Else ' if = 10 Digit = 64 ' show "-" End If End If End Function ' ' ***************** Converte 1 byte in two "temperature" figures ************* ' This procedure converts 1 byte from 0 to 255 in a temperature, that is a ' value from 0 to 125 means positive centigrad degreeses (+0..+125), while a ' value from 255 to 201 means negative centigrad degreeses (-0..-55), bit 7 is ' the sign bit, negative degreeses are obtained complementing. ' E.g.: Not 255=0, Not 201=54 ' Parameters: ' Input : Valore as byte, value from 0 to 255 ' Uscita : Cifc As Byte , Cifd As Byte , Cifu As Byte, which contain hundreds, ' decines and unitsofthe temperature. ' ***************************************************************************** ' Sub Cifre (numero As Byte) If Numero > 127 Then ' temperature results negative Cifc = 10 ' minus sign actived Numero = Not Numero ' complement the value Else If Numero > 99 Then ' if positive, over 99 Cifc = 1 ' activate figure "1" of hundreds Numero = Numero - 100 ' subtract 100 Else Cifc = 11 ' not over 99, turn off the figure ' of hundreds End If End If If Numero > 9 Then ' freater than 9 Cifd = Numero / 10 ' calculate figure of decines Cifu = Cifd * 10 ' calculate figure of units Numero = Numero - Cifu Cifu = Numero Else If Cifc = 11 Then ' lower than 9 and figure of hundreds off Cifd = 11 ' turn off figure of decines Else If Cifc = 10 Then ' lower than 9, and negative temperature Cifd = 11 ' turn off figure of decines Else Cifd = 0 ' lower than 9 and figure of hundreds on ' show a zero End If End If Cifu = Numero ' save value of units End If End Sub ' ' *********************** Temperature visualization *********************** ' This procedure allows to visualize the temperature stored in 2 bytes ' TH high byte and TL low byte ' TL indicates helf degree 0 or 128 (0 .. 0.5). ' TH indicates degreeses from 0 to 125 (+0..+125), and from 255 to 21 ( -0..-54). ' Parameters: ' Input : TH as byte, high byte value ' TL as byte, low byte value ' Output : nothing ' ************************************************************************ ' Sub Vis_temp (th As Byte , Tl As Byte) ' Temperature visualization I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' Communicate the Slave address I2cwbyte Dig1 ' point to display 1 (DY1) Call Cifre(th ) ' convert in 3 figures Cifra = Digit(cifc) ' convert figure of hundreds in 7 segments I2cwbyte Cifra ' output to the first display Cifra = Digit(cifd) ' convert figure of decines in 7 segments I2cwbyte Cifra Cifra = Digit(cifu) ' convert figure of units in 7 segments Cifra = Cifra Or 128 ' turn on decimal point I2cwbyte Cifra ' output to third terzo display If Tl = &H80 Then ' visualize the half degree Cifra = Digit(5) ' convert figure of units in 7 segments Else Cifra = Digit(0) ' convert figure of units in 7 segments End If I2cwbyte Cifra ' output to fourth display I2cstop ' Stop sequence for I2CBUS End Sub ' ' ************* Conversion table for 7 segments figure in the range from 0 to 15 ******* Tab_7seg: ' num. 0 1 2 3 4 5 6 7 8 9 Data &H3F , &H06 , &H5B , &H4F , &H66 , &H6D , &H7D , &H07 , &H7F , &H6F ' ************************************************************************************** '