Digital Image Processing

Download Report

Transcript Digital Image Processing

CoE3DJ4
Digital Systems Design
Chapter 5: Serial Port Operation
Serial port
• 8051 includes an on-chip serial port
• Hardware access to the port is through TXD and RXD (Port 3
bits 1 and 0)
• Serial port is full duplex (simultaneous transmission and
reception)
– Note: In Mode 0, it is half duplex
• Serial port has a receiving buffering allowing one character to
be received and held in a buffer while a second character is
received
• Two registers SBUF and SCON provide access to the serial
port
• Serial buffer (SBUF) is in fact two buffers one write only for
transmission and one read-only for receiving
The SCON Register
MSB
SM0
Bit
LSB
SM1
SM2
REN
TB8
RB8
TI
RI
Name
Description
SCON.7
SM0
Serial Port Mode bit 0
SCON.6
SM1
Serial Port Mode bit 1
SCON.5
SM2
Multiprocessor Communication Enable
SCON.4
REN
Receive Enable
Set to enable reception. CLR to disable reception.
SCON.3
TB8
Bit 8 of message to transmit
Used to transmit optional parity bit
SCON.2
RB8
Bit 8 of received message
Receives optional parity bit
SCON.1
TI
Transmit Interrupt Flag
Set when Byte in SBUF is completely transmitted.
SCON.0
RI
Receive Interrupt Flag
Set when a valid byte is received into SBUF
The SCON Register
•
SMO, SM1, SM2 Serial Mode Control Bits
SM0 SM1
Mode Baud Rate
Description
0
0
1
1
•
0
1
0
1
0
1
2
3
fosc/12
variable
fosc/32 or fosc/64
variable
8-bit shift register
8-bit UART
9-bit UART
9-bit UART
SM2 Multiprocessor Mode Control Bit
– 1 = Multi-processor mode
– 0 = Normal mode
•
REN Receiver Enable Bit
– 1 = Receive Enable
– 0 = Receive Disabled
•
•
•
TB8 9th Transmit Bit Enabled only in modes 2 and 3
RB8 9th Bit Received Used in modes 2 and 3
RI, TI Serial Interrupts
– RI is set to indicate receipt of a serial word and TI is set to indicate
completion of a serial transmission.
Modes
• Mode of operation of serial port is set by writing to SCON.
• Serial port has four modes of operation selected by writing 1s
and 0s to SM0 and SM1 bits in SCON
• Mode 0 puts the serial port in 8-bit shift register mode
• Serial data enter and exit through RXD.
• TXD serves as clock (outputs shift clock)
• Eight bits are transmitted or received with the LSB first
• Baud rate is fixed at 1/12th of on-chip oscillator
• Transmission is initiated by any instruction that writes data to
SBUF
• Data are shifted out on RXD line with clock pulses sent out
the TXD
Mode 0
• Each transmitted bit is valid on RXD pin for one machine
cycle
• Reception is initiated as soon as REN bit is set to 1 and the
receive interrupt (RI) bit is cleared.
– Usually, REN is set at the beginning of the program to initialize the
serial port, then RI is cleared to start a data input operation.
• As soon as RI is cleared, the shift clock will be produced on
the TxD pin.
– At the beginning of the following machine cycle, data will be clocked
in from the RxD line.
• The clocking occurs on the rising edge of the TxD line.
– After the 8th clocking cycle, the data is copied to SBUF and the RI bit
is set.
• It is up to the attached circuitry to provide data on RXD line
synchronized by the clock signal on TXD.
Mode 1
• Mode 1 is an 8-bit UART with variable baud rate
• UART (universal asynchronous receiver/transmitter): a device
that receives and transmits serial data with each data character
preceded by a start bit (low) and followed by a stop bit (high)
• In mode 1, 10 bit are transmitted on TXD or received on RXD
• These consist of a start bit (always 0), eight data bits (LSB
first) and stop bit (always 1)
• For receive operation, stop bit goes into RB8 in SCON
• Baud rate is set by timer 1.
Framing
• An 8-bit message needs to be “framed” so that the receiver can detect
correctly its beginning and end.
• Standard framing:
– Start bit – always 0, Stop bit – always 1.
– Optional parity bit
– The message now becomes:
• Start bit (10), LSB, …, MSB, <parity bit>, Stop bit (1),
Start
0
1
2
3
4
5
6
7
<P>
Stop
<St>
Time
Mode 1
•
•
•
Transmission is initiated by writing to SBUF
Shifted data are outputted on the TXD line beginning with
start bit and followed by eight bit data bits then stop bit
The transmit interrupt (TI) flag is set as soon as the stop bit
appears on TxD.
Mode 1
•
•
•
Reception is initiated by a 1 to 0 transition on RXD
(assuming REN is 1).
The start bit is skipped and eight data bits are clocked into
the serial port shift register
When all eight bits have been clocked in, the following
occur:
1. Ninth bit (stop bit) is clocked into RB8 in SCON
2. SBUF is loaded with eight data bits
3. Receiver interrupt flag (RI) is set
•
These only occur if following conditions exist:
1. RI=0 (ensures software has read the previous character)
2. SM2=0 (applies in multiprocessor communications)
Modes 2 & 3
• Mode 2: 9-bit UART with fixed baud rate
• Eleven bits are transmitted or received: a start bit, eight data
bits, a programmable ninth bit and a stop bit
• On transmission, the ninth bit is whatever has been put in TB8
in SCON
• On reception, the ninth bit received is placed in RB8
• Baud rate in mode 2 is either1/32nd or 1/64th on-chip oscillator
• Mode 3: 9-bit UART with variable baud rate
• Mode 3 is same as Mode 2 except baud rate is programmable
and provided by Timer 1
Initialization and Accessing
• Receiver enable bit REN in SCON must be set by software to
enable reception of characters
• This is done at the beginning of a program when port is
initialized
• SETB REN
• MOV SCON, #xxx1xxxxB
• Ninth data bit transmitted in modes 2 and 3 must be loaded
into TB8 by software
• Ninth bit received is placed in RB8
Initialization and Accessing
• A common use for ninth data bit is to add parity to a character
• P bit in PSW is set or cleared every machine cycle to establish
even parity with bits in accumulator
• Example: transmitting eight bits in accumulator with an even
parity added in ninth bit:
MOV C,P
MOV TB8,C
MOV SBUF,A
– Odd parity
MOV C,P
CPL C
MOV TB8,C
MOV SBUF, A
Initialization and Accessing
• RI and TI in SCON play an important role in 8051 serial
communications
• Both bits are set by hardware but must be cleared by software
• Typically RI is set at the end of character reception and
indicates “receive buffer full”
• If software wishes to input a character from the device
connected to serial port, it must wait until RI is set, then clear
RI and read character from SBUF
WAIT:
JNB RI, WAIT
CLR RI
MOV A, SBUF
Initialization and Accessing
• TI is set at the end of character transmission and indicates
“transmit buffer empty”.
• If software wishes to send a character to the device connected
to the serial port, it must first check that the serial port is
ready.
WAIT:
JNB TI, WAIT
CLR TI
MOV SBUF, A
Baud rate
• Baud rate is fixed in modes 0 and 2
• In mode 0 it is always on-chip oscillator frequency divided by
12
• Default baud rate for mode 2 (after system reset) is oscillator
frequency divided by 64
• Bit 7 of PCON is the SMOD bit
• Setting SMOD doubles the baud rate in mode 2 to 1/32nd of
oscillator frequency
• Since PCON is not bit-addressable, setting SMOD has to be
done using the following approach:
MOV A, PCON
SETB ACC.7
MOV PCON,A
Baud rate
• The baud rates in Modes 1 and 3 are determined by
Timer1overflow rate
• Since timer operates at a relatively high frequency, it is further
divided by 32 (or 16 if SMOD=1) before driving the serial
port
• The usual technique for baud rate generation is to initialize
TMOD for 8-bit auto-reload mode (mode 3) and put the
correct reload value in TH1 to yield proper overflow rate for
baud rate
Serial port
• Example: 1200 baud operation:
BAUD RATE=TIMER 1 OVERFLOW RATE / 32
TIMER 1 OVERFLOW RATE =1200*32=38.4kHz
– If a 12 MHz oscillator is used, timer 1 is clocked at 1 MHZ (1000
kHz).
– Since the timer must overflow at a rate of 38.4 kHz and the timer is
clocked at 1000 kHz, an overflow is required every 1000/38.4=26.04
clocks.
– Since timer counts up and overflows on FF to 00 transition, 26 counts
less than 0 is the required reload value for TH1:
MOV TH1, #-26
• Table 5-3 gives reload values for different baud rates
Steps to Transmit a Byte
1.
2.
3.
4.
5.
6.
7.
8.
Program T1 for Mode2 (TMOD  0x20)
Load TH1 and TL1 with the initial value (baud rate dependant) (TH1 
FD / FA / F4 / E8)
Program SCON for Mode1 (SCON  0x50)
Start Timer1 (SETB TR1)
Clear TI
Load SBUF with the byte to be transferred (SBUF  byte)
Wait until TI becomes 1 (a wait loop using: JNB TI)
Go back to Step5 for next byte
Examples: Transmit a character
• Transfer ASCII “A” serially at 9600 baud continuously
START:
AGAIN:
HERE:
MOV TMOD, #20H
MOV TH1, #-7
MOV SCON, #50H
SETB TR1
CLR TI
MOV SBUF, #’A’
JNB TI, HERE
SJMP AGAIN
;Put T1 in mode2
;9600 baud
;8b, 1stop, 1start, REN enabled
;start timer T1
;ready to transmit
;letter A is to be transmitted
;poll TI until all the bits are transmitted
;loop (forever loop)
Steps to Receive a Byte
1.
2.
3.
4.
5.
6.
7.
8.
Program T1 for Mode2 (TMOD  0x20)
Load TH1 and TL1 with the initial value (baud rate dependant)
(TH1  FD / FA / F4 / E8)
Program SCON for Mode1 (SCON  0x50)
Start Timer1 (SETB TR1)
Clear RI
Wait until RI becomes 1 (a wait loop using JNB RI)
Store SBUF (A  SBUF)
Go back to Step5 for next byte
Example: Receive Data
• Receive bytes serially and display them on P1, continuously.
START:
AGAIN:
HERE:
MOV TMOD, #20H
MOV TH1, #-7
MOV SCON, #50H
SETB TR1
CLR RI
JNB RI, HERE
MOV A, SBUF
MOV P1, A
SJMP AGAIN
;T1 in mode 2
;9600 baud
;8b, 1start, 1stop
;start T1
;ready to receive a byte
;wait until one byte is Rx-ed
;read the received byte from SBUF
;display on P1
;loop forever