' ********************************************************************** ' ** Program: g51terme.BAS - Version : 1.3 - 16 april 2003 ** ' ** Compilee : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Scheda : K51-AVR driven by GMB HR84 and GMM 5115 ** ' ** 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 demo allows to drive an I2C BUS peripheral on board of K51-AVR, PCF8574, ' through mini-BLOCK module GMB HR84 and grifo(r) mini module. ' PCF 8574 is an I/O expander that allows to drive 8 TTL digital lines, both as ' input and as output, all the operations of read and write are performed ' through synchronous serial interface I2C BUS. ' This demo activates in sequence one line at a time, creating the classic bit ' shift to the right and to the left, and writes 'out' on the 7 segments ' display of K51-AVR, near the characters 'out'. ' ' ' 05.06.2000 by Adriano Pedrielli (for K51 only) ' ' 16.04.2003 by Graziano Gaiba ' ' Modified to drive the managed peripheral on K51-AVR through a GMB HR84 ' and a GMM 5115. ' ' Before compiling set in menu Option/Compiler/Misc/ : ' Byte End 80; ' '*********************************************************************** ' '****************** Compiler directives ************************** ' $regfile = "grifo_mm.dat" $romstart = &H0 ' start address of code for FLASH $ramstart = &H0 ' start address of external RAM $ramsize = &H100 ' 256 bytes of external RAM $crystal = 14745600 ' Microcontroller clock $baud = 19200 ' RS-232 baud rate '$large ' 16 bit addressing for jumps ' (Not for demo version) $map ' Generates address map Config Sda = P2.0 ' I2C serial DATA Config Scl = P2.1 ' I2C serial CLK ' '********************* Constants declaration *************************** ' Dim Saa1064 As Const &H38 ' Slave address SAA1064 Dim Wsaa1064 As Const &H70 ' Slave address SAA1064 in Write Dim Rsaa1064 As Const &H71 ' Slave address SAA1064 in Read '****************** Addresses list for Saa1064 ************************* Dim Ctb As Const 0 ' Address Control byte Dim Dig1 As Const 1 ' Address Digit 1 Dim Dig2 As Const 2 ' Address Digit 2 Dim Dig3 As Const 3 ' Address Digit 3 Dim Dig4 As Const 4 ' Address Digit 4 ' ' ********************************************************************** ' Dim Ds1621 As Const &H4C ' Slave address DS1621 Dim Wds1621 As Const &H99 ' Slave address DS1621 in Write Dim Rds1621 As Const &H98 ' Slave address DS1621 in Read ' ***************** Commands list of DS1621 **************************** Dim Cfg As Const &HAC ' R/W Config register of DS1621 Dim Rtemp As Const &HAA ' read temperature 2 bytes Dim Strct As Const &HEE ' Start temperature conversion Dim Stpct As Const &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 Getdati(2) As Byte ' 3 bytes vector ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization Declare Sub Temperatura ' reads temperature Declare Sub Cifre(valore As Byte , Cifc As Byte , Cifd As Byte , Cifu As Byte) ' Converts TH in degreeses into figures Declare Sub Digit(dig 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: ' Ritardo Assestamento segnali Waitms 1 Print Chr(12) Print "This demo has been made to run only on a GMM 5115." Print "Proceed only if you have the correct Mini Module!" Print "Press a key if you want to continue..." Do Valore = Inkey Loop Until Valore <> 0 Call Iniz ' initializations Do Call Temperatura ' read temperature Th = Getdati(1) ' get high byte Tl = Getdati(2) ' get low byte Call Vis_temp(th , Tl) ' 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 : 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 : dig as byte, value in 7 segments format. ' ************************************************************************ ' Sub Digit(dig As Byte ) ' Converts a figure in range from 0 to F in 7 segments If Dig < 10 Then ' if number is lower thawn 16 Dig = Lookup(dig , Tab_7seg) ' read value from table Else If Dig > 10 Then ' if true trun off dispaly Dig = 0 Else ' if = 10 Dig = 64 ' show "-" End If End If End Sub ' ' ***************** 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 , Cifc As Byte , Cifd As Byte , Cifu 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(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 , Cifc , Cifd , Cifu) ' convert in 3 figures Call Digit(cifc) ' convert figure of hundreds ' in 7 segments I2cwbyte Dig ' output to the first display Call Digit(cifd) ' convert figure of decines ' in 7 segments I2cwbyte Dig Call Digit(cifu) ' convert figure of units ' in 7 segments Dig = Dig Or 128 ' turn on decimal point I2cwbyte Dig ' output to third terzo display If Tl = &H80 Then ' visualize the half degree Call Digit(5) ' convert figure of units ' in 7 segments Else Call Digit(0) ' convert figure of units ' in 7 segments End If I2cwbyte Dig ' 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 ' ****************************************************************************** '