PIC_DAC_Rev1

advertisement
Development of a Matlab Data Acquisition and Control Toolbox
for PIC Microcontrollers

Abstract—This paper presents a personal computer (PC)-based data acquisition and control
tool that is based upon a Peripheral Interface Controller (PIC) microcontroller, Matlab, and
Simulink. Specifically, a library of PIC microcontroller functions for Simulink is created.
Moreover, PIC microcontroller and Matlab are merged by exploiting their serial communication
capability and to produce an inexpensive data acquisition and control platform. Finally, the
efficacy of this data acquisition and control platform is illustrated by performing angular position
control of a DC motor.
I. INTRODUCTION
Data acquisition and control boards are essential for interfacing sensors/actuators with decision
making devices such as a PC. Thus, data acquisition and control systems are required in
monitoring/instrumentation applications involving machinery, processes, environment, etc., and
in automatic control applications. Even though a variety of data acquisition and control systems
have become widely available in the last one-and-a-half decade, the systems that target the
educational sector and provide support for icon-based programming environments, e.g.,
LabVIEW and Simulink, tend to be quite expensive (over $500 to several thousand dollars).
Moreover, instructional labs generally do not require the intrinsic high-performance features of
many of the current data acquisition and control hardware (e.g., high sampling rates, high
resolution analog to digital converters, etc.) for the typical electro-mechanical laboratory
experiments. This paper proposes a microcontroller-based data acquisition and control system
that is particularly suitable for educators interested in developing lab experiments that do not
require high-cost, high-performance data acquisition hardware and yet can benefit from the iconbased programming environment of Simulink.
Several recent papers [1—3] have focused on interfacing low-cost microcontrollers (such as
Basic Stamp 2 (BS2) and PIC) with icon-based programming environments such as LabVIEW
and Simulink. Specifically, references [1—3] concentrated primarily on endowing
microcontrollers with graphical user interface (GUI) capability by exploiting the GUI tools of
LabVIEW and Simulink. However, the methodology of references [1—3] requires manually
programming the microcontroller for all sensing, control computation, and actuation tasks and
for serial communication with the GUI running on the PC. To program a PIC microcontroller or
a BS2 microcontroller using PIC assembly programming language or PBasic programming
language, respectively, requires knowledge and experience with the syntax of these languages
and is often tedious.
This paper details the development of PIC microcontroller based low-cost data acquisition and
control system that exploits Matlab and Simulink as the key software components for
implementing data acquisition and control algorithms using a block-diagram format. Specifically,
the paper exploits a newly developed library of PIC functions for Simulink and the serial
communication capability of both PIC microcontrollers and Matlab to produce a seamless
integration between them. The framework of this paper completely obviates the need to manually
program the PIC microcontroller by creating a library of PIC microcontroller functions for
Simulink. Specifically, the data acquisition and control toolbox of this paper facilitates i)
automatic generation of proper PIC assembly codes for a variety of sensors and actuators, ii)
automatic programming of the PIC microcontroller, and iii) data communication between the PIC
microcontroller and Matlab. In an instructional laboratory, this approach allows instructors and
students to focus on hardware-in-the-loop implementation, experimental validation, and industrystyle rapid control prototyping. Finally, this paper is in the spirit of [4], which provided a Matlab
data acquisition and control toolbox for the BS2 microcontrollers. However, whereas the BS2
microcontroller costs over $45 and includes only digital input/output (I/O) functionality, thus
requiring external analog to digital converter (ADC) to interface with analog sensors, several
PIC microcontrollers costs under $5 and include built-in ADC functionality.
This paper is organized as follows. Section II describes the PIC microcontroller and the related
development hardware. Section III describes the software environment used in this work. Section
IV gives details concerning the software integration of Simulink with the PIC microcontroller.
Section V illustrates the functionality and capability of the data acquisition and control hardware
and software of this paper by performing position control of a DC motor. Finally, Section VI
provides some concluding remarks.
II. HARWARE ENVIRONMENT
In this development, we use a PIC16F74, 40-pin, 8-bit Complementary Metal-Oxide
Semiconductor (CMOS) Fast Low-Latency Access with Seamless Handoff (FLASH) dual inline
package IC. The proposed environment is particularly beneficial to educators interested in
building systems that use basic controls with minimal hardware costs.
The hardware environment required for this DAC system consists of a PIC microcontroller, a
PC, a PIC-PG2C programmer, a PIC development board, a RS232 driver/receiver, and a DB-9
serial cable. The PIC microcontroller is interfaced with external devices such as sensors (e.g.,
potentiometers, photoresistors) and actuators (e.g., LEDs, DC motors). The PC hosts the
interactive DAC Toolbox for the user to manipulate control variables and visualize sensory data.
The PIC microcontroller and the PC communicate with each other using a serial interface. The
PIC development board (see section II—F) and a DC motor experiment test bed (see section V)
are used to illustrate our PIC-based DAC approach.
Figure 1: Hardwares
A. Peripheral Interface Controller
PIC microcontrollers, manufactured by Microchip, Inc., are small, low-cost controllers that
include a processor and a variety of peripherals. PIC microcontrollers are significantly easier to
use vis-à-vis embedded microprocessors. As an example, users can assign desired functionality
(e.g., ADC, USART1) to I/O pins of PIC microcontrollers. PIC microcontrollers can be operated
at various clock speeds (32 kHz to 20 MHz). PIC microcontrollers’ memory architecture
separates its data memory from its program memory with the program memory available as OneTime Programmable (OTP), Erasable Programmable Read-Only Memory (EPROM), or FLASH.
PIC microcontrollers are programmed in the PIC assembly language using a 35 single-word
instruction set. See [4] for more details on hardware and software features of PIC
microcontrollers.
The user specified embedded PIC program in the Matlab-based DAC toolbox is written on the
PC and downloaded from the PC to the PIC microcontroller using the DB-9 serial cable
connection between the PC and the PIC Development Programmer on which the PIC
microcontroller is mounted. Commonly available PIC Development Programmers include
PICSTART Plus from Microchip, Inc. and PIC-PG2C from Olimex, Ltd. [5]. The PIC-PG2C is a
handy and low-cost programmer. In this project, we use the PIC-PG2C programmer that requires
ICPROG, free software available on the website [6], for programming PIC microcontrollers.
In this paper, we employ a PIC16F74, a 40-pin CMOS FLASH-based, 8-bit, mid-range (14-bit
instruction word length) microcontroller. The PIC16F74 has 4 Kbytes of FLASH program
memory and 192 bytes of data memory. Furthermore, it has 33 digital I/O pins organized in 5
groups of I/O ports that can be assigned as 8-bit ADCs, Capture/Compare/PWMs2 (CCPs), the 3wire Serial Peripheral Interfaces (SPIs), the 2-wire Inter-Integrated Circuit (I2C) buses, USART
ports, etc. In this project, one set of I/O port is assigned for 8 channel 8-bit ADCs and another set
of ports is assigned for 8 channel outputs. We use an external 20 MHz high-speed crystal
oscillator to supply operating clock cycles. The PIC16F74 can be powered using a wide range of
voltage sources, e.g., 2-volt direct current (VDC) to 5.5VDC, and each of its I/O pins can sink or
1
2
Universal synchronous/asynchronous receiver and transmitter.
Pulse width modulation.
source up to 25mA of current. It is ideal for not only laboratory data acquisition (the application
considered in this paper), but also automotive, industrial, and consumer applications.
B. PIC-PG2C Programmer
PIC-PG2C programmer (see Figure 2) is a PIC microcontroller programmer manufactured by
Olimex, Ltd. The programmer is used to download PIC HEX code, converted from PIC assembly
code, into a PIC microcontroller via a serial cable. Different from other PIC programmers, it
doesn’t require any additional power supply for itself. Instead it is supplied with not only all
necessary signals but also power from a serial port on a PC. So it may not work properly if a
laptop computer is used because of power shortage from the laptop computer.
(a)
(b)
Figure 2: (a) PIC-PG2C programmer and a PIC, (b) PIC-PG2C programmer with a PIC mounted
C. RS232Driver/Receiver
MAX232 is a 2-channel, RS232 driver and receiver manufactured by Maxim Integrated
Products, Inc [7]. It requires a 5VDC power supply and converts voltage levels between PC-based
logic and PIC microcontroller-based logic. Specifically, the voltage levels of logic high and logic
low for PIC microcontrollers, like many other microcontrollers, correspond to 5VDC and 0VDC,
respectively, whereas ones of logic high and low for the PC correspond to -12VDC and 12VDC,
respectively. The MAX232 is used with five 1μF capacitors to adjust the voltage level
differences between the PC-based logic and the PIC-based logic. See [7] for more details of the
MAX232 hardware features.
D. DB-9 Serial Cable
Serial communication between a PIC microcontroller and a PC is performed through a DB-9
serial cable. This cable facilitates data communication between the PIC and the PC. It also allows
programming the PIC microcontroller from the PC using serial port programmers.
E. Personal Computer
In this paper, an IBM-compatible Pentium 4 PC running Microsoft Windows XP operating
system is used. As previously mentioned, the PC is used to write, debug, and download
embedded PIC programs. One of the serial ports on the PC is reserved for serial communication
with the PIC microcontroller. MPASM, IC_PROG, Matlab (version 6.5), and Simulink are
installed on the PC. Experimental data is collected and displayed on the PC as well.
F. PIC Development Board
The PIC development board consists of a photoresistor, a potentiometer, LEDs, a 20MHz
crystal oscillator, a MAX232 with five 1μF capacitors, a PIC16F74 microcontroller, a
breadboard, and a DB-9 connector. The photoresistor provides light intensity measurement and
the potentiometer measures angular position. Those sensors are interfaced to pins allocated as 8bit ADCs in port A of the PIC16F74 microcontroller. The LEDs express digital outputs (on/off)
and are connected to pins allocated as digital outputs on port B of the PIC microcontroller. The
PIC microcontroller transmits/receives sensory data to/from the PC via a MAX232 circuit.
III. SOFTWARE ENVIRONMENT
The software environment for this project consists of the PIC assembly language, Matlab,
Simulink, the predefined DAC toolbox, MPASM, and IC-Prog. The PIC assembly language is a
primitive programming language consisting of a 35 single-word instruction set. Matlab is
interactive technical computing software. Simulink is Matlab’s model-based, system-level, visual
programming environment that is widely used to simulate and analyze dynamic system models
using icon-based tools. The predefined DAC toolbox of Simulink provides a library of the PIC
microcontroller so that Matlab can interact with the PIC microcontroller. Users, therefore, no
longer need to know PIC assembly language to program the PIC microcontroller to be functional.
The PIC library is the predefined Simulink library of additional blocks that are used to
communicate data with sensors and actuators connected to the PIC microcontroller. MPASM is
an assembler that translates PIC assembly code into PIC HEX code and can be invoked through
command line interface. Finally, IC-Prog is a program that can be used to download the PIC
HEX code converted from the PIC assembly code via the MPASM assembler and includes a
command line interface. The software interface of our DAC system consists of two main
components: i) a Simulink model file named Template.mdl and ii) a block library named PIC
library. Template.mdl is the Simulink model file where the user designs the Simulink block
diagram for interaction with the PIC microcontroller.
A. Template.mdl
The Template.mdl model file (see Figure 3) is a predestined Simulink design platform, on
which users design the Simulink block diagram for interaction with the PIC microcontroller. The
key property of Template.mdl is the inclusion of a function within the callback parameters of this
Simulink model file, where this function is to be executed before the block diagram actually runs.
Furthermore, renaming this Template.mdl file still preserves the same callback property, whereas
opening a new Simulink model file does not. In our case, when starting the Simulink block
diagram, TotalCompile, simulation start function at callback property, is executed at the
beginning of each Simulink block diagram cycle. This TotalCompile function performs several
important tasks and enables the communication between Matlab and the PIC microcontroller.
Details of this function are provided in a subsequent section.
Figure 3: template and model properties
B. PIC Library
The PIC Library is a custom library for Simulink which provides blocks (in the form of sfunctions) that interface with sensors and actuators connected to the PIC microcontroller.
Currently available blocks are PIC_ADC and PinStateOut. The PIC_ADC block provides an 8bit digital output from a specified ADC channel of the PIC microcontroller. The PinStateOut
block provides the state of a pin (e.g., on or off). Both blocks contain block parameters that need
to be set for appropriate hardware configuration. Furthermore, this library contains a block
labeled IOBlock which enables serial communication between the PIC microcontroller and
Matlab. The IOBlock is required in all user-designed Simulink block diagrams that will
incorporate sensors and actuators connected with the PIC microcontroller. Finally, these blocks
are responsible for writing to or reading from global variables, which are to be sent or received
from the PIC microcontroller, respectively. Details of these operations are provided in a
subsequent section. The following describes hardware settings and parameter requirements of
each block.
1) PIC_ADC Block: This block (see Figure 4) configures the analog to digital conversion
module of the PIC microcontroller, that is, it allocates an I/O pin of the PIC microcontroller to be
capable of analog to digital conversion so that the pin can receive analog inputs from a sensor. A
maximum of eight channels are available at port A of the PIC16F74 microcontroller and able to
be allocated to receive analog inputs without any needs of external analog to digital converters.
The parameter that the PIC_ADC block requires is a pin number. The pin number is used to
assign which I/O pin of the PIC microcontroller to be set to an analog to digital converter.
Figure 4: PIC_ADC block and parameter
2) PinStateOut Block: This block (see Figure 5) configures digital outputs from I/O pins of
another port of the PIC microcontroller. Eight channels of port B in the PIC microcontroller are
able to be allocated to submit digital outputs. The parameter of PinStateOut block is a pin
number.
Figure 5: PinStateOut block and parameter
3) IOBlock: The IOBlock is all the time necessary to every Simulink block diagram so that the
PIC microcontroller is able to interact with sensors and actuators connected. The block first
initiates serial communication between the PIC microcontroller and Matlab when it is executed
in the beginning. And it sends and receives data between the PIC microcontroller and Matlab
while running. Finally it terminates this serial communication link at the end. The IOBlock
includes two callback functions in the block properties. These two callback function, start_serial
and stop_serial, initiates and terminates serial communication, which are executed when the
Simulink block diagram is started and stopped, respectively. The IOBlock is programmed to have
the first priority. Therefore it is the first block executed in the Simulink block diagram. This
ensures that all sensor and actuator data in Matlab are first received and sent, respectively, which
then is used by the appropriate sensor and actuator blocks in the Simulink block diagram.
Another important role of IOBlock is to provide the sampling period of a given Simulink block
diagram. This feature allows blocks that require the sampling period to be used properly, e.g., an
integrator block. We denote the sampling period as the amount of time per one cycle of the
Simulink block diagram to be executed. The sampling period provided by IOBlock is an
arithmetically averaged sampling period that is calculated by averaging the time taken to run a
specified number of cycles of the Simulink block diagram. The number of cycles used for
averaging is user definable, such that the user can adjust the resolution of the sampling period,
i.e., the larger number of cycles will provide the finer resolution of the sampling period compared
with the smaller number of cycles. However, this arithmetically averaged sampling period does
not provide the exact sampling period for each Simulink block cycle. Thus this DAC toolbox is
not quite suitable for any real-time requirements, which means enforcing a specific sampling
period for the Simulink block diagram is not permissible.
IV. SIMULINK DIAGRAM DETAILS
This section depicts details in the TotalCompile function and the serial data communication
between Matlab and PIC microcontroller. The TotalCompile function is a custom Matlab
function which has the sequence of tasks that are performed while a Simulink block diagram is
running.
A. TotalCompile
The TotalCompile function is designed specifically to perform the sequence of tasks. The
TotalCompile function is set as a simulation start function of callbacks function in model
properties of Template.mdl. The TotalCompile function also determines the order of data
received from sensors and sent to actuators when a set of sensors and actuators to be used in a
Simulink block diagram. The following subsections describe the sequence of tasks.
1) Using Global Variables: The TotalCompile function declares global variables. The global
variables are universally used in every function in order to commonly share data with any sensor
or actuator block from the PIC library.
2) Storing Sensors and Actuators Blocks: The next step after defining global variables is
looking up all the actuators/sensors blocks by name in the Simulink model file and matching
with the ones in the PIC library. When a matching block is found, it is then categorized as either
a sensor or an actuator. The matched type of block is then used in conjunction with the specified
block parameters to be stored in a data structure depending on its category, e.g., a sensor or an
actuator structure. Therefore, for multiple sensor blocks present in a Simulink block diagram, an
array of sensor structures is allocated to carry data of the sensor blocks. Similarly, an array of
actuator structures is allocated for multiple actuator blocks. On the contrary a “null” object is
stored indicating an empty array if there is no sensor or actuator block present in the diagram. It
is important that a Simulink model file must be saved every time the Simulink block diagram is
modified. Unsaved Simulink model files may not include the most recent updates to the sensor or
actuator block parameters in which this sequence of tasks may not be performed correctly.
The sensor/actuator blocks have parameters, which are used to distinguish each block
exclusively in case that a Simulink block diagram has multiple blocks with the same name. The
parameter of the sensor/actuator blocks is a pin number of the PIC microcontroller connected to
the sensor/actuator. The sensor or actuator array information is also applied to organize data sent
via serial communication.
3) Generating PIC Assembly and Matlab Code: In this step, PIC assembly code and Matlab
code are generated by using information of sensor and actuator arrays stored in the previous step.
The PIC assembly code generated is used to program the PIC microcontroller. The
sensor/actuator blocks in the PIC Library contain corresponding PIC assembly codes. Therefore
any sensor/actuator block inserted into a Simulink block provides the associated PIC assembly
code. Finally, the PIC assembly code is organized as follows: i) declaration and initialization of
PIC microcontroller functionality, ii) data is gathered from the sensors, iii) sensor data is sent to
the Simulink block diagram, iv) the PIC microcontroller waits for Matlab to send the actuator
data, and v) this actuator data drives the actuators. Next, Matlab code is generated to facilitate
serial communication between Matlab and the PIC microcontroller. In particular, this Matlab
code sends and receives the same amount of data that the PIC receives and sends, respectively.
4) Programming the PIC microcontroller: Programming the PIC microcontroller step involves
two sequence of steps; i) the generation of corresponding PIC HEX code from PIC assembly
code generated in previous step and ii) the programming the PIC microcontroller with this PIC
HEX code via serial communication. The generation of corresponding HEX code process is
executed by using the MPASM.exe program, which is provided for free from Microchip Co. and
stores the result in a text file. The PIC HEX code is sent to the PIC microcontroller via the PICPG2C programmer using ICPROG.exe program, which has command line programming capacity
and is available on the web with free of charge. See [6] for details on how to program the PIC
microcontroller in command line via serial communication. Figure 6 shows a flow diagram of
programming the PIC microcontroller. After attempting to program the PIC microcontroller
using the programmer, the PIC microcontroller has to be removed from the programmer and
mounted onto the PIC development board.
Figure 6: Flow diagram of programming the PIC microcontroller
5) Starting a Simulink Block Diagram: When Simulink block diagram starts, the PIC
microcontroller will be programmed as previously described. Simulink waits for users to confirm
that the PIC microcontroller is removed from the PIC programmer and properly mounted onto
the PIC development board once the PIC microcontroller is successfully programmed. Then
serial communication between PIC and Matlab starts. If Simulink needs to be run without
reprogramming the PIC microcontroller, then programming the PIC microcontroller procedure
can be skipped by clicking “NO” at the step of asking for programming the PIC microcontroller
and Simulink will start executing serial communication between PIC microcontroller and Matlab
without reprogramming the PIC microcontroller.
B. PIC microcontroller and Matlab Serial Communication
Since both PIC microcontroller and Matlab provide serial communication capabilities, PIC
microcontroller and Matlab communicate with each other using the their serial communication
ports. When sensor/actuator data are transmitted through serial communication, those data are
carried by units of each packet. Therefore, sensor data in the sensor packet can be retrieved by
the corresponding sensor blocks in the Simulink block diagram, whereas data from the actuator
blocks are packaged into a single actuator packet for transmission to the PIC. The reason why
packets are used for data communication between Matlab and PIC is because of efficiency
compared with the individual transmission of sensor/actuator data. By transmitting
sensor/actuator data in packets form, the amount of information needed to be carried with the
data via serial communication is reduced, i.e., in each sensor or actuator packet, the necessary
data for serial communication is only one start and stop bit, whereas for individual, disjoint
sensor or actuator data, multiple start and stop bits are necessary for serial communication.
The IOBlock receives the sensor packet and stores the data in a sensor global variable. The
sensor packet received from the PIC is first converted to the appropriate sensor data, i.e., a
numerical value dependent on the sensor type, and is then stored in the sensor global variable.
The IOBlock transmits data from the actuator global variable to the PIC for execution. In
constructing the actuator global variable, each actuator block converts the numerical value of
their actuator data into a set of bytes. Next, the sets of bytes for all actuator blocks are packaged
into a packet and saved into the actuator global variable. Lastly, data communication between
PIC and Matlab will continue until the Simulink block diagram is stopped.
V. EXAMPLE – DC MOTOR CONTROL
This example explores a DC motor control experiment using the PIC microcontroller and our
Matlab data acquisition and control toolbox. The DC motor test-bed, shown in Figure 7, consists
of a DC motor, a continuous rotation potentiometer, and an H-Bridge. The potentiometer sensor
measures angular position of the DC motor and is used in the feedback control algorithm. The
potentiometer outputs a +5VDC signal corresponding to the absolute angular position of the
motor. The position of the DC motor is sent to Matlab as an analog signal converted by the
microcontroller using an ADC pin. The PIC microcontroller supplies logic signals to the operate
H-Bridge to control the DC motor angular position.
In this experiment, we used Matlab version 6.5, which has a built-in serial communication
library, in addition to Simulink version 5.0. This experiment utilizes the on-off controller using
deadband to control the position of the DC motor. Referring to Figure 7, a PIC_ADC block is
used to import sensor data to the PIC microcontroller. The output from a sensor block is then
connected to an appropriate calibration gain block. Furthermore, PinStateOut blocks are used to
transmit H-Bridge controller logic output to control position of the DC motor. We note that in
this Simulink block diagram an IOBlock from the PIC library is used to obtain the sampling
period.
Figure 7: Hardware layer schematic
A. Experimental Results
A sliding mode on-off controller is implemented in Simulink to control the DC motor using
the PIC Library. In this experiment, we command the DC motor arm to track set points between
90 degrees and -90 degrees, which produced a 2 percent settling time of 1.37sec. Figure 8 shows
the DC motor position response.
Figure 8: DC motor position tracking response
VI. CONCLUSION
In this paper, we exploited the serial communication capabilities of Matlab and the PIC
microcontroller to develop an inexpensive data acquisition and control system. Using the
advanced features of Simulink, our software environment allows for the generation of PIC
assembly code for a variety of sensors and actuators, programming of the PIC microcontroller,
and data communication between PIC and Matlab. Furthermore, a DC motor control experiment
was conducted to show the salient features of our data acquisition and control toolbox.
Specifically, a sliding mode on-off controller was implemented in a Simulink block diagram to
control the DC motor arm position.
REFERENCES
[1] C.
J.
Radcliffe,
“The
Basic
Stamp
II
and
LabVIEW,”
http://www.parallax.com/dl/sw/labviewbs2.pdf.
[2] S.-H. Lee, Y.-F. Li, and V. Kapila, “Development of a Matlab-Based Graphical User
Interface for PIC Microcontroller Projects,” Proceedings of the American Society of
Engineering Education Conference, Salt Lake City, UT, Session 2220, 2004.
[3] Y. F. Li, S. Harari, H. Wong, and V. Kapila, “Matlab-Based Graphical User Interface
Development for Basic Stamp 2 Microcontroller Projects,” Proceedings of the American
Control Conference, Boston, MA, pp. 3233–3238, 2004.
[4] A. Panda, H. Wong, V. Kapila, and S.-H. Lee, “Matlab Data Acquisition Control Toolbox for
Basic Stamp Microcontrollers,” Proceedings of the Conference on Decision and Control, San
Diego, CA, 2006.
[5] Online: http://www.microchip.com/1010/index.htm, website of Microchip Technology, Inc.
[6] Online: http://www.olimex.com/dev/, website of Olimex Ltd., (access link for PIC-PG2B
Development Programmer).
[7] Online: http://www.ic-prog.com/, website of IC-Prog software developer of the PIC
microcontroller programming (access link for the command line programming for PIC
microcontrollers).
[8] Online: http://pdfserv.maxim-ic.com/en/ds/MAX220-MAX249.pdf, website of Maxim
Integrated Products, (access link for MAX232 datasheet).
[9] Online: http://www.mathworks.com/products/simulink/, website of MathWorks Inc.,
developer and distributor of Simulink.
Download