Development of a Matlab Data Acquisition and Control

Development of a Matlab Data Acquisition and Control Toolbox for PIC Microcontrollers Sang-Hoon Lee Department of Mechanical, Aerospace, and Manufacturing Engineering Polytechnic University, Brooklyn, NY 11201 • Mechanical Engineering Seminar • March 27, 2007

Matlab Data Acquisition and Control Toolbox for PICs 2007 Outline • Background • Motivation • Goals • Prior Research • Hardware Environment • Software Environment • Integration of Simulink and PIC • Programming the PIC microcontroller • Illustrative Example • Conclusion 1

Matlab Data Acquisition and Control Toolbox for PICs 2007 Background: Data Acquisition • Data acquisition (DAQ) refers to automatic acquisition of real-world sensory information • DAQ is used for test instruments, condition monitoring of industrial machinery, process industry, medical instruments, environment monitoring, robotics, etc. • DAQ can be used to develop virtual instruments for productivity enhancement 2

Matlab Data Acquisition and Control Toolbox for PICs 2007 Background: Data Acquisition and Control • DAQ systems are useful for monitoring and data analysis but if one needs to command a realworld device into action based on the measurement of some real-world phenomenon, then a DAQ system is not sufficient • In this case one needs a data acquisition and control (DAC) system A DAC system collects data from sensors, and using computing resources of a PC or an on-board computer processes sensory information, computes control command, and commands control actuators 3

Matlab Data Acquisition and Control Toolbox for PICs 2007 Background: PIC • Peripheral Interface Controllers (PICs) – Inexpensive microcontroller units (few dollars) that include • Central processing unit • Peripherals: memory, timers, and I/O functions – Provide functionality for multitude of applications (e. g. , automobile, consumer electronics, safety/security, telecommunication) – Popular in educational, hobby, and industrial applications 4

Matlab Data Acquisition and Control Toolbox for PICs 2007 Motivation—I • PC-based data acquisition and control (DAC) boards – High-end DAC boards (e. g. , Quanser’s Multi. Q 3, National Instruments, etc. ) – Advanced hardware capabilities and sophisticated software environment – Drawback: cost! (high hundreds to few thousand dollars) 8 Analog Inputs Digital I/Os 8 Encoder Signals Terminal Board 8 Analog Outputs Multi. Q 3 ISA DAC Board 5

Matlab Data Acquisition and Control Toolbox for PICs 2007 Motivation—II • Data Acquisition and Control (DAC) Boards – Low-end DAC boards – Relatively low cost – Drawback: use proprietary software – DAC boards supported by Matlab – Costly and usually include additional hardware features that may not be fully used (e. g. , high sampling rates and high resolution analog to digital converter) 6

Matlab Data Acquisition and Control Toolbox for PICs 2007 Goals • Create a Matlab DAC toolbox for PIC microcontrollers – Exploit serial communication capability of PIC microcontrollers and Matlab software – Use icon-based programming environment of Simulink – Illustrate the integration of low-cost PIC microcontrollers with Matlab DAC toolbox environment • Use the Matlab DAC toolbox to facilitate – Automatic generation of proper PIC assembly codes for a variety of sensors and actuators – Automatic programming of the PIC microcontroller – Data communication between the PIC microcontroller and Matlab 7

Matlab Data Acquisition and Control Toolbox for PICs 2007 Prior Research • Basic Stamp 2 (BS 2) microcontroller to Lab. VIEW interface by Radcliffe, 2001 • GUI capabilities for PIC microcontroller via a Matlab interface by Lee et al. , 2004 • Matlab data acquisition and control toolbox for BS 2 microcontroller by Panda et al. , 2004 Lab. VIEW interface with BS 2 Matlab DAC for BS 2 8

Matlab Data Acquisition and Control Toolbox for PICs 2007 Hardware Environment • PIC development board consisting of – PIC 16 F 74 microcontroller with a 20 MHz crystal oscillator – MAX 232 with five 1μF capacitors – DB-9 connector • PIC development board transmits/receives data to/from a PC via MAX 232 • PIC-PG 2 C programmer – Receives power from the PC’s serial port – IC-Prog to download PIC HEX code to the PIC microcontroller PC and PIC development board PIC-PG 2 C programmer and PIC 9

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment I • Matlab – An interactive technical computing software • Simulink – Matlab’s icon-based programming environment • The PIC assembly language – A primitive programming language (35 single-word instruction set) • A newly developed Simulink library for the PIC microcontroller – Automatically produces and downloads the proper PIC assembly code to the microcontroller – Allows data communication between the PIC microcontroller and Matlab • MPASM • IC-Prog 10

Matlab Data Acquisition and Control Toolbox for PICs 2007 Aside: PIC Assembly Programming • Sample PIC assembly code to Blink an LED ; LED blink __CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON list P=16 F 74 include "p 16 f 74. inc" temp 1 temp 2 EQU EQU ORG GOTO 0 x 00 START MOVLW MOVWF CLRF BSF 0 x 00 TRISB PORTB BEGIN BCF ; CALL 20 h 21 h 22 h STATUS, 5 GOTO DELAY MOVLW BEGIN 0 x 3 f MOVWF temp w MOVLW 0 xff MOVWF temp 2 w 1 MOVLW 0 xff MOVWF temp 1 w 2 DECFSZ temp 1 GOTO w 2 DECFSZ temp 2 GOTO w 1 DECFSZ temp GOTO w RETURN END PORTB, 0 DELAY 11

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment II • Template. mdl model file – Predesigned Simulink model file for interaction with the PIC microcontroller – Must be used to design Simulink block diagrams for interaction with the PIC microcontroller – Total. Compile has been embedded within the callback parameters Template and model properties 12

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment III • PIC Library – Custom library of Simulink blocks for interaction with sensors and actuators connected to the PIC microcontroller – Construct a Simulink block diagram by dragging and dropping blocks into the Template model file – IOBlock – Digital input/output – Pin. State. In block (8 channels) – Pin. State. Out block (8 channels) – Analog input/output – ADC block (8 channels) – PWM block (2 channels) PIC Library 13

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment IV • ADC block – Use the 8 -bit analog to digital conversion module of the PIC microcontroller ADC block and parameter 14

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment V • PWM block – Use the PWM modules of the PIC microcontroller – Produce the required analog voltage output by varying the duty cycle of the PWM signal PWM block and parameter 15

Matlab Data Acquisition and Control Toolbox for PICs 2007 Software Environment VI • IOBlock – Initiate serial communication between Matlab and the PIC – Transmit/receive data between Matlab and the PIC – Terminate serial communication between Matlab and the PIC – Compute the average sampling period for Simulink block diagram execution IOBlock and parameters 16

Matlab Data Acquisition and Control Toolbox for PICs 2007 Integration of Simulink and PIC • Template file is used to construct a Simulink block diagram • Before the start of the Simulink block diagram a Matlab function (Total. Compile) is executed in the following sequence – Declare global variables – Categorize and specify sensor and actuator blocks used in the Simulink diagram – Generate a PIC assembly code – For each sensor/actuator block in the PIC Library the corresponding PIC assembly code has already been created and saved as an m-file – Generate a portion of the IOBlock Matlab code – Program the PIC microcontroller • At the start of the Simulink block diagram the IOBlock is executed first 17

Matlab Data Acquisition and Control Toolbox for PICs 2007 Programming the PIC microcontroller I • PIC assembly code is generated by Total. Compile • PIC assembly code is converted to PIC HEX code by the MPASM assembler • PIC HEX code is downloaded by IC-Prog via the serial port Flow diagram of programming the PIC microcontroller 18

Matlab Data Acquisition and Control Toolbox for PICs 2007 Aside: LED Blinker Simulink Diagram 19

Matlab Data Acquisition and Control Toolbox for PICs 2007 Aside: LED Blinker Code from Matlab Toolbox LIST p=16 f 74 MOVWF TRISA MOVLW d'10' INCLUDE "p 16 f 74. inc" MOVLW b'1111' MOVWF SPBRG BSF CCP 2 CON, CCP 2 M 3 __CONFIG _CP_OFF & MOVWF TRISB MOVLW b'00100100' BSF CCP 2 CON, CCP 2 M 2 _WDT_OFF & _HS_OSC & MOVLW b'0000' MOVWF TXSTA _PWRTE_ON MOVWF TRISD BANKSEL RCSTA ADCInitialization temp EQU 20 h temp 1 EQU 21 h tempval EQU 22 h ORG 0 CLRF STATUS GOTO Boot. Start BANKSEL PORTA CLRF PORTB CLRF PORTC CLRF PORTD BANKSEL TRISA MOVLW b'1111' CLRF CCP 2 CON Main receive 1 MOVLW b'10010000' BCF STATUS, RP 0 MOVWF RCSTA BCF STATUS, RP 1 MOVLW B'10000001' MOVF RCREG, W BTFSS PIR 1, RCIF MOVWF ADCON 0 MOVF RCREG, W GOTO receive 1 BSF STATUS, RP 0 MOVF RCREG, W MOVLW b'0000' MOVWF tempval MOVWF ADCON 1 PWMinitialization BSF T 2 CON, T 2 CKPS 1 USARTinitialization BSF T 2 CON, TMR 2 ON BTFSS tempval, 0 BSF STATUS, RP 0 GOTO STATUS, RP 0 compare 1 MOVLW b'00000001' MOVLW d'250' BSF MOVWF OPTION_REG MOVWF PR 2 GOTO BCF STATUS, RP 0 MOVLW b'10000100' MOVWF INTCON CLRF TMR 0 Baud. Rate. Settingsfor. USART BSF STATUS, RP 0 BCF TRISC, 1 BCF PORTD, 1 jmp 1 skp 1 TRISC, 2 STATUS, RP 0 skp 1 BCF PORTD, 1 jmp 1 CLRF CCP 1 CON NOP BSF CCP 1 CON, CCP 1 M 3 GOTO BSF CCP 1 CON, CCP 1 M 2 END Main 20

Matlab Data Acquisition and Control Toolbox for PICs 2007 Illustrative Example–I • Position control of a DC motor is performed to show the efficacy of Matlab data acquisition and control toolbox • The testbed has a DC motor, a continuous rotation potentiometer, and a power module Hardware layer schematic 21

Matlab Data Acquisition and Control Toolbox for PICs 2007 Illustrative Example–II • A PID controller is used for position control of the DC motor – ADC_Pot block for the analog output of the potentiometer – PWM_Motor block for the analog input to the motor Simulink block diagram 22

Matlab Data Acquisition and Control Toolbox for PICs 2007 Illustrative Example–III • Experimental setup for the DC motor angular position – PID controller (KP=1. 43, KI=1. 97, and KD=0. 5) – Simulink’s ODE 4 (Runge-Kutta) algorithm with a sampling period of 0. 13 second • Experimental response of the DC motor angular position – An average 2% settling time of 5. 9 seconds and a percentage overshoot of 18. 25% DC motor angular position tracking response 23

Matlab Data Acquisition and Control Toolbox for PICs 2007 Conclusion • Developed a low-cost Matlab Data Acquisition and Control Toolbox for PIC microcontrollers by exploiting – Matlab and Simulink – Serial communication capabilities of Matlab and PIC • Data Acquisition and Control Toolbox designed using our framework allows the user to focus on – Hardware-in-the-loop implementation – Experimental validation – Industry-style rapid control prototyping • Our framework allows the use of a microcontroller as a low-cost data acquisition and control board 24
- Slides: 25