' ********************************************************************** ' ** Program: k51terme.BAS - Version : 1.2 ** ' ** Compilee : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Scheda : K51-AVR driven by GMB HR168 and GMM ACZero ** ' ** 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 ** ' ** ** ' ** Written by: Graziano Gaiba 02.02.2005 ** ' ********************************************************************** ' ' ' This demo allows to drive an I2C BUS peripheral on board of K51-AVR, ' PCF8591, through mini-BLOCK module GMB HR168 and grifo(r) mini module. ' PCF 8591 is provided with four analog inputs for A/D conversions with 8 bits ' resolution, all the operations of conversion and combination acquisition are ' performed through synchronous serial interface I2C BUS. ' This demo allows to set which channel to convert through PC keyboard, and to ' read the combination on the 7 segments display of K51-AVR. ' ' ' Rel. 1.1 04.03.04 - By Graziano Gaiba ' ' Rel. 1.2 02.02.05 - By Graziano Gaiba ' ' Modified to work with GMM ACZero ' ' !!!!!!!!!!!!!!!!!!!!!!! ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options/Compiler/Misc set: ' - Byte End(hex) at least A0 ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' '****************** Compiler directives ************************** ' $regfile = "8951cc03.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.1 ' I2C serial DATA Config Scl = P2.0 ' 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 Pcf8591 As Const &H48 ' Slave address PCF8591 Dim Wpcf8591 As Const &H90 ' Slave address PCF8591 in Write Dim Rpcf8591 As Const &H91 ' Slave address PCF8591 in Read ' '*********************** Variables declaration ************************* ' Dim Dig As Byte ' value of a digit Dim Cifh As Byte ' 4 bit high in HEX for figure Dim Cifl As Byte ' 4 bit low in HEX for figure Dim N1 As Byte ' number 1 byte for DY3 and DY4 Dim Valore As Byte ' numbeo to convert Dim C As Byte ' indicates channel to convert ' '********************* Procedures declaration ************************** ' Declare Sub Iniz ' Peripherals initialization Declare Sub Ad(c As Byte , Valore As Byte) ' A/D converter management Declare Sub Canale ' Channel number management Declare Sub Cifre(valore As Byte , Cifh As Byte , Cifl As Byte ) ' converts a number in 2 HEX figures Declare Sub Digit(dig As Byte ) ' Converts a number in range ' 0-F in 7 segments Declare Sub Vis_num(n1 As Byte) ' Shows a byte in HEX ' '*************************** Main program ****************************** ' Main: ' Delay for signals settling Waitms 1 Call Iniz ' initializations C = 0 Valore = 0 Print Print "Press + to increment the channel to convert" Print "Press - to decrement the channel to convert" Print Do Call Ad(c , Valore) ' Perform acquisition Call Vis_num(valore) ' Show byte in HEX Call Canale Loop Return End ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' Channel number management ' Sub Canale Valore = Inkey If Valore = "+" Then If C = 3 Then C = 0 Else Incr C End If End If If Valore = "-" Then If C = 0 Then C = 3 Else Decr C End If End If End Sub ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ********************************************************************** ' Sub Iniz ' Inizializzazione periferiche Do I2creceive Rsaa1064 , Valore ' read 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 End Sub ' ' ******************** A/D converter management ************************ ' This procedure allows to perform an analogic conversion through one of ' the four channels available on IC2 (PCF8591). ' Parameters: ' Input : C as byte, indicates the channel to convert (0...3) ' Output : Valore as byte, value in the range from 0 to 255 (0v...+Vref) ' ********************************************************************** ' Sub Ad(ch As Byte , Valore As Byte) ' A/D converter management C = C And &H03 ' keep Bit 0 and Bit 1 I2csend Wpcf8591 , C ' point to the channel to convert I2creceive Rpcf8591 , Valore ' read previous conversion I2creceive Rpcf8591 , Valore ' read current conversion End Sub ' ' ******** Converts a figure in range from 0 to F in 7 segments ******** ' This procedure converts a figure in range from 0 to F in 7 segments ' format, if value is greater than F the display is off. ' Parameters: ' Input : dig as byte, value in the range from 0 to F ' Outout : dig as byte, value in 7 segments format. ' ********************************************************************** ' Sub Digit(dig As Byte ) ' Converts a number in range ' 0-F in 7 segments If Dig < 16 Then ' if number is lower than 16 Dig = Lookup(dig , Tab_7seg) ' read value from table Else Dig = 0 ' if = 16 or greater reset End If End Sub ' ' ***************** Converte 1 byte in due cifre HEX ******************** ' This procedure converts 1 byte in range from 0 to 255 in two 4 bits ' hexadecimal figures. ' Parameters: ' Input : Valore as byte, value in the range from 0 to 255 ' Output : Cifh as byte, figure of high nibble ' Cifl as byte, figure of low nibble ' ************************************************************************ ' Sub Cifre(valore As Byte , Cifh As Byte , Cifl As Byte ) ' converts a number in two HEX figures Cifh = Valore And &HF0 ' mask 4 high bits Cifh = Cifh / 16 ' shift to low the 4 high bits Cifl = Valore And &H0F ' mask the 4 low bits End Sub ' ' *********************** Visualizes a byte in HEX ********************* ' The first display visualizes the number of the channel being ' converted, the second display is off. ' This procedure allows to show a byte in hexadecimal format ' in the two displays on the right. E.g.: 255= FFH, 32= 20H etc. ' Parameters: ' Input : N1 as byte, number to visualize in HEX ' Output : nothing ' ********************************************************************** ' Sub Vis_num(n1 As Byte) ' visualizes a byte in HEX I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig1 ' point to display 1 (DY1) Call Digit(c) ' convert the decines figure ' in 7 segments I2cwbyte Dig ' output to first display DY1 I2cwbyte 0 ' output to second display DY2 Call Cifre(n1 , Cifh , Cifl) ' convert number in 2 HEX figure Call Digit(cifh) ' convert figure in HEX ' in 7 segments I2cwbyte Dig ' output to display DY3 Call Digit(cifl) ' convert figure in HEX ' in 7 segments I2cwbyte Dig ' output to display DY4 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 ' num. A b C d E F Data &H77 , &H7C , &H39 , &H5E , &H79 , &H71 ' ********************************************************************** '