103 lines
1.8 KiB
C
103 lines
1.8 KiB
C
#ifndef _PWM_H
|
|
#define _PWM_H
|
|
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
|
|
static inline void hbridge_init(void) {
|
|
DDRB |= (1 << PB2); // PWM
|
|
DDRB |= (1 << PB0) | (1 << PB1); // H1 L1
|
|
DDRB |= (1 << PB3) | (1 << PB4); // H2 L2
|
|
|
|
DDRD |= (1 << PD1);
|
|
}
|
|
|
|
|
|
static inline void hbridge_set_h1(void) {
|
|
PORTB |= (1 << PB0);
|
|
}
|
|
static inline void hbridge_unset_h1(void) {
|
|
PORTB &= ~(1 << PB0);
|
|
}
|
|
|
|
static inline void hbridge_set_l1(void) {
|
|
PORTB |= (1 << PB1);
|
|
}
|
|
static inline void hbridge_unset_l1(void) {
|
|
PORTB &= ~(1 << PB1);
|
|
}
|
|
|
|
static inline void hbridge_set_h2(void) {
|
|
PORTB |= (1 << PB3);
|
|
}
|
|
static inline void hbridge_unset_h2(void) {
|
|
PORTB &= ~(1 << PB3);
|
|
}
|
|
|
|
static inline void hbridge_set_l2(void) {
|
|
PORTB |= (1 << PB4);
|
|
}
|
|
static inline void hbridge_unset_l2(void) {
|
|
PORTB &= ~(1 << PB4);
|
|
}
|
|
|
|
const double delay_time = 1;
|
|
|
|
static inline void pwm_cycle_backwards(uint8_t speed) {
|
|
PIND |= (1 << PD1);
|
|
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
if((speed & (1 << i)) != 0) {
|
|
hbridge_unset_h1();
|
|
hbridge_set_h2();
|
|
hbridge_set_l1();
|
|
hbridge_unset_l2();
|
|
}
|
|
else {
|
|
hbridge_unset_h1();
|
|
hbridge_unset_h2();
|
|
hbridge_unset_l1();
|
|
hbridge_set_l2();
|
|
}
|
|
for(uint8_t j = 0; j < (1 << i); j++) {
|
|
_delay_us(delay_time);
|
|
}
|
|
}
|
|
|
|
hbridge_unset_h1();
|
|
hbridge_unset_h2();
|
|
hbridge_unset_l1();
|
|
hbridge_unset_l2();
|
|
}
|
|
|
|
|
|
static inline void pwm_cycle_forwards(uint8_t speed) {
|
|
PIND |= (1 << PD1);
|
|
|
|
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
if((speed & (1 << i)) != 0) {
|
|
hbridge_set_h1();
|
|
hbridge_unset_h2();
|
|
hbridge_unset_l1();
|
|
hbridge_set_l2();
|
|
}
|
|
else {
|
|
hbridge_unset_h1();
|
|
hbridge_unset_h2();
|
|
hbridge_set_l1();
|
|
hbridge_unset_l2();
|
|
}
|
|
for(uint8_t j = 0; j < (1 << i); j++) {
|
|
_delay_us(delay_time);
|
|
}
|
|
}
|
|
|
|
hbridge_unset_h1();
|
|
hbridge_unset_h2();
|
|
hbridge_unset_l1();
|
|
hbridge_unset_l2();
|
|
}
|
|
|
|
#endif
|