MHVLib  20111227
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_StepperMotorUnipolar.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #include <MHV_StepperMotorUnipolar.h>
00029 #include <avr/pgmspace.h>
00030 
00031 // coil patterns for wave drive
00032 const uint8_t wavedrive[] PROGMEM = {
00033 //      Phase  xxxx DCBA
00034                 0x01, 0x02, 0x04, 0x08
00035 };
00036 
00037 // coil patterns for full step drive
00038 const uint8_t fulldrive[] PROGMEM = {
00039 //      Phase  xxxx DCBA
00040                 0x03, 0x06, 0x0c, 0x09
00041 };
00042 
00043 // coil patterns for half step drive
00044 const uint8_t halfdrive[] PROGMEM = {
00045 //      Phase  xxxx DCBA
00046                 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09
00047 };
00048 
00049 
00050 
00066 MHV_StepperMotorUnipolar::MHV_StepperMotorUnipolar(MHV_RTC &rtc, MHV_STEPPER_MODE mode, MHV_DECLARE_PIN(phaseA)) :
00067                 MHV_StepperMotor(rtc),
00068                 _phaseAOut(phaseAOut),
00069                 _phaseAPin(phaseAPin),
00070                 _phaseMask(0x0f << phaseAPin),
00071                 _mode(mode),
00072                 _state(0),
00073                 _steps((MHV_STEPPER_MODE_HALF == _mode) ? 8 : 4) {
00074 //      mhv_setOutput(MHV_PIN_PARMS(phaseA));
00075         mhv_setOutput(phaseADir, phaseAOut, phaseAIn, phaseAPin + 0, phaseAPinchangeInterrupt);
00076         mhv_setOutput(phaseADir, phaseAOut, phaseAIn, phaseAPin + 1, phaseAPinchangeInterrupt);
00077         mhv_setOutput(phaseADir, phaseAOut, phaseAIn, phaseAPin + 2, phaseAPinchangeInterrupt);
00078         mhv_setOutput(phaseADir, phaseAOut, phaseAIn, phaseAPin + 3, phaseAPinchangeInterrupt);
00079 
00080         setOutput();
00081 }
00082 
00086 void MHV_StepperMotorUnipolar::setOutput() {
00087         uint8_t coilPattern = 0;
00088 
00089         switch (_mode) {
00090         case MHV_STEPPER_MODE_WAVE:
00091                 coilPattern = pgm_read_byte(wavedrive + _state);
00092                 break;
00093         case MHV_STEPPER_MODE_FULL:
00094                 coilPattern = pgm_read_byte(fulldrive + _state);
00095                 break;
00096         case MHV_STEPPER_MODE_HALF:
00097                 coilPattern = pgm_read_byte(halfdrive + _state);
00098                 break;
00099         }
00100 
00101         coilPattern <<= _phaseAPin;
00102         *_phaseAOut = (*_phaseAOut & ~_phaseMask) | coilPattern;
00103 }
00104 
00105 void MHV_StepperMotorUnipolar::step(bool forwards) {
00106         if (forwards) {
00107                 if (++_state >= _steps) {
00108                         _state = 0;
00109                 }
00110         } else {
00111                 if (--_state < 0) {
00112                         _state = _steps - 1;
00113                 }
00114         }
00115 
00116         setOutput();
00117 }