' ********************************************************************** ' * File: uk_BAS51_052.BAS * ' * Version: 1.1 * ' * Date: 25.09.11 * ' * Development Tools: Bascom 8051 COMP.,IDE 2.0.14.0 + FLIP 2.4.6 * ' * Cards: GMM 5115 + GMM TST3 * ' * Developed by: GRIFO(r) Italian Technology * ' * via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * Author: Gianluca Angelini * ' ********************************************************************** ' Example program 052 of BASCOM 8051 course. ' The program reads and writes messages on I2C BUS EEPROM, Atmel 24C08A. ' It performs the foundamental operations on the component by using a software ' I2C BUS interface and by interacting with user on a serial console provided ' of monitor and keyboard with a fixed physical protocol at 19200 Baud, 8 Bit ' x chr, 1 Stop bit, No parity. ' This console can be another system capable to support a serial RS 232 ' communication. In order to simplify the use it can be used a PC provided of ' one COMx line, that execute a terminal emulation program as HYPERTERMINAL or ' the homonym modality provided by BASCOM 8051 (see IDE Configuration). ' The program works only when the GMM 5115 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: None. ' ' 25/09/11: uk_BAS51_052.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to correctly use this demo program, please execute the following ' steps: ' 1) Check the availability of 89C5115.DAT file into the directory where the ' BASCOM 8051 is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Misc" set: ' Register File = 89C5115.DAT ' Byte End(Hex) = A0 ' Size warning = selected at 16384 (=4000H) ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM 5115, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' Run emulator modal = not selected ' 4) At the end of compilation, after the code is programmed on GMM 5115, select ' RUN mode and open the terminal emulation window of BASCOM 8051 with the ' option: Tools | Terminal emulator (Ctrl+T) and then reset or powen on the ' Mini Module. '************************* Compiler directives ********************************* $regfile "89C5115.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $iramstart = &H0 ' Data start address on internal RAM $ramstart = &H0 ' Data start address on external RAM $ramsize = &H100 ' External RAM size $crystal = 14745600 ' Microcontroller crystal frequency $large ' Code size > 2K $map ' Generate debug information $baud = 19200 ' Serial communication speed: 19200 Baud ' Other parameters fixed to: 8 bit x chr ' 1 Stop bit ' No parity '******************************* Definitions *********************************** ' The resources used by program are connected as described in following table. ' !!! Note: On GMM TST3 the jumpers must be set in following positions: ' J1 in 2-3 ; J2 in 1-2 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3!!! ' ' 24C08A GMM TST3 pin Z2 pin Signal Signal ' signal resource GMM TST3 GMM 5115 GMM 5115 uP ' SCL CN3.15 12 6 P2.1 P2.1 ' SDA CN3.16 13 7 P2.0 P2.0 ' Vcc CN4.18 34 28 +5 Vdc Vcc ' GND CN4.17 20 14 GND GND ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Used up ' PC DB9 GMM TST3 GMM TST3 GMM 5115 GMM 5115 signal ' TX 3 3 9 3 RxD RS232 P3.0 ' RX 2 2 10 4 TxD RS232 P3.1 ' GND 5 5 20 14 GND GND ' This table shows that the connection cable between PC COM line and CN5 of ' GMM TST3 is a normal pin to pin cable or direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* Const Max_msg_ee = 4 ' Maximum number of messages on EEPROM, starting from 0 Const Cod_msg_ee = &H5A ' Code that denote message saved on EEPROM '************************* Variables declaration ******************************* Dim Hlpb As Byte ' General purpose help byte variable Dim Choice As Byte ' Selected operation Dim I2cbyte As Byte ' Byte to communicate with I2C BUS interface Dim Addi2c As Word ' Address to send with I2C BUS interface Dim Dati2c As Byte ' Data to communicate with device I2C BUS interface Dim Msgstr As String * 20 ' String with message for EE Dim Lstr As Byte ' String with message length Dim Pstr As Byte ' Pointer to string with message Dim Chrstr As String * 1 ' Character of the message to generate Dim Nmsg As Byte ' Message number to read/write from/on EE Dim Msgsaved As Boolean ' Message saved on EE flag '************************ Subroutines declaration ****************************** Declare Sub Ask_key() ' Ask and wait a key pressure Declare Sub Ini_i2c() ' Initialize lines used for the software I2C BUS interface Declare Sub Read_i2cee() ' Read a byte from I2C BUS EE Declare Sub Write_i2cee() ' Write a byte on I2C BUS EE Declare Sub Read_msg_i2cee() ' Read a message from I2C BUS EE Declare Sub Write_msg_i2cee() ' Write a message on I2C BUS EE Declare Sub Show_msg_i2cee() ' Show the messages saved on I2C BUS EE '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Call Ini_i2c() ' Initialize lines used for software I2C BUS interface Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Messages management on 24C08A I2C BUS EEPROM with GMM 5115 + GMM TST3" Print "Mount Mini Module on Z2 of GMM TST3, connect EEPROM to CN3 as described in" Print " electric diagram." Print "The program save/acquire messages of maximum 20 characters on/from EEPROM, and" Print " it shows them on serial console: through a menu on PC it can be performed the" Print " selected operations." Do ' Begin endless loop Print ' Shows menu with available operations Print "W -> Write message on EEPROM" Print "R -> Read message from EEPROM" Print "Perform the choice by pushing the associated key: "; Choice = Waitkey() ' Wait selection of operation to execute Printbin Choice ' Shows performed choice Print If Choice >= "a" Then ' If choice is lower case Choice = Choice And &HDF ' Convert it in upper case End If Print ' Separate menu from following visualizations Select Case Choice ' Check converted choice Case "W": ' Selected operation is write message on EE Call Show_msg_i2cee() ' Show the messages saved on I2C BUS EE Input "Message number to save: " , Nmsg ' Ask and gest the message number to save Print "Insert message to save on EEPROM (20 characters max.): " Input Msgstr ' Get message to save on I2C BUS EEPROM Call Write_msg_i2cee() ' Save message on I2C BUS EE Case "R": ' Selected operation is read message from EEPROM Call Show_msg_i2cee() ' Show the messages saved on I2C BUS EEPROM Input "Message number to read: " , Nmsg ' Ask and gest the message number to read Call Read_msg_i2cee() ' Read message from I2C BUS EEPROM If Msgsaved = 1 Then ' If message saved on I2C BUS EEPROM can be read Print "Message available: " ; Msgstr ' Show indications and message Else ' Message not saved and not read Print "Message not available" ' Show proper indication End If Case Else: ' Selected operation is not valid Printbin &H07 ' Generate an advise BEL on console End Select Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Require and wait a key pressure on console ' Input: None ' Output: None Sub Ask_key() Print "Press a key to continue..."; Hlpb = Waitkey() Print End Sub ' Initialize signals and variables used for the I2C BUS interface managed by ' software ' Input: None ' Output: None Sub Ini_i2c() ' This subroutine has been developed for compatibility reason with demo ' programs for EEPROM with other protocols at low level. For I2C BUS protocol ' are used the BASCOM high level instructions. ' Connection of software I2C BUS interface signals (see on line help and ' previous table for further information) Config Scl = P2.1 ' Microcontroller signal used as SCL signal of I2C BUS Config Sda = P2.0 ' Microcontroller signal used as SDA signal of I2C BUS End Sub ' Read a byte from 24C08A I2C BUS EEPROM. ' Input: Addi2c = EEPROM location address to read ' Output: Dati2c = data read from EEPROM Sub Read_i2cee() I2cbyte = High(addi2c) ' Obtain high byte of EEPROM address to read I2cbyte = I2cbyte And &H07 ' Mantain meaning part for 24Cxx EEPROM Shift I2cbyte , Left , 1 ' Convert high byte of address into slave address I2cbyte = I2cbyte Or &HA8 ' Add write slave address I2cstart ' Send start I2cwbyte I2cbyte ' Send obtained slave address I2cbyte = Low(addi2c) ' Obtain address, that is low byte of EEPROM address I2cwbyte I2cbyte ' Send address to read I2cstart ' Send repeated start I2cbyte = High(addi2c) ' Obtain high byte of EEPROM address to read I2cbyte = I2cbyte And &H07 ' Mantain meaning part for 24Cxx EEPROM Shift I2cbyte , Left , 1 ' Convert high byte of address into slave address I2cbyte = I2cbyte Or &HA9 ' Add read slave address I2cwbyte I2cbyte ' Send obtained slave address I2crbyte Dati2c , Nack ' Read data and arrange end of reading I2cstop ' Send stop End Sub ' Write a byte to 24C08A I2C BUS EEPROM. ' Input: Addi2c = EEPROM location address to write ' Dati2c = data to write on EEPROM ' Output: None Sub Write_i2cee() I2cbyte = High(addi2c) ' Obtain high byte of EEPROM address to write I2cbyte = I2cbyte And &H07 ' Mantain meaning part for 24Cxx EEPROM Shift I2cbyte , Left , 1 ' Convert high byte of address into slave address I2cbyte = I2cbyte Or &HA8 ' Add write slave address I2cstart ' Send start I2cwbyte I2cbyte ' Send obtained slave address I2cbyte = Low(addi2c) ' Obtain address, that is low byte of EEPROM address I2cwbyte I2cbyte ' Send address to write I2cbyte = Dati2c ' Data to write I2cwbyte I2cbyte ' Send data to write I2cstop ' Send stop Waitms 10 ' Wait end of write time for SPI EEPROM (5 msec on data sheet) End Sub ' Each message occupies 22 bytes of I2C BUS EEPROM: the first dedicated to a message ' saved indicator, the second dedicated to message length and the following ' 20 bytes dedicated to the maximum 20 characters of the message. ' ' Read a message from I2C BUS EEPROM, by checking if it has been previously saved. ' Input: Nmsg = variable with message number to read ' Output: Msgstr = string variable with read message ' Msgsaved = flag for message saved and thus read Sub Read_msg_i2cee() Addi2c = Nmsg * 22 ' Calculate start address of message saved on I2C BUS EEPROM Call Read_i2cee() ' Get message saved indicator from I2C BUS EEPROM If Dati2c = Cod_msg_ee Then ' If message is already saved on I2C BUS EEPROM Incr Addi2c ' Increase the I2C BUS EEPROM read address Call Read_i2cee() ' Get message length from I2C BUS EEPROM Lstr = Dati2c Msgstr = Space(lstr) ' Arrange string long as message to read For Pstr = 1 To Lstr ' Cycle repeated for characters of message Incr Addi2c ' Increase the I2C BUS EEPROM read address Call Read_i2cee() ' Get ASCII code of current character from I2C BUS EEPROM Chrstr = Chr(dati2c) ' Obtain character from ASCII code Mid(msgstr , Pstr , 1) = Chrstr ' Save current character of message Next Pstr Msgsaved = 1 ' Message was saved on I2C BUS EEPROM and it has been read Else Msgsaved = 0 ' Message was not saved on I2C BUS EEPROM and it hasn't been read End If End Sub ' Write a message from I2C BUS EEPROM, by adding proper message saved indicator ' Input: Nmsg = variable with message number to write ' Msgstr = string variable with message to write ' Output: None Sub Write_msg_i2cee() Addi2c = Nmsg * 22 ' Calculate start address of message saved on I2C BUS EEPROM Dati2c = Cod_msg_ee ' Write message saved indicator on I2C BUS EEPROM Call Write_i2cee() Lstr = Len(msgstr) ' Obtain length of string with message Dati2c = Lstr ' Write message length on I2C BUS EEPROM Incr Addi2c ' Increase I2C BUS EEPROM write address Call Write_i2cee() For Pstr = 1 To Lstr ' Cycle repeated for characters of message Chrstr = Mid(msgstr , Pstr , 1) ' Get current character of message Dati2c = Asc(chrstr) ' Obtain ASCII code of current character Incr Addi2c ' Increase I2C BUS EEPROM write address Call Write_i2cee() ' Write current character ASCII code on I2C BUS EEPROM Next Pstr End Sub ' Show on serial console the list of message currently written=saved on I2C BUS EEPROM ' Input: None ' Output: None Sub Show_msg_i2cee() Print "Message currently saved on I2C BUS EEPROM:" ' Show list head description For Nmsg = 0 To Max_msg_ee ' Loop that shows messages numbered from 0 Print Nmsg ; " = "; ' Show message number of the list Call Read_msg_i2cee() ' Read current message of I2C BUS EEPROM list If Msgsaved = 1 Then ' If message saved on I2C BUS EEPROM and read Print Msgstr ' Show read message of the list Else ' Message not saved and not read Print ' Show empty message of the list End If Next Nmsg End Sub '******************* End of subroutines used by program ************************