Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| learn:courses:unit-4-2:start [2017/03/20 19:55] – created Martha | learn:courses:unit-4-2:start [2022/10/04 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Unit 4 Part 2: Communications - Asynchronous Serial Protocols ====== | ====== Unit 4 Part 2: Communications - Asynchronous Serial Protocols ====== | ||
| - | === Unit 4 Part 2 Labs === | + | [[{}/ |
| - | * [[https:// | + | |
| - | * [[https:// | + | |
| + | * [[/ | ||
| + | * [[/ | ||
| + | * [[/ | ||
| + | * [[/ | ||
| === Download This Document === | === Download This Document === | ||
| - | * {{ : | + | {{ : |
| + | |||
| + | ===== 1. Introduction ===== | ||
| + | Asynchronous communication is the transmission of data between two devices that are not synchronized with one another via a clocking mechanism or other technique. Data is exchanged in blocks of a fixed number of bits that are bracketed by a START and STOP bit. The term asynchronous implies the sender can initiate data transmission at any time, and the receiver must be ready to accept information when it arrives. The two devices must be operating at or nearly equal to the same frequency. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 2. Objectives: ===== | ||
| + | - Program the PIC32MX370 to use the Universal Asynchronous Receiver Transmitter ([[https:// | ||
| + | - Control the stepper motor speed and direction of rotation using commands entered from a [[https:// | ||
| + | - Display serial communications on the LCD and PC terminal. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 3. Basic Knowledge: ===== | ||
| + | - How to map PIC32MX370 I/O pins to special function registers. | ||
| + | - How to initialize special functions using the PLIB functions. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| + | ===== 4. Unit Equipment List ===== | ||
| + | ==== 4.1. Hardware ==== | ||
| + | - [[https:// | ||
| + | - 2 [[https:// | ||
| + | - Workstation computer running Windows 10 or higher, MAC OS, or Linux | ||
| + | - [[https:// | ||
| + | - [[https:// | ||
| + | |||
| + | In addition, we suggest the following instruments: | ||
| + | - [[https:// | ||
| + | |||
| + | ==== 4.2. Software ==== | ||
| + | The following programs must be installed on your development work station: | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[https:// | ||
| + | - [[http:// | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 5. Project Takeaways ===== | ||
| + | - Understanding of the basics of telecommunications. | ||
| + | - Understanding of requirements and implementations of asynchronous communications. | ||
| + | - Application of asynchronous communications | ||
| + | * Knowledge of a PC terminal emulation program. | ||
| + | * How to develop a library of PIC32 software to provide bi-directional communications of single characters and strings of characters. | ||
| + | * How to use the UART for diagnostics and as a [[https:// | ||
| + | - How to recognize the handshaking methods used in a communications protocol. | ||
| + | - How to setup the Analog Discovery 2 to display UART waveforms. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 6. Fundamental Concepts ===== | ||
| + | ==== 6.1. Asynchronous Communications ==== | ||
| + | Serial communications is the process of sending data one bit at a time, sequentially, | ||
| + | |||
| + | Although some references claim that asynchronous communications take place outside of real-time, this does not mean that timing is not critical to the transmission of data. Each data symbol is uniform in period and has critical timing requirements. In [[http:// | ||
| + | |||
| + | A user unfamiliar with asynchronous communications and UART operations should refer to Reference 3 in section 8 of this document. Asynchronous communications is a serial data protocol that has been in use for many years. Normally, eight bits of data are transmitted at a time. There are other less commonly used modes that can send 5, 6, or 7 bits of data. Each byte of data is framed by a start bit and a stop bit. A symbol is defined as a start, data, parity, or stop bit. It is common to define communications speed as bits per second. The bit rate is defined as the inverse of the period of a unit symbol. Although the common standard bit rates are 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, and 115200, communications is possible at any rate provided that the sender and receiver use the same rate. For most asynchronous communications, | ||
| + | |||
| + | Figure 6.1 shows a typical asynchronous transmission of two 8-bit data bytes. We see that the idle state of the transmit signal is a logic one, or a high voltage level. A START bit is signified by a high to low transition and remains low for one symbol time. The START bit is followed by eight data bits with the least significant bit being sent first. A logic zero is sent when the signal is a low voltage level for one symbol time. Similarly, a logic one is sent when the signal is a high voltage level for one symbol time. After the transmitting of the data there may be an optional parity bit. The [[http:// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 6.1. Example of 115 kB asynchronous serial data stream with odd parity.// | ||
| + | |||
| + | The receiving device unit must use the same bit rate as the sending unit. In the process of receiving serial communications, | ||
| + | |||
| + | Each new start bit synchronizes the sampling of the receiving unit. Generally, the receiver recovers the transmission using a clock that is an even multiple of the bit rate. When the receiving unit detects the falling edge of the START bit, it waits one half the symbol period and samples the receive data line. This is illustrated by the processor sample points in Fig. 6.1. If the logic state of the line is zero, then it is recognized as a valid start condition. The receiver then waits full periods to sample the receive data line until all data bits, plus any parity bit, plus the stop bit have been received. If a stop bit is not received, a framing error is generated. | ||
| + | |||
| + | Just like with parallel communications, | ||
| + | |||
| + | The actual data rate is defined as the number of data bits per unit time. Data efficiency is defined as the number of data bits divided by the total number of bits. The efficiency of asynchronous serial communications is at best 80% because there are always two extra data bits (start and stop bit) sent for each 8 data bits. If parity is used, the efficiency drops to 73% because 11 bits are needed to communicate 8 bits of data. | ||
| + | |||
| + | |||
| + | ==== 6.2. UART and FTDI-232 ==== | ||
| + | The PIC32 processors, as do most microprocessors, | ||
| + | |||
| + | |||
| + | ==== 6.3. UART Hardware ==== | ||
| + | The Basys MX3 processor board uses an FTDI USB to serial adaptor that interfaces directly with the PIC32MX370 UART pins, as shown in Fig. A.1, with the physical location of the UART USB connector shown in Fig. A.2. The UART_RX and UART_TX connect to the PIC32MX370 processor I/O pins RF12R and RF13R respectively. The result of this wiring will be discussed below in the UART Software section. When the USB cable is connected to both the PC and J10 of the Basys MX3 processor board, the PC software will automatically enumerate the connection to a COMM port. To determine which COMM port is enumerated, you will need to click on the Windows Start icon in the lower left corner followed by clicking on “Device Manager” and then on “Ports (COM & LPT).” You will then see a listing as “USB Serial Port (COM##)” where “##” is the COMM number used when you open the terminal emulation application. | ||
| + | |||
| + | ==== 6.4. UART Software ==== | ||
| + | As stated above, the UART_TX and UART_RX connect to the PIC32MX370 processor I/O pins RF12R and RF13R respectively. Table 6.1 shows that port F, pin 13 is mapped to U4RX using the C program statement “U4RXR = 0x09;”. Similarly, Table 6.2 shows that port F, pin 12 is mapped to U4TX using the C program statement “RPF12R = 0x02;”. After the PPS pin mapping shown in Listing B.1, all serial communications will use UART 4. The complete function to initialize UART 4 is shown in Listing B.1. | ||
| + | |||
| + | **Table 6.1. PPS mapping of PIC32 port F pin 13 to UART 4 receive adapted from [[http:// | ||
| + | |||
| + | ^ Peripheral Pin ^ [pin name]R SFR ^ [pin name]R bits ^ [pin names]R Value to RPn Pin Selection | ||
| + | | INT1 | INT1R | INTR< | ||
| + | | T3CK | T3CKR | T3CKR< | ||
| + | | IC1 | IC1R | IC1R< | ||
| + | | $\overline{(U3CTS)}$ | ||
| + | | U4RX | U4RXR | U4RXR< | ||
| + | | U5RX | U5RXR | U5RXR< | ||
| + | | $\overline{(SS2)}$ | ||
| + | | OCFA | OCFAR | OCFAR< | ||
| + | |||
| + | |||
| + | **Table 6.2. PPS mapping of PIC32 port F pin 12 to UART 4 transmit adapted from [[http:// | ||
| + | |||
| + | ^ RPn Port Pin ^ RPnR SFR ^ RPnR bits ^ RPnR Value to Peripheral Seleciton | ||
| + | | RPD9 | RPD9R | RPD9R< | ||
| + | | RPG6 | RPG6R | RPG6R< | ||
| + | | RPB8 | RPB8R | RPB8R< | ||
| + | | RPB15 | RPB15R | ||
| + | | RPD4 | RPD4R | RPD4R< | ||
| + | | RPB0 | RPB0R | RPB0R< | ||
| + | | RPE3 | RPE3R | RPE3R< | ||
| + | | RPB7 | RPB7R | RPB7R< | ||
| + | | RPB2 | RPB2R | RPB2R< | ||
| + | | RPF12< | ||
| + | | RPD12< | ||
| + | | RPF8< | ||
| + | | RPC3< | ||
| + | | RPE9< | ||
| + | |||
| + | - This selection is not available on 64-pin USB devices. | ||
| + | - This selection is only available on 100-pin General Purpose devices. | ||
| + | - This selection is not available on 64-pin General Purpose devices. | ||
| + | - This selection is not available when USBID functionality is used. | ||
| + | |||
| + | |||
| + | The C language “printf” function will output text to UART 4 if the “mon_putc(char c)” function, as shown in Listing B.2, is added to the project. | ||
| + | |||
| + | Listing B.3 and B.4 show the code for sending and receiving a single character. These functions are non-blocking, | ||
| + | |||
| + | The primitive functions provided by the code in Listing B.1 through Listing B.4 provide the building blocks of asynchronous communications and will be used to complete the application assignment for Labs 4a and 4b. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 7. Background Information ===== | ||
| + | See the general discussion concerning computer communications discussed in Unit 4 Part 1. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 8. References ===== | ||
| + | - [[http:// | ||
| + | - “Using the USART in Asynchronous Mode”, [[http:// | ||
| + | - “Asynchronous Communications with the PICmicro® USART”, [[http:// | ||
| + | - [[http:// | ||
| + | - __Embedded Computing and Mechatronics with the PIC32 Microcontroller__, | ||
| + | - “PIC32MX330/ | ||
| + | - “Using the USART in Asynchronous Mode”, [[http:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix A: Unit 4 Part 2 Parts Configuration ===== | ||
| + | {{ : | ||
| + | //Figure A.1. PIC32 to FT232RQR IC schematic diagram.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure A.2. Unit 4 hardware and instrumentation configuration.// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix B: Lab 4a and 4b UART Functions ===== | ||
| + | ==== Listing B.1. C Function that Initializes PF12 and PF13 to UART 4 ==== | ||
| + | < | ||
| + | void uart_init(unsigned int baud, int parity) | ||
| + | { | ||
| + | // The next two statements map the PPS IO pins to the UART 4 Tx and Rx | ||
| + | | ||
| + | U4RXR = 0x09; // Mapping U4RX to RPF13 | ||
| + | |||
| + | | ||
| + | | ||
| + | // Note the need to specify the UART number twice in the following statement | ||
| + | | ||
| + | | ||
| + | { | ||
| + | case NO_PARITY: | ||
| + | | ||
| + | UART_STOP_BITS_1); | ||
| + | | ||
| + | case ODD_PARITY: | ||
| + | | ||
| + | UART_STOP_BITS_1); | ||
| + | | ||
| + | case EVEN_PARITY: | ||
| + | | ||
| + | UART_STOP_BITS_1); | ||
| + | | ||
| + | } | ||
| + | | ||
| + | </ | ||
| + | |||
| + | ==== Listing B.2. C code to allow the “printf” output to be redirected to UART 4 ==== | ||
| + | < | ||
| + | void _mon_putc(char c) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } /* End of _mon_putc */ | ||
| + | </ | ||
| + | |||
| + | ==== Listing B.3. C Code for Sending a Single Character to UART 4 ==== | ||
| + | < | ||
| + | BOOL putcU4( int ch) | ||
| + | { | ||
| + | UART_DATA c; | ||
| + | BOOL done = FALSE; | ||
| + | | ||
| + | | ||
| + | { | ||
| + | | ||
| + | done = TRUE; | ||
| + | } | ||
| + | return done; | ||
| + | } /* End of putU4 */ | ||
| + | </ | ||
| + | |||
| + | ==== Listing B.4. C Code for Receiving a Single Character from UART 4 ==== | ||
| + | < | ||
| + | BOOL getcU4( char *ch) | ||
| + | { | ||
| + | UART_DATA c; | ||
| + | BOOL done = FALSE; | ||
| + | | ||
| + | { | ||
| + | c = UARTGetData(UART4); | ||
| + | *ch = (c.data8bit); | ||
| + | done = TRUE; /* Return new data available flag */ | ||
| + | } | ||
| + | | ||
| + | }/* End of getU4 */ | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix C: Common Definitions ===== | ||
| + | - UART: A Universal Asynchronous Receiver/ | ||
| + | - Communications Speed: BAUD is a unit used to measure the speed of signaling or data transfer, equal to the number of pulses or bits per second, or //baud rate//. | ||
| + | - Data Rate: Typical data rates are multiples of 120 BAUD to 115.2 KB. | ||
| + | - Stop Bits: Only one is necessary for the receiving device. The sending device has the option of sending one or two minimum stop bits. | ||
| + | - Bus Idle: This is the nominal state of the data signal when no data is being transmitted. The idle condition is a logic level 1 (or high condition). The bus idle condition is an extension of stop bits; however, the period bus idle condition may be any time duration. | ||
| + | - Parity: Used for single bit error detection. | ||
| + | - None – no error checking | ||
| + | - Even parity refers to a parity checking mode in asynchronous communication systems in which an extra bit, called a parity bit, is set to one if there is an odd number of one bits in a one-byte data item. | ||
| + | - Odd parity refers to a parity checking mode in asynchronous communication systems in which an extra bit, called a parity bit, is set to one if there is an even number of one bits in a one-byte data item. | ||
| + | - Information direction: | ||
| + | - Simplex – communications is one direction only | ||
| + | - Full-duplex – bi-directional simultaneous asynchronous communications | ||
| + | - Half-duplex – bi-directional exclusive communications. Devices can send and receive, but not simultaneously. | ||
| + | |||
| + | ---- | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | |||
| + | {{tag> | ||

