' ********************************************************************** ' ** Program name: DA_au.BAS - Version : 1.2 - 31 January 2001 ** ' ** Compiler : BASCOM AVR, (IDE V.1.11.4.0, LIB 1.11.4.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: Graziano GAIBA ** ' ********************************************************************** ' ' This program monitors one D/A converter channel on IC2 ' (PCF8591), key T1 increments the value, while key T2 decrements the value ' which is shown in hexadecimal formaton the 7 sements displays. ' Whenever a key is pressed, an acoustic signal is emitted. ' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' ' $regfile = "8515def.dat" ' AT90s8515 registers $romstart = &H0 ' code start address $crystal = 5529600 ' micro clock frequence $baud = 19200 ' baud rate Config Debounce = 50 ' time to wait to convalidate ' a key pressed ' Es: 10= 10ms, 50= 50ms ' ' *************************** PIN list AT90s8515 ************************** Config Sda = Portd.6 ' Pin 19 DATA signal for I2CBUS Config Scl = Portd.7 ' Pin 18 Clock signal for I2CBUS T1 Alias Pinb.0 ' Pin 17 connected to key T1 Config Pinb.0 = Input T2 Alias Pinb.1 ' Pin 16 connected to key T2 Config Pinb.1 = Input T3 Alias Pinb.2 ' Pin 15 connected to key T3 Config Pinb.2 = Input Buz Alias Portd.5 ' Pin 9 connected to Buzzer Config Pind.5 = Output ' '********************* 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 ' Ind. Control byte Const Dig1 = 1 ' Ind. Digit 1 Const Dig2 = 2 ' Ind. Digit 2 Const Dig3 = 3 ' Ind. Digit 3 Const Dig4 = 4 ' Ind. Digit 4 ' ********************************************************************** ' Const Pcf8591 = &H48 ' Slave address PCF8591 Const Wpcf8591 = &H90 ' Slave address PCF8591 in Write Const Rpcf8591 = &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 ' number to convert Dim Tensione As Byte Dim Valori As Byte Dim Cifra As Byte ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization Declare Sub Dac(tensione As Byte) ' D/A converter management Declare Sub Cifre (valori As Byte , Cifh As Byte , Cifl As Byte ) ' converts a number in 2 HEX figures Declare Function Digit (dig As Byte ) 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 Valore = 0 Do Call Dac(valore) ' D/A converter management Call Vis_num(valore) ' show a byte in HEX Debounce T1 , 0 , Increment , Sub Debounce T2 , 0 , Decrement , Sub Loop Increment: Buz = 0 ' turn on the buzzer Waitms 50 ' delay Incr Valore Buz = 1 ' turn off the buzzer Return Decrement: Buz = 0 ' turn on the buzzer Waitms 50 ' delay Decr Valore Buz = 1 ' turn off the buzzer Return End ' '**************************** Program end ******************************** ' ' '**************************** Procedures ********************************* ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz ' Peripherals initialization Buz = 1 ' Silents the buffer 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 indifferente I2cwbyte 94 ' output to DY1 the char "d" I2cwbyte 119 ' output to DY2 the char "A" I2cwbyte 0 ' DY3 off I2cwbyte 0 ' DY4 off I2cstop End Sub ' ' ******************** D/A converter management **************************** ' This procedure allows to set an output value on the D/A converter, ' available on IC2 (PCF8591). ' Parameters: ' Input : Valore as byte, value from 0 to 255 (0v ..+Vref) ' Output : ' ************************************************************************ ' Sub Dac(tensione As Byte) ' D/A converter management I2cstart ' Start sequence for I2CBUS I2cwbyte Wpcf8591 ' Communicate the Slave address I2cwbyte &H40 ' Communicate D/A activation command I2cwbyte Tensione ' value for D/A I2cstop 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 from 0 to F ' Output : dig as byte, value in 7 segments format. ' ************************************************************************ ' Function Digit (dig As Byte ) As Byte ' Converts a number in range 0-F in 7 segments If Dig < 16 Then ' number is lower than 16 Digit = Lookup(dig , Tab_7seg) ' read value in table Else Digit = 0 ' if = 16 or greater reset End If End Function ' ' ***************** Converts 1 byte in two HEX figures ******************** ' This procedure converts 1 byte in range from 0 to 255 in two 4 bits hexadecimal ' figures. ' Parameters: ' Input : Valore as byte, value 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 ********************** ' This procedure allows to show a byte in hexadecimal format ' on the displays DY3 and DY4. E.g.: 255= FFH, 32= 20H etc. ' Parametri: ' 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 I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig3 ' point to display 3 (DY3) Call Cifre(n1 , Cifh , Cifl) ' convert number in 2 HEX figures Cifra = Digit(cifh) ' convert the decines figure in HEX in 7 segments I2cwbyte Cifra ' output to display DY3 Cifra = Digit(cifl) ' convert the units figure in HEX in 7 segments I2cwbyte Cifra ' output to display DY4 I2cstop ' stop sequence 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 ' ************************************************************************** '