/firmware/src/Extruder/boards/ecv34/ExtruderMotor.cc

http://github.com/makerbot/G3Firmware · C++ · 59 lines · 31 code · 6 blank · 22 comment · 9 complexity · b18e443b97aa095df80f061d23ad7680 MD5 · raw file

  1. /*
  2. * Copyright 2010 by Adam Mayer <adam@makerbot.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>
  16. */
  17. #include <avr/io.h>
  18. #include <util/atomic.h>
  19. #include "Configuration.hh"
  20. #include "Pin.hh"
  21. // Enable pin D5 is also OC0B.
  22. int16_t last_extruder_speed;
  23. // TIMER0 is used to PWM motor driver A enable on OC0B, and
  24. // channel C on OC0A.
  25. // All timers are initialized in the ExtruderBoard file.
  26. // Timer0 has a duty cycle of 1/16 ms.
  27. void initExtruderMotor() {
  28. last_extruder_speed = 0;
  29. MOTOR_ENABLE_PIN.setDirection(true);
  30. MOTOR_ENABLE_PIN.setValue(false);
  31. MOTOR_DIR_PIN.setDirection(true);
  32. }
  33. void setStepperMode(bool mode, bool external/* = false*/) {
  34. // New boards do not drive their own extruder stepper.
  35. }
  36. void setExtruderMotor(int16_t speed) {
  37. if (speed == last_extruder_speed) return;
  38. last_extruder_speed = speed;
  39. bool backwards = speed < 0;
  40. if (backwards) { speed = -speed; }
  41. if (speed > 255) { speed = 255; }
  42. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  43. if (speed == 0 || speed == 255) {
  44. TCCR0A &= 0b11001111;
  45. MOTOR_ENABLE_PIN.setValue(speed==255);
  46. } else {
  47. MOTOR_ENABLE_PIN.setValue(true);
  48. TCCR0A |= 0b00100000;
  49. }
  50. MOTOR_DIR_PIN.setValue(!backwards);
  51. OCR0B = speed;
  52. }
  53. }