Blockset described on this wiki is deprecated since 2012.

For Model Based Design (MBD), use the free MPLAB Device Blocks for Simulink, tool from Microchip.
Updated Rapid Control Prototyping (RCP) custom projects are published at: https://lubin.kerhuel.eu.

DsPIC Block/Interface Tx-Matlab

From http://www.kerhuel.eu/wiki - Simulink device driver Blockset for dsPIC / PIC24 / PIC32 Microcontrollers --[[User:LubinKerhuel|LubinKerhuel]] 12:40, 3 September 2009 (UTC)
Jump to navigation Jump to search
Block UART Interface Tx-Matlab

Open Graphical User Interface to receive data from the microcontrollers throw the PC serial port. Data can be plotted in real time.

Double click on this block open the GUI to log data from the dsPIC into matlab.


Related blocks : Block/UART_Configuration Block/Rx_Input Block/Tx_Output Block/Tx_Output_Multiplexed_For_Matlab-Labview Block/Interface_Tx-Matlab

Interface PIC-Matlab

User Interface for logging data from UART with matlab

Set the connexion of the PC serial port. You can select the COM port used, the baudrate ( select the same used by the dsPIC) and the hardware control flow (usually, set to none if you are using a simple UART interface like 2 wires). External module like bluetooth, you may need hardware control Flow.

The connexion and Reset button connect and release the COM port. Once the connexion is established, modification on port parameters have no influence. MAtlab's Java may crash (Matlab just close in such a case) when connectiing while the dsPIC is sending data. The workaround is to disconnect the PIC while pushing the connection button.

The data received are decoded using the PIC - Matalb protocole created and stored into the R matrix. Each column of R correspond to one channel. lines are added to R until the max buffer size (number of line) is reached (here 50000). Then, oldest data are deleted at the profit of newest one.

Time T_R is the estimated time of the data reception.

R, T_R, Rn and T_Rn are accessible from the workspace. R and Rn matrix contains NaN values. The function padr allow to remove NaN values.

The Rn matrix is a subset of the R matrix. Rn contain the last 10000 values of R. Name and size of both matrix can midified. Rn is a smaller matrix that may be used for real time plotting on slow system.

The central text box contain the command that are interpreted iteratively with time delay configurable (0.2 seconds here). Any matalb command can be used. It is also possible to call a matlab script file. The four buttons on top: Default, Dflt signe, Simulink, Emission are four example of script. The default script plot unsigned data. Dflt signe plots signed int16 data.

Long log: When large log are required (for example, 10 min with constant flow data from the UART), increasing the max size of the buffer (matrix R) may not work at the constant matrix size increase (until it reach the max size defined) may slow down the computer and causing data lost. The method is to keep R and Rn small and to log directly the upcomming raw data with the buttons Log.txt / Ren / Load and Delete button.

For all theses button, it may be necessary to click several time (alternatively, you can increase the delay value at the bottom of the interface.

Log.txt : Start a logging raw data in the file Log.txt. The value below the button is the number of characters logged. If stopped and started again, value are concatenated into the file.

Ren : Rename the raw data file Log.txt with a name containing current date and time. This button may be pressed during a log, or after it has been stopped with the first button (which is renamed as Stop)

Load : Choose and load one (or many) raw data file (txt file stored), decode the raw data file into a workspace variable Rn that can be traced with the Plot Buffer button (that execute the text box script). Data are also stored into a .mat file with the same name as the txt file. Variable stored in the mat file is Rn which is renamed into Rtotal.

Delete : Delete the file Log.txt (can be pressed during a log, reseting thus the log).


Start and Stop button is for start and stop the reception-execution of the script. Do not forget to release ("reset button") the COM port before leaving.

The Send1 and Send2 button allow to send the vector next to the button (respect Send1 and Send2) to the Serial PORT.

Real-Time Consideration

T_R is an approximation of time only, and shoudl be used for vewing purpose only. If you need precise data time stapm, follow the following procedure :

  • On the simulink model, make sure to not overload the UART. Check that you are sending less data than max allowed by the UART bauds. To count the number of bytes used, when using the Tx-Labview-Matlab block, add one byte for each data sent. For example sending one uint8 data takes 2 bytes, uint16 data takes 3 bytes and uint32 data takes 5 bytes.
  • Reconstruct a vector timing corresponding to the simulink's UART timing.

T_R (and T_Rn) approximate the time using the computer clock. Each time Matlab read an UART sequence, time is read. Then, the first byte of the sequence is assigned to the time of the previous clock read, and the last byte of the sequence is assigned with current time. Any bytes in between is assigned to a time using a linear approximation (with time of sequenc start and time of sequence stop). Thus, the time received of the first trame may be much longer if no data is send at the beginning of the logging process.

Forum related question