' ********************************************************************** ' ** Program name: WEEF2.BAS - Version : 1.1 - 12 April 2000 ** ' ** Compiler : BASCOM 8051, (IDE V.1.0.0.20, LIB V.1.20) ** ' ** Board : K51-AVR and GPC F2 ** ' ** 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: Adriano Pedrielli ** ' ********************************************************************** ' ' This program allows to write to a serial EEPROM on IC4 (max 24c08), ' with addresses ranging from &H400 to &H7ff, addresses from &H0 to ' &H0FF are taken by IC7 (RTC PCF8583) while addresses from &H100 to ' &H3FF are free space. ' At start the program shows the address where to write, through keys ' 1 and 2 of the PC keyboard the value in incremented or decremented. ' Through key 3 the address is accepted, then the value to write is ' selected through 1 and 2, as last press key 3 to write. ' Whenever a key is pressed, an acoustic signal is emitted. ' After the operation is terminated the selected address and the written ' data are shown one after the other. ' This program is controlled through the RS 232 serial line so it is essential ' to connect a free COM port on the PC to the connector CN3 of GPC(r) F2. ' Configure the BASCOM 8051 terminal using menu Option/Communication, select ' the COM port and set baud rate to 9600, parity to none, databits to 8 and ' stopbits to 1. ' ' Perform the following connections: ' GPC F2 K51 AVR ' +5Vdc (pin15 CN3) ----> +5Vdc (pin1 CN6) ' Gnd (pin16 CN3) ----> Gnd (pin2 CN6) ' T0 P3.4 (pin3 CN3) ----> SD (pin7 CN6) ' T1 P3.5 (pin1 CN3) ----> SC (pin8 CN6) ' ' Before compiling set in menu Option/Compiler/Misc: ' Byte End 5F. ' '*********************************************************************** ' '********************* Compiler Directives ***************************** ' $regfile = "REG51.DAT" ' list of CPU registers $romstart = &H8050 ' code start address ' &H8050 for MO52 Rel.1.1 in RAM IC8 ' &H0000 for EPROM withput RAM IC8 $ramstart = &HD000 ' add. of variables external RAM $ramsize = &H2800 ' assign 10K of external RAM $crystal = 11059200 ' CPU clock frequence $baud = 9600 ' RS-232 communication speed $large ' 16 bit addressing ' ' ******************************* 8xc51 PIN list *********************** ' Config Sda = P3.4 ' line P3.4 or T0 (pin 3 of CN3) ' on GPC(r) F2 is Data signal. Config Scl = P3.5 ' line P3.5 or T1 (pin 1 of CN3) ' on GPC(r) F2 is Clock signal. Buz Alias P1.2 ' Pin 3 of GPC F2 microcontroller ' is connected to the on board ' buzzer. Const Cler = 12 ' clear screen code Const Bel = 7 ' Bell code ' '********************* 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 Wee As Const &HA0 ' Slave address 24cXX in Write Dim Ree As Const &HA1 ' Slave address 24cXX in Read ' ' ********************************************************************** ' Dim R As Const 1 ' value to read from EEPROM Dim W As Const 0 ' value to write to EEPROM ' '****************** Dichiarazione delle variabili ********************** ' 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 Nw As Word ' word for DY1, DY2, DY3 and DY4 Dim Ind As Word ' 16 Bits address for I2CBUS etc. Dim W_r As Byte ' select write or read by R and W Dim Hind As Byte ' byte Hight of Ind Dim Lind As Byte ' byte Low of Ind Dim Valore As Byte ' generic use Dim X As Byte ' value read Dim T As Byte ' code of key pressed Dim Tstr As String * 1 ' string of one character ' '********************** 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 Declare Sub Vis_word (nw As Word) ' Shows a word in HEX Declare Sub I2cbus (ind As Word , Valore As Byte , W_r As Byte) ' Reads/Writes to I2CBUS ' '****************************** Main program ****************************** ' Main: ' Delay for signals settling Waitms 1 Call Iniz ' initializations Ind = &H400 ' address of IC4 X = 0 ' data to write Do Do Call Vis_word(ind) ' show address T = Inkey ' read a char form serial port Loop Until T <> 0 ' exit if char received Tstr = Chr(t) ' convert char in string Select Case Tstr ' check if key pressed Case "1" : Gosub Inc_ind ' if pressed increment Ind Case "2" : Gosub Dec_ind ' if pressed decrement Ind Case "3" : Sound Buz , 100 , 250 ' turn on the buzzer Buz = 1 ' turn off the buzzer Print Print " 1) Increment data to write" Print " 2) Decrement data to write" Print " 3) Accept data and write it" Print " 4) Exit" Print Print "Press one of the 4 keys on P.C. keyboard" Do Do Call Vis_byte(x) ' show value to write T = Inkey ' read a char form serial port Loop Until T <> 0 ' exit if char received Tstr = Chr(t) ' convert char in string Select Case Tstr ' according to key pressed Case "1" : Gosub Inc_x ' if pressed increment X Case "2" : Gosub Dec_x ' if pressed decrement X Case "3" : Sound Buz , 100 , 250 ' turn on the buzzer Buz = 1 ' turn off the buzzer Call I2cbus(ind , X , W) ' write to I2cBUS Do Call Vis_byte(x) ' show value to write Wait 1 'delay 1 second Call Vis_word(ind) ' show address Wait 1 ' delay 1 second T = Inkey ' read a char from serial port If T <> 0 Then Tstr = Chr(t) End If Loop Until Tstr = "4" ' exit if char "4" received Ljmp 0 ' exit and restart monitor Case "4" : Ljmp 0 ' exit and restart monitor Case Else : Print Chr(bel); ' key non valid End Select Loop Case "4" : Ljmp 0 ' exit and restart monitor Case Else : Print Chr(bel); ' key non valid End Select Loop ' ' ' Inc_ind: Waitms 50 ' delay Incr Ind ' increment Ind Sound Buz , 100 , 250 ' turn on buzzer Buz = 1 ' turn off buzzer Return Dec_ind: Waitms 50 ' delay Decr Ind ' decrement Ind Sound Buz , 100 , 250 ' turn on buzzer Buz = 1 ' turn off buzzer Return Inc_x: Waitms 50 ' delay Incr X ' increment X Sound Buz , 100 , 250 ' turn on the buzzer Buz = 1 ' turn off the buzzer Return Dec_x: Waitms 50 ' delay Decr X ' decrement X Sound Buz , 100 , 250 ' turn on the buzzer 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 Print Chr(cler); ' clear screen Print "Demo K51-AVR write to EEPROM IC4(24c0x) through GPC F2 rel.1.1 22/05/2000" Print Print " 1) Increment address where to write" Print " 2) Decrement address where to write" Print " 3) Accept address where to write" Print " 4) Exit" Print Print "Press one of the 4 keys on P.C. keyboard" 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 indifferent I2cwbyte 0 ' sets DY1 off I2cwbyte 0 ' sets DY2 off I2cwbyte 0 ' sets DY3 off I2cwbyte 0 ' sets DY4 off I2cstop End Sub ' ' ************* Reads from/Writes to I2CBUS ****************************** ' This procedure reads from or writes to I2CBUS in the addresses range ' from &H000 to &H7FF on IC4 (EEPROM 24cxx) or IC7 (RTC PCF8583). ' Parameters: ' Input : Ind as words, contains the device's address ' Valore as byte, value to write ' W_R as byte, = 0 writes, = 1 reads. ' Uses 2 global variables : Hind and Lind type Byte. ' Output : Valore as byte, value read. ' ******************************************************************* ' Sub I2cbus(ind As Word , Valore As Byte , W_r As Byte) ' Reads from/Writes to I2CBUS Hind = High(ind) ' store the high byte Lind = Low(ind) ' store the low byte Hind = Hind And 7 ' mask the 3 low bits clr c ' reset carry Rotate Hind , Left ' left shift of 1 bit Hind = Hind Or Wee ' sum slave address with writing If W_r = 0 Then ' check if must read or write I2cstart ' Start sequence for I2CBUS I2cwbyte Hind ' Communicate the Slave address I2cwbyte Lind ' address of EEPROM I2cwbyte Valore ' value to write I2cstop ' Stop sequence for I2CBUS Waitms 10 ' delay of 10ms needed by the ' EEPROM after each writing Else I2cstart ' Start sequence for I2CBUS I2cwbyte Hind ' Communicate the Slave address I2cwbyte Lind ' address of EEPROM Hind = Hind Or Ree ' sum slave address with reading I2cstart ' repeat start sequence for I2CBUS I2cwbyte Hind ' Communicate the Slave address I2crbyte Valore , Nack ' read I2cstop ' stop sequence for I2CBUS End If 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 ' set DY1 off I2cwbyte 0 ' set 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 ' ' *********************** 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 ' Output : nothing ' ************************************************************************ ' Sub Vis_word (nw As Word) ' visualizza un byte in HEX I2cstart ' Start sequence for I2CBUS 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 HEX figure in 7 segment I2cwbyte Dig ' output to display DY1 Call Digit(cifl) ' convert HEX figure in 7 segment I2cwbyte Dig ' output to display DY2 Nb = Low(nw) Call Cifre(nb , Cifh , Cifl) ' convert number in 2 HEX figures Call Digit(cifh) ' convert HEX figure in 7 segment I2cwbyte Dig ' output to display DY3 Call Digit(cifl) ' convert HEX figure in 7 segment 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 ' ********************************************************************** '