' ********************************************************************** ' ** Program: k51terme.BAS - Version : 1.1 - 15 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM32 plus K51-AVR ** ' ** Society: 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 ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 15.06.05 - Rel 1.1 By Graziano Gaiba ' This demo allows to drive an I2C BUS peripheral on board of K51-AVR, ' DS1621, through mini-BLOCK module GMB HR168 and grifo(r) mini module. ' DS1621 is a programmable digital thermometer, whose resolution is half ' Celsius degreese, all the operations of programming and temperature ' acquisition are performed through synchronous serial interface I2C BUS. ' This demo shows the temperature acquired on the 7 segments display of ' K51-AVR. ' ' Compiled file is smaller than 2048 bytes, so it can becompliled with free ' demo of BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M32 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m32def.dat" $crystal = 7372800 $baud = 19200 Config Sda = Portc.1 ' I2C serial DATA Config Scl = Portc.0 ' I2C serial CLK ' '********************* 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 ' Address Control byte Const Dig1 = 1 ' Address Digit 1 Const Dig2 = 2 ' Address Digit 2 Const Dig3 = 3 ' Address Digit 3 Const Dig4 = 4 ' Address Digit 4 ' ' ********************************************************************** ' Const Ds1621 = &H4C ' Slave address DS1621 Const Wds1621 = &H98 ' Slave address DS1621 in Write Const Rds1621 = &H99 ' 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 ************************* ' 'Generic use Dim T1 As Byte , T2 As Byte Dim A As Eram Byte At 1023 Dim Cifc As Byte ' figure of hundreds Dim Cifd As Byte ' figure of decines Dim Cifu As Byte ' figure of units Dim Getdati(2) As Byte ' 2 bytes vector ' '********************** Procedure declarations ************************* ' Declare Sub Check_ready() Declare Sub Iniz ' Peripherals initialization Declare Sub Temperatura() ' reads temperature Declare Sub Cifre(byval Valore As Byte ) ' converts a number in 2 HEX figures Declare Function Digit(byval Dig As Byte ) As Byte ' Converts a number in range ' 0 to 9 in 7 segments Declare Sub Vis_temp(byval Th As Byte , Byval Tl As Byte) ' visualizes temperature ' '*************************** Main program ****************************** ' Main: Print Chr(12) ' Clears the screen Print "Demo of thermometer DS1621." Print Call Check_ready() Print "7 segments displays show temperature measured." Call Iniz() ' initializations Do Call Temperatura() ' read temperature T1 = Getdati(1) ' get high byte T2 = Getdati(2) ' get low byte Call Vis_temp(t1 , T2) ' 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 Local Valore As Byte 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 : Getdati(0), contains TH, high byte of temperature ' Getdati(1), contains TL, low byte of temperature ' ************************************************************************ ' Sub Temperatura ' read temperature I2csend Wds1621 , Rtemp ' ask to read temperature I2creceive Rds1621 , Getdati(1) , 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 : byte, value in 7 segments format. ' ************************************************************************ ' Function Digit(byval Dig As Byte ) As Byte ' Converts a figure in range from 0 to 9 in 7 segments Local Valore As Byte If Dig < 10 Then ' if number is lower thawn 10 Valore = Lookup(dig , Tab_7seg) ' read value from table Else If Dig > 10 Then ' if true trun off dispaly Valore = 0 Else ' if = 10 Valore = 64 ' show "-" End If End If Digit = Valore 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(valore As Byte ) If Valore > 127 Then ' temperature results negative Cifc = 10 ' minus sign actived Valore = Not Valore ' complement the value Else If Valore > 99 Then ' if positive, over 99 Cifc = 1 ' activate figure "1" of hundreds Valore = Valore - 100 ' subtract 100 Else Cifc = 11 ' not over 99, turn off the figure ' of hundreds End If End If If Valore > 9 Then ' freater than 9 Cifd = Valore / 10 ' calculate figure of decines Cifu = Cifd * 10 ' calculate figure of units Valore = Valore - Cifu Cifu = Valore 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 = Valore ' 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(byval Th As Byte , Byval Tl As Byte) ' Temperature visualization Local Valore As Byte 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 Valore = Digit(cifc) ' convert figure of hundreds ' in 7 segments I2cwbyte Valore ' output to the first display Valore = Digit(cifd) ' convert figure of decines ' in 7 segments I2cwbyte Valore Valore = Digit(cifu) ' convert figure of units ' in 7 segments Valore = Valore Or 128 ' turn on decimal point I2cwbyte Valore ' output to third terzo display If Tl = &H80 Then ' visualize the half degree Valore = Digit(5) ' convert figure of units ' in 7 segments Else Valore = Digit(0) ' convert figure of units ' in 7 segments End If I2cwbyte Valore ' output to fourth display I2cstop ' Stop sequence for I2CBUS End Sub ' ' ' Check for hardware ready ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: 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 ' ****************************************************************************** '