Você está na página 1de 28

AVR Microcontroller

Prepared by:
Eng. Ashraf Darwish

Ashraf.emad.darwish@gmail.com

Session 6

USART

How to make C function

How to make your own library and


Header file

Introduction

USART stands for :


Universal Synchronous and Asynchronous
serial Receiver and Transmitter

We use USART to make communication


between microcontroller and Computer .
We can use it also to interface with some
devices .

Introduction
To be able to deal with USART you must know:

TTL & RS 232


Baud rate
Data framing
USART Registers
Hyper Terminal

Introduction
Data transmission techniques can be :

serial
paralle
l

Long
distance and
low noise
Less wires
High speed
More wires
and high
noise

Methods of
communication
1-synchronous:
In

this method a block of data is transferred at a time.

The

clock of transmitter and receiver must be


common to make the synchronization.

2-Asynchronous:
Single
It

byte is transferred at a time.

is the widely used method.

No

common clock.

Ways of communication:

TTL vs RS232
Logic 1
Logic zero
Usage

TTL

RS 232

5V
0V
Digital IC
( microcontroller)

-3V to -25V
+3V to +25V
Computers

TTL vs RS 232

All computers working on RS 232 protocol


but microcontrollers working on TTL protocol.
So, we need to convert from TTL to RS 232
and vice versa

We will use an intermediate IC called MAX


232 to interface between computer and
microcontroller

TTL vs RS 232

TTL vs RS 232

When you connect between microcontroller


and computer all grounds should by
connected together .

Male Socket:

Baud rate

Baud rate is the data transfer rate


( Bits per second )

The available values for Baud rates are


300 ,600 ,1200 ,1800 ,2000 ,2400 ,
4800 ,9600 and 19200 bits per second.

We usually use 9600 bps

Baud rate

Each character is placed between start and stop


bits.
Start bit is always one bit. It is always low (zero).
Stop bit can be one or two bits. They are always high (logic

one).

Baud rate

Due to the slowness of receiving device , two stop


bits may be used to give the device sufficient time to
organize itself before transmission of the next byte

Parity Bit

In some systems, the parity bit of character


byte is included in the data frame.

Odd

1s
Even 1 s

parity bit is 1.
parity bit is 0.

Registers
1-USART Control and Status Register A

(UCSRA).
2-USART I/O Data Register (UDR).
3-USART Control and Status Register B
(UCSRB).

Registers
1-USART Control and Status Register A (UCSRA).

Bit
Bit
Bit
Bit
Bit
Bit

7
6
5
4
3
2

RXC: USART Receive Complete


TXC: USART Transmit Complete
UDRE: USART Data Register Empty
FE: Frame Error
DOR: Data OverRun
PE: Parity Error

Registers
2-USART I/O Data Register (UDR).

The

transmitted or received data are saved


in this register

Registers
3-USART Control and Status Register B (UCSRB).

Bit 7 RXCIE: RX Complete Interrupt Enable


Bit 6 TXCIE: TX Complete Interrupt Enable
Bit 5 UDRIE: USART Data Register Empty Interrupt
Enable
Bit 4 RXEN: Receiver Enable
Bit 3 TXEN: Transmitter Enable

Very important note

To Transmit/Receive data properly using


USART we have to make our AVR chip working
on 8 MHz oscillator

Hyper Terminal

Hyper Terminal

Hyper Terminal

Hyper Terminal

How can we test our serial port and the


cable?

1-Connect the serial cable to the PC.


2-Make a new connection using hyper terminal.
3-connect pin 2 & 3 of the cable using short wire.
4-type anything in the hyper terminal window, if
characters appear in the hyper terminal window
your serial port and cable is running correctly.

Coding

When we use USART we mast #include <stdio.h> to easy our


work and use printf & scanf functions .

1- to print text on the screen :


Printf(Hello World)
2- Printf(%c,charname); to transmit data ( character )
Printf(%u,integer name); to transmit data (unsigned integer)
Or
Scanf(%c,&temp);
to receive data ( character )
Scanf(%c,&str[0]);
to receive data ( string)
3- you can use putchar( x) & x=getchar( ) to transmit & receive
USART

Coding
#include <mega16.h>
#include <stdio.h>
void main(void)
{
char temp;
DDRA=0x00;
PORTA=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop,
No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

while (1)
{
if(PINA.1==1){printf("Enter
password:");
scanf("%c",&temp);
if (temp==65){printf("
Welcome USER");
temp=0;}
else {printf(" wrong password
");}}
};
}
`

How to make C function


1- write your function prototype before the main
function folowed by semicolon ;
Ex: Display( int x);
2- define your function after the end of the main
function
Ex: Display ( int x );
{
if (x==0) PORTA=0x49;
If (x==1) PORTA=0x79;
}
3- to call this function anywhere just type
Display(0) or Display (1)

Thank you
Contact:
http://www.facebook.com/groups/263197427113025
/
Ashraf.darwish@ymail.com
Ashraf.emad.darwish@gmail.com
+201064897791

Você também pode gostar