Report 2 Serial Communication ============================= Jonathan Higgins 1. Introduction =============== 1.1. What is Serial Communication ? ----------------------------------- RS-232 was introduced in 1960, and is currently the most widely used communication protocol. It is simple, inexpensive to implement, and though relatively slow, it is more than adequate for most simple serial communication devices such as keyboards and mice. RS-232 is a single-ended data transmission system, which means that it uses a single wire for data transmission. (Since useful communication is generally two way, a two-wire system is employed, one to transmit and one to receive.) Signals are processed by determining whether they are positive or negative when compared with a ground. Because signals traveling this single wire are vulnerable to degradation, RS-232 systems are recommended for communication over short distances (up to 50 feet) and at relatively slow data rates, (up to 20 kbps). However, in practice, these limits can be exceeded. 2. Getting Started. =================== 2.1. What is needed? -------------------- The components needed are exactly the same as the previous report. Not all of these items listed will be used, but they may be useful in determining problems with your program, or your setup. The compiler and upload programs will be necessary. A terminal program will be needed to test the code that will be written. Windows 9x comes with a terminal emulator called Hyperterm. This program is very simple to use and basic instructions are available here. Parts needed: IBM compatible PC with serial ports Micro-Stamp11 MS11M32K-FRA Serial Cable Docking Module MS11DM-RA-M1 ribbon cable solder-less breadboard adapter disk 68HC11 Utilities 30 gauge Jumper Wires solder-less breadboard +5v Power Supply several LED's (8) piezo speaker 2.2. Using HyperTerminal ------------------------ On most Windows 95 machines, HyperTerminal should be installed without explicitly requesting it. If you need to install a copy of the standard Hyperterminal program, go to the Start menu, choose the Settings option, and click on Control Panels option. Double Click on the Add/Remove Programs icon and click on the Windows Setup tab at the top of the window. Hyperterminal is located in the Communications group. The program that starts HyperTerminal is HYPERTRM.EXE. It is located under Start Menu: Programs; Accessories: Hyperterminal. The ?HYPERTRM.EXE icon looks like a computer connected to a modem. Double-click on the icon to start the modem program. The first time you start HYPERTRM.EXE, you will be prompted for country code and area code. You do not have to fill in these codes. To avoid problems with the default value for the area code, uncheck the "Use country code and area code" box at the bottom of the dialog window. Choose OK to proceed and you will not encounter this screen again. HyperTerminal will then prompt you for information on the connection you would like to establish. On the line "Connect using:" click on the drop down menu and select the port that connects the PC to the MicroStamp11.(Direct to Com 2) Then press Ok. A Com port Properties window will open asking you to configure the port settings. Change the following parameters Bits per second: 9600 Data bits: 8 Parity: None Stop bits: 1 Flow control: Hardware Click on Ok to close this dialog box. Your terminal should be ready to communicate with the MicroStamp11. If you need to make changes to this connection, go to File:Properties: and then select the port that the connection is on or press Configure to change the parameters. Like many Windows applications, HyperTerminal has a button bar on the top of its screen that incorporates the most often-used features of the program. The functions of these buttons are as follows: New: Allows you to enter to a different phone number / group of settings.This process follows steps #1-#4 in the previous section. Open: Allows you to open a different phone number / group of settings that you have saved previously. Connect: If you are not already connected, this dials the current phone number. Disconnect: Hangs up a connection. Send: Sends a file from your local computer to the remote computer to which you are connected. If you are not sure where the file is you wish to send, click on the Browse button. You may also want to check to see if the protocol menu item is set to Zmodem (the transfer option most commonly used at CCSF). Receive: Once a file has been sent from the remote computer, click on this button to start receiving it on your local computer. Again, check to make sure the protocol matches the one used to send the file (typically Zmodem). Properties: Allows you to change the various configuration options for the session. 3. Writing Serial Programs ========================== 3.1. Simple Serial Program -------------------------- This first program will provide the tools needed to write serial communication programs. It is a very basic program that will send a single character to a terminal program and print it to the screen. org $e000 ;start at top of 8k EEPROM begin: ldaa #$04 ;disable COP in CONFIG register staa $3f ldaa #$30 ;load the bit-pattern 0011 0000 staa $2b ;Store this in the SCI Baud Rate Control ; Register, This will effectively set the ; baud rate for serial communication to 9600 ldaa #$8 ;load the bit-pattern 0000 1000 staa $2d ;Store this into the SCI Control Register 2 ; This will enable the Transmitter only Repeat: clr $0 ;clear port A ldab #$41 ;load register B with the ASCII value for "A" bsr Trans ;Branch to Subroutine Trans bsr Delay ;Delay for .5 seconds bra Repeat ;Loop back to start Trans: brclr $2e $80 Trans ;Branch to Trans if $2e (SCI Status Register) ; matches the bit-pattern 1000 0000 stab $2f ;Store B in $2f (SCI Data Register) rts ;return to subroutine Delay: ldy #$ffff D1: dey ;pad delay loop with extra cycles iny ; for total of 15 cycles dey ;this gives approx. 1/2 second at 8MHz bne D1 rts org $fffe ;define the reset vector to point to fdb begin ; the beginning of your code The first few lines of this program are identical to some of the other programs that have been presented earlier. Line 6 "ldaa #$30" starts the block of new code for this program. This line of code loads the bit-pattern of 0011 000 into register A and then stores this into the SCI Baud Rate Control Register at $2b. These 2 lines of code will set the Baud rate of the serial communication to 9600. The next few lines load another bit-pattern and store it to the SCI Control Register 2 which enables the Transmitter function only. The Repeat block starts a section of code that will continue until the program is stopped. After clearing port A, register B is loaded with an ASCII value for the letter "A". The Branch to subroutine will jump execution down to the Trans section of code. The next branch to Delay is the exact same code that was presented earlier for the flashing lights, loops and time delay program and will slow the execution down by about .5 seconds. The branch to Repeat will continue the loop. The Trans block starts with a branch if clear instruction. This instruction is somewhat complex, and may seem unclear as to why it is here. This instruction is going to examine the memory register $2e and compare it to the bit-mask 1000 0000 or $80. If the register matches the pattern then execute the same exact line of code and continue to do this until the value of $2e changes. The next line "stab $2f will store the contents of register B into $2f the SCI Data Register, and the last line will return from subroutine. The reasons for the first line of code in this block are not apparent the first time you trace through the code, but become important after the program has made a complete loop through the Repeat function. The "stab $2f" essentially places data on the serial Txd wire and sends it off to the terminal system or to wherever. This transmission takes time, in fact it takes a long time. When data is placed into $2f, something happens at the hardware level, and the Transmit Complete bit on the SCI Status Register ($2e) will change from 1 to 0. When the transmit is completed this bit will then flip back to 1, which will break the loop that the first line of the Trans block creates. That was fun. Lets do another program. 3.2. Send a String of Characters to a Terminal ---------------------------------------------- The following program will send a string of characters to a terminal through serial communication. This program is similar to the previous, but a few new elements are introduced. org $0040 ;start at bottom of memory block charcnt rmb 1 ;Variable Definition reserving 1 byte org $e000 ;start at top of 8k EEPROM begin: ldaa #$04 ;disable COP in CONFIG register staa $3f lds #$ff ;initialize the Stack pointer to high memory ldaa #$30 ;load the bit-pattern 0011 0000 staa $2b ;Store this in the SCI Baud Rate Control ; Register, This will effectively set the ; baud rate for serial communication to 9600 ldaa #$8 ;load the bit-pattern 0000 1000 staa $2d ;Store this into the SCI Control Register 2 ; This will enable the Transmitter only clr charcnt ;initialize the variable (Not sure if this is ; needed) Print: clr $0 ldaa #endmsg-begmsg ;load A with the difference between the ; addresses of endmsg and begmsg cmpa charcnt ;Compare A to charcnt blo Here ;Branch if lower Trans: brclr $2e #$80 Trans ;Branch to Trans if $2e (SCI Status Register) ; matches the bit-pattern 1000 0000 ldx #begmsg ;load register X with the address of begmsg ldab charcnt ;load register b with the value in charcnt abx ;add the value in b to the value in x and ; store this in x ldaa 0,x ;load register a with x and 0 offset staa $2f ;store this value into $2f (SCI Data Register) inc charcnt ;increment the value in charcnt bra Print ;branch to subroutine Print Here bra Here ;loop until program stops begmsg fcc 'The program has started' ;This is the message to be ; printed fcb $0d ;line feed endmsg fcb $0a ;carriage return org $fffe ;define the reset vector to point to fdb begin ; the beginning of your code This code block is very similar to the previous program with a few minor changes. The Print subroutine starts by clearing port A. The next line of code loads register A with the difference in the values of the addresses for endmsg and begmsg. A comparison to charcnt is then tested to see if the value in register A is lower than charcnt. If this is true then the program will branch to subroutine "Here" and wait till the system is reset. If this comparison is false then the program will move forward into the "Trans" subroutine and begin to check to see if the SCI Status Register is ready for more data. The "Trans" subroutine loads the address of "begmsg" into X and then loads the value in charcnt into register B. These 2 values are then added together and stored into register x and stored into $2f (SCI Data Register). "charcnt" is then incremented and the program branches to Print to repeat the process until the value in charcnt is larger than the difference between "endmsg" and "begmsg". Below the "Here" subroutine is the text message that will be printed to the screen. Compile all of the previous programs with the Mstamp11 software and store them in eeprom to execute. Follow the example instructions from the first report to compiling and uploading of source code.