' ********************************************************************** ' ** Program: k51terme.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 + 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 ** ' ********************************************************************** ' ' 20.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. ' ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' '********************* Constants declaration *************************** ' ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' TWI reading options Const Twi_ack = 1 Const Twi_nack = 0 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 , I2cstatus As 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 ' 2 bytes vector ' '********************** Procedure declarations ************************* ' ' Send a command to TWI interface, wait for it to complete and returns status Declare Function Twi_cmd(byval Cmd As Byte) As Byte Declare Sub Twiinit(byval Clockrate As Byte) ' Initialize TWI hardware interface Declare Function Twistart() As Byte ' Generate a start by TWI hardware interface Declare Function Twiwbyte(byval Dato As Byte) As Byte ' Send a byte by TWI hardware interface Declare Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte ' Read a byte from TWI hardware interface Declare Function Twistop() As Byte ' Generate a stop by TWI hardware interface Declare Sub Check_ready() Declare Function Check_ready_2() As Byte 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 ' Initialize hardware I2C BUS interface at about 72 kHz Call Twiinit(72) Do I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(rsaa1064) I2cstatus = Twirbyte(valore , Twi_nack) ' Read the status register I2cstatus = Twistop() 'I2creceive Rsaa1064 , Valore Loop Until Valore = 0 ' Wait for SAA1064 to turn on I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wsaa1064) ' Communicate the Slave address I2cstatus = Twiwbyte(ctb) ' Point to control register I2cstatus = Twiwbyte(&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 I2cstatus = Twiwbyte(0) ' set DY1 off I2cstatus = Twiwbyte(0) ' set DY2 off I2cstatus = Twiwbyte(0) ' set DY3 off I2cstatus = Twiwbyte(0) ' set DY4 off I2cstatus = Twistop() I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wds1621) ' point to Configuration register I2cstatus = Twiwbyte(cfg) I2cstatus = Twistop() 'I2csend Wds1621 , Cfg I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(rds1621) I2cstatus = Twirbyte(valore , Twi_nack) I2cstatus = Twistop() 'I2creceive Rds1621 , Valore ' read Configuration register Valore = Valore And 1 ' mask Bit 0 If Valore = 1 Then ' if temperature conversion not actived I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wds1621) ' Communicate the Slave address I2cstatus = Twiwbyte(cfg) ' point to Configuration register I2cstatus = Twiwbyte(&B00001010) ' write Configuration register ' bit1= 1 polarity "1" ' bit0= 0 continuous conversion I2cstatus = Twistop() Waitms 50 ' delay End If I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wds1621) ' read Configuration register I2cstatus = Twiwbyte(strct) I2cstatus = Twistop() '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 Local Temp1 As Byte , Temp2 As Byte ' ask to read temperature I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wds1621) ' read Configuration register I2cstatus = Twiwbyte(rtemp) I2cstatus = Twistop() 'I2csend Wds1621 , Rtemp I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(rds1621) I2cstatus = Twirbyte(temp1 , Twi_ack) I2cstatus = Twirbyte(temp2 , Twi_nack) I2cstatus = Twistop() Getdati(1) = Temp1 Getdati(2) = Temp2 '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 I2cstatus = Twistart() ' Start sequence for I2CBUS I2cstatus = Twiwbyte(wsaa1064) ' Communicate the Slave address I2cstatus = Twiwbyte(dig1) ' point to display 1 (DY1) Call Cifre(th ) ' convert in 3 figures Valore = Digit(cifc) ' convert figure of hundreds in 7 segments I2cstatus = Twiwbyte(valore) ' output to the first display Valore = Digit(cifd) ' convert figure of decines in 7 segments I2cstatus = Twiwbyte(valore) Valore = Digit(cifu) ' convert figure of units in 7 segments Valore = Valore Or 128 ' turn on decimal point I2cstatus = Twiwbyte(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 I2cstatus = Twiwbyte(valore) ' output to fourth display I2cstatus = Twistop() ' Stop sequence for I2CBUS End Sub ' '****************************************************************************** '* I2C BUS hardware management procedures * '****************************************************************************** ' ' ' ' Writes command passed in parameter cmd into control register TWI and waits ' for the completion of operation, then reads the status of TWI interface and ' returns it. ' Function Twi_cmd(byval Cmd As Byte) As Byte Local Time_out As Word , I As Byte Twcr = Cmd ' Writes the command Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout ' Reads status and masks not significant bits I = Twsr I = I And &HF8 Twi_cmd = I End Function ' ' ' Initializes hardware TWI interface ' Sub Twiinit(byval Clockrate As Byte) Twsr = Twsr And &HFC ' No prescaler Twbr = Clockrate ' Set clock rate End Sub ' ' ' Send start for hardware TWI interface ' Function Twistart() As Byte Twistart = Twi_cmd(&Ha4) ' Start End Function ' ' ' Send byte to hardware TWI interface ' Function Twiwbyte(byval Dato As Byte) As Byte Twdr = Dato ' Write data Twiwbyte = Twi_cmd(&H84) End Function ' ' ' Read byte from hardware TWI interface ' Function Twirbyte(twi_data As Byte , Byval Assert_ack As Byte) As Byte If Assert_ack = 1 Then Twirbyte = Twi_cmd(&Hc4) Else Twirbyte = Twi_cmd(&H84) End If Twi_data = Twdr End Function ' ' ' Send stop on hardware TWI interface. ' Returns 0 if there are no errors, a value different from 0 in case of ' timeout. ' Function Twistop() As Byte Local Time_out As Word Twcr = &H94 ' Stop ' Waits for the stop completer or timeout Time_out = 0 Do Incr Time_out Loop Until Twcr.twsto = 0 Or Time_out = Ee_timeout If Time_out = Ee_timeout Then Twistop = 1 Else Twistop = 0 End If End Function ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 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 ' ****************************************************************************** '