From 9e3787b314e9ccb409902f1263fef8470dbbe605 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 17 Jun 2023 13:30:12 -0400 Subject: [PATCH] Bare minimum working PWM --- flake.nix | 1 + tron-bag/main.c | 54 ++++++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/flake.nix b/flake.nix index 015bec0..4afc50f 100644 --- a/flake.nix +++ b/flake.nix @@ -304,6 +304,7 @@ pbuildInputs = [ (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + pkgs.pkgsCross.avr.buildPackages.simavr ]; }; diff --git a/tron-bag/main.c b/tron-bag/main.c index 14acafc..55b10ab 100644 --- a/tron-bag/main.c +++ b/tron-bag/main.c @@ -3,58 +3,62 @@ #include /* -const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = { +#include +AVR_MCU(F_CPU, "attiny85"); + +const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = { + { AVR_MCU_VCD_SYMBOL("GTCCR"), .what = (void *)>CCR, }, + { AVR_MCU_VCD_SYMBOL("TCCR0A"), .what = (void *)&TCCR0A, }, + { AVR_MCU_VCD_SYMBOL("TCCR0B"), .what = (void *)&TCCR0B, }, { AVR_MCU_VCD_SYMBOL("PORTB"), .what = (void *)&PORTB, }, + { AVR_MCU_VCD_SYMBOL("TCNT0"), .what = (void *)&TCNT0, }, }; */ -#define FPS 100 +#define FPS 10 #define FPS_MS 1000 / FPS #define MULT 5 +/* typedef struct { uint8_t value; int8_t inc; } counter_t; -void step_counter(counter_t *counter) { - int16_t value = counter->value; - value += counter->inc; +void counter_update(counter_t *self) { + int16_t value = self->value; + value += self->inc; if (value >= 255) { - counter->value = 255; - counter->inc = -1; + self->value = 255; + self->inc = -1; } else if (value <= 0) { - counter->value = 0; - counter->inc = 1; + self->value = 0; + self->inc = 1; } else { - counter->value = value; + self->value = value; } } +*/ int main(void) { - dio_t dio0 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; - dio_t dio1 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }; - dio_set_direction(&dio0, LINE_OUT); - dio_set_direction(&dio1, LINE_OUT); + DDRB = 0xff; + PORTB = 0; - // GTCCR = _BV(TSM) | _BV(PSR0); - TCCR0A = _BV(COM0A1) | _BV(COM0A0) | _BV(COM0B1) | _BV(COM0B0) | _BV(WGM01) | _BV(WGM00); - TCCR0B = _BV(WGM02) | _BV(CS00); + TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00); + TCCR0B = _BV(CS00); - counter_t red = (counter_t){ .value = 128, .inc = -1 }; - counter_t blue = (counter_t){ .value = 128, .inc = -1 }; + uint8_t brightness = 1; - // GTCCR &= ~_BV(TSM); + GTCCR = 0; while(1) { - step_counter(&red); - step_counter(&blue); + OCR0A = brightness; + OCR0B = brightness; - OCR0A = red.value; - OCR0B = blue.value; + _delay_ms(100); - _delay_ms(FPS_MS); + brightness += 1; } return 0;