#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define DDR DDRB
#define NBIT PB0
#define BYTES (NUM_LEDS*3) // ogni led ha 3 colori
#define NUM_LEDS 32
#define PORT PORTB
#define LEDPIN PORTB0 // pin 8
uint8_t* leds = NULL;
void setup() {
DDRB = DDRB | B00000001; // imposta il pin 8 come output
leds = (uint8_t*) malloc(BYTES); // alloco area di memoria
memset(leds, 0, BYTES); // azzero l'area di memoria
}
void loop()
{
//cambia colore e luminosità
aggiornaLed();
}
uint8_t vp[NUM_LEDS];
void aggiornaLed()
{
for(int i= 0; i< BYTES -2; i += 3)
{
vp[i] = 0; //0 verde, 1 rosso, 2 blu
vp[i+ 1] = 255;
vp[i+ 2] = 0;
}
pix_show(NUM_LEDS*3, vp);
}
volatile void pix_show(uint8_t nb, uint8_t *vp) {
sbi(DDR, NBIT); // output
cbi(PORT, NBIT);
cli();
asm volatile (
"lwhile0:\n\t"
"ld r24, %a[vp]+ \n\t"
"ldi r25, 8 \n\t"
"lwhile:\n\t"
"nop \n\t"
"sbi %[port],%[nbit] \n\t"
"rjmp .+0 \n\t"
"rjmp .+0 \n\t"
"sbrs r24, 7 \n\t"
"cbi %[port], %[nbit] \n\t"
"add r24, r24 \n\t"
"subi r25, 1 \n\t"
"breq vnextb \n\t"
"rjmp .+0 \n\t"
"cbi %[port], %[nbit] \n\t"
"rjmp .+0 \n\t"
"nop \n\t"
"rjmp lwhile \n\t"
"vnextb:\n\t"
"subi %[nb], 1 \n\t"
"cbi %[port],%[nbit] \n\t"
"brne lwhile0 \n\t"
: [vp] "+e" (vp)
: [nb] "d" (nb),
[port] "i" (_SFR_IO_ADDR(PORT)),
[nbit] "i" (NBIT)
: "r24", "r25"
);
sei();
cbi(DDR, NBIT);
}