' ********************************************************************** ' ** Program name: TERMOF2.BAS - Version : 1.1 - 05 June 2000 ** ' ** Compiler : BASCOM 8051 Demo, (IDE V.2.0.0, LIB V.2.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: Adriano Pedrielli ** ' ********************************************************************** ' ' This program reads the temperature measured by IC3 (DS1621) and shows ' it in centigrad degreeses with values ranging from -55 to +125 through ' two TTL lines driven by the family 51 microcontroller-based GPC F2 ' board. ' ' This program is controlled through the RS 232 serial line so it is essential ' to connect a free COM port on the PC to the connector CN3 of GPC(r) F2. ' Configure the BASCOM 8051 terminal using menu Option/Communication, select ' the COM port and set baud rate to 9600, parity to none, databits to 8 and ' stopbits to 1. ' ' Perform the following connections: ' GPC F2 K51 AVR ' +5Vdc (pin15 CN3) ----> +5Vdc (pin1 CN6) ' Gnd (pin16 CN3) ----> Gnd (pin2 CN6) ' T0 P3.4 (pin3 CN3) ----> SD (pin7 CN6) ' T1 P3.5 (pin1 CN3) ----> SC (pin8 CN6) ' ' Before compiling set in menu Option/Compiler/Misc: ' Byte End 5F.' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' ' $regfile = "REG51.DAT" ' list of CPU registers $romstart = &H8050 ' code start address ' &H8050 for MO52 Rel.1.1 in RAM IC8 ' &H0000 for EPROM withput RAM IC8 $ramstart = &HD000 ' add. of variables external RAM $ramsize = &H2800 ' assign 10K of external RAM ' to variables $crystal = 11059200 ' CPU clock frequence $baud = 9600 ' RS-232 communication speed $large ' 16 bit addressing ' ' ******************************* 8xc51 PIN list *********************** ' Config Sda = P3.4 ' line P3.4 or T0 (pin 3 of CN3) ' on GPC(r) F2 is Data signal. Config Scl = P3.5 ' line P3.5 or T1 (pin 1 of CN3) ' on GPC(r) F2 is Clock signal. Buz Alias P1.2 ' Pin 3 of GPC F2 microcontroller ' is connected to the on board ' buzzer. Const Cler = 12 ' clear screen code Const Bel = 7 ' Bell code ' '********************* 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 ' ******************** List addresses of 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 Dim T As Byte ' code of key pressed ' '********************** 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: ' Delay for signals settling Waitms 1 Call Iniz Do ' 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 T = Inkey ' read a char from serial port Loop Until T <> 0 ' exit if received a character If T = 27 Then Exit Do ' exit Else Print Chr(bel); ' char non valid End If Loop End ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz ' Peripherals initialization Print Chr(cler); ' clear screen Print "Demo K51-AVR to visualize temperature by IC3(DS1621) through GPC F2" Print "rel.1.1 06/05/2000" Print Print "Press ESC to exit" 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() , 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 ' ****************************************************************************** '