avr/tron-bag/main.c

53 lines
989 B
C
Raw Normal View History

2023-06-17 15:52:27 +00:00
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <dio.h>
2023-06-17 17:30:12 +00:00
#define FPS 10
2023-06-17 15:52:27 +00:00
#define FPS_MS 1000 / FPS
#define MULT 5
typedef struct {
uint8_t value;
int8_t inc;
} counter_t;
2023-06-17 17:30:12 +00:00
void counter_update(counter_t *self) {
int16_t value = self->value;
value += self->inc;
2023-06-17 15:52:27 +00:00
if (value >= 255) {
2023-06-17 17:30:12 +00:00
self->value = 255;
self->inc = -1;
2023-06-17 15:52:27 +00:00
} else if (value <= 0) {
2023-06-17 17:30:12 +00:00
self->value = 0;
self->inc = 1;
2023-06-17 15:52:27 +00:00
} else {
2023-06-17 17:30:12 +00:00
self->value = value;
2023-06-17 15:52:27 +00:00
}
}
int main(void) {
2023-06-17 17:30:12 +00:00
DDRB = 0xff;
PORTB = 0;
2023-06-17 15:52:27 +00:00
GTCCR = 0;
2023-06-17 17:30:12 +00:00
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = _BV(CS00);
2023-06-17 15:52:27 +00:00
counter_t red = (counter_t){ .value = 128, .inc = 1 };
counter_t blue = (counter_t){ .value = 0, .inc = 1 };
2023-06-17 15:52:27 +00:00
while(1) {
OCR0A = red.value;
OCR0B = blue.value;
2023-06-17 15:52:27 +00:00
_delay_ms(10);
2023-06-17 15:52:27 +00:00
counter_update(&red);
counter_update(&blue);
2023-06-17 15:52:27 +00:00
}
return 0;
}