Using the Serial Port on the M
作者:dolphin
时间:2012-07-30
Using the Serial Port on the MAXQ610 Microcontroller
Abstract:The MAXQ610 microcontroller provides two standard USART serial ports. The asynchronous mode 1, one of the modes supported by the MAXQ610's serial ports, can be used to communicate with PC COM ports and many other types of legacy equipment. This application note explains how the interrupt handling support provided by the MAXQ610 for serial-port transmit and receive operations allows the application to respond quickly to the serial port when a new character is received or when a character has completed transmission.
Overview
The MAXQ610 microcontroller includes peripherals to allow communication with external devices or systems. These peripherals include a master/slave Serial Peripheral Interface (SPI); a carrier generation/modulation system for infrared (IR) communication; and two separate universal synchronous/asynchronous receiver/transmitters (USARTs), commonly known as serial ports.This application note demonstrates the use of the serial port on the MAXQ610 in 10-bit asynchronous mode. This mode is commonly used for communications with, and/or sending debug output to, a standard COM port on a PC. This mode allows two devices or systems to communicate by simply agreeing on a common bit format and baud rate, regardless of the actual operating frequency found on either side of the communications channel.Demonstration code for this application note is written for the MAXQ610 and the MAXQ610 EV (evaluation) kit, using the assembly-based MAX-IDE environment. The code and project files for the demo application covered in this application note are available for download.The following hardware and software are required to run the demonstration code.- MAXQ610 EV kit board
- MAXQJTAG board (serial-to-JTAG interface)
- JTAG programming cable (2 x 5 female-to-female 0.100" header, 10-connector ribbon cable)
- DB9 straight-through male-to-female serial cable
- 5VDC regulated, center-post-positive power supply
- PC with available COM (serial) port or USB-to-serial adapter
- MAX-IDE environment
- Terminal emulator (such as MTK operating in the Dumb Terminal mode, HyperTerminal, or TeraTerm)
- MAX-IDE installation
- MAXQ Core Assembly Guide
- Development Tools Guide
Setting Up the MAXQ610 EV Kit
The demonstration code covered in this application note operates on the MAXQ610 EV kit. However, to make the code run properly and to enable the features in the code, the jumpers on the EV kit board must be configured correctly. The same jumper configuration is used on the kit board for both steps of running the demo (i.e., loading code and executing code).Configure the jumpers on the MAXQ610 EV kit.- JH1: connect pins 2 and 3.
- JH2: connect pins 2 and 3.
- JH3: connect pins 2 and 3.
- Connect the following jumpers (pins 1 and 2): JH14, JH20, JH22, JH23, JH24, JH25, and JH26.
- Connect jumpers JH1, JH2, and JH3 on the serial-to-JTAG board.
- Connect the 5VDC power supply to plug J2 on the serial-to-JTAG board.
- Connect the DB9 cable from the COM1 port on the PC to the J1 connector on the serial-to-JTAG board.
- Connect the JTAG programming cable from P2 on the serial-to-JTAG board to P5 on the MAXQ610 EV kit board. The red wire on the JTAG cable should go to pin 1 (TCK) on both connectors.
- Turn on the power to the 5VDC supply.
- Asynchronous transmission (on TXD) and reception (on RXD) of serial data
- Baud clock provided by a dedicated baud clock generator (programmable using the PR register)
- Eight data bits, 1 start bit, 1 stop bit
- No parity
Baud clock frequency (BAUD) = system clock frequency × PR/217 | (Eq. 1) |
When SMOD=0, baud rate = BAUD/64 | (Eq. 3) |
Baud rate = (system clock frequency × PR/217)/16 | (Eq. 5) |
Baud rate = (system clock frequency × PR)/221 | (Eq. 6) |
PR = baud rate × 221/(system clock frequency) | (Eq. 7) |
;===================================================================;=;= InitSerial0;=;= Set up serial port 0 to run in 10-bit asynchronous mode at;= 9600 baud.;=InitSerial0:move SCON0.6, #1 ; Set to mode 1 (10-bit asynchronous).move SCON0.4, #1 ; Enable receiver. move SMD0.1, #1 ; Baud rate = 1/16 of baud clockmove PR0, #0068Dh ; P = 2^21 × 9600/12.000MHzmove SCON0.0, #0 ; Clear received character flag.move SCON0.1, #0 ; Clear transmit character flag.move SMD0.2, #1 ; Enable receive/transmit interrupt.retFor more information on the operating modes of the USART peripheral, refer to the MAXQ610 User's Guide and to Section 10 (Serial I/O Module) of the MAXQ Family User's Guide.
Transmitting and Receiving Characters
Once the serial port has been properly configured, it is ready to transmit and receive characters. In mode 1, a character consists of 1 start bit, 8 data bits, and 1 stop bit. The start and stop bits are used for synchronization purposes and are handled by the USART hardware. The remaining 8 bits carry the actual data, which means that a single 8-bit byte can be transmitted or received by the serial port at once.Transmitting a character over the serial port involves the following three steps.- Write the byte value to transmit to the SBUF register.
- Wait for the TI (transmit interrupt) bit in the SCON register to go high (set to 1). This indicates that the hardware has finished transmitting the character over the serial port.
- Clear the TI bit to 0.
- Wait for the RI (receive interrupt) bit to go high, which indicates that a new character has been received by the serial port.
- Read from the SBUF register to obtain the data byte.
- Clear the RI bit to 0.
Handling Serial-Port Interrupts
A simple method of operating the serial port would be to simply poll (check repeatedly) the values of the RI and TI bits, as needed, for each transmit or receive operation. For example, when writing a character, the application code would write the data byte to SBUF and then poll the TI bit until it was set to 1 by hardware. No other operations would occur until the transmission had completed. Similarly, to receive a new byte the application would simply poll the RI bit until it was set high by hardware, then unload the received byte by reading SBUF.This method of polling the RI and TI bits sacrifices performance for simplicity, because considerable time is spent waiting for characters to be transmitted or received. Additionally, the application must know in advance when to expect a new character from the external device, and the application cannot transmit and receive characters at the same time.A more flexible (but slightly more complicated) method takes advantage of the fact that TI and RI are not merely status bits, but are also synchronous interrupt sources. Instead of continuously polling the status of the TI and RI bits to determine when a transmit or receive operation has completed, the MAXQ610 can enable an interrupt to occur when TI or RI changes from 0 to 1. This method allows the application to spend its time more productively, as it only responds to the serial port when needed.The first step in this process is to enable all interrupts by setting the IGE bit (IC.0) to 1. This enables interrupt handling at a global level for the entire MAXQ610.Next, an interrupt handling routine must be installed. The MAXQ610 uses a multiple fixed-interrupt vector system, with different (nonprogrammable) vector addresses assigned to each interrupt source or group of interrupt sources. For our purposes, we are interested in the Serial Port Interrupt vector, which is assigned to word address 0040h.The interrupt handler routine below performs the following functions.- Prevents other interrupts (of a higher priority) from occurring by temporarily setting IC.0 to 0.
- Pushes the PSF and Acc registers to the stack to save their values. Since an interrupt can be triggered at any time, interrupt handlers must always save and restore any registers that are used by other application code.
- Since the Serial Port Interrupt can be triggered by either the TI (transmit) or RI (receive) flags, the handler code must check to see why the interrupt was triggered.
- If a transmit interrupt was triggered, clear the TI bit and set a flag value (stored in A[15]) to indicate to the application that the transmission had completed. This is simply for demonstration purposes; the interrupt handler could also react to this condition by loading a new value into SBUF to transmit the next character.
- If a receive interrupt was triggered, clear the RI bit; read the received character from SBUF; and take the appropriate action, depending on the character that was received.
- Restore the Acc and PSF register values by popping them from the stack.
- Re-enable interrupts by setting IC.0 to 1.
- Exit the interrupt handler using the RETI instruction.
org 0040hserialInt:move IC.0, #0 ; Block any other interrupts from triggering.push PSFpush Accmove C, SCON0.0 ; Check for receive character interrupt.jump C, serialInt_RxserialInt_Tx:move SCON0.1, #0 ; Clear transmit complete interrupt flag.move A[15], #1 ; Set flag to indicate transmit complete.serialInt_done:move IC.0, #1 ; Re-enable interrupts.pop Accpop PSFretiserialInt_Rx:move SCON0.0, #0 ; Clear receive character interrupt flag.move Acc, SBUF0 ; Get character from serial port.cmp #'0'jump E, serialInt_Rx0cmp #'1'jump E, serialInt_Rx1cmp #'2'jump E, serialInt_Rx2cmp #'3'jump E, serialInt_Rx3cmp #'4'jump E, serialInt_Rx4jump serialInt_doneserialInt_Rx0:move Acc, PO3or #0Fh ; Turn all LEDs off.move PO3, Accjump serialInt_done....serialInt_Rx3:move Acc, PO3xor #04h ; Toggle P3.2 state. move PO3, Accjump serialInt_doneserialInt_Rx4:move Acc, PO3xor #08h ; Toggle P3.3 state.move PO3, Accjump serialInt_done
Running the Demo
Once the demo code has been compiled using MAX-IDE and loaded into the MAXQ610 EV kit, it can be executed as follows.- Turn power off and disconnect the JTAG, power and serial cables.
- Connect the serial cable from COM1 on the PC to plug J1 on the MAXQ610 EV kit board.
- Connect the power cable to plug J3 on the MAXQ610 EV kit board.
- Turn power on.
- Open your terminal emulator program on the PC. Configure it to communicate over COM1 at 9600 baud with 8 data bits, 1 stop bit, and no parity.
- Press and release RESET (SW1) on the MAXQ610 EV kit board. The banner text (MAXQ610 Serial Port Demo...) should display on your terminal emulator screen. If it does not, check your connections and jumper settings.
- Type characters to toggle the LED states as follows: type "1" to toggle DS1; type "2" to toggle DS2; type "3" to toggle DS3; type "4" to toggle DS4; or type "0" to turn all LEDs off. Characters are not echoed.
Conclusion
The MAXQ610 provides two instances of the standard USART serial ports found on many MAXQ microcontrollers. One of the modes supported by the MAXQ610's serial ports, the asynchronous mode 1, can be used to communicate with PC COM ports and many other types of legacy equipment. The interrupt handling support provided by the MAXQ610 for serial-port transmit and receive operations allows the application to respond quickly to the serial port when a new character is received or a character has completed transmission.The USART serial ports on the MAXQ610 support a wide range of configuration options and communications modes which are beyond the scope of this application note. For more information, refer to the MAXQ610 User's Guide and section 10 of the MAXQ Family User's Guide noted above.关键词: 电子电路图,Microcontr

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW
或用微信扫描左侧二维码