' ********************************************************************** ' ** Program: rs232e.BAS - Version : 1.1 - 16 July 2003 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.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 sends to the serial line, set with 19200 baud, character ' "1" if T1 is pressed or character "2" if T2 is pressed. ' It also shows on the 7 segment display the hexadecimal numeric value ' of last byte received from serial port. ' This version of the program is suitable for use with microcontrollers ' AT89c2051 or AT89c2051. ' ' Before compiling set inl menu Option/Compiler/Misc/: ' Byte End 5F. ' ' $regfile = "89c4051.DAT" ' 89c4051 registers list '$regfile = "REG51.DAT" ' 8xc51 registers list $romstart = &H0 ' code start address $crystal = 11059200 ' micro clock frequence $baud = 19200 ' Baud rate of serial port $romstart = &H0 ' indirizzo di partenza del codice $crystal = 11059200 ' frequenza di clock del micro Config Debounce = 50 ' time to waits before acqiring ' a key pressed (debouncing) ' E. g.: 10= 10ms, 50= 50ms ' ' *************** List PINs for use with 89c1051/2051/4051 ********************* Config Sda = P1.7 ' Pin 19 DATA signal for I2CBUS Config Scl = P1.6 ' Pin 18 Clock signal for I2CBUS T1 Alias P1.5 ' Pin 17 connected to key T1 T2 Alias P1.4 ' Pin 16 connected to key T2 T3 Alias P1.3 ' Pin 15 connected to key T3 Buz Alias P3.5 ' Pin 9 connected to Buzzer ' ' *************************** List PINs for use with per 8xc51 ********************* 'Config Buz Alias P3.5 ' Pin 15 connected to buzzer 'Config Sda = P3.6 ' Pin 16 DATA signal for I2CBUS 'Config Scl = P3.7 ' Pin 17 Clock signal for I2CBUS 'T1 Alias P1.0 ' Pin 1 connected to key T1 'T2 Alias P1.1 ' Pin 2 connected to key T2 'T3 Alias P1.2 ' Pin 3 connected to key T3 ' '********************* 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 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 ' '*********************** Variables declaration ************************* ' ' Dim Valore As Byte ' valore for I2CBUS communication 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 DY3 and DY4 Dim S As Byte ' Character read from serial port ' '********************** Procedure declarations ************************* ' Declare Sub Iniz ' Peripherals initialization 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_byte(nb As Byte) ' Shows a byte in HEX Main: Nb = 0 Waitms 1 Call Iniz ' Initializations Do Debounce T1 , 0 , Invia_1 , Sub ' If key T1 pressed, send "1" Debounce T2 , 0 , Invia_2 , Sub ' If key T2 pressed, send "2" S = Inkey ' Read serial port If S <> 0 Then ' If one character present Nb = S ' Store it End If Call Vis_byte(nb) ' Show last character stored Loop End ' loop forever Invia_1: Print "1"; Return Invia_2: Print "2"; Return ' '**************************** Program end ****************************** ' ' '**************************** Procedures ******************************* ' ' ********************** Peripherals initialization ******************** ' This procedure performs all the system initializations. ' Parameters: ' Input : nothing ' Output : nothing ' ************************************************************************ ' Sub Iniz 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 0 ' sets DY1 off I2cwbyte 0 ' sets DY2 off I2cwbyte 0 ' sets DY3 off I2cwbyte 0 ' sets DY4 off 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. ' ************************************************************************ ' 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 byte in HEX ********************** ' This procedure allows to show a byte in hexadecimal format. ' E.g.: 255= FFH, 32= 20H etc. ' Parameters: ' Input : Nb as byte, figure to show in HEX format ' Output : nothing ' ************************************************************************ ' Sub Vis_byte(nb As Byte) ' visualizes a byte in HEX I2cstart ' Start sequence for I2CBUS I2cwbyte Wsaa1064 ' slave address I2cwbyte Dig1 ' point to display 1 (DY1) I2cwbyte 0 ' DY1 off I2cwbyte 0 ' DY2 off Call Cifre(nb , Cifh , Cifl) ' convert number in 2 HEX figures Call Digit(cifh) ' convert the figure in 7 segments I2cwbyte Dig ' output to display DY3 Call Digit(cifl) ' convert the figure 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 ' ************************************************************************************** '