Lem
Lem
Lem
6 * http://simple-circuit.com/
7 */
10 #define SPEED_UP A0
11 #define SPEED_DOWN A1
13 #define PWM_MIN_DUTY 50
15
17 unsigned int i;
18 void setup() {
19 DDRD |= 0x38; //
Configure pins 3, 4 and 5 as
20 outputs
21 PORTD = 0x00;
22 DDRB |= 0x0E; //
Configure pins 9, 10 and 11 as
23
outputs
24
PORTB = 0x31;
25
26 // Timer1 module setting: set
clock source to clkI/O / 1 (no
27 prescaling)
28 TCCR1A = 0;
29 TCCR1B = 0x01;
30 // Timer2 module setting: set
31 clock source to clkI/O / 1 (no
prescaling)
32
TCCR2A = 0;
33
TCCR2B = 0x01;
34
// Analog comparator setting
35
ACSR = 0x10; //
36 Disable and clear (flag bit)
analog comparator interrupt
37
pinMode(SPEED_UP,
38
INPUT_PULLUP);
39
pinMode(SPEED_DOWN,
40 INPUT_PULLUP);
41 }
43 ISR (ANALOG_COMP_vect) {
44 // BEMF debounce
67 while(1) {
68 while(!(digitalRead(SPEED_UP))
&& motor_speed < PWM_MAX_DUTY){
69 motor_speed++;
SET_PWM_DUTY(motor_speed);
70
delay(100); }
71 while(!(digitalRead(SPEED_DOWN))
&& motor_speed > PWM_MIN_DUTY){
72
motor_speed--;
73
SET_PWM_DUTY(motor_speed);
74
delay(100);
75
}
76
}
77
}
78
79
void BEMF_A_RISING(){
80 ADCSRB = (0 << ACME); //
Select AIN1 as comparator negative
81 input
82 ACSR |= 0x03; // Set
83 interrupt on rising edge
84 }
85 void BEMF_A_FALLING(){
113 }
122
void AH_BL(){
123
PORTB = 0x04;
124
PORTD &= ~0x18;
125
PORTD |= 0x20;
126
TCCR1A = 0; // Turn
127
pin 11 (OC2A) PWM ON (pin 9 & pin
128 10 OFF)
130 }
TCCR2A = 0x81; //
void BH_CL(){
PORTB = 0x02;
PORTD |= 0x10;
TCCR2A = 0; // Turn
pin 10 (OC1B) PWM ON (pin 9 & pin
11 OFF)
TCCR1A = 0x21; //
void BH_AL(){
PORTB = 0x08;
PORTD |= 0x10;
TCCR2A = 0; // Turn
pin 10 (OC1B) PWM ON (pin 9 & pin
11 OFF)
TCCR1A = 0x21; //
void CH_AL(){
PORTB = 0x08;
PORTD |= 0x08;
TCCR2A = 0; // Turn
pin 9 (OC1A) PWM ON (pin 10 & pin
11 OFF)
TCCR1A = 0x81; //
void CH_BL(){
PORTB = 0x04;
PORTD |= 0x08;
TCCR2A = 0; // Turn
pin 9 (OC1A) PWM ON (pin 10 & pin
11 OFF)
TCCR1A = 0x81; //
duty = PWM_MAX_DUTY;
OCR1A = duty; //
Set pin 9 PWM duty cycle
OCR1B = duty; //
Set pin 10 PWM duty cycle
OCR2A = duty; //
Set pin 11 PWM duty cycle