-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.c
77 lines (66 loc) · 2.27 KB
/
main2.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
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 24000000UL
#define IN1_PORT PORTD // Change to the appropriate port
#define IN2_PORT PORTD // Change to the appropriate port
#define IN3_PORT PORTD // Change to the appropriate port
#define IN4_PORT PORTD // Change to the appropriate port
#define ENA_PORT PORTB // Change to the appropriate port
#define ENB_PORT PORTB // Change to the appropriate port
#define IN1_PIN PD2 // Change to the appropriate pin
#define IN2_PIN PD3 // Change to the appropriate pin
#define IN3_PIN PD4 // Change to the appropriate pin
#define IN4_PIN PD5 // Change to the appropriate pin
#define ENA_PIN PB1 // Change to the appropriate pin
#define ENB_PIN PB2 // Change to the appropriate pin
#define BUTTON_FORWARD_PIN PD6 // Change to the appropriate pin
#define BUTTON_REVERSE_PIN PD7 // Change to the appropriate pin
#define BUTTON_STOP_PIN PB0 // Change to the appropriate pin
void initializePorts() {
// Configure motor control pins as outputs
DDRD |= (1 << IN1_PIN) | (1 << IN2_PIN) | (1 << IN3_PIN) | (1 << IN4_PIN);
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;
}