PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
18#include <avr/io.h>
19#include <util/atomic.h>
20#include "Configuration.hh"
21#include "Pin.hh"
22
23// Enable pin D5 is also OC0B.
24
25int16_t last_extruder_speed;
26
27// TIMER0 is used to PWM motor driver A enable on OC0B, and
28// channel C on OC0A.
29// All timers are initialized in the ExtruderBoard file.
30// Timer0 has a duty cycle of 1/16 ms.
31void initExtruderMotor() {
32	last_extruder_speed = 0;
33	MOTOR_ENABLE_PIN.setDirection(true);
34	MOTOR_ENABLE_PIN.setValue(false);
35	MOTOR_DIR_PIN.setDirection(true);
36}
37
38
39void setStepperMode(bool mode, bool external/* = false*/) {
40	// New boards do not drive their own extruder stepper.
41}
42void setExtruderMotor(int16_t speed) {
43	if (speed == last_extruder_speed) return;
44	last_extruder_speed = speed;
45	bool backwards = speed < 0;
46	if (backwards) { speed = -speed; }
47	if (speed > 255) { speed = 255; }
48	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
49		if (speed == 0 || speed == 255) {
50			TCCR0A &= 0b11001111;
51			MOTOR_ENABLE_PIN.setValue(speed==255);
52		} else {
53			MOTOR_ENABLE_PIN.setValue(true);
54			TCCR0A |= 0b00100000;
55		}
56		MOTOR_DIR_PIN.setValue(!backwards);
57		OCR0B = speed;
58	}
59}