-
Notifications
You must be signed in to change notification settings - Fork 0
/
main4.c
80 lines (65 loc) · 1.82 KB
/
main4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
#define ENA_PORT PORTB
#define ENB_PORT PORTB
#define IN1_PORT PORTC
#define IN2_PORT PORTC
#define IN3_PORT PORTC
#define IN4_PORT PORTC
#define IN1_PIN PC0
#define IN2_PIN PC1
#define IN3_PIN PC2
#define IN4_PIN PC3
#define ENA_PIN PB1
#define ENB_PIN PB2
#define BUTTON_FORWARD_PIN PD6
#define BUTTON_REVERSE_PIN PD7
#define BUTTON_STOP_PIN PB0
void initializePorts() {
// Configure motor control pins on Port C as outputs
DDRC |= (1 << IN1_PIN) | (1 << IN2_PIN) | (1 << IN3_PIN) | (1 << IN4_PIN);
// Configure motor enable pins on Port B as outputs
DDRB |= (1 << ENA_PIN) | (1 << ENB_PIN);
// Configure button pins as inputs with pull-up resistors
DDRD &= ~((1 << BUTTON_FORWARD_PIN) | (1 << BUTTON_REVERSE_PIN));
PORTD |= (1 << BUTTON_FORWARD_PIN) | (1 << BUTTON_REVERSE_PIN);
DDRB &= ~(1 << BUTTON_STOP_PIN);
PORTB |= (1 << BUTTON_STOP_PIN);
}
void motorForward() {
IN1_PORT |= (1 << IN1_PIN);
IN2_PORT &= ~(1 << IN2_PIN);
IN3_PORT &= ~(1 << IN3_PIN);
IN4_PORT |= (1 << IN4_PIN);
ENA_PORT |= (1 << ENA_PIN);
ENB_PORT |= (1 << ENB_PIN);
}
void motorReverse() {
IN1_PORT &= ~(1 << IN1_PIN);
IN2_PORT |= (1 << IN2_PIN);
IN3_PORT |= (1 << IN3_PIN);
IN4_PORT &= ~(1 << IN4_PIN);
ENA_PORT |= (1 << ENA_PIN);
ENB_PORT |= (1 << ENB_PIN);
}
void motorStop() {
ENA_PORT &= ~(1 << ENA_PIN);
ENB_PORT &= ~(1 << ENB_PIN);
}
int main() {
initializePorts();
while (1) {
if (!(PIND & (1 << BUTTON_FORWARD_PIN))) {
motorForward();
_delay_ms(100); // delay
} else if (!(PIND & (1 << BUTTON_REVERSE_PIN))) {
motorReverse();
_delay_ms(100); // delay
} else if (!(PINB & (1 << BUTTON_STOP_PIN))) {
motorStop();
_delay_ms(100); // delay
}
}
return 0;
}