EET 203 MICROCONTROLLE R SYSTEMS DESIGN 1 EET
- Slides: 46
EET 203 MICROCONTROLLE R SYSTEMS DESIGN 1 EET 203 Serial Port Interfacing
Objectives 2 Explain serial communication protocol Describe data transfer rate and bps rate Interface the PIC 16 with an RS 232 connector Describe the main registers used by serial communication of the PIC 16 Program the PIC 16 serial port in C language
Introduction 3 Computers transfer data in two ways: Parallel and Serial. Parallel data transfer: Eight or more data lines (data is sent a byte or more at a time) Short distance A lot of data can be transferred in a short time Serial data transfer: Single data line (data is sent one bit at a time) Long distance Serial versus Parallel Data Transfer
Basics of Serial Communication 4 The PIC 16 has serial communication capability built into it. Serial communication is used for transferring data between two systems located at distances of hundreds of feet to millions of miles apart. Thereby making possible fast data transfer using only a few wires. Much cheaper Enables two computers located in two different cities to communicate The byte of data must be converted to serial bits using a parallel-in-serial-out shift register. The receiving end must be a serial-in-parallel-out shift register and pack them into a byte.
5 Basics of Serial Communication (cont’d) Short distance: digital signal can be transferred on a simple wire and requires no modulation Long distance (e. g. telephone): serial data communication requires a modem to modulate (convert from 0 s and 1 s to audio tones) and demodulate (convert from audio tones to 0 s and 1 s). Two methods of serial data communication: Synchronous: Transfers a block of data at a time Asynchronous: Transfers a single byte at a time The PIC 16 chip has a built-in USART (universal synchronous-asynchronous receiver-transmitter). Special IC chip for serial data communications
6 Half-and Full-Duplex Transmission Simplex: data transmitted OR received only Duplex: data can be both transmitted and received. Half Duplex: data is transmitted one way at a time. Full Duplex: data can go both ways at the same time.
Start and Stop Bits 7 In the asynchronous method, each data/character is placed between start and stop bits framing Start bit: Always ONE (1) bit ‘ 0’ (LOW) Transmission begins Stop bit: Mark: No transfer Signal is ‘ 1’ ONE (1) or TWO (2) bits ‘ 1’ (HIGH) MSB LSB Space: Signal is ‘ 0’ Framing ASCII ‘A’ (41 H)
Data Transfer Rate 8 Rate of data transfer: bps (bits per second) Another widely used terminology for bps is baud rate For asynchronous serial data communication, the baud rate is generally limited to 100, 000 bps.
RS 232 Standard 9 Most widely used as Serial I/O Interfacing Standard (COM port) 1: -3 V to -25 V; 0: +3 V to +25 V Reason: for long distance wired line Input-output voltage levels are not TTL compatible. To connect any RS 232 to a u. C system, we must use voltage converters such as MAX 232/233. To convert TTL logic levels to RS 232 voltage level, and vice versa. Commonly referred as line drivers. TTL voltage levels are acceptable to PIC
RS 232 Pins 10 Connectors: Minimum 3 pins: Rx. D, Tx. D, GND Could have 25 -pin OR 9 -pin (serial I/O standard) DB-25 DB-9 25 -Pin Connector 9 -Pin Connector #2: Transmitted data (Tx. D) (Rx. D) #3: Received data (Rx. D) #7: Signal ground (GND) #2: Received data #3: Transmitted data (Tx. D) #5: Signal ground (GND) (Details on Text Book)
RS 232 Pins (cont’d) 11 Data in Data out DB-9 9 -Pin Connector IBM PC DB-9 Signals Pin 1 – Data Carrier Detect (DCD) Pin 2 – Received Data (Rx. D) Pin 3 – Transmitted Data (Tx. D) Pin 4 – Data Terminal Ready (DTR) Pin 5 – Signal Ground (GND) Pin 6 – Data Set Ready (/DSR) Pin 7 – Request to Send (/RTS) Pin 8 – Clear to Send (/CTS) Pin 9 – Ring Indicator (RI)
PIC 16 Connection to RS 232 12 PIC 16 has TWO (2) pins for transfer and receive data TX (PORTC 6): transmit data RX (PORTC 7): receive data These pins are TTL compatible A line driver (voltage converter) is required to make them RS 232 compatible
13 PIC 16 Connection to RS 232 (cont’d) MAX 232 Convert RS 232’s signal to TTL voltage levels, vice versa Use +5 V power source (same source voltage with PIC) TWO (2) sets of line drivers for transferring and receiving data Line driver for TX: T 1 and T 2 Line driver for RX: R 1 and R 2 In many applications, only one of each is used. Example: T 1 and R 1 are used together for TX and RX of PIC, second set is unused.
14 PIC 16 Connection to RS 232 (cont’d) In many applications, only one of each is used. Example: T 1 and R 1 are used together for TX and RX of PIC, second set is unused. Capacitors: most widely used value is 22 u. F Connected to TX pin of u. C Connected to RX pin of RS 232 DB connector Connected to TX pin of RS 232 DB connector
15 PIC 16 Connection to RS 232 (cont’d) RS 232 Line driver (a) Inside MAX 232 (b) its Connection to the PIC 16
PIC 16 Serial Port Programming 16 In PIC u. C, six major registers are associated with the USART (universal synchronous asynchronous receiver): SPBRG: Serial Port Baud Rate Generator TXREG: Transfer Register RCREG: Receive Register TXSTA: Transmit Status and Control Register RCSTA: Receive Status and Control Register PIR 1: Peripheral Interrupt Request Register 1
SPBRG Register and Baud Rate in the PIC 16 17 PIC transfers and receives data serially at many different baud rates. The baud rate in the PIC 16 is programmable The value loaded into the 8 bit register SPBRG decides the baud rate Depend on crystal frequency Baud Rate SPBRG (Hex Value) 38400 19200 3 7 9600 4800 2400 1200 F 20 40 81 *For XTAL = 10 MHz only!
18 SPBRG Register and Baud Rate in the PIC 16 (cont’d) Desired Baud Rate = Fosc/64 X: value loaded in SPBRG reg. (X+1) XTAL oscillator Example: 10 MHz ÷ 4 ÷ 16 by UART Instruction cycle freq 2. 5 MHz To UART to set the baud rate 156, 250 Hz Desired Baud Rate = Fosc/64 (X+1) Desired Baud Rate = 10 MHz/64 (X+1) X = (156, 250/Desired Baud Rate) – 1 Note: Dividing the instruction cycle frequency by 16 is the setting upon Reset. Can get a higher baud rate by making bit BRGH = 1 in TXSTA register
19 SPBRG Register and Baud Rate in the PIC 16 (cont’d) With Fosc = 10 MHz, find the SPBRG value needed to have the following baud rates: (a) 9600 (b) 4800 (c) 2400 (d) 1200 Solution: Desired Baud Rate = Fosc/64 (X+1) Desired Baud Rate = 10 MHz/64 (X+1) X = (156, 250/Desired Baud Rate) – 1 (a) X = (156, 250/9600) – 1 = 15. 28 = 15 = F (hex) (b) X = (156, 250/4800) – 1 = 31. 55 = 32 = 20 (hex) (c) X = (156, 250/2400) – 1 = 64. 10 = 64 = 40 (hex) (d) X = (156, 250/1200) – 1 = 129. 21 = 129 = 81 (hex)
Quadrupling the baud rate in PIC 16 20 There are two ways to increase the baud rate of data transfer in PIC 16: 1. 2. Use a higher frequency crystal not feasible! Change a bit in TXSTA register Option 2 can be used while the crystal frequency stays the same. Change bit of TXSTA<2>: BRGH bit Upon Reset, BRGH = 0 Low Speed When BRGH = 1 High Speed Baud rate is quadrupled with this method!
21 Quadrupling the baud rate in PIC 16 (cont’d) Baud rates for BRGH = 0 Default when PIC is powered up. PIC divides Fosc/4 by 16 once more. Use that frequency for UART to set the baud rate. Example: XTAL = 10 MHz Instruction cycle freq = 10 MHz/4 = 2. 5 MHz And This is the frequency used by UART to set the 2. 5 MHz/16 = 156, 250 Hz baud rate for BRGH = 0 The baud rate for BRGH = 0 was discussed in previous slides.
22 Quadrupling the baud rate in PIC 16 (cont’d) Baud rates for BRGH = 1 With fixed crystal frequency, the baud rate can be quadrupled PIC divides Fosc/4 by 4 once more. Use that frequency for UART to set the baud rate. Example: XTAL = 10 MHz Instruction cycle freq = 10 MHz/4 = 2. 5 MHz And This is the frequency used by UART to set the 2. 5 MHz/4 = 625, 000 Hz baud rate for BRGH = 1
23 Quadrupling the baud rate in PIC 16 (cont’d) X = (625, 000/Desired Baud Rate) – 1 X: value loaded in SPBRG reg. SPBRG Values for Various Baud Rates (Fosc = 10 MHz and BRGH = 1) Baud Rate SPBRG (Hex Value) 57600 38400 19200 9600 4800 0 A 0 F 20 40 81
24 Quadrupling the baud rate in PIC 16 (cont’d) For XTAL = 10 MHz, find the SPBRG value (in both decimal and hex) to set the baud rate to each of the following: (a) 9600 if BRGH = 1 (b) 4800 if BRGH = 1 Solution: X = (625, 500/Desired Baud Rate) XTAL = 10 MHz, Fosc/4 = 2. 5 MHz, – 1 BRGH = 1 UART frequency = 2. 5 MHz/4 = 625, 000 Hz (a) X = (625, 500/9600) – 1 = 64 D = 40 H (b) X = (625, 500/4800) – 1 = 129 D = 81 H BRGH = 1 ÷ 4 XTAL oscillator Example: 10 MHz ÷ 4 Instruction cycle freq 2. 5 MHz BRGH = 0 ÷ 16 625, 500 Hz To UAR T to set the baud rate 156, 250 Hz
PIC 16 Serial Port Programming 25 Only asynchronous mode and full-duplex serial data communication will be covered. In PIC u. C, six major registers are associated with the USART (universal synchronous asynchronous receiver): SPBRG: Serial Port Baud Rate Generator TXREG: Transfer Register RCREG: Receive Register TXSTA: Transmit Status and Control Register RCSTA: Receive Status and Control Register PIR 1: Peripheral Interrupt Request Register 1
TXREG Register 26 8 -bit register used for serial communication in the PIC 16 For a byte of data to be transferred via the TX pin, it must be placed in the TXREG register The moment a byte is written into TXREG, the 8 -bit data is framed with the START and STOP bits. The 10 -bit data is transferred serially via TX pin.
RCREG Register 27 8 -bit register used for serial communication in the PIC 16 When the bits are received serially via the RX pin, the PIC 16 de-frames them by eliminating the START and STOP bit. Making a byte out of data received and then placing in the RCREG register
TXSTA Register 28 Transmit Status and Control Register 8 -bit register Used to select the synchronous/asynchronous modes and data framing size. BRGH bit: select a higher speed for transmission. Default is lower baud rate transmission D 6 of TXSTA: determines the framing of data by specifying the number of bits per character
TXSTA Register 29
RCSTA Register 30 Receive Status and Control Register 8 -bit register Used to enable the serial port to receive data.
RCSTA Register 31
PIR 1 Register 32 Peripheral Interrupt Request Register 1 8 -bit register TWO (2) of PIR 1 register bits are used by UART: TXIF (transmit interrupt flag) RCIF (receive interrupt flag) We monitor (poll) the TXIF flag bit to make sure that all the bits of the last byte are transmitted before we write another byte into TXREG. By the same logic, we monitor the RCIF flag bit to see if a byte of data has come in yet.
PIR 1 Register 33
PIR 1 Register (cont’d) 34
35 Programming the PIC 16 to Transfer Data Serially 1. Load TXSTA register = 20 H Indicating asynchronous mode with 8 -bit data frame, low baud rate and transmit enabled 2. Make TX pin (PORTC 6) an output 3. Load SPBRG to set the baud rate 4. Enabled the serial port: RCSTA<7> (SPEN = 1) 5. Character byte to be transmit is written into TXREG 6. Keep monitor TXIF flag bit of PIR 1 register to make sure UART is ready for next byte 7. To transmit next character, go to Step 5
Example 1 Write a C program for the PIC 16 to transfer the letter 'G' serially at 9600 baud continuously. Assume XTAL = 10 MHz 36 #include <htc. h> PIC configuration _CONFIG … ; #define _XTAL … void main (void) { TRISC = 0 x 00; // TX pin (PORTC 6) as output TXSTA = 0 x 20; // 0 b 00100000; choose low baud rate, 8 -bit, asynchronous mode SPBRG = 15; // or 0 x 0 F; 9600 baud rate, XTAL = 10 MHz TXSTAbits. TXEN = 1; // enable transmit RCSTAbits. SPEN = 1; // enable serial port while(1) { } } TXREG = ‘G’; // place value in buffer while(PIR 1 bits. TXIF==0); // wait until transmitted
Example 2 Write a C program to transfer the message “YES” serially at 9600 baud, 8 -bit data, and 1 STOP bit. Do this continuously. Assume XTAL = 10 MHz 37 // PIC configuration void Ser. Tx(unsigned char); // function for sending character void main(void) { TRISC = 0 x 00; // TX pin (PORTC 6) as output TXSTA = 0 x 20; // 0 b 00100000; choose low baud rate, 8 -bit, asynchronous mode SPBRG = 15; // or 0 x 0 F; 9600 baud rate, XTAL = 10 MHz TXSTAbits. TXEN = 1; // enable transmit RCSTAbits. SPEN = 1; // enable serial port while(1) { Ser. Tx(‘Y’); Ser. Tx(‘E’); Ser. Tx(‘S’); } } void Ser. Tx(unsigned char c) { } while(PIR 1 bits. TXIF==0); // wait until transmitted TXREG = c; // place character in buffer
Example 2 (cont’d) 38 // PIC configuration void main(void) { TRISC = 0 x 00; // TX pin (PORTC 6) as output TXSTA = 0 x 20; // 0 b 00100000; choose low baud rate, 8 -bit, asynchronous mode SPBRG = 15; // or 0 x 0 F; 9600 baud rate, XTAL = 10 MHz TXSTAbits. TXEN = 1; // enable transmit RCSTAbits. SPEN = 1; // enable serial port unsigned char z; unsigned char Mess[] = “YES”; while(1) { for (z=0; z<3; z++) { } } } // write message while(PIR 1 bits. TXIF==0); // wait for transmit TXREG = Mess[z]; // place char in buffer
Example 3 39 Write a C program to send the two messages “Normal Speed” and “High Speed” to the serial port. Assuming that SW is connected to pin PORTB 0, monitor its status and set the baud rate as follows: SW = 0 : 9600 baud rate SW = 1 : 38400 baud rate Assume that XTAL = 10 MHz for both cases. Solution: BRGH = 0 UART freq = 156, 250 Hz Hz X = (156, 250/9600) - 1 = 15 D BRGH = 1 UART freq = 625, 000 X = (625, 000/38400) - 1 = 15 D
Example 3 (cont’d) 40 // PIC configuration #define SW RB 0; // PORTB 0 as input switch SW void main (void) { TRISB = 0 x 01; // PORTB 0 as input switch TRISC = 0 x 00; // TX pin (PORTC 6) as output TXSTA = 0 x 20; // 0 b 00100000; choose low baud rate, 8 -bit, asynchronous mode SPBRG = 15; // 9600 baud rate, XTAL = 10 MHz TXSTAbits. TXEN = 1; // enable transmit RCSTAbits. SPEN = 1; // enable serial port unsigned char z; unsigned char Mess 1[] = “Normal Speed”; unsigned char Mess 2[] = “High Speed”;
Example 3 (cont’d) 41 if (SW==0) { for (z=0; z<12; z++) { while(PIR 1 bits. TXIF==0); // wait for transmit TXREG = Mess 1[z]; // place value in buffer } } else { TXSTA = TXSTA | 0 x 04; // for high speed (BRGH=1) for (z=0; z<10; z++) { while(PIR 1 bits. TXIF==0); // wait for transmit TXREG = Mess 2[z]; // place value in buffer } } while(1); } // stay in loop
42 Programming the PIC 16 to Receive Data Serially 1. Load RCSTA register = 90 H: To enable the continuous receive in addition to the 8 -bit data size option 2. Load TXSTA register = 00 H: To choose the low baud rate option 3. Load SPBRG to set the baud rate 4. Make RX pin (PORTC 7) an input 5. Keep monitor RCIF flag bit of PIR 1 register for a HIGH to see if an entire character has been received yet 6. When RCIF = 1, RCREG has a byte, its contents are moved into a safe place 7. To receive next character, go to step 5
Example 4 Write a C program to receive bytes of data serially and put them on PORTB. Set the baud rate at 9600, 8 -bit data, and 1 STOP bit. 43 // PIC configuration void main(void) { TRISC = 0 x 00; // TX pin (PORTC 6) as output TXSTA = 0 x 20; // 0 b 00100000; choose low baud rate, 8 -bit, asynchronous mode SPBRG = 15; // or 0 x 0 F; 9600 baud rate, XTAL = 10 MHz TXSTAbits. TXEN = 1; // enable transmit RCSTAbits. SPEN = 1; // enable serial port unsigned char z; unsigned char Mess[] = “YES”; while(1) { for (z=0; z<3; z++) // write message { } } } while(PIR 1 bits. TXIF==0); // wait for transmit TXREG = Mess[z]; // place char in buffer
Summary 44 Serial Communication- data is sent one bit a time, is used in situations where data is sent over significant distances because in parallel communications, where data is sent a byte or more a time, great distances can cause distortion of the data. Additional advantage- allowing transmission over phone lines Asynchronous communication - data is sent one byte at a time and Synchronous communication - data is sent in blocks of bytes Simplex - can sent but cannot receive, Half duplex - can send and receive, but not at the same time and Full duplex - can send and receive at the same time RS 232 - standard foe serial communication connectors
45 End of Chapter “Life is not so short but that there is always time enough for courtesy” – Quote by Ralph Waldo Emerson
46 IMPORTANT Next Lecture Solve all the attached questions related to PIC application & design I will, provide you the solution later
- Embeded systems design
- Eet 203
- Eet 203
- Eet 203
- Eet 203
- Rad 203
- Eecs 203
- Fha 203h loans
- Rad etx-203ax commands
- Etx-203ax
- Afi 10-203
- Cmsc 203 umbc
- 258 x 203
- Rad etx-203ax-dsl
- Eecs 203
- Lol 203
- Master203
- 1948 peugeot 203
- 250 hangi yüzlüğe yuvarlanır
- 203 fourgonnette
- Lol 203
- Vena saphena parva
- Dorm room 203
- 4 8 13 19 26,… 3 suku berikut ? *
- Suku keenam dari barisan 2/3 2/9
- Etx203am
- Cpsc 203
- State postulate
- [email protected]
- 203-019-4
- 952 203-0000
- Resource management guide 203
- Finc 203
- Mathematical induction
- Master 203
- Cst 203
- Cmsc 203 umbc
- Lg bp 203
- Eet speech
- Email format eet
- Eet205
- Waar leeft een koala
- Eet test
- Eet 2023
- Analog electronics
- Eet machine
- Eet machine