E l e c t r o n i c s E n g g S o c i e t y Page 1

shrewdnessfreedomΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

70 εμφανίσεις




E l e c t r o n i c s E n g g S o c i e t y


Page
1

















E l e c t r o n i c s E n g g S o c i e t y


Page
2

Contents




Introduction



PC


Serial Port



RS
-
232 Protocol



Theory of Operation



Setting UART in
micro
-
controller



Hyper Terminal







E l e c t r o n i c s E n g g S o c i e t y


Page
3

















Introduction:

There are many ways to interface y
our microcontroller to computer. The
easiest way is to use

UART i.e. Unive
rsal Asynchronous Receiver
Transmitter
.

It is a way of communication between the microcontroller and
the computer system or another microcontroller.


Data exchange can be done using serial or parallel techniques. In parallel
communication, the complete
byte of data is sent at one time with each bit
having a separated dedicated data line. This method is very fast but not
cost effective. On the other hand serial communication is somewhat slower
as every bit of data is sent serially one by one but is cost e
ffective as only
two data lines i.e. transmitter and receiver lines are requ
ired

(of course a
ground line is also required in addition to these two)
. S
erial data
communication

can be further divided into two categories
-

synchronous
and asynchronous.

In sy
nchronous communication transmitter and
receiver are synchronised by a clock whereas in asynchronous system no
clock pulse is shared between receiver and transmitter. As the name
suggests UART is an example of asynchronous communication. Since no
common cl
ock is shared, a known data transfer rate (baud rate) must be
agreed upon prior to data transmission i.e. the receiving UART needs to
know the transmitting UART’s baud rate (and conversely the transmitter
needs to know the receiver’s baud rate).




E l e c t r o n i c s E n g g S o c i e t y


Page
4

Serial
Communication in PC


The
Serial Port:


Figure 1.1: Serial Port of a computer


We will be using Serial Port

for communication between the
microcontroller
and the computer. A serial port has 9 pins as shown. If you
have a laptop, then most probably there w
on’t be a serial port. Then you
can use a USB to serial
Converter
.

If you have to transmit one byte of data, the serial port will transmit 8 bits
as one bit at a time. The advantage is that a serial port needs only one wire
to transmit the 8 bits
.








E l e c t r o n i c s E n g g S o c i e t y


Page
5

RS
-
232 Protocol:

The standard used for serial communication is RS
-
232 (Recommended
Standard 232). The standard defines

voltage levels that correspond to
logical zero and logical one level.

Voltages between plus and minus 3 to 15
volts are allowed voltages.

L
ogic one is defined as a negat
ive voltage;

the
signal condition is called marking, and has the functional significance of
OFF. Logic zero is positive; the signal condition is spacing, and has the
function ON
.

Voltage between plus or minus 3 volts near z
e
ro are not
allowed. This is not the voltage
level at w
hich our microcontroller works.
We have t
wo different machines with
two

different ways to define 0 & 1
and we want to exchange information between them. Consider
microcontroller as a French and Compute
r's Serial Port as an Indian person
(obviousl
y no common language in between
!)
.
If they want to exchange
information they basically need a mediator
who knows both the language.
In other words,
we need a device which c
an convert serial port’s voltage
level
to that of CMOS(

i.e. logic 1 = +5V and logic 0 = 0V
)
. This task is carried
out by an IC
MAX 232
, which is always used with four 10uF ca
pacitors. The
circuit is shown:




E l e c t r o n i c s E n g g S o c i e t y


Page
6


Figure 1
.2
: MAX 232 circuit


Theory of Operation:

Figure 1.
3

illustrates a basic UART
data packet. While no data is being
transmitted,

logic 1 must be placed in the Tx line. A data packet is
composed of 1 start bit, which is always a logic 0, followed by a
programmable number of data bits (typically between 6 to 8), an optional
parity bit,
and a programmable number of stop bits (typically 1). The stop
bit must always be logic 1.

Most UART uses 8bits for data, no parity and 1 stop bit. Thus, it takes 10
bits to transmit a byte of data.





E l e c t r o n i c s E n g g S o c i e t y


Page
7


Figure 1.3
: Basic UART Frame Format



BAUD Rate:
This parameter specifies the desired baud rate (bits per
second) of the UART. Most typical standard baud rates are: 300, 1200,
2400, 9600, 19200, etc. However, any baud rate can be used. This
parameter affects both the receiver and the transmitter. The def
ault is 2400
(bauds).

In the UART protocol, the transmitter and the receiver do not share a clock
signal. That is, a clock signal does not emanate from one UART transmitter
to the other UART receiver. Due to this reason the protocol is said to be
asynchro
nous
.

Since no common clock is shared, a known data transfer rate (baud rate)
must be agreed upon prior to data transmission. That is, the receiving
UART needs to know the transmitting UART’s baud rate (and conversely
the transmitter needs to know the rec
eiver’s baud rate, if any). In almost all
cases the receiving and transmitting baud rates are the same. The
transmitter shifts out the data starting with the LSB first.

Once the baud rate has been established (prior to initial communication),
both the tra
nsmitter and the receiver’s internal clock is set to the same
frequency (though not the same phase). The receiver "synchronizes" its



E l e c t r o n i c s E n g g S o c i e t y


Page
8

internal clock to that of the transmitter’s at the beginning of every data
packet received. This allows the receiver to sam
ple the data bit at the bit
-
cell centre.

A key concept in UART design is that UART’s internal clock runs at much
faster rate than the baud rate. For example, the popular 16450 UART
controller runs its internal clock at 16 times the baud rate. This allows
the
UART receiver to sample the incoming data with granularity of 1/16 the
baud
-
rate period. This "oversampling" is critical since the receiver adds
about 2 clock
-
ticks in the input data synchronizer uncertainty. The
incoming data is not sampled directly b
y the receiver, but goes through a
synchronizer which translates the clock domain from the transmitter’s to
that of the receiver. Additionally, the greater the granularity, the receiver
has greater immunity with the baud rate error.

The receiver detects th
e start bit by detecting the transition from logic 1 to
logic 0 (note that while the data line is idle, the logic level is high). In the
case of 16450 UART, once the start
-
bit is detected, the ne
xt data bit’s
"centre
" can be assured to be 24 ticks minus 2
(worse case synchronizer
uncertainty) later. From then on, every next data bit centre is 16 clock ticks
later. Figure 2 illustrates this point.

Once the start bit is detected, the subsequent data bits are assembled in a
de
-
serializer. Error condition mayb
e generated if the parity/stop bits are
incorrect or missing
.





E l e c t r o n i c s E n g g S o c i e t y


Page
9



Figure 1.3: Data sampling points by the UART Receiver


Using UART in microcontroller:


The USART has to be initialized before any communication can take pl
ace.
The initialization process
normally consists of setting the baud rate, setting
frame format and enabling the

Transmitter or the Receiver depending on
the usage.

Setting these in ATMEGA16 requires knowledge about USART
registers (
UCSR, UBRR and UDR
) and

their programming. Here our
CV
-
AVR
code wizard comes to our help. It generates codes for initialising USART in
ATMEGA16 automatically! So you need not mess with the AVR Registers.
All you have to do is to click some check
-
boxes and the code wizard will
generate all the initialisation

codes.







E l e c t r o n i c s E n g g S o c i e t y


Page
10

Using CV
-
AVR Code Wizard:

Create a new project in CV
-
AVR as usual using

code wizard and click on
USART tab.

Now depending upon your requirement, you can either check
receiver, transmitter or both.



You get a list of options to select from for
the baud rate.
Baud Rate
is the
unit of data transfer, defined as bits per second. We will select 9600 as it is
fair enough for our purpose, and default setting in most of the applications
(like Matlab, Docklight, etc.). Keep the communication parameters a
s
default, i.e., 8 data, 1 stop and no parity and mode asynchronous. You also
have option of enabling Rx and Tx Interrupt functions
. Enabling them will



E l e c t r o n i c s E n g g S o c i e t y


Page
11

generate an interrupt on reception and transmission completion. But as
we don’t need an interrupt at thi
s point we will not enable interrupts here.


Once you generate and save the code, all the register values are set by
CVAVR and you only need to know some of the C functio
ns to transfer data.
Some of the
m are:

1.

putchar:
Sends one character to the buffer that can be received by
receiving device.
e.g.


p
utchar(‘a’)
;

//sends character a to the buffer

p
utchar(‘F’)
;

//sends character F to the buffer


2.
g
etchar:
To receive one character from the buffer, which might have
been s
ent by the other uC or the computer. E.g., if we have already defined a
variable char c, then

c = getchar();


// receives the character from buffer and save it in variable c


Similarly, there are functions like
puts()
and
gets()

that work on string in

similar fashion.


Hyper
-
Terminal:

To communicate with the computer, you need a terminal where you can
send data through keyboard and the received data can be displayed on the
screen. There are many softwares which provide such terminal
, but we will
be
using Hyper
-
terminal
. Its evaluation version is free for download on
internet, which is sufficient for our purpose.

You can download the evaluation version of hyper
-
terminal from
http://www.hi
lgraeve.com/htpe/support/htpe7.exe





E l e c t r o n i c s E n g g S o c i e t y


Page
12

To use hyper
-
terminal, just click on its icon from programs folder in
your start menu. In the new connection dialog box that appears, enter a
name for your connection and click OK.


Figure 1.4

In the next dialog box ent
er the com port on which your usb to serial
connector is working and click OK. To find out the COM port on which your
serial connector is working, check the Device Manager in System
Properties.

Note: The COM port changes every time you plug
-
in the connecto
r.




E l e c t r o n i c s E n g g S o c i e t y


Page
13


Figure 1.5



In the dialog box that appears enter the values for baud rate, character size
of uart data frame, parity bits, number of stop bits and flow control. These
values should be same as that you have configured in your microcontroller.

However, most of the systems use 9600 baud
-
rate, 8 data bits and no
parity. If you are also using this configuration, click RESTORE DEFAULT
button.




E l e c t r o n i c s E n g g S o c i e t y


Page
14


Figure 1.6

Click on OK and you are
ready to send and receive data over serial port.
Type anything on the keyboard and it will be transmitted over uart.
Anything received over UART will appear on the screen.




E l e c t r o n i c s E n g g S o c i e t y


Page
15

Figure 1.7















If you have any suggestion that you would like to share with u
s feel free to
contact at:

akash.singh.ece10@itbhu.ac.in

Akash Singh

Electronics Engg.

IT BHU