Cleaned up pwm

This commit is contained in:
Sebastian 2020-07-29 19:07:03 +02:00
parent 7505d280e2
commit 5285de0767
3 changed files with 87 additions and 67 deletions

View File

@ -1,33 +1,49 @@
#include "input.h"
enum Edge {
RISING = 0,
FALLING = 1,
};
uint16_t volatile last_count;
enum Edge volatile next_edge;
void input_init() {
MCUCR |= (1 << ISC10);
EIFR |= (1 << INTF1);
MCUCR |= (1 << ISC10) | (1 << ISC11);
GIMSK |= (1 << INT1);
next_edge = RISING;
last_count = 0;
}
ISR(INT1_vect) {
if((PORTB & (1 << PD3)) != 0) {
TCCR1B |= (1 << CS11); //Prescaler 8, enable Timer
if(next_edge == RISING) {
TCNT1 = 0;
} else {
TCCR1B &= ~(1 << CS11); //Prescaler 8, disable Timer
TCCR1B |= (1 << CS11); //Prescaler 8, enable Timer
MCUCR &= ~(1 << ISC10); //Trigger on falling edge next
next_edge = FALLING;
} else if(next_edge == FALLING) {
last_count = TCNT1;
TCCR1B &= ~(1 << CS11); //Prescaler 8, disable Timer
MCUCR |= (1 << ISC10); //Trigger on falling edge next
next_edge = RISING;
}
}
int16_t input_get_result() {
uint16_t tmp = last_count;
if(tmp < 1000) {
tmp = 0;
int16_t tmp = last_count - 1500;
if(tmp < -500) {
tmp = -500;
}
tmp = (tmp - 1000);
if(tmp > 1000) {
tmp = 1000;
if(tmp > 500) {
tmp = 500;
}
return ((int16_t)tmp) / 2 - 1000;
return tmp;
}

View File

@ -4,53 +4,34 @@
#include "pwm.h"
#include "input.h"
int main(void) {
_delay_ms(5000);
hbridge_init();
input_init();
sei();
DDRD |= (1 << PD1);
while(1) {
for(uint8_t speed = 50; speed < 255; speed++) {
for(uint16_t i = 0; i < 1000; i++) {
pwm_cycle_forwards(speed);
}
int16_t speed = input_get_result();
if(speed > 20) {
pwm_cycle_forwards(speed / 2);
} else if (speed < -20){
pwm_cycle_backwards(-speed / 2);
} else {
pwm_cycle_coast();
}
for(uint16_t i = 0; i < 10000; i++) {
pwm_cycle_forwards(254);
}
PIND |= (1 << PD1);
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 = 50; speed < 255; speed++) {
for(uint16_t i = 0; i < 1000; i++) {
pwm_cycle_backwards(speed);
}
}
for(uint16_t i = 0; i < 10000; i++) {
pwm_cycle_backwards(254);
}
hbridge_unset_h2();
hbridge_unset_h2();
hbridge_set_l1();
hbridge_set_l2();
_delay_ms(1000);
hbridge_unset_l2();
hbridge_unset_l2();
}

View File

@ -8,8 +8,6 @@ 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);
}
@ -43,16 +41,24 @@ static inline void hbridge_unset_l2(void) {
const double delay_time = 0.25;
static inline void pwm_delay(uint16_t slices) {
for(uint16_t i = 0; i < slices; i++) {
_delay_us(10);
}
}
static inline void pwm_cycle_backwards(uint8_t speed) {
if(speed > 0) {
uint16_t on_time = speed;
//Always have a a least on slice of off time to charge the bootstrap cap
uint16_t off_time = 256 - on_time;
if(on_time > 0) {
hbridge_unset_h1();
hbridge_set_h2();
hbridge_set_l1();
hbridge_unset_l2();
for(uint8_t i = 0; i < speed; i++) {
_delay_us(delay_time);
}
pwm_delay(on_time);
}
@ -61,10 +67,7 @@ static inline void pwm_cycle_backwards(uint8_t speed) {
hbridge_unset_l1();
hbridge_set_l2();
// Always have one cycle l2 low to recharge the bootstrap cap
for(uint8_t i = 0; i < (255 - speed) + 1; i++) {
_delay_us(delay_time);
}
pwm_delay(off_time);
hbridge_unset_h1();
hbridge_unset_h2();
@ -74,15 +77,17 @@ static inline void pwm_cycle_backwards(uint8_t speed) {
static inline void pwm_cycle_forwards(uint8_t speed) {
if(speed > 0) {
uint16_t on_time = speed;
//Always have a a least on slice of off time to charge the bootstrap cap
uint16_t off_time = 256 - on_time;
if(on_time > 0) {
hbridge_set_h1();
hbridge_unset_h2();
hbridge_unset_l1();
hbridge_set_l2();
for(uint8_t i = 0; i < speed; i++) {
_delay_us(delay_time);
}
pwm_delay(on_time);
}
@ -91,10 +96,28 @@ static inline void pwm_cycle_forwards(uint8_t speed) {
hbridge_set_l1();
hbridge_unset_l2();
// Always have one cycle l1 low to recharge the bootstrap cap
for(uint8_t i = 0; i < (255 - speed) + 1; i++) {
_delay_us(delay_time);
}
pwm_delay(off_time);
hbridge_unset_h1();
hbridge_unset_h2();
hbridge_unset_l1();
hbridge_unset_l2();
}
static inline void pwm_cycle_coast() {
hbridge_unset_h1();
hbridge_unset_h2();
hbridge_set_l1();
hbridge_unset_l2();
pwm_delay(128);
hbridge_unset_h1();
hbridge_unset_h2();
hbridge_unset_l1();
hbridge_set_l2();
pwm_delay(128);
hbridge_unset_h1();
hbridge_unset_h2();