Beruflich Dokumente
Kultur Dokumente
Serial Communication
Serial Communication
Introduction
Serial communication buses
Asynchronous and synchronous communication
UART block diagram
UART clock requirements
Programming the UARTs
Operation modes
Baud rate calculationstimer 1
Initializing the UARTusing timer 1
Baud rate calculationstimer 2
Initializing the UARTusing timer 2
UARTx interrupt flagsreceiving data
UARTx Interrupt Flagssending data
Introduction
Parallel communication implies sending a whole byte (or
more) of data over multiple parallel wires
Serial communication implies sending data bit by bit over a
single wire
There are 2 types of serial communication:
Asynchronous
Synchronous
Transmitter +
Data
Receiver
1 byte-wide Data
1 byte-wide Data
The Transmitter
The Receiver
D0
Parity Bit
D1
D2
D3
D4
D5
D6
1 or 2 Stop Bits
D7
1 Asynchronous Byte
Receiver
Transmitter
Data
1 byte-wide Data
The Transmitter
Shifts the data onto the serial line
using its own clock
Provides the clock as a separate signal
No start, stop, or parity bits added to
data
1 byte-wide Data
The Receiver
Extracts the data using
the clock provided by the
transmitter
Converts the serial data
back to the parallel form
UART Block
Each UART is accessed by two SFRsSBUFx and SCONx
The Serial Port Buffer (SBUFx) is essentially two buffers:
writing loads data to be transmitted to the buffer and reading
accesses received data from the buffer.
These are two separate and distinct buffers (registers): the transmit
write-only buffer and the receive read-only register
10
11
Step 2: initialize the appropriate timers for desired baud rate generation
Timer 1 can be used to generate baud rate for UART0 and UART1
Timer 2 can be used to generate baud rate for UART0
Timer 4 can be used to generate baud rate for UART1
12
Operation Modes
The UARTs have four modes of operation, selectable by
configuring the SM0x-SM1x bits in SCONx register
Three modes enable asynchronous communications (modes
1 to 3) while the fourth mode (Mode 0) operates as a simple
shift register (synchronous)
8-bit shift register (mode 0)
Used for port expansion using an external latch
13
SCONx Register
Bit
7-6
SM0x-SM1x
Description
Serial Port Operation Mode
00: Mode 0: Shift Register Mode
01: Mode 1: 8 Bit UART, Variable Baud Rate
10: Mode 2: 9 Bit UART, Fixed Baud Rate
11: Mode 3: 9 Bit UART, Variable Baud Rate
SM2x
RENx
Receive Enable
0: UARTx reception disabled
1: UARTx reception enabled
TB8x
RB8x
14
Symbol
TIx
RIx
Symbol
Description
SMOD0
SSTAT0
Reserved
SMOD1
SSTAT1
Reserved
15
STOP
IDLE
16
SYSCLK 12 T 1M 1
256 TH 1
17
16 256 TH
1
115200
1 22118400
12
16
115200
256 TH 1
TH 1 256 12 244
TH 1 0 xF 4
18
//Timer1overflowsareusedforreceive
//andtransmitclock.RCLK0=0andTCLK0=0
//SetupUART0
PCON|=0x80;
//SMOD0=1(UART0baudratedivideby2disabled)
SCON0=0x50;
//UART0Mode1,Logiclevelofstopbitignored
//andReceiveenabled
//EnableUART0interrupt
IE|=0x10;
IP|=0x10;
//Settohighprioritylevel
RI0=0;
//Clearthereceiveinterruptflag;
//readytoreceivemore
}
19
20
BaudRate
SYSCLK
32 65536 RCAP 2 H , RCAP 2 L
22118400
115200
32 65536 RCAP 2
22118400
65536 RCAP 2
6
32 115200
RCAP 2 65536 6 65530
RCAP 2 0 xFFFA
21
22
//Clearthereceiveinterruptflag;
//readytoreceivemore
voidUART0_ISR(void)interrupt4
{
//PendingflagsRI0(SCON0.0)andTI0(SCON0.1)
if(RI0==1)
//Interruptcausedby
{
//receivedbyte
received_byte=SBUF0; //Readtheinputbuffer
RI0=0;
//Cleartheflag
new_cmd_received=1;
}
if(TI0==1)
{
TI0=0;
}
}
25
//Interruptcausedby
//transmittedbyte
//Cleartheflag
26
//SMOD0=1(UART0baudratedivideby2
//disabled)
//UART0Mode1,Logiclevelofstopbit
//ignoredandReceiveenabled
//EnableUART0interrupt
IE|=0x10;
RI0=0;
TI0=1;
}
27
//Clearthereceiveinterruptflag;
//Readytoreceivemore
//TX0readytotransmit
//Buffertoholdstringfor
//transmission
n=sprintf(sendbuf,"Hello!%c",'\0');
for(i=0;i<n;i++)
{
while(TI0==0);
//Waitwhilethetransmissionis
//goingon
TI0=0;
//ClearTI0
SBUF0=sendbuf[i];
//Loadtheserialbuffer
//withthechartosend
}
28
www.silabs.com/MCU