From c4e5eb81869574e886ac7a86920de1af750744d3 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 10 Jun 2026 12:57:14 +0200 Subject: [PATCH] Leonie's flashlight --- src/Makefile | 3 +- src/blink.c | 127 ++++++++++++++++----------------------------------- 2 files changed, 40 insertions(+), 90 deletions(-) diff --git a/src/Makefile b/src/Makefile index ac5ab0c..9510eb8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -27,8 +27,7 @@ CC=avr-gcc -Wall -Wno-parentheses -MMD -std=c99 -O3 \ -fpack-struct \ -fshort-enums \ -mtiny-stack \ - -mint8 \ - -fverbose-asm + -mint8 SN = $(SN_$(PROJ)) CFLAGS = $($*_CFLAGS) $(DEBUG) -I. -DSN="$(SN)" diff --git a/src/blink.c b/src/blink.c index 4e25198..5970d8c 100644 --- a/src/blink.c +++ b/src/blink.c @@ -9,126 +9,77 @@ #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 -#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; -} +uint8_t tick; ISR(TIM0_COMPB_vect) { - SetPORT(LED1_PORT); + PORTB |= 1 << LED2_PORT; } ISR(TIM0_COMPA_vect) { - TIFR0 = Bit(OCF0B); - ClrPORT(LED1_PORT); - OCR0B = MAX_DC + 1 - (uint8_t)(led1_dc >> 24); + PORTB &=~ (1 << LED2_PORT); + TIFR0 = (1<>= 1; - } sei(); sleep_mode(); - led1_dc = fade(led1_dc); + + 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); + } + } + } }