Bare minimum working PWM

This commit is contained in:
Savanni D'Gerinel 2023-06-17 13:30:12 -04:00
parent ef1c0c5834
commit 9e3787b314
2 changed files with 30 additions and 25 deletions

View File

@ -304,6 +304,7 @@
pbuildInputs = [ pbuildInputs = [
(packages."x86_64-linux"."dio" { inherit gcc cflags; }) (packages."x86_64-linux"."dio" { inherit gcc cflags; })
pkgs.pkgsCross.avr.buildPackages.simavr
]; ];
}; };

View File

@ -3,58 +3,62 @@
#include <dio.h> #include <dio.h>
/* /*
const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = { #include <simavr/avr/avr_mcu_section.h>
AVR_MCU(F_CPU, "attiny85");
const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = {
{ AVR_MCU_VCD_SYMBOL("GTCCR"), .what = (void *)&GTCCR, },
{ 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("PORTB"), .what = (void *)&PORTB, },
{ AVR_MCU_VCD_SYMBOL("TCNT0"), .what = (void *)&TCNT0, },
}; };
*/ */
#define FPS 100 #define FPS 10
#define FPS_MS 1000 / FPS #define FPS_MS 1000 / FPS
#define MULT 5 #define MULT 5
/*
typedef struct { typedef struct {
uint8_t value; uint8_t value;
int8_t inc; int8_t inc;
} counter_t; } counter_t;
void step_counter(counter_t *counter) { void counter_update(counter_t *self) {
int16_t value = counter->value; int16_t value = self->value;
value += counter->inc; value += self->inc;
if (value >= 255) { if (value >= 255) {
counter->value = 255; self->value = 255;
counter->inc = -1; self->inc = -1;
} else if (value <= 0) { } else if (value <= 0) {
counter->value = 0; self->value = 0;
counter->inc = 1; self->inc = 1;
} else { } else {
counter->value = value; self->value = value;
} }
} }
*/
int main(void) { int main(void) {
dio_t dio0 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 0 }; DDRB = 0xff;
dio_t dio1 = (dio_t){ .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 }; PORTB = 0;
dio_set_direction(&dio0, LINE_OUT);
dio_set_direction(&dio1, LINE_OUT);
// GTCCR = _BV(TSM) | _BV(PSR0); TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0A = _BV(COM0A1) | _BV(COM0A0) | _BV(COM0B1) | _BV(COM0B0) | _BV(WGM01) | _BV(WGM00); TCCR0B = _BV(CS00);
TCCR0B = _BV(WGM02) | _BV(CS00);
counter_t red = (counter_t){ .value = 128, .inc = -1 }; uint8_t brightness = 1;
counter_t blue = (counter_t){ .value = 128, .inc = -1 };
// GTCCR &= ~_BV(TSM); GTCCR = 0;
while(1) { while(1) {
step_counter(&red); OCR0A = brightness;
step_counter(&blue); OCR0B = brightness;
OCR0A = red.value; _delay_ms(100);
OCR0B = blue.value;
_delay_ms(FPS_MS); brightness += 1;
} }
return 0; return 0;