Microcontroller Applications ELEC 421 Dr. Ron Hayne ELEC 330
Embedded System Typical analog/digital system Transducer A/D converter Converts non-electrical signals into electrical signals A/D converter Converts analog signals into digital signals Digital processor Processes digital data D/A converter Converts digital signals into equivalent analog signals Converts electrical signals into non-electrical signals ELEC 421
Microcontroller Applications Analog to Digital Conversion Interface Analog Sensors (Transducers) Pulse Width Modulation DC Motor Control MPLAB X IDE Programming and Debugging chipKIT Wi-Fire Board PIC32 with lots of I/O H-Bridge Motor Driver Control Speed and Direction ELEC 421
Target System for Development: PIC32MZ EF MIPS32 Warrior M-Class processor core runs the program’s instructions PIC32MZ microcontroller adds memory, control, interfacing and robustness circuits chipKIT Wi-FIRE development board (REV C) adds inputs, outputs and power supply Images courtesy of Digilent and Microchip
PIC32 ADC Input Support Hardware Makes the ADC more flexible Analog Input Voltages Sample & Hold Digital Output Code ADC Sample or hold? Input Channel Select Input multiplexer Sample and hold circuit Allows one ADC to read one of multiple inputs, reducing need for multiple ADCs Samples input voltage before conversion Hold last sampled voltage for conversion by ADC, reducing errors if input voltage changes
Basic Analog-to-Digital Conversion Process Uses software to start conversion and detect completion Select Channel Trigger Conversion Await Conversion Complete Flag Read Data Software Sample & Hold Sample Hold ADC Convert Hardware Data Ready Status Ready Not ready Conversion Result New Result Old result Select analog input channel End of conversion Trigger ADC ADC writes value to result register, updates conversion status flag, may generate interrupt Sample: S&H circuit samples input voltage Hold and convert S&H switches to hold mode ADC converts S&H voltage to a digital value
Potentiometer Connections on Wi-FIRE Board Potentiometer wiper connected to AN8 – analog channel 8 #define VR1_AN_CHAN_NUM (8) Ends of potentiometer resistor connected to 3.3V and 0V (ground) Wiper will have an analog voltage from 0 to 3.3V based on its position
Motivation for Output Compare Module Off Period Off-Time On-Time Enables generation of signals with precise timing Example: Pulse-Width Modulated signal Periodic digital signal with adjustable on-time Duty cycle = fraction of time on, = On-Time/Period Period = On-Time + Off-Time Duty cycle determines average signal level Often used for applications where slow response of user or circuit “averages out” PWM signal Human eye averages out brightness changes which are faster than ~20 Hz (~50 ms) So can dim a light by driving it with a PWM signal with a period under 50 ms Also: servo motor position, electric heater control, power conversion electronics, motor speed control, … Example: Duty Cycle 73% On Off 10 ms 7.3 ms 2.7 ms Average value 73%
Hardware Implementation: Output Compare Module Use TMRy value to control when output OCx changes Compare TMRy against output compare registers OCxR, OCxRS OCxR OCxRS Logic OCx Equal Comparator TMRy PRy Comparator Clock Input Equal Reset OCxCON
Dual H-Bridge Motor Driver ELEC 421
PICKit 3 Programmer/Debugger ELEC 421
Building and Downloading Code MPLAB X IDE We are using the XC32 toolchain Download Controls xc32-as assembler xc32-gcc C compiler Build the files which have changed, and dependent files xc32-g++ C++ compiler xc32-ld linker/loader Delete all temporary files and rebuild all files xc32-ar archive (library) manager Download executable to MCU flash memory Upload executable from MCU flash memory Debug the program
Running Code with the Debugger We can download the code to the MCU and let it run Hard to know what the program is doing and why We will perform on-chip debugging to monitor and control the program as it runs on the MCU Start debugging: Debugger features Program execution control options Start, pause, reset program Breakpoints: pause program if it reaches this code Program inspection options Variable watches Function call stack
Program Execution Control Options Exit debugger (Shift-F5) Continue running program – run until paused or breakpoint reached (F5) Pause program – temporarily stop program execution Reset program – next run starts from beginning Step over function – execute entire function, pause (F8) Step into function – enter function, pause before executing (F7) Run to cursor – run to code corresponding to current cursor location (F4) Set PC at cursor – update program counter to code at cursor Focus cursor at PC – update window to show code for current PC PC (Program counter) value – address of next instruction to execute
Using Breakpoints Use breakpoints to pause program execution at a specific point in the program If processor tries to execute code at a breakpoint, it will pause the program and give control to the debugger Now you can examine and change variables, step through the program to see what executes next, etc. Set/remove breakpoints by Left-clicking in gray column, or Right-clicking on source code and selecting “Toggle Line Breakpoint” Limited number of hardware breakpoints in processor (8). Need one free to single-step program execution
Monitoring Program Data Variable watch window allows you to see a variable’s value (and even change it) Also shows other information: data type, address Can add a variable to this window by… Right-clicking on variable name in source code window and selecting “New Watch…” Typing in name of variable in here variable window: Or clicking on Add Watch icon Can see variable’s value by hovering cursor over variable name in source code
Microcontroller Applications Analog to Digital Conversion Interface Analog Sensors (Transducers) Pulse Width Modulation DC Motor Control MPLAB X IDE Programming and Debugging chipKIT Wi-Fire Board PIC32 with lots of I/O H-Bridge Motor Driver Control Speed and Direction ELEC 421