' ********************************************************************** ' * File: uk_BASAVR_050.BAS * ' * Version: 1.1 * ' * Date: 04.09.11 * ' * Development Tools: Bascom-AVR Demo Ver. 1.11.9.1 + * ' * + AVR bootloader grifo(r) Ver. 1.2 * ' * Cards: GMM AM08 + 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 50 of BASCOM AVR course. ' The program reads and writes messages on SPI EEPROM, Microchip 25LC040. ' It performs the foundamental operations on the component by using a software ' SPI 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 AVR (see IDE Configuration). ' The program works only when the GMM AM08 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: None. ' ' 04/09/11: uk_BASAVR_049.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to coorectly use this demo program, please execute the following ' steps: ' 1) Check the availability of M8DEF.DAT file into the directory where the ' BASCOM AVR is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Chip" set: ' Chip: m8def.dat ' XRAM: None ' HW Stack: 64 ' Soft Stack: 32 ' Framesize: 64 ' XRAM waitstate: disabled ' External Access Enable: disabled ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM AM08, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' 4) At the end of compilation, after the code is programmed on GMM AM08, open ' the terminal emulation window of BASCOM AVR with the option: Tools | ' Terminal emulator (Ctrl+T) and then reset or powen on the Mini Module. '************************* Compiler directives ********************************* $regfile "M8DEF.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $crystal = 7372800 ' Microcontroller crystal frequency $hwstack = 64 ' Hardware stack space $swstack = 32 ' Software stack space $framesize = 64 ' Frame space $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 !!! ' ' 25LC040 GMM TST3 pin Z2 pin Signal Signal ' signal resource GMM TST3 GMM AM08 GMM AM08 uP ' /CS CN4.1 32 26 PCO ADC0 PC0 ' SCK CN4.4 31 25 PC1 ADC1 PC1 ' SI CN4.3 30 24 PB1 OC1A PB1 ' SO CN4.6 29 23 PB0 ICP PB0 ' Vcc CN4.18 34 28 +5 Vdc Vcc ' GND CN4.17 20 14 GND GND ' ' Signal pin COMx pin CN5 pin Z2 pin Signal Signal ' PC DB9 GMM TST3 GMM TST3 GMM AM08 GMM AM08 uP ' TX 3 3 9 3 RxD RS232 PD0 ' RX 2 2 10 4 TxD RS232 PD1 ' GND 5 5 20 14 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. Pincs Alias Portc.0 ' Bit with output signal connected to CS of EE Pinsck Alias Portc.1 ' Bit with output signal connected to SK of EE Pinsi Alias Portb.1 ' Bit with output signal connected to SI of EE Pinso Alias Pinb.0 ' Bit with input signal connected to SO of EE Pinrx Alias Ddrd.0 ' Bit with direction signal connected to GMM AM08 RxD Pintx Alias Ddrd.1 ' Bit with direction signal connected to GMM AM08 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 Spibyte As Byte ' Byte to communicate with SPI interface Dim Addspi As Word ' Address to send with SPI interface Dim Datspi As Byte ' Data to communicate on SPI 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 Ini_spi() ' Initialize lines used for the software SPI interface Declare Sub Comin_spi() ' Acquire a byte from software SPI interface Declare Sub Comout_spi(byval Spibyte As Byte) ' Send a byte on software SPI interface Declare Sub Read_spiee() ' Read a byte from SPI EE Declare Sub Write_spiee() ' Write a byte on SPI EE Declare Sub Enawr_spiee() ' Enable write on SPI EE Declare Sub Diswr_spiee() ' Disable write on SPI EE Declare Sub Read_msg_spiee() ' Read a message from SPI EE Declare Sub Write_msg_spiee() ' Write a message on SPI EE Declare Sub Show_msg_spiee() ' Show the messages saved on SPI EE '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Call Ini_spi() ' Initialize lines used for software SPI interface Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Messages management on 25LC040 SPI EEPROM with GMM AM08 + GMM TST3" Print "Mount Mini Module on Z2 of GMM TST3, connect EEPROM to CN4 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_spiee() ' Show the messages saved on Microwire 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 SPI EEPROM Call Write_msg_spiee() ' Save message on Microwire EE Case "R": ' Selected operation is read message from SPI EEPROM Call Show_msg_spiee() ' Show the messages saved on SPI EEPROM Input "Message number to read: " , Nmsg ' Ask and gest the message number to read Call Read_msg_spiee() ' Read message from SPI EEPROM If Msgsaved = 1 Then ' If message saved on SPI EEPROM and 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 *************************** ' Initialize signals and variables used for the SPI interface managed by ' software ' Input: None ' Output: None Sub Ini_spi() ' Connection of software SPI interface signals (see on line help and previous ' table for further information) Ddrb.0 = 0 ' Initialize PB0=SO as digital input Pinsck = 0 ' Initialize PC1=SCK as digital output at low level Ddrc.1 = 1 Pinsi = 0 ' Initialize PB1=SI as digital output at low level Ddrb.1 = 1 Pincs = 0 ' Initialize PC0=/CS as digital output at low level Ddrc.0 = 1 End Sub ' Communicate on SPI interface managed by software by receiving a byte from ' connected device. The 8 bits of byte are acquired on high level of SCK ' signal, with MSB first. ' Input: None ' Output: Spibyte = Acquired byte Sub Comin_spi() For Hlpb = 1 To 8 ' Loop repeated for all the bits to communicate Shift Spibyte , Left , 1 ' Update current bit of received data Pinsck = 1 ' SCK signal at high level Spibyte.0 = Pinso ' Get current bit from SO signal and save it on received data Pinsck = 0 ' SCK signal at low level Next Hlpb End Sub ' Communicate on SPI interface managed by software by sending a byte to ' connected device. The 8 bits of byte are set on low level of SCK ' signal, with MSB first. ' Input: Spibyte = Byte to send ' Output: None Sub Comout_spi(byval Spibyte As Byte) For Hlpb = 1 To 8 ' Loop repeated for all the bits to communicate Pinsi = Spibyte.7 ' Set current bit status to send on SI signal Pinsck = 1 ' SCK signal at high level Shift Spibyte , Left , 1 ' Update current bit of data to send Pinsck = 0 ' SCK signal at low level Next Hlpb End Sub ' Read a byte from 25LC040 SPI EEPROM. ' Input: Addspi = EEPROM location address to read ' Output: Datspi = data read from EEPROM Sub Read_spiee() Pincs = 0 ' /CS signal at low level Spibyte = High(addspi) ' Obtain high byte of EEPROM address to read Shift Spibyte , Left , 3 Spibyte = Spibyte Or &H03 ' Add READ command code Call Comout_spi(spibyte) ' Send to EEPROM the byte with high part of address and command Spibyte = Low(addspi) ' Obtain low byte of EEPROM address to read Call Comout_spi(spibyte) ' Send to EEPROM the byte with low part of address Call Comin_spi() ' Read byte from EEPROM Datspi = Spibyte ' Return read data Pincs = 1 ' /CS signal at high level End Sub ' Write a byte to 25LC040 SPI EEPROM. ' Input: Addspi = EEPROM location address to write ' Datspi = data to write on EEPROM ' Output: None Sub Write_spiee() Pincs = 0 ' /CS signal at low level Spibyte = High(addspi) ' Obtain high byte of EEPROM address to read Shift Spibyte , Left , 3 Spibyte = Spibyte Or &H02 ' Add READ command code Call Comout_spi(spibyte) ' Send to EEPROM the byte with high part of address and command Spibyte = Low(addspi) ' Obtain low byte of EEPROM address to read Call Comout_spi(spibyte) ' Send to EEPROM the byte with low part of address Spibyte = Datspi ' Save data to write in byte to send Call Comout_spi(spibyte) ' Send to EEPROM byte to write Pincs = 1 ' /CS signal at high level Waitms 10 ' Wait end of write time for SPI EEPROM (5 msec on data sheet) End Sub ' Enable write operations on 25LC040 SPI EEPROM ' Input: None ' Output: None Sub Enawr_spiee() Pincs = 0 ' /CS signal at low level Spibyte = &H06 ' WREN command code (enable write operation) Call Comout_spi(spibyte) ' Send to EEPROM the byte with command Pincs = 1 ' /CS signal at high level End Sub ' Disable write operations on 25LC040 SPI EEPROM ' Input: None ' Output: None Sub Diswr_spiee() Pincs = 0 ' /CS signal at low level Spibyte = &H04 ' WRDI command code (disable write operation) Call Comout_spi(spibyte) ' Send to EEPROM the byte with command Pincs = 1 ' /CS signal at high level End Sub ' Each message occupies 22 bytes of SPI 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 SPI 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_spiee() Addspi = Nmsg * 22 ' Calculate start address of message saved on SPI EEPROM Call Read_spiee() ' Get message saved indicator from SPI EEPROM If Datspi = Cod_msg_ee Then ' If message is already saved on SPI EEPROM Incr Addspi ' Increase the SPI EEPROM read address Call Read_spiee() ' Get message length from SPI EEPROM Lstr = Datspi Msgstr = Space(lstr) ' Arrange string long as message to read For Pstr = 1 To Lstr ' Cycle repeated for characters of message Incr Addspi ' Increase the SPI EEPROM read address Call Read_spiee() ' Get ASCII code of current character from SPI EEPROM Chrstr = Chr(datspi) ' Obtain character from ASCII code Mid(msgstr , Pstr , 1) = Chrstr ' Save current character of message Next Pstr Msgsaved = 1 ' Message was saved on SPI EEPROM and it has been read Else Msgsaved = 0 ' Message was not saved on SPI EEPROM and it hasn't been read End If End Sub ' Write a message from SPI 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_spiee() Addspi = Nmsg * 22 ' Calculate start address of message saved on SPI EEPROM Datspi = Cod_msg_ee ' Write message saved indicator on SPI EEPROM Call Enawr_spiee() ' Enable erase and write operations on SPI EEPROM Call Write_spiee() Lstr = Len(msgstr) ' Obtain length of string with message Datspi = Lstr ' Write message length on SPI EEPROM Incr Addspi ' Increase SPI EEPROM write address Call Enawr_spiee() ' Enable erase and write operations on SPI EEPROM Call Write_spiee() For Pstr = 1 To Lstr ' Cycle repeated for characters of message Chrstr = Mid(msgstr , Pstr , 1) ' Get current character of message Datspi = Asc(chrstr) ' Obtain ASCII code of current character Incr Addspi ' Increase SPI EEPROM write address Call Enawr_spiee() ' Enable erase and write operations on SPI EEPROM Call Write_spiee() ' Write current character ASCII code on SPI EEPROM Next Pstr End Sub ' Show on serial console the list of message currently written=saved on SPI EEPROM ' Input: None ' Output: None Sub Show_msg_spiee() Print "Message currently saved on SPI 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_spiee() ' Read current message of SPI EEPROM list If Msgsaved = 1 Then ' If message saved on SPI 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 ************************