spokepov.lss
来自「旋转16个LED灯控制程序」· LSS 代码 · 共 1,919 行 · 第 1/5 页
LSS
1,919 行
// 15ms must elapse per trigger, which translates to about 4000
// rpm.
if (hall_debounce > HALL_DEBOUNCE_THRESH) {
stopcomputertx = 1;
// We know the number of ms since the last hall sensor trigger
// and there are 128 radial 'pixels' per sweep so divide to get
// the necessary ms between the pixel interrupts
// QUESTION: 128 or 256?
// Then we just make TIMER1 trigger at that rate!
// Reset the Timer Count Register for TIMER1 to 0, so it will
// begin counting up.
TCNT1 = 0;
// sensor_timer contains the number of TIMER0 interrupts since
// the last time we updated TIMER1. If it has a reasonable
// value, then we use it to reset the TIMER1 clock.
if ((sensor_timer < 0xFF) && (sensor_timer > 0x3)) {
// TIMER1 works differently from TIMER0. It's a 16-bit timer
// that apparently increments at the system clock rate.
//
// Because TIMER0 increments at 1/256 of the clock rate, and
// fires the interrupt only when it overflows, sensor_timer
// is incremented once ever 256*256 cycles.
//
// We want TIMER1 to fire off 256 times around the loop, so
// we can display 256 lines of pixels. We do this by putting
// sensor_timer into the high byte of TIMER1's comparator
// value, and the residual of TIMER0 (what it's counted up
// to since the last time sensor_timer was incremented) into
// the low byte, effectively a fractional value!
//
// Since TIMER0 is incrementing at 1/256 of the rate of TIMER1,
// this results in TIMER1 firing off 256 times per rotation,
// with excellent time resolution.
//
// I was quite touched by the elegance of how this works out;
// it may be able to handle the extreme RPMs of BrightSaber
// without modification...
// Set the TIMER1 comparator value
OCR1A = (sensor_timer << 8) | TCNT0;
// Clear the residual of TIMER0
TCNT0 = 0;
// Set the character and pixel numbers so they will overflow
// on the next pixel interrupt, and cause the correct data to
// be loaded.
charNum = 15; // will wrap to 0, the first char
pixelNum = 15; // will wrap to 0, the first pixel
clean = 0; // flag that we changed things
// Start TIMER1 on its merry way...
TCCR1B |= _BV(CS10); // increment at clock/1
TIMSK |= _BV(OCIE1A); // enable interrupt when it matches OCR1A
} else {
// Since we don't have a valid setting for the rotation
// speed, set a couple of LEDs to let the human know we
// aren't dead yet, and turn off the timer.
set_led(2);
TCCR1B &= ~_BV(CS10); // no incrementing = no interrupting
}
// Whether we're displaying or not, we reset sensor_timer so we can
// time the next revolution.
sensor_timer = 0;
}
// Finally, reset hall_debounce so we won't execute the timer reset code
// until the Hall Effect sensor hasn't bothered us for a reasonable while.
hall_debounce = 0;
PORTB &= ~0x8;
}
// Initialize the IO pins on the ATMEL.
void ioinit(void) {
// Set the data direction for the PORTD and PORTB pins; see the
// circuit diagram for more information on this.
DDRD = 0x73; // input on PD2 (button), PD3 (sensor), all other output
DDRB = 0xDF; // input on MOSI/DI (for SPI), all others output
// Deselect EEPROM. Not being an EE, I'm not going to worry about
// how the ATMEL talks to the EEPROM. It's black magic.
PORTB = _BV(SPIEE_CS);
// Just above, we set PD2 and PD3 to input. If we now set those
// bits to 1, they set into pullup mode (again, not EE, claim
// ignorance), which is essential for them to work. We also set
// the SENSORPOWER bit to 1, which sends out little dribbles of
// electrons to the hall effect sensor (see circuit diagram)
//
// Finally, we write 0's to the FRONT and BACK pins, which control
// which bank of 30 LEDs we are talking to. Having both of these
// on at the same time probably causes horrible things to happen.
PORTD = (_BV(BUTTON) | _BV(SENSOR) | _BV(SENSORPOWER))
& ~_BV(FRONT) & ~_BV(BACK);
// Rather than poll to see when the hall effect sensor and
// button are pressed, we configure an interrupt handler. If you
// look at the circuit diagram, you'll see that PD3 and PD2, which
// are wired to SENSOR IN and BUTTON IN, do double-duty as INT1
// and INT0. They are both SUPPOSEDLY set to interrupt on the
// falling edge of a pulse from the devices. (Page 63)
// POSSIBLE BUG: ISC0{1,0} seems to be being set to 00, not 10
// as ISC1{1,0} is being set to. So ISC0 will trigger when
// the button interrupt line goes low. Either this is a bug,
// or the original comment was not correct (likely, IMHO)
MCUCR = _BV(ISC11) & ~_BV(ISC01) & ~_BV(ISC00) & ~_BV(ISC10);
// Activate the interrupts by setting the General Interrupt Mask
// Register (Page 63)
GIMSK = _BV(INT1) | _BV(INT0);
// The ATMEL has built-in timers that can trigger an interrupt.
// SpokePOV uses them to update the LEDs 256 times per rotation.
// Timer 0 is set to update at a rate system-clock / 256 and
// interrupt when it overflows (8 bit). This means that it
// triggers every 65536 cycles.
TCCR0A = 0; // normal, overflow (count up to 256 == num pixels)
TCCR0B = _BV(CS02); // clk/256
TIMSK |= _BV(TOIE0); // turn on overflow interrupt
// Timer 1 (T1) is the pixel timer, which is used to update the
// LEDs 256 times per rotation. It's set up as a normal timer
// as well. See Page 108&71; it is apparently being set into CTC
// mode 4. This means that the counter is compared to a 16-bit value
// and interrupts when it reaches this value.
//
// Adjusting this value is how the SpokePOV compensates for
// changes in the rotation speed of the device.
//
// Note that at this point, the timer is initialized, but not
// activated.
TCCR1A = 0;
TCCR1B = _BV(WGM12);
// Clear the debounce values, which I haven't sussed out yet.
hall_debounce = 0;
sensor_timer = 0;
}
// Delay for a specified number of milliseconds using some
// assembly code. Will this be dependant on the clock speed?
void delay_ms(unsigned char ms)
{
unsigned short delay_count = F_CPU / 4000;
b0: 20 ed ldi r18, 0xD0 ; 208
b2: 37 e0 ldi r19, 0x07 ; 7
000000b4 <L_dl137>:
unsigned short cnt;
asm volatile ("\n"
b4: e2 2f mov r30, r18
b6: f3 2f mov r31, r19
000000b8 <L_dl237>:
b8: 31 97 sbiw r30, 0x01 ; 1
ba: f1 f7 brne .-4 ; 0xb8 <L_dl237>
bc: a8 95 wdr
be: 8a 95 dec r24
c0: c9 f7 brne .-14 ; 0xb4 <L_dl137>
c2: 08 95 ret
000000c4 <__vector_1>:
c4: 1f 92 push r1
c6: 0f 92 push r0
c8: 0f b6 in r0, 0x3f ; 63
ca: 0f 92 push r0
cc: 11 24 eor r1, r1
ce: 2f 93 push r18
d0: 3f 93 push r19
d2: 4f 93 push r20
d4: 5f 93 push r21
d6: 6f 93 push r22
d8: 7f 93 push r23
da: 8f 93 push r24
dc: 9f 93 push r25
de: af 93 push r26
e0: bf 93 push r27
e2: cf 93 push r28
e4: df 93 push r29
e6: ef 93 push r30
e8: ff 93 push r31
ea: c2 9a sbi 0x18, 2 ; 24
ec: c0 e0 ldi r28, 0x00 ; 0
ee: d0 e0 ldi r29, 0x00 ; 0
f0: 82 99 sbic 0x10, 2 ; 16
f2: 05 c0 rjmp .+10 ; 0xfe <__stack+0x1f>
f4: 21 96 adiw r28, 0x01 ; 1
f6: 81 e0 ldi r24, 0x01 ; 1
f8: db df rcall .-74 ; 0xb0 <delay_ms>
fa: 82 9b sbis 0x10, 2 ; 16
fc: fb cf rjmp .-10 ; 0xf4 <__stack+0x15>
fe: c5 36 cpi r28, 0x65 ; 101
100: d1 05 cpc r29, r1
102: 60 f0 brcs .+24 ; 0x11c <__stack+0x3d>
104: c4 5f subi r28, 0xF4 ; 244
106: d1 40 sbci r29, 0x01 ; 1
108: 18 f4 brcc .+6 ; 0x110 <__stack+0x31>
10a: 88 e0 ldi r24, 0x08 ; 8
10c: 81 bd out 0x21, r24 ; 33
10e: ff cf rjmp .-2 ; 0x10e <__stack+0x2f>
110: 8f ef ldi r24, 0xFF ; 255
112: 9f ef ldi r25, 0xFF ; 255
114: 90 93 8c 00 sts 0x008C, r25
118: 80 93 8b 00 sts 0x008B, r24
11c: c2 98 cbi 0x18, 2 ; 24
11e: ff 91 pop r31
120: ef 91 pop r30
122: df 91 pop r29
124: cf 91 pop r28
126: bf 91 pop r27
128: af 91 pop r26
12a: 9f 91 pop r25
12c: 8f 91 pop r24
12e: 7f 91 pop r23
130: 6f 91 pop r22
132: 5f 91 pop r21
134: 4f 91 pop r20
136: 3f 91 pop r19
138: 2f 91 pop r18
13a: 0f 90 pop r0
13c: 0f be out 0x3f, r0 ; 63
13e: 0f 90 pop r0
140: 1f 90 pop r1
142: 18 95 reti
00000144 <ioinit>:
144: 83 e7 ldi r24, 0x73 ; 115
146: 81 bb out 0x11, r24 ; 17
148: 8f ed ldi r24, 0xDF ; 223
14a: 87 bb out 0x17, r24 ; 23
14c: 80 e1 ldi r24, 0x10 ; 16
14e: 88 bb out 0x18, r24 ; 24
150: 8c e4 ldi r24, 0x4C ; 76
152: 82 bb out 0x12, r24 ; 18
154: 98 e0 ldi r25, 0x08 ; 8
156: 95 bf out 0x35, r25 ; 53
158: 80 ec ldi r24, 0xC0 ; 192
15a: 8b bf out 0x3b, r24 ; 59
15c: 10 be out 0x30, r1 ; 48
15e: 84 e0 ldi r24, 0x04 ; 4
160: 83 bf out 0x33, r24 ; 51
162: 89 b7 in r24, 0x39 ; 57
164: 82 60 ori r24, 0x02 ; 2
166: 89 bf out 0x39, r24 ; 57
168: 1f bc out 0x2f, r1 ; 47
16a: 9e bd out 0x2e, r25 ; 46
16c: 10 92 8a 00 sts 0x008A, r1
170: 10 92 8c 00 sts 0x008C, r1
174: 10 92 8b 00 sts 0x008B, r1
178: 08 95 ret
0000017a <spi_transfer>:
"L_dl1%=:\n\t"
"mov %A0, %A2\n\t"
"mov %B0, %B2\n"
"L_dl2%=:\n\t"
"sbiw %A0, 1\n\t"
"brne L_dl2%=\n\t"
"wdr\n\t"
"dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
:"r"(ms), "r"((unsigned short) (delay_count))
);
}
// All of the following routines have been modified so they
// only deal with the front leds, for speed, and to save
// code space!
// Sends the 4-byte LED pixel data block out
// over the serial link. Front LEDs only
void clock_leds(void) {
// QUESTION: this code sends 4 bytes over the link to
// update the LEDs. But spieeprom_read_into_leds() in
// eeprom.c sends 5. Why the difference?
spi_transfer(fleds[3]);
spi_transfer(fleds[2]);
spi_transfer(fleds[1]);
spi_transfer(fleds[0]);
LATCH_SELECT_PORT |= _BV(FRONT);
NOP; NOP; NOP; NOP;
LATCH_SELECT_PORT &= ~_BV(FRONT);
}
// Turn on a single LED, turning off all the other LEDs
void set_led(uint8_t led) {
fleds[0] = fleds[1] = fleds[2] = fleds[3] = 0xFF;
fleds[led/8] = ~_BV(led%8);
clock_leds();
}
// Set all the LEDs on a side to have the same
// repeating 8-bit value (ie: 0x00 = all on, 0xFF = all off)
// Added by RJW to permit a more comprehensive reset display
void set_all(uint8_t blockValue) {
fleds[0] = fleds[1] = fleds[2] = fleds[3] = blockValue;
clock_leds();
}
// Test the LEDs on power-on. Runs through them
// quickly, then displays alternating LEDs, and
// finally puts them all on. This test sequence
// is slightly modified from the original, and
// makes it easier to see problems with the LEDs.
void test_leds(void) {
uint8_t i;
// Quick run through the LEDs
for(i=0; i< 33; i++) {
set_led(i);
delay_ms(10);
}
// Set groups of 8 LEDs to the same value.
// Note that the LED state is the opposite
// of what you might expect:
//
// 0 bits = on, 1 bits = off!
// Light every other LED
set_all(0xAA);
delay_ms(50);
// Now light the other LEDs
set_all(0x55);
set_all(0x55);
delay_ms(50);
// Now light all LEDs
set_all(0x00);
delay_ms(255);
delay_ms(255);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?