0% found this document useful (0 votes)
87 views19 pages

GS MCU04 UART Communication

Here are the steps to send and receive messages between MATLAB and an MCU board via UART: 1. In Simulink, create a model with blocks to send and receive messages over UART. 2. Configure the MCU device in the model to set the UART baud rate and message format. 3. Build and load the model onto the MCU board. 4. In MATLAB, open the serial port connection to the MCU at the configured baud rate. 5. Use fprintf to send messages in the defined format from MATLAB to the MCU over UART. 6. Use fscanf to receive the response messages from the MCU in MATLAB and check the results

Uploaded by

Xiaofang Jiang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views19 pages

GS MCU04 UART Communication

Here are the steps to send and receive messages between MATLAB and an MCU board via UART: 1. In Simulink, create a model with blocks to send and receive messages over UART. 2. Configure the MCU device in the model to set the UART baud rate and message format. 3. Build and load the model onto the MCU board. 4. In MATLAB, open the serial port connection to the MCU at the configured baud rate. 5. Use fprintf to send messages in the defined format from MATLAB to the MCU over UART. 6. Use fscanf to receive the response messages from the MCU in MATLAB and check the results

Uploaded by

Xiaofang Jiang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Example: UART Comm

Universal Asynchronous
Receiver/Transmitter

1
Methods of Data Transfer
Main 2 Ways of Data Transfer
Parallel
Simultaneous data transfer via often 8 or more lines
(wire conductors)
Fast data transfer, but bulky and complex structure
Serial
Data is transferred consecutively via a single line
Slower data transfer, but simple in structure

Serial Data Communication


At the Tx end,
The data must be formed in Serial Format (one
successive bits) by using a Parallel-in-Serial-out shift
register
At the Rx end,
The received serial data is packed into desired format
by using a Serial-in-Parallel-out shift register

2
 Synchronous & Asynchronous  Data Transfer Rate
Synchronous Asynchronous • Unit in bps (bits per second)
a block of data at a time a single byte at a time
• Called baud rate also
USART UART
(Universal Asynchronous (U Synchronous Asynchro- • As far as the conductor wire is concerned, the baud
Receiver Transmitter) nous RT) rate and bps are the same
 Configuration
 Protocol • Simplest connection
• A set of rules (by sender and receiver) on: ex) between PC and MCU
• How the data is packed • Three pins are required !!!
• How many bits constitute a character
• When the data begins and ends  TxD, RxD, and GND
 Start and Stop Bits
• Asynchronous serial data communication is
widely used for character-oriented
transmissions
• Each character is placed in between start and stop
bits, this is called framing
• Start bit: always one bit with always 0 (Low)
• Stop bit(s): one or two bits with 1 (High)

USB TX TX
PC To MCU
RX RX
UART

3
Case 1: Control by Message
Setting Device Configuration of MCU,
The design can be made with the Waijung
Blockset.

Main Circuit

3. Double Click

2. Click & Drag

4. Setting

1. Click

5. Click

4
Simulink Library

Main Circuit

1. Double Click
1. Click

Main Circuit

2.Setting 2. Click & Drag

3.Click

5
Simulink Library

Main Circuit

1. Click
1.Double click

2. Click, Drag, & Copy 4 times

SUB-Circuit 1
SUB-Circuit 1

6
Simulink Library

SUB-Circuit 1
1. Double Click

Set “1”
Set “2”
Set “3”

Set “4” 1. Click


5. Repeat

2. Click & Drag

SUB-Circuit 1

2. Set “==”

3. Set “1”

4.Click

7
SUB-Circuit 1 SUB-Circuit 1
2. Connection

1.Double click

2.Setting

Main Circuit

3. Connection

3.Click

8
Connect the Board to your computer,
and then click build button

Power

On, but no blink

Main Circuit 1. Click to


Build Model

9
Connect the STM32F407VG to PC
via UART Comm Board

1. Setting
USB TX TX
PC To MCU
UART RX RX

3. Click 2. Click

UART Comm
Board

10
1. Right Click

2. Select Properties

Check

3. Click

11
Type
s=serial(‘com3’)

Type
Port information a.BaudRate=11520

Check Port’s Baud Rate


to UART is 115200.

Open Port

12
Send data to STM32F4 with the following
format (LED ON %d\r\n).
For example, we send ‘1’,
then you can see PD12 LED light bright.

13
Case 2: Message Transmission
MATLAB send a message to the MCU Board and 1. Click Simulink Library

then receives the return message


Message Format
TX Message: ‘a’ + symbol + ‘b’
‘a’ , ‘b’ : float single
symbol : <+> , <-> , <*> , </> 4. Double Click
RX Message: ‘a’ + symbol + ‘b’ + ‘=’ + ‘c’
‘c’ : Returned calculation result
With setting the MCU Device Configuration 5. Setting

3. Click & Drag

2. Click 6. Click

14
2. Click & Drag

1. Click

4. Setting

3. Double Click

5. Click
1. Click 4. Model Design

2. Click & Drag

5. Double Click

6. Setting
3. Double Click

7. Click
2. Click to build model

1. Connection
Type
s=serial(‘com6’)

Type
a=BaudRate-115200

s=serial(‘com6’)
s.BaudRate=115200
s.Timeout=1
fopen(s)
fprintf(s,'%f*%f\r',[10.1 0.5])
fprintf(s,'%f+%f\r',[10.1 0.5]) fscanf(s)
fscanf(s)

fprintf(s,'%f-%f\r',[10.1 0.5])
fscanf(s) fprintf(s,'%f/%f\r',[10.1 0.5])
fscanf(s)

You might also like