summaryrefslogtreecommitdiff
path: root/ferment.c
blob: aba1916de01cbbac8df9858376c8194bb4354be5 (plain)
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
#define F_CPU 1000000UL
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>

/* port D
 *
 *  4 1- (NMOS, inverted) 
 *  5 1+ (PMOS)
 *  6 2- (NMOS, inverted)
 *  7 2+ (PMOS)
 *
 */

#define D_1_MINUS	(1 << 4)
#define D_1_PLUS	(1 << 5)
#define D_2_MINUS	(1 << 6)
#define D_2_PLUS	(1 << 7)

/* Turns the H-bridge off, putting the output
 * into high impedance mode. */
static void bridge_off(void)
{
	PORTD |= D_1_MINUS | D_2_MINUS;
	PORTD &= ~(D_1_PLUS | D_2_PLUS);
}

/* Turns the H-bridge on, setting 1+ 2- */
static void bridge_on_a(void)
{
	bridge_off();
	_delay_ms(100);
	PORTD |= D_1_PLUS;
	PORTD &= ~D_2_MINUS;
}

/* Turns the H-bridge on, setting 1- 2+ */
static void bridge_on_b(void)
{
	bridge_off();
	_delay_ms(100);
	PORTD |= D_2_PLUS;
	PORTD &= ~D_1_MINUS;
}

/* Initializes the output port needed for the H-bridge */
static void bridge_init(void)
{
	bridge_off();
	DDRD |= D_1_PLUS | D_1_MINUS | D_2_PLUS | D_2_MINUS;
}

int main()
{
	bridge_init();
	/* simple demo, just flips polarity every now and then */
	while (1) {
		bridge_on_a();
		_delay_ms(200);
		bridge_on_b();
		_delay_ms(200);
	}
}