From c4e5eb81869574e886ac7a86920de1af750744d3 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 10 Jun 2026 12:57:14 +0200 Subject: [PATCH 1/5] 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); + } + } + } } From 1465947402a2a4a86aa77c5c34b65a166e135d23 Mon Sep 17 00:00:00 2001 From: jonatan Date: Mon, 15 Jun 2026 14:42:18 +0200 Subject: [PATCH 2/5] copy leonie's blink.c to ntc.c --- src/ntc.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/ntc.c diff --git a/src/ntc.c b/src/ntc.c new file mode 100644 index 0000000..5970d8c --- /dev/null +++ b/src/ntc.c @@ -0,0 +1,85 @@ +// +// blink.c +// + +// !!! int = int8_t + +#include +#include +#include +#include + +#define RES_PORT 0 +#define TRIG_PORT 1 +#define OUT_PORT 2 +#define LED2_PORT 3 +#define LED1_PORT 4 + +uint8_t tick; + +ISR(TIM0_COMPB_vect) +{ + PORTB |= 1 << LED2_PORT; +} + +ISR(TIM0_COMPA_vect) +{ + PORTB &=~ (1 << LED2_PORT); + TIFR0 = (1< Date: Tue, 16 Jun 2026 11:23:33 +0200 Subject: [PATCH 3/5] new PROJ=ntc LED1 brightness controlled by an NTC on pin LED2 --- src/Makefile | 6 ++++- src/ntc.c | 73 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/Makefile b/src/Makefile index 9510eb8..423fed6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ -PROJ=blink +PROJ=ntc default: all all: $(PROJ).hex @@ -7,6 +7,9 @@ all: $(PROJ).hex SN_blink = 1 MCU_blink = attiny13a +SN_ntc = 1 +MCU_ntc = attiny13a + SN_kennung = 1 MCU_kennung = attiny13a @@ -69,6 +72,7 @@ OBJCOPY = avr-objcopy # [1:0] CKSEL = 10 (9.6MHz internal oscillator) lfuse_blink = 0x7a +lfuse_ntc = 0x7a # [6]: EESAVE = 0 ! (EEPROM not preserved @ chip erase) # [5]: WDTON = 0 ! (watchdog disabled) diff --git a/src/ntc.c b/src/ntc.c index 5970d8c..ed026b9 100644 --- a/src/ntc.c +++ b/src/ntc.c @@ -15,16 +15,58 @@ #define LED2_PORT 3 #define LED1_PORT 4 +#define NTC_PORT LED2_PORT +#define NTC_ADC 3 + +static inline +void init_adc() +{ + ADMUX = 0x23; + DIDR0 |= 1 << NTC_PORT; + // ADCSRB = 0x00; + ADCSRA = 0xff; +} + +uint16_t ntc; + +uint16_t t_min = 0x8618; + +void set_dc(uint8_t dc); + +ISR(ADC_vect) +{ + uint16_t a = ADCL; + a |= (uint16_t)ADCH << 8; +#if 0 + ntc = ((uint32_t)ntc * 255 + a) / 256; +#else + uint32_t a1 = (uint32_t)ntc << 8; + a1 -= ntc; + a1 += a; + ntc = a1 >> 8; +#endif + uint16_t dc; + if (t_min < ntc) + dc = 0; + else + dc = t_min - ntc; + dc >>= 6; + if (dc > 0xff) + dc = 0xff; + set_dc(dc); + ADCSRA = 0xff; +} + uint8_t tick; ISR(TIM0_COMPB_vect) { - PORTB |= 1 << LED2_PORT; + PORTB |= 1 << LED1_PORT; } ISR(TIM0_COMPA_vect) { - PORTB &=~ (1 << LED2_PORT); + PORTB &=~ (1 << LED1_PORT); TIFR0 = (1< Date: Wed, 17 Jun 2026 19:10:31 +0200 Subject: [PATCH 4/5] ntc logtable --- src/Makefile | 3 +++ src/logtable.c | 38 ++++++++++++++++++++++++++++++++++++++ src/logtable.py | 17 +++++++++++++++++ src/ntc.c | 10 ++++++---- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/logtable.c create mode 100755 src/logtable.py diff --git a/src/Makefile b/src/Makefile index 423fed6..99a2c59 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,6 +9,9 @@ MCU_blink = attiny13a SN_ntc = 1 MCU_ntc = attiny13a +logtable.c: ./logtable.py + ./$< >$@ +C_FILES_ntc = logtable.c SN_kennung = 1 MCU_kennung = attiny13a diff --git a/src/logtable.c b/src/logtable.c new file mode 100644 index 0000000..f7e5394 --- /dev/null +++ b/src/logtable.c @@ -0,0 +1,38 @@ + +/* generated by logtable.py */ +#include +const uint8_t log_table[] = { + + 0, // a+1=1 log2=0.000 dc=0 + 51, // a+1=2 log2=1.000 dc=51 + 80, // a+1=3 log2=1.585 dc=80 + 102, // a+1=4 log2=2.000 dc=102 + 118, // a+1=5 log2=2.322 dc=118 + 131, // a+1=6 log2=2.585 dc=131 + 143, // a+1=7 log2=2.807 dc=143 + 153, // a+1=8 log2=3.000 dc=153 + 161, // a+1=9 log2=3.170 dc=161 + 169, // a+1=10 log2=3.322 dc=169 + 176, // a+1=11 log2=3.459 dc=176 + 182, // a+1=12 log2=3.585 dc=182 + 188, // a+1=13 log2=3.700 dc=188 + 194, // a+1=14 log2=3.807 dc=194 + 199, // a+1=15 log2=3.907 dc=199 + 204, // a+1=16 log2=4.000 dc=204 + 208, // a+1=17 log2=4.087 dc=208 + 212, // a+1=18 log2=4.170 dc=212 + 216, // a+1=19 log2=4.248 dc=216 + 220, // a+1=20 log2=4.322 dc=220 + 224, // a+1=21 log2=4.392 dc=224 + 227, // a+1=22 log2=4.459 dc=227 + 230, // a+1=23 log2=4.524 dc=230 + 233, // a+1=24 log2=4.585 dc=233 + 236, // a+1=25 log2=4.644 dc=236 + 239, // a+1=26 log2=4.700 dc=239 + 242, // a+1=27 log2=4.755 dc=242 + 245, // a+1=28 log2=4.807 dc=245 + 247, // a+1=29 log2=4.858 dc=247 + 250, // a+1=30 log2=4.907 dc=250 + 252, // a+1=31 log2=4.954 dc=252 + 255, // a+1=32 log2=5.000 dc=255 +}; diff --git a/src/logtable.py b/src/logtable.py new file mode 100755 index 0000000..64b8228 --- /dev/null +++ b/src/logtable.py @@ -0,0 +1,17 @@ +#! /usr/bin/python3 + +from math import log + +print(""" +/* generated by logtable.py */ +#include +const uint8_t log_table[] = { +""") + +for a in range(32): + log2 = log(a+1)/log(2) + dc = int(255/5 * log2) + print(f" {dc}, // {a+1=} {log2=:.3f} {dc=}") + + +print("};") diff --git a/src/ntc.c b/src/ntc.c index ed026b9..26bb8e1 100644 --- a/src/ntc.c +++ b/src/ntc.c @@ -9,6 +9,8 @@ #include #include +extern const uint8_t log_table[]; + #define RES_PORT 0 #define TRIG_PORT 1 #define OUT_PORT 2 @@ -50,10 +52,10 @@ ISR(ADC_vect) dc = 0; else dc = t_min - ntc; - dc >>= 6; - if (dc > 0xff) - dc = 0xff; - set_dc(dc); + dc >>= 9; + if (dc > 0x1f) + dc = 0x1f; + set_dc(log_table[dc]); ADCSRA = 0xff; } From ce09f610cedb568a3df10e8b4b9d0b3bfdfc7f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20I=2E=20B=C3=B6ttcher?= Date: Thu, 18 Jun 2026 15:12:53 +0200 Subject: [PATCH 5/5] ntc: use exp_table[] for LED brightness --- src/.gitignore | 2 ++ src/Makefile | 3 ++- src/exptable.py | 14 ++++++++++++++ src/logtable.c | 38 -------------------------------------- src/ntc.c | 40 +++++++++++++++++++++++++++++++++------- 5 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 src/.gitignore create mode 100755 src/exptable.py delete mode 100644 src/logtable.c diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..2dc174a --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,2 @@ +logtable.c +exptable.c diff --git a/src/Makefile b/src/Makefile index 99a2c59..50ef56e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,8 +10,9 @@ MCU_blink = attiny13a SN_ntc = 1 MCU_ntc = attiny13a logtable.c: ./logtable.py +exptable.c: ./exptable.py ./$< >$@ -C_FILES_ntc = logtable.c +C_FILES_ntc = exptable.c SN_kennung = 1 MCU_kennung = attiny13a diff --git a/src/exptable.py b/src/exptable.py new file mode 100755 index 0000000..e65b97e --- /dev/null +++ b/src/exptable.py @@ -0,0 +1,14 @@ +#! /usr/bin/python3 + +print(""" +/* generated by exptable.py */ +#include +const uint8_t exp_table[] __attribute__((section(".text"))) = { +""") + +for a in range(256): + aa = 2 ** ((a+36) / (256+35) * 8) + dc = int(aa)-1 + print(f" {dc}, // {a=} {aa=:.1f} {dc=}") + +print("};") diff --git a/src/logtable.c b/src/logtable.c deleted file mode 100644 index f7e5394..0000000 --- a/src/logtable.c +++ /dev/null @@ -1,38 +0,0 @@ - -/* generated by logtable.py */ -#include -const uint8_t log_table[] = { - - 0, // a+1=1 log2=0.000 dc=0 - 51, // a+1=2 log2=1.000 dc=51 - 80, // a+1=3 log2=1.585 dc=80 - 102, // a+1=4 log2=2.000 dc=102 - 118, // a+1=5 log2=2.322 dc=118 - 131, // a+1=6 log2=2.585 dc=131 - 143, // a+1=7 log2=2.807 dc=143 - 153, // a+1=8 log2=3.000 dc=153 - 161, // a+1=9 log2=3.170 dc=161 - 169, // a+1=10 log2=3.322 dc=169 - 176, // a+1=11 log2=3.459 dc=176 - 182, // a+1=12 log2=3.585 dc=182 - 188, // a+1=13 log2=3.700 dc=188 - 194, // a+1=14 log2=3.807 dc=194 - 199, // a+1=15 log2=3.907 dc=199 - 204, // a+1=16 log2=4.000 dc=204 - 208, // a+1=17 log2=4.087 dc=208 - 212, // a+1=18 log2=4.170 dc=212 - 216, // a+1=19 log2=4.248 dc=216 - 220, // a+1=20 log2=4.322 dc=220 - 224, // a+1=21 log2=4.392 dc=224 - 227, // a+1=22 log2=4.459 dc=227 - 230, // a+1=23 log2=4.524 dc=230 - 233, // a+1=24 log2=4.585 dc=233 - 236, // a+1=25 log2=4.644 dc=236 - 239, // a+1=26 log2=4.700 dc=239 - 242, // a+1=27 log2=4.755 dc=242 - 245, // a+1=28 log2=4.807 dc=245 - 247, // a+1=29 log2=4.858 dc=247 - 250, // a+1=30 log2=4.907 dc=250 - 252, // a+1=31 log2=4.954 dc=252 - 255, // a+1=32 log2=5.000 dc=255 -}; diff --git a/src/ntc.c b/src/ntc.c index 26bb8e1..95d0ad6 100644 --- a/src/ntc.c +++ b/src/ntc.c @@ -9,7 +9,31 @@ #include #include -extern const uint8_t log_table[]; +/* y = exp(x) = exp_table[x] + * exp_table[] is stored in flash (section .text) + * To read from flash we need the opcode LPM "Load Program Memory" + * exp8() is a C wrapper for that asm opcode. + * + * avr-gcc copies .rodata to RAM, to allow direct variable access. + * ATtiny13a has only 64 Bytes of RAM. A table of size 256 Bytes + * needs to go into flash. + * + * The linker script may provide for a data section in flash, but here + * we just drop the table into the .text section. + */ + +extern const uint8_t exp_table[256] __attribute__((section(".text"))); + +static inline +uint8_t exp8(uint8_t x) +{ + uint8_t r; + __asm__(" lpm %[R], Z" + : [R] "=d" (r) + : [Z] "z" (exp_table+x) + ); + return r; +} #define RES_PORT 0 #define TRIG_PORT 1 @@ -50,12 +74,14 @@ ISR(ADC_vect) uint16_t dc; if (t_min < ntc) dc = 0; - else - dc = t_min - ntc; - dc >>= 9; - if (dc > 0x1f) - dc = 0x1f; - set_dc(log_table[dc]); + else { + dc = (t_min - ntc) >> 6; + if (dc > 0xff) + dc = 0xff; + else + dc = exp8(dc); + } + set_dc(dc); ADCSRA = 0xff; }