Add and use wait_ms(); fix some timings

This commit is contained in:
Thammi 2013-08-29 19:14:30 +02:00
parent 4c65d04c3e
commit e09eeb7cc2
5 changed files with 46 additions and 25 deletions

View File

@ -15,7 +15,7 @@ static void init(void) {
static void blinker(void) { static void blinker(void) {
led_inv(RIGHT); led_inv(RIGHT);
led_inv(LEFT); led_inv(LEFT);
_delay_ms(500); wait_ms(500);
} }
REGISTER(blinker, init, NULL); REGISTER(blinker, init, NULL);

View File

@ -24,16 +24,16 @@ static void run(void) {
size_t i; size_t i;
for(i = 0; i < sizeof(notes) / sizeof(*notes); ++i) { for(i = 0; i < sizeof(notes) / sizeof(*notes); ++i) {
if(notes[i] == NOTE_BREAK) { if(notes[i] == NOTE_BREAK) {
_delay_ms(120); wait_ms(180);
} else if(notes[i] == NOTE_SHORT) { } else if(notes[i] == NOTE_SHORT) {
_delay_ms(20); wait_ms(30);
} else { } else {
set_note(notes[i], 4); set_note(notes[i], 4);
_delay_ms(180); wait_ms(500);
test_stop_app(); test_stop_app();
stop_note(); stop_note();
_delay_ms(10); wait_ms(10);
test_stop_app(); test_stop_app();
} }
} }

View File

@ -1,15 +0,0 @@
#include <stdlib.h>
#include <avr/io.h>
#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>
#include <pentabug/app.h>
#include <pentabug/lifecycle.h>
static void tester(void) {
PORTD ^= 1 << 4;
_delay_ms(100);
}
REG(tester);

View File

@ -30,7 +30,11 @@ void led_inv(uint8_t led);
// MOTOR // MOTOR
void motor_on(uint8_t led); void motor_on(void);
void motor_off(uint8_t led); void motor_off(void);
// WAITING
void wait_ms(uint16_t ms);
#endif /* HAL_H */ #endif /* HAL_H */

View File

@ -9,8 +9,9 @@
#include <pentabug/timer.h> #include <pentabug/timer.h>
static uint8_t ir_active = 0; static volatile uint8_t ir_active = 0;
static int int_skip = 0; static int int_skip = 0;
static volatile int16_t wait_time = 0;
static int button_count[2]; static int button_count[2];
@ -25,7 +26,8 @@ inline static void major_interrupt(void) {
++button_count[i]; ++button_count[i];
} }
if(button_count[i] == 400) { // 1s pressed
if(button_count[i] == 200) {
next_app(i ? 1 : -1); next_app(i ? 1 : -1);
PORTC ^= 1 << 2; PORTC ^= 1 << 2;
} }
@ -39,10 +41,12 @@ ISR(TIMER0_COMPA_vect) {
++int_skip; ++int_skip;
if(int_skip >= 38 * 5) { if(int_skip >= 64 * 5) {
int_skip = 0; int_skip = 0;
major_interrupt(); major_interrupt();
} }
--wait_time;
} }
void init_hw(void) { void init_hw(void) {
@ -109,3 +113,31 @@ void led_inv(uint8_t led) {
} }
} }
void motor_on(void) {
PORTB |= 1 << 6;
}
void motor_off(void) {
PORTB &= ~(1 << 6);
}
void wait_ms(uint16_t ms) {
// TODO: this function seems to be ~10% too fast
int32_t cycles = ms * (int32_t)64;
while(cycles >= INT16_MAX) {
cycles -= INT16_MAX;
wait_time = INT16_MAX;
while(wait_time > 0) {
test_stop_app();
}
}
wait_time = cycles;
while(wait_time > 0) {
test_stop_app();
}
}