USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most...

103
1 USART

Transcript of USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most...

Page 1: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

1

USART

Page 2: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

2

Learn how to communicate

• Programmed I/O (Software Polling)

• Interrupt Driven I/O

• Direct Memory Access (DMA)

Page 3: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

3

Programmed I/O (Polling)

• Processor must read and check I/O ready bits for proper value before a transfer. Requires looping, reading status port, and constantly waiting for correct value.

• Processor then inputs or outputs data value to/from I/O device.

• This approach is limited to systems with just a few I/O devices – Processor overhead to loop and check in software

Page 4: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

4

I/O operation • Structure of an I/O operation

– Phase 1: prepare the device for the operation • In case of output, data is transferred to the data buffer

registers

• The operation parameters are set with the control registers

• The operation is triggered

– Phase 2: wait for the operation to be performed • Devices are much slower than the processor

• It may take a while to get/put the data on the device

– Phase 3: complete the operation • Error checking

• Clean up the control registers

Page 5: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

5

Example of input operation

Page 6: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

6

Example of output operation

Page 7: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

7

Programmed I/O Pseudo Code

// loop until output ready=1

do { // read status and mask off ready bit

status = read_port(status_port_address);

output_ready = status & 0x01;

}

while (output_ready == 0);

// output new data value

write_port(data_port_address, data_value);

Page 8: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

8

Polling

• If the device is very fast, then polling may be the best approach

– In fact, an interrupt based mechanism involves at least a pair of process switches

– It the duration of the operation is less than two process switches, then polling is the most optimized solution

• Almost all devices are much slower than the processor – usually, a data transfer takes much more than two process

switches

– For these devices, an interrupt-based mechanism is the best approach

Page 9: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

9

How much slower for I/O device?

Page 10: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

10

Interrupts

• Processor can service something else until interrupt came

• I/O ready signals generate a hardware Interrupt signal. Eliminates Processor I/O ready wait loops.

• An interrupt signal stops the Processor at the next instruction and the Processor calls the Interrupt service routine (ISR)

• Interrupt service routine transfers the data

• Interrupt routines must save all registers on the stack for the interrupt return to work (ISRs like subroutines - but “called” by a hardware signal)

Page 11: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

11

Interrupts

• Interrupt control hardware is needed to support

and enable/disable multiple interrupt signals

• Lower number interrupts typically have a higher

priority (can even interrupt a higher number

interrupt’s ISR) Gives fast I/O devices priority.

• Most modern processors have a vectored

interrupt system. Each interrupt jumps to a

different ISR address (X86 uses a table lookup

of addresses in low memory to jump to ISR)

Page 12: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

12

Interrupt vector

Page 13: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

13

Servicing an Interrupt

1. Currently Running

Process is Interrupted

3. IST

Launched

(Optional)

2. ISR

Code

executes

4. ISR

Returns

…….

Mov…

Add…

……

…..

…….

……

…….

……

………

…..

…….

…..

…..

….

…….

……

Mov..

Out..

…….

Reti

……….

…………

…..

……..

……..

…….

……..

.

Page 14: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

14

Input with interrupts

Page 15: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

15

Interrupt handler

Main CPU function()

while(true){

Map_computation();

}

Interrupt handler()

If left sensor

turn right

If right sensor

turn left

Line follower robot inside the maze

Page 16: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

16

Temporal diagram

Page 17: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

17

Polling vs. Interrupt

Phase 1: prepare the device for the operation

Phase 2: wait for the operation to be performed

Phase 3: complete the operation

Page 18: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

18

Question?

An Embedded System has three periodic devices

Service time: how long it takes to run interrupt handler for each device

Interrupt latency (allowable latency): maxim elapse time between an interrupt request and the start of interrupt handler routine

If program P takes 100 seconds with interrupt disabled,how long will it takes with interrupt enabled, assuming no interrupt overlapped?

Page 19: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

19

Solution

• Device 1 shall take (150+50)/800 = 0.25

• Device 2 shall take (50+50)/1000 = 0.10

• Device 3 shall take (100+100)/800 = 0.25

In one unit real time, it will take 0.6 unit time to service interrupt

For a program with 100 seconds, it will take 250 seconds to finish the entire program with interrupt enabled

Page 20: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

20

Direct Memory Access

• A DMA controller transfers blocks of data

directly to/from memory and the I/O device.

• DMA controller is a state machine with an

address pointer and a counter. Counts down

number of memory locations for transfer and

drives bus address and control lines (is a Bus

Master)

• Processor not involved in transfer once it starts

Page 21: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

21

Direct Memory Access

• DMA controller and Processor must both share the bus. Need bus arbitration hardware to control bus access (DMAs or Processor).

• DMA controller interrupts Processor when it’s block transfer is complete.

• Processor programs DMA controller’s address register and counter to start a new transfer.

• Need hardware for each DMA controller and an interrupt system

Page 22: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

22

DMA Bus Cycle

• Processor does not drive the bus during a

DMA bus cycle

• Bus Arbitration hardware is needed to

support multiple bus masters

Processor

With

Cache

Memory

I/O

Hardware

using DMA

System Bus

Page 23: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

23

DMA Support

• Phase 1: setup all required control

registers for DMA access (OS support)

• Phase 2: do something else

• Phase 3: do something else, OS

acknowledge DMA completion

Page 24: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

24

Comparison

• Polling – Faster

• Interrupt – More efficient

– Complex in the case of service many interrupts

– Limited number of interrupt

– Lower power

• DMA – Less processor overhead

– Less power (processor can go to sleep)

– Require hardware support

– Require interrupt support

Page 25: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

25

Tradeoffs

Transfer Technique Hardware CPU Overhead

Programmed I/O

Interrupt

DMA

Page 26: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

USART

• USART stands for Universal Synchronous

Asynchronous Receiver Transmitter

• Full-duplex NRZ asynchronous serial data

transmission

• Offer wide ranges of baud rate

26

Page 27: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

27

Serial communication

• Can support high speed communication

• Support Synchronous, Asynchronous, and

Iso-synchronous

Page 28: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

28

RS232C

• RS232C communication is between Data

Terminal Equipment (DTE) e.g. computer

and Data Communication Equipment

(DCE) e.g. modem

• RS232C (Recommend Standard for

Number 232C) specify communication

standard such as voltage level, terminating

resistances, cable length etc.

Page 29: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

29

RS232C port connection

Page 30: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

30

RS-232 Serial Interface

• Transmit and Receive data lines

• No clock signal exchanged – Sender and

receiver need to have same baud rate

• Baud rate is the clock rate of the bits

• Normal Bits: Start, 8 Data, Stop

• Voltage levels: a 0 is >3V and a 1 is <-3V

• Special RS232 voltage level converter

chips are typically used in interface

Page 31: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

31

RS-232 standard

• Data rate from 20 kbps to over 1 Mbps

• Range up to 50 feet maximum

• It is robust interface up to 115,200 baud rate (pulse per second)

• Voltage as high/low as 15 Volt

• Single-ended means communication is over a single wire reference to ground

• There are 9 pins (DB-9) and 25 pins format (DB-25)

Page 32: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

32

RS-232 signal

1

0

Page 33: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

33

RS-232 single ended uni-direction

Page 34: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

34

RS-232 (DB9) male connector

Pin 1: Carrier Detect (CD)

Pin 2: Receive Data (RD)

Pin 3: Transmit Data (TD)

Pin 4: Data Terminal Ready (DTR)

Pin 5: Ground (GND)

Pin 6: Data Set Ready (DSR)

Pin 7: Request to Send (RTS)

Pin 8: Clear to Send (CTS)

Pin 9: Ring Indicator (RI)

Page 35: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

35

Connect computer-modem

Page 36: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

36

From DTE-DCE

Page 37: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

37

Connect two PC directly

Page 38: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

38

RS232 Handshaking

Assume modem wants to send data to PC

• RI indicate data available

• When modem connects, modem will send DCD signal at time t0

• Modem will send DSR signal at time t1 when it receive data to send

• PC will response with DTR at time t2

• Modem will send RTS at time t3

• PC response with CTS at time t4

RTS and CTS can also be sent again during the transaction

Page 39: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

39

UART

• UART is the name for the hardware used

for a RS-232 Serial Interface

• UART – Universal Asynchronous Receiver

Transmitter

• Early PCs had a UART chip, but this

functionality is now found inside a larger

chip that also contains other I/O features

Page 40: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

40

UART transmission

Page 41: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

41

UART initial communication

• Need to know how fast the data bits are coming

• Need to know where the starts bit begins

• Then, we know when to sample

Page 42: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

42

UART communication

• Non-return to zero. In the idle state, the logic state is 1.

• Start bit: transition to 0

• Data bit consists of start bit, 8 bit data, P-bit and stop bit

• Data bits can be changed to 5,6,7, and 8 bits

• The stop bit can be for a minimum of 1.5T, 2T instead of T, when T is normal interval

• P bit can be priority or for other purpose

• Stop bit: transition to 1

Page 43: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

43

RS-232C Serial interface

transmission of an 8-bit data value

0 0 0 0 1 0 1 0

Start

Bit

0 1 2 3 4 5 6 7 Stop

BitData Bit number

Mark (1)

Space (0)

0x50 = ASCII “P”

LSB MSB

Page 44: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

44

UART output 8 bits in 10T

Page 45: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

45

UART output 7 bits in 9T

Page 46: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

46

UART output 6 bits in 8T

Page 47: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

47

UART output 8 bit in 11T

Page 48: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

ARM USART block diagram

48

Page 49: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Control register in ARM

• Word length can be set by programming M

bit in USART_CR1 register

• Stop bit can be set by programming

USART_CR2, bit 12-13

– 1 stop bit (default)

– 2 stop bits used in modem

– 0.5 stop bits used in smart card

– 1.5 stop bits used in smart card

• Parity bit is set by USART_CR1, PCE bit 49

Page 50: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Word length setting

50

Page 51: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Stop bit programming

51

Page 52: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Receiver

• Initial Start bit detection: the sequence is

1 1 1 0 X 0 X 0 X 0 0 0 0

52

Page 53: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Start bit detection

53

Page 54: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Data Transmission

1. Enable the USART by writing the UE bit in

USART_CR1 to 1

2. Program the M bit in USART_CR1 to define the word

length

3. Program the number of stop bit in USART_CR2

4. Select the baud rate using USART_BRR

5. Set the TE bit in USART_DR register to send an idle

frame as first transmission

6. Write the data to send in USART_DR register

7. After write the last data, wait for TC bit = 1. This

indicates that the transmission is complete

54

Page 55: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Data receive

1. Enable the USART by writing the UE bit in

USART_CR1 to 1

2. Program the M bit in USART_CR1 to define the word

length

3. Program the number of stop bit in USART_CR2

4. Select the baud rate using USART_BRR

5. Set the RE bit in USART_CR1 register. This enable the

receiver to search for a start bit

When a character is received

• RXNE bit is set

• An interrupt is generated, if RXNEIE bit is set 55

Page 56: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

USART programming

int main(void){

RCC_Configuration(); // System Clocks Configuration

NVIC_Configuration(); // NVIC Configuration

USART1GPIOInit(); // Configure the GPIO for USART1

USART1Init(); // Init USART1

USART_Communication();

}

56

Page 57: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

void USART1GPIOInit(void){

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable GPIOA and USART1 clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |

RCC_APB2Periph_USART1, ENABLE);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Rx (PA.10) as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

57

Page 58: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

void USART1Init(void){

USART_InitTypeDef USART_InitStructure;

/* USART1 is configured as follow:

- BaudRate = 115200 baud

- Word Length = 8 Bits

- One Stop Bit

- No parity

- Hardware flow control disabled (RTS and CTS signals)

- Receive and transmit enabled*/

USART_InitStructure.USART_BaudRate = 115200;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl =

USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure); /* Configure USART1 */

USART_Cmd(USART1, ENABLE); /* Enable the USART1 */

}

58

Page 59: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

void USART_Communication(void){

char ch;

while(1)

{

SendCharUSART1(0x0D);

SendCharUSART1(0x0A);

SendCharUSART1('U');

SendCharUSART1('S');

SendCharUSART1('A');

SendCharUSART1('R');

SendCharUSART1('T');

SendCharUSART1('1');

SendCharUSART1('>');

// Get and echo USART1

ch = GetCharUSART1();

while (ch != 0x0D)

{

SendCharUSART1(ch);

ch = GetCharUSART1();

}

}

} 59

Page 60: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

void SendCharUSART1(char ch){

// Wait until TXE is set

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)

{

}

USART_SendData(USART1, ch);

// Wait until the end of transmit

while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

{

}

}

char GetCharUSART1(void){

char ch;

// Wait until the USART1 Receive Data Register is not empty

while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)

{

}

ch = (USART_ReceiveData(USART1) & 0xFF);

return ch;

} 60

Page 61: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulink UART setup

• To setup UART communication protocol

61

Page 62: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Setup configuration

62

Page 63: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

UART RX

• To receive data from UART

63

Page 64: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Binary blocking mode

• Blocking mode:

The cpu processor will

wait for data until full

Packet received.

64

Page 65: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

ASCII blocking mode

65

Page 66: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

ASCII block mode to string

buffer

66

Page 67: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Non-blocking mode

• The block will come with an addition

output port “ready”

– A value of 1 means the new output is ready

– A value of 0 means data is not available

67

Page 68: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Limitation

68

Page 69: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

UART Tx

• For sending data to UART

69

Page 70: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Transmit Binary Packet

70

Page 71: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Transmit ASCII packet

71

Page 72: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Transmit ASCII packet to

Stream buffer

72

Page 73: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Example

73

Page 74: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Enable Subsystem

74

Page 75: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Enable Subsystem1

75

Page 76: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Enable Subsystem2

76

Page 77: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Testing

77

Page 78: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Testing (setting up the config.)

78

Page 79: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Hardware Setup

79

Page 80: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Output

80

Page 81: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulink hi-speed

communication: target

81

Page 82: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Configurations

82

Page 83: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Configurations

83

Page 84: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulink hi-speed

communication: host

84

Page 85: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulation

85

Page 86: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Software setup

86

Page 87: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Setup

87

Page 88: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Setup

88

Page 89: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Hardware Setup

89

Page 90: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Output

• The speed samping data is at 1KHz

90

Page 91: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Blocking example board1

91

Page 92: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulink: two boards

communication blocking mode

92

Page 93: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Blocking Example board2

93

Page 94: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Blocking mode

94

Page 95: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Hardware

95

Page 96: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

What should be happening?

96

Page 97: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Output

97

Page 98: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Non-blocking example

98

Page 99: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Hardware

99

Page 100: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Output

100

Page 101: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Simulink: two boards non-

blocking mode

101

Page 102: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Question?

Write a program to read from USART1 and

write to USART2 BaudRate = 115200

baud, Word Length = 8 Bits, one Stop Bit,

No parity

102

Page 103: USART - Asian Institute of Technologyesl.ait.ac.th/courses/UGMicroprocessor/class9.pdf · •Most modern processors have a vectored interrupt system. Each interrupt jumps to a ...

Questions?

103