4f3ff0ac921304ce61c3762751d61f36.ppt
- Количество слайдов: 28
Embedded System Spring, 2011 PIC 18 Serial Port Programming in Assembly. Eng. Wazen M. Shbair
Today’s Lecture o o Explain serial communication protocol Describe data transfer rate and bps rate Interface the PIC 18 with an RS 232 connector Describe the main registers used by serial communication of the PIC 18 o Program the PIC 18 serial port in Assembly IUG- Embedded System 2
Introduction o Computers transfer data in two ways: Parallel and Serial. o Parallel: Eight or more data lines, few feet only, short time o Serial: Single data line, long distance o The PIC 18 has serial communication capability built into it. 10 -3
Basics of Serial Communication o The byte of data must be converted to serial bits using a parallel-in-serial-out shift register Serial versus Parallel Data Transfer 10 -4
Basics of Serial Communication (cont’d) o The receiving end must be a serial-inparallel-out shift register and pack them into a byte. o Two methods of serial data communication: Asynchronous and Synchronous Transfers a single byte at a time Transfers a block of data at a time 10 -5
Half-and Full-Duplex Transmission IUG-Embedded System 6
Start and Stop Bits o In the asynchronous method, each character is placed between start and stop bits (framing) MSB LSB Framing ASCII ‘A’ (41 H) 10 -7
Data Transfer Rate of data transfer: bps (bits per second) o Another widely used terminology for bps is baud rate o For Asynchronous serial data communication, the baud rate is generally limited to 100, 000 bps 10 -8
RS 232 Standard o Standard for serial comm (COM port) 1: -3 V to -25 V; 0: +3 V to +25 V n Reason: for long distance wired line o Input-output voltage are not TTL compatible o So, we need MAX 232/233 for voltage converter. Commonly known as line drivers 10 -9
RS 232 Pins Connectors: Minimally, 3 wires: Rx. D, Tx. D, GND Could have 9 -pin or 25 -pin DB-25 DB-9 25 -Pin Connector 9 -Pin Connector 10 -10
RS 232 Pins (cont’d) IBM PC DB-9 Signals Data in Data out Pin Pin Pin DB-9 9 -Pin Connector 10 -11 1 2 3 4 5 6 7 8 9 – – – – – Data Carrier Detect (DCD) Received Data (Rx. D) Transmitted Data (Tx. D) Data Terminal Ready (DTR) Signal Ground (GND) Data Set Ready (/DSR) Request to Send (/RTS) Clear to Send (/CTS) Ring Indicator (RI)
PIC 18 Connection to RS 232 Line driver (a) Inside MAX 232 (b) its Connection to the PIC 18 10 -12
Null Modem Connection o Null modem is a communication method to connect two DTEs (computer, terminal, printer etc. ) directly using a RS 232 serial cable. o With a null modem connection the transmit and receive lines are cross linked. o Depending on the purpose, sometimes also one or more handshake lines are crosslinked. 10 -13
PIC 18 Serial Port Programming in Assembly o We need to show to program PIC 18 to transfer and receive data using asynchronous mode. o The USART (universal synchronous asynchronous receiver) has both : n Synchronous o Used to transfer data between the PIC and external peripherals (ADC, EEPROMs). n Asynchronous (we concern this type) o Used to connect PIC 18 to IBM PC serial port for the purpose of full-duplex. IUG-Embedded System 2011 IUGEmbedded System 14
PIC 18 Serial Port Programming in Assembly o In the PIC there are 6 major register used for serial communication n n n SPBRG ( Serial Port Baud rate Generator ) TXREG (Transfer Register ) RCREG (Receiver Register ) TXSTA (Transmit status and control register) RCSTA (Receive status and control register) PIR 1 (Peripheral interrupt request register 1) IUG-Embedded System 2011 IUGEmbedded System 15
SPBRG Register and Baud Rate in the PIC 18 o The baud rate in is programmable o loaded into the SPBRG decides the baud rate o Depend on crystal frequency n BR = F Fosc 4*16*(X+1)) Baud Rate SPBRG (Hex Value) 38400 19200 9600 4800 2400 1200 3 7 F 20 40 81 *For XTAL = 10 MHz only! 10 -16
Baud rate Formula If Fosc = 10 MHz X = (156250/Desired Baud Rate) - 1 Example: Desired baud rate = 1200, Clock Frequency = 10 MHz X = (156250/1200) – 1 X = 129. 21 = 129 = 81 H 10 -17
TXREG Register o 8 -bit register used for serial communication in the PIC 18 o For a byte of data to be transferred via the Tx pin, it must be placed in the TXREG register first. o The moment a byte is written into TXREG, it is fetched into a non-accessible register TSR MOVFF PORTB, TXREG o The frame contains 10 bits 10 -18
RCREG Register o 8 -bit register used for serial communication in the PIC 18 o When the bits are received serially via the Rx pin, the PIC 18 deframes them by eliminating the START and STOP bit, making a byte out of data received and then placing it in the RCREG register MOVFF RCREG, PORTB 10 -19
TXSTA (Transmit Status and Control Register) 10 -20
RCSTA register o Its 8 - bit register used to enable the serial port to receive data, among other things. o We use 8 -bit data frame. IUG-Embedded System 2011 IUGEmbedded System 21
PIR 1 (Peripheral Interrupt Request Register 1)
Programming the PIC 18 to Transfer Data Serially 1. TXSTA register = 20 H: Indicating asynchronous mode with 8 -bit data frame, low baud rate and transmit enabled 2. Set Tx pin an output (RC 6) 3. Loaded SPBRG for baud rate 4. Enabled the serial port (SPEN = 1 in RCSTA) 5. The character byte to transmit must be written into TXREG 6. Keep Monitor TXIF bit 7. To transmit next character, go to step 5 10 -23
Example 10. 2 ; Write a program for the PIC 18 to transfer the letter 'G' serially ; at 9600 baud continuously. Assume XTAL = 10 MHz OVER S 1 MOVLW MOVWF BCF BSF MOVLW BTFSS BRA MOVWF BRA B'00100000' TXSTA D'15'; 9600 bps SPBRG TRISC, RCSTA, A'G' PIR 1, TXIF S 1 TXREG OVER 10 -24 TX SPEN
TXSTA: Transmit Status and Control Register 10 -25
Programming the PIC 18 to Receive Data Serially 1. RCSTA register = 90 H: To enable the continuous receive in addition to the 8 -bit data size option 2. The TXSTA register = 00 H: To choose the low baud rate option 3. Loaded SPBRG for baud rate 4. Set Rx pin an input 5. Keep Monitor RCIF bit 6. Move RCREG into a safe place 7. To receive next character, go to step 5 10 -26
Example 10. 4 ; Write a program for the PIC 18 to receive data serially and ; put them on PORTB. Set the baud rate at 9600, 8 -bit data ; and 1 stop bit R 1 MOVLW MOVWF BSF CLRF BTFSS BRA MOVFF BRA B'10010000' RCSTA D'15' SPBRG TRISC, RX TRISB PIR 1, RCIF R 1 RCREG, PORTB R 1 10 -27
References o Jie Hu , ECE 692 Embedded Computing Systems , Fall 2010. o PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. Mc. Kinlay and D. Causey, Prentice Fall, 2008. o Eng. Husam Alzaq, Embedded System Course, IUG, 2010 IUG- Embedded System 28
4f3ff0ac921304ce61c3762751d61f36.ppt