133 lines
2.2 KiB
C
133 lines
2.2 KiB
C
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
|
|
static inline void hbridge_init() {
|
|
DDRB |= (1 << PB2); // PWM
|
|
DDRB |= (1 << PB0) | (1 << PB1); // H1 L1
|
|
DDRB |= (1 << PB3) | (1 << PB4); // H2 L2
|
|
}
|
|
|
|
|
|
static inline void hbridge_set_h1() {
|
|
PORTB |= (1 << PB0);
|
|
}
|
|
static inline void hbridge_unset_h1() {
|
|
PORTB &= ~(1 << PB0);
|
|
}
|
|
|
|
static inline void hbridge_set_l1() {
|
|
PORTB |= (1 << PB1);
|
|
}
|
|
static inline void hbridge_unset_l1() {
|
|
PORTB &= ~(1 << PB1);
|
|
}
|
|
|
|
static inline void hbridge_set_h2() {
|
|
PORTB |= (1 << PB3);
|
|
}
|
|
static inline void hbridge_unset_h2() {
|
|
PORTB &= ~(1 << PB3);
|
|
}
|
|
|
|
static inline void hbridge_set_l2() {
|
|
PORTB |= (1 << PB4);
|
|
}
|
|
static inline void hbridge_unset_l2() {
|
|
PORTB &= ~(1 << PB4);
|
|
}
|
|
|
|
|
|
static inline void pwm_cycle_backwards(uint8_t speed) {
|
|
hbridge_unset_l2();
|
|
hbridge_set_l1();
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
if((speed & (1 << i)) != 0) {
|
|
hbridge_set_h2();
|
|
}
|
|
for(uint8_t j = 0; j < (1 << i); j++) {
|
|
_delay_us(2);
|
|
}
|
|
hbridge_unset_h2();
|
|
}
|
|
}
|
|
|
|
|
|
static inline void pwm_cycle_forwards(uint8_t speed) {
|
|
hbridge_unset_l1();
|
|
hbridge_set_l2();
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
if((speed & (1 << i)) != 0) {
|
|
hbridge_set_h1();
|
|
}
|
|
for(uint8_t j = 0; j < (1 << i); j++) {
|
|
_delay_us(2);
|
|
}
|
|
hbridge_unset_h1();
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
|
|
|
|
_delay_ms(5000);
|
|
|
|
hbridge_init();
|
|
|
|
|
|
while(1) {
|
|
//hbridge_set_l1();
|
|
|
|
for(uint8_t speed = 20; speed <= 230; speed++) {
|
|
for(uint16_t i = 0; i < 250; i++) {
|
|
pwm_cycle_forwards(speed);
|
|
}
|
|
}
|
|
|
|
hbridge_unset_h2();
|
|
hbridge_unset_h2();
|
|
hbridge_set_l1();
|
|
hbridge_set_l2();
|
|
_delay_ms(1000);
|
|
hbridge_unset_l2();
|
|
hbridge_unset_l2();
|
|
|
|
for(uint8_t speed = 20; speed <= 230; speed++) {
|
|
for(uint16_t i = 0; i < 250; i++) {
|
|
pwm_cycle_backwards(speed);
|
|
}
|
|
}
|
|
|
|
hbridge_unset_h2();
|
|
hbridge_unset_h2();
|
|
hbridge_set_l1();
|
|
hbridge_set_l2();
|
|
_delay_ms(1000);
|
|
hbridge_unset_l2();
|
|
hbridge_unset_l2();
|
|
|
|
/*
|
|
|
|
for(uint16_t i = 0; i < 5000; i++) {
|
|
hbridge_set_h2();
|
|
_delay_ms(0.9);
|
|
hbridge_unset_h2();
|
|
_delay_ms(0.1);
|
|
}
|
|
|
|
hbridge_set_l2();
|
|
_delay_ms(100);
|
|
hbridge_unset_l1();
|
|
|
|
|
|
for(uint16_t i = 0; i < 5000; i++) {
|
|
hbridge_set_h1();
|
|
_delay_ms(0.9);
|
|
hbridge_unset_h1();
|
|
_delay_ms(0.1);
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
}
|