' ********************************************************************** ' ** Program name: AD11chau.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 analogic channel out of eleven, managed by ' IC12 (TLC2543), visualization of the channel is in hexadecimal format, ' through T1 and T2 the channel to convert is selected, T1 increments ' while T2 decrements. ' The display shows first the channel being converted, then the 12 bits ' wide hexadecimal value of the channel converted: ' ' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' ' $regfile = "8515def.dat" ' AT90s8515 $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 Cs Alias Portb.3 ' PIN 14 Chip Select signal for IC12 Config Pinb.3 = Output Clk Alias Portb.4 ' PIN 13 Clock signal for IC12 Config Pinb.4 = Output Din Alias Portb.5 ' PIN 12 Dato In signal for IC12 Config Pinb.5 = Output Dout Alias Pinb.6 ' PIN 11 Dato Out signal for IC12 Config Pinb.6 = Input ' '****************** Dichiarazione delle costanti *********************** ' Const Saa1064 = &H38 ' Slave address SAA1064 Const Wsaa1064 = &H70 ' Slave address SAA1064 in Write Const Rsaa1064 = &H71 ' Slave address SAA1064 in Read ' ***************** Elenco indirizzi per 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 ' '*********************** 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 Nb As Byte ' byte for DY1 and DY2, or DY3 and DY4 Dim Nw As Word ' word for DY1, DY2, DY3 and DY4 Dim Adin As Byte ' data to send to IC12 Dim Adout As Word ' conversion read by IC12 Dim C As Byte ' indicates the channel to convert Dim Ch As Byte ' channel being converted Dim Valore As Byte ' generic use Dim X As Byte , Y As Byte , Z As Byte ' generic use Dim 1bit As Bit ' generic use ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization Declare Sub Ad(byval Ch As Byte , Adout As Word) ' Analogic conversion Declare Sub Ad12(adin As Byte , Adout As Word) ' A/D converter 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_word (nw As Word) ' Shows a word in HEX Declare Sub Vis_ch(byval C As Byte) ' Shows the channel being converted ' '*************************** Main program ****************************** ' Main: ' Delay for signals settling Waitms 1 Call Iniz ' initializations C = 0 ' channel 0 Do For Z = 1 To 10 ' visualization for 10 times Call Vis_ch(c) ' of the channel to convert Debounce T1 , 0 , Incrementa , Sub Debounce T2 , 0 , Decrementa , Sub Waitms 50 ' delay Next Z For Z = 1 To 35 ' visualization for 35 times Call Ad(c , Adout) ' A/D conversion Call Vis_word(adout) ' visualization of A/D value in HEX Debounce T1 , 0 , Incrementa , Sub Debounce T2 , 0 , Decrementa , Sub Waitms 50 ' delay Next Z Loop ' loop forever Incrementa: ' Increment of the channel to convert Buz = 0 ' turn on the buzzer Waitms 50 ' delay If C < 10 Then ' if lower than 10 Incr C ' increment Else ' otherwise C = 0 ' select channel 0 End If Buz = 1 ' turn off the buzzer Return Decrementa: ' decrement the channel to convert Buz = 0 ' turn on the buzzer Waitms 50 ' delay If C = 0 Then ' if channel 0 selected C = 10 ' select channel 10 Else ' otherwise Decr C ' decrement channel End If 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 buzzer Cs = 1 Clk = 0 ' Set to 0 the Clk signal Din = 0 ' Set to 0 the signal of data to send 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 ' Comunicate 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 ' sets DY1 off I2cwbyte 0 ' sets DY2 off I2cwbyte 0 ' sets DY3 off I2cwbyte 0 ' sets DY4 off I2cstop End Sub ' ' ************************************************************************ ' ' ********************* Analogic conversion ****************************** ' This procedure allows to perform a 12 bit conversion through ' IC12 (TLC2543), on all the 11 external channels of the component. ' Parameters: ' Input : ch as byte, indicates the channel to convert 0..10 ' Output : Adout as word, indicates the 12 bits converted value 0..FFF(0V..+Vref) ' ************************************************************************ ' Sub Ad(ch As Byte , Adout As Word) ' Analogic conversion If Ch > 10 Then ' if channel greater than 10 Ch = 10 ' Set it to 10 End If Ch = Ch * &H10 ' Right shift of 4 Ch = Ch Or &B00001000 ' Select the 12 bit conversion Call Ad12(ch , Adout) ' Read the previous conversion Delay ' Delay Call Ad12(ch , Adout) ' Read the requested conversion End Sub ' ********************** A/D converter management ********************** ' This procedure performs the serial communication with IC12 (TLC2543), ' on each Clock impulse it sends a bit and reads another bit. ' Parameters: ' Input : adin as byte, indicates the data to send to IC12 ' Output : Adout as word, indicates the 12 bits converted value 0..FFF(0V..+Vref) ' ************************************************************************ ' Sub Ad12(adin As Byte , Adout As Word) ' A/D converter management Adout = 0 ' reset the converted value Cs = 0 ' signal CS setlow For X = 1 To 12 ' perform 12 cycles Clk = 0 ' set low Clock Y = Adin And &H80 ' read Bit7 of Adin Adin = Adin And &H7F ' mask bit7 of Adin Adin = Adin * 2 ' right shift of 1 position If Y = &H80 Then ' test bit7 of Adin Din = 1 ' set to 1 signal for IC12 Else ' otherwise Din = 0 ' set to 0 signal for IC12 End If Clk = 1 ' set Clock signal high 1bit = Dout ' read data to send to IC12 If 1bit = 1 Then ' test if equal to 1 Adout = Adout Or 1 ' set to 1 bit0 of Adout End If If X <> 12 Then ' Not 12th cycle Adout = Adout * 2 ' right shift of 1 position End If Next X ' end cycle Clk = 0 ' set low Clock Cs = 1 ' set high CS 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. ' ************************************************************************ ' Sub Digit (dig As Byte ) ' Converts a number in range 0-F in 7 segments If Dig < 16 Then ' number is lower than 16 Dig = Lookup(dig , Tab_7seg) ' read value in table Else Dig = 0 ' if = 16 or greater reset End If End Sub ' ' ***************** 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 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 Word in HEX ********************** ' The first two displays (DY1 and DY2) show the high byte, while the last two ' show the low byte (DY3 and DY4). ' This procedure allows to show a Word in hexadecimal format. ' E.g.: 65535= FFFFH, 257= 101H etc. ' Parameters: ' Input : Nw as word, number to visualize in HEX ' Nb as byte, stores high byte or low byte of Nw ' Output : nothing ' ************************************************************************ ' Sub Vis_word (nw As Word) ' visualizes a byte in HEX I2cstart ' START sequence I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig1 ' point to display 1 (DY1) Nb = High(nw) Call Cifre(nb , Cifh , Cifl) ' convert number in 2 HEX figures Call Digit(cifh) ' convert figure in HEX in 7 segments I2cwbyte Dig ' output to display DY1 Call Digit(cifl) ' convert figure in HEX in 7 segments I2cwbyte Dig ' output to display DY2 Nb = Low(nw) Call Cifre(nb , Cifh , Cifl) ' convert number in 2 HEX figures 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 End Sub ' ' ********************* Analogica conversion ****************************** ' This procedure allows to visualize which channel is being converted, writing ' "C", "H", and number of channel in Hex. ' Parameters: ' Input : c as byte, indicates which channel is being converted 0..10 ' Output : nothing ' ************************************************************************* ' Sub Vis_ch (c As Byte) ' visualizes the channel being converted I2cstart ' START sequence I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig1 ' point to display 1 (DY1) I2cwbyte 0 ' output to display DY1, off I2cwbyte &H39 ' output to display DY2, C I2cwbyte &H76 ' output to display DY3, H Call Digit(c) ' convert figure in HEX in 7 segments I2cwbyte Dig ' 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 ' ************************************************************************************** '