diff --git a/src/Makefile b/src/Makefile index 9510eb8..ac5ab0c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,7 +27,8 @@ CC=avr-gcc -Wall -Wno-parentheses -MMD -std=c99 -O3 \ -fpack-struct \ -fshort-enums \ -mtiny-stack \ - -mint8 + -mint8 \ + -fverbose-asm SN = $(SN_$(PROJ)) CFLAGS = $($*_CFLAGS) $(DEBUG) -I. -DSN="$(SN)" diff --git a/src/blink.c b/src/blink.c index 5970d8c..4e25198 100644 --- a/src/blink.c +++ b/src/blink.c @@ -9,77 +9,126 @@ #include #include +#define Bit(x) (1<<(x)) +#define SetPORT(x) PORTB |= Bit(x) +#define ClrPORT(x) PORTB &=~ Bit(x) +#define GetPORT(x) (PINB & Bit(x)) + #define RES_PORT 0 #define TRIG_PORT 1 #define OUT_PORT 2 #define LED2_PORT 3 #define LED1_PORT 4 -uint8_t tick; +#define START_PORT OUT_PORT + +// 9.6MHz/8/240 = 5kHz +#define MAX_DC 240 +#define DC_EXP 12 + +static uint32_t led1_dc; + +static inline +uint8_t get_dc(uint32_t dc) { + return dc >> 24; +} +static inline +uint32_t set_dc(uint8_t dc) { + return ((uint32_t)dc << 24) - 1; +} +static inline +uint32_t fade(uint32_t dc) +{ + uint32_t diff = dc >> (8*((DC_EXP+4)>>3)); + if (DC_EXP & 4) + diff <<= 8 - (DC_EXP&7); + else + diff >>= DC_EXP & 3; + return dc - diff; +} ISR(TIM0_COMPB_vect) { - PORTB |= 1 << LED2_PORT; + SetPORT(LED1_PORT); } ISR(TIM0_COMPA_vect) { - PORTB &=~ (1 << LED2_PORT); - TIFR0 = (1<> 24); } -void set_dc(uint8_t dc) +static inline +void pulse_led1(uint8_t dc) { - OCR0B = 255 - dc; -} - -uint8_t get_dc() -{ - return 255 - OCR0B; + led1_dc = set_dc(dc); + OCR0B = MAX_DC + 1 - dc; } static inline void init_timer() { - DDRB = (1<>= 1; + } sei(); sleep_mode(); - - if (PINB & (1 << TRIG_PORT)) - PORTB &=~ (1 << LED1_PORT); - else - PORTB |= (1 << LED1_PORT); - - uint8_t dc = get_dc(); - - if (dc == 0 && !(PINB & (1 << OUT_PORT))) - set_dc(255); - - if (tick) { - tick = 0; - n = n - 1; - if (n == 0) { - n = nticks; - if (dc) - set_dc(dc-1); - } - } - + led1_dc = fade(led1_dc); } }