#define F_CPU 1000000
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
const uint8_t sequences[] = {
0b00000001,
0b00000011,
0b00000110,
0b00001100,
0b00011000,
0b00110000,
0b00100000,
0b00110000,
0b00011000,
0b00001100,
0b00000110,
0b00000011,
0b00000001,
};
const uint8_t totalSequences = sizeof(sequences) / sizeof(sequences[0]);
int main(void) {
DDRB = 0b00111111;
uint8_t i;
uint8_t b;
while(1) {
for(i = 0; i < totalSequences; i++) {
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
// blend between sequences
for(b = 0; b < 100; b++) {
PORTB = currentSequence; _delay_us(b);
PORTB = nextSequence; _delay_us(100-b);
}
_delay_ms(50);
}
}
return 0;
}
This is the entirety of my program. When I was setting PORTB directly (without the blend) like below, the compiled binary is 214 bytes. When I include the second for loop, my compiled binary is over 4kb. I only have 1kb of FLASH available so I need to fit this in there.
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
//// blend between sequences
//for(b = 0; b < 100; b++) {
// PORTB = currentSequence; _delay_us(b);
// PORTB = nextSequence; _delay_us(100-b);
//}
PORTB = currentSequence;
PORTB = nextSequence;
_delay_ms(50);
My toolchain is WINAVR, compiled with the following:
avr-gcc -Os -mmcu=attiny13 -Wall -std=c99 main.c -o main.out
avr-objcopy -O binary main.out main.bin
I don't know enough to decompile the binary and see what the compiler did, but whatever it was, it was wrong. Why is the binary with the inner loop 4kb, and how I can fix it?
Revised loop (278 bytes):
while(1) {
for(i = 0; i < totalSequences; i++) {
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
// blend between sequences
for(b = 0; b < 100; b++) {
int d;
PORTB = currentSequence; for(d = 0; d < b; d++, _delay_us(1));
PORTB = nextSequence; for(d = 100; b < d; d--, _delay_us(1));
}
_delay_ms(50);
}
}
The delay() function blows up the code size unless the parameter is a compile time constant. It also won't delay for the correct time with a variable parameter.