This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-2.c
109 lines (83 loc) · 1.49 KB
/
task-2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Jacob Koziej
* H-Bridge Activity (Task 2)
* 2020-10-08
* 2020-10-12
*/
/*
* PIN MAPPING
*
* Q1 = PORTB1/OC1A
* Q2 = PORTB2/OC1B
*/
#include <avr/io.h>
#include <util/delay.h>
// Constants
#define DELAY 2000
// Function prototypes
void setup(void);
void CW(uint8_t);
void CCW(uint8_t);
void brake(void);
void coast(void);
// BEGIN
int main(void)
{
setup();
while (1) {
CW(128);
_delay_ms(DELAY);
coast();
_delay_ms(DELAY);
CCW(128);
_delay_ms(DELAY);
brake();
_delay_ms(DELAY);
}
return 0;
}
// Function declarations
void setup(void)
{
// Set outputs
DDRB = _BV(1) | _BV(2);
// 8-bit Fast PWM for OCA1A:B w/o prescaler
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10);
TCCR1B = _BV(WGM12) | _BV(CS10);
// Set everything high
PORTB = OCR1A = OCR1B = 0xFF;
}
void CW(uint8_t speed)
{
// Disable PWM for OCA1A
if (bit_is_set(TCCR1A, COM1A1))
TCCR1A &= ~_BV(COM1A1);
// Enable PWM for OCA1B
if (bit_is_clear(TCCR1A, COM1B1))
TCCR1A |= _BV(COM1B1);
PORTB |= _BV(1);
OCR1B = speed;
}
void CCW(uint8_t speed)
{
// Disable PWM for OCA1B
if (bit_is_set(TCCR1A, COM1B1))
TCCR1A &= ~_BV(COM1B1);
// Enable PWM for OCA1A
if (bit_is_clear(TCCR1A, COM1A1))
TCCR1A |= _BV(COM1A1);
PORTB |= _BV(2);
OCR1A = speed;
}
void brake(void)
{
// Disable PWM for OCA1A:B
TCCR1A &= ~(_BV(COM1A1) | _BV(COM1B1));
PORTB &= ~(_BV(1) | _BV(2));
}
void coast(void)
{
// Disable PWM for OCA1A:B
TCCR1A &= ~(_BV(COM1A1) | _BV(COM1B1));
PORTB |= _BV(1) | _BV(2);
}