diff options
Diffstat (limited to 'bridge.c')
-rw-r--r-- | bridge.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/bridge.c b/bridge.c new file mode 100644 index 0000000..5f1806b --- /dev/null +++ b/bridge.c @@ -0,0 +1,72 @@ +#include <avr/io.h> +#include <util/delay.h> + +#include "bridge.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) + +enum bridge_state { + BRIDGE_OFF, + BRIDGE_HEATING, + BRIDGE_COOLING +}; + +static enum bridge_state bridge_state; + +/* Turns the H-bridge off, putting the output + * into high impedance mode. */ +void bridge_off(void) +{ + if (bridge_state == BRIDGE_OFF) + return; + + PORTD |= D_1_MINUS | D_2_MINUS; + PORTD &= ~(D_1_PLUS | D_2_PLUS); + _delay_ms(100); + bridge_state = BRIDGE_OFF; +} + +/* Turns the H-bridge on, setting 1+ 2- */ +void bridge_on_heat(void) +{ + if (bridge_state == BRIDGE_HEATING) + return; + + bridge_off(); + bridge_state = BRIDGE_HEATING; + PORTD |= D_1_PLUS; + PORTD &= ~D_2_MINUS; +} + +/* Turns the H-bridge on, setting 1- 2+ */ +void bridge_on_cool(void) +{ + if (bridge_state == BRIDGE_COOLING) + return; + + bridge_off(); + bridge_state = BRIDGE_COOLING; + PORTD |= D_2_PLUS; + PORTD &= ~D_1_MINUS; +} + +/* Initializes the output port needed for the H-bridge */ +void bridge_init(void) +{ + PORTD |= D_1_MINUS | D_2_MINUS; + PORTD &= ~(D_1_PLUS | D_2_PLUS); + DDRD |= D_1_PLUS | D_1_MINUS | D_2_PLUS | D_2_MINUS; + bridge_state = BRIDGE_OFF; +} |