PIC Motor Control with EEPROM and Interactive Menu Example
This project is based on the PIC Motor Control and Serial Port Example project, but includes an interactive interface for manipulating the controller variables. This project was originally written to control a 10,000 rpm motor with a 100 count/revolution encoder with a 4x QEI, so has a higher sampling rate and different controller gains than the previous project. To accommodate the higher sampling rate, this PIC uses a 40Mhz external oscillator.
This program implements a closed-loop feedback velocity controller for a motor with a quadrature encoder on its shaft. Use HyperTerminal on a PC to control the motor. In HyperTerminal, select a baud rate of 115200 bps, 8 data bits, 1 stop bit, no parity, no flow control, and use ANSI emulation. The motor controller will stream the velocity of the motor (in encoder counts per period) to the window. The values for the PWM configuration registers were calculated manually instead of using the initialization functions in motor_pwm.h to optimize the code and performance. Press 'p' while the PIC is running to access the menu
Before turning on the PIC for the first time, disable the motor, or it may spin wildly because the controller variables have not been initialized. After programming, press 'p' to access the menu, then press '9' to initialize the variables to their default values. A strange bug seems to prevent the effects from taking place right after programming, so if changes do not take effect, try powering down the PIC and turning it back on. Variable values are stored in the EEPROM and are non-volatile. Floating point numbers must be entered in the floating point format, e.g. 3.00 instead of 3. Because the PIC's C library has no float-string conversion, floating point numbers will be displayed in the raw hexadecimal IEEE 754 format. Hexadecimal numbers are indicated with the prefix 0x.
Download the MPLAB project here.
This was compiled using MPLAB 7.62 and C18 3.12 (student version). If MPLAB has problems finding files or does not compile, it may be because the directories on your computer are set up differently. In this case, create a new project following these instructions, and copy in the source files.
The source code is split into five files:
- main.c contains the code entry point and main control loop.
- main.h contains miscellaneous functions and macros used by other functions.
- Lead_Lag(EEPROM_tutorial).h contains the code for the motor control algorithm.
- motor_pwm(EEPROM_tutorial).h contains functions for setting up and using PWM.
- QEI.h contains functions for setting up and using the Quadrature Encoder Interface.
- eeprom.h contains functions for writing and reading the EEPROM.
- menu.h contains functions for implementing the interactive menu.