📄 main.c
字号:
// We have not seen the magnet in a while, so turn off the // pixel timer... // *** PORTA |= 0x2; // Turn off this pixel timer // Question: this is different from the code in SIG_INT1. Why? cli(); TCCR1B &= ~0x7; sei(); // Turn off all but one LED set_led(2); // *** PORTA &= ~0x2; } // *** PORTB &= ~0x2; }// Interrupt 0 executes when the button is pressed.// QUESTION: unlike the pixel output interrupt, this one// doesn't sei(). Why?SIGNAL (SIG_INT0) { uint16_t timer; // *** PORTB |= 0x4; // Twiddle our thumbs until the user releases the // button - but measure how long he takes... timer = 0; while (! (BUTTON_PIN & _BV(BUTTON))) { timer++; delay_ms(1); } // A short (<500ms) press will just restart the watchdog // timer. I think this explains the structure of the // main() function; it doesn't have a loop to keep it // listening for commands if it times out, but pressing // the button will restart it. // We do expect that the button will be down at least // a small period of time... if (timer > BUTTON_DEBOUNCE) { // If a quick press... if (timer < 500UL) { // Re-enable the watchdog timer, then loop until // it fires off. WDTCSR = _BV(WDE); while (1); } else { // We want to shut everything down. Setting sensor_timer // to the pin value will cause both the communications // loop and the regular timeout loop in the main() to // give up, which results in the device going to sleep. sensor_timer = 0xFFFF; } } // *** PORTB &= ~0x4;}// Interrupt 1 executes when the hall effect sensor fires// QUESTION: unlike the pixel output interrupt, this one// doesn't sei(). Why?SIGNAL (SIG_INT1) { uint8_t cLine; // *** PORTB |= 0x8; // The first issue we need to deal with when the hall-effect // sensor tells us it sees the magnet is to avoid doing any // processing if we get an interrupt too soon after the previous // interrupt. // hall_debounce is incremented by TIMER0, which fires every 3ms // or so. At the current setting of 4, this means that at least // 15ms must elapse per trigger, which translates to about 4000 // rpm. if (hall_debounce > HALL_DEBOUNCE_THRESH) { // 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; // Check the line timer; if it has reached a particular value // then increment line_shift. When that reaches 16, wrap it // and increment cur_line. This system lets us be a little // more flexible in our timing system, and permits long delays // in the scrolling. if (line_timer_l > 15) { line_timer_l = line_timer_l - 16; // reset in a safe way that retains any residual line_shift = (line_shift + 1) & 0x0f; // increment line_shift if (line_shift == 0x00) { // Move down 1 line in the line list, wrapping around cur_line = (cur_line + 1) % NUM_LINES; // move the new first line into topLine. We must shift // the line number 4 bits (x16) to index the correct line. memcpy_P(topLine,lines+(cur_line << 4),16); // get the second line, which may wrap.. cLine = (cur_line + 1) % NUM_LINES; memcpy_P(botLine,lines+(cLine << 4),16);#ifdef SMOOTHSCROLL // get the third line, which may wrap.. cLine = (cLine + 1) % NUM_LINES; memcpy_P(scrollLine,lines+(cLine << 4),16);#endif } } // 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 // reset the line timers so that when we get a valid spinup, // they will start clocking the lines across the display line_timer_l = SCROLLSPEED; // delay figure, will trigger wrap line_shift = 0x0f; // subcharacter shift, will trigger wrap cur_line = 0xff; // will wrap to first line } // 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; unsigned short cnt; asm volatile ("\n" "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)) );}// Turn on a single LED, turning off all the other LEDsvoid 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 displayvoid 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); // likely the 1-second reset timer will go off before // this ends. But no biggy, since if it does, it'll // reset the LEDs.. } int main(void) { uint8_t cmd; // the reason we reset // MCUSR is the MCU Status Register (page 40). It tells us // why we reset, and a reset is the only way to get here. cmd = MCUSR; // The first order of business is to tell the chip that // we've got things under control. MCUSR = 0; // Turn on watchdog timer immediately, this protects against // a 'stuck' system by resetting it. // WDTCSR is the Watchdog Timer Control Register (page 45). // We set it so that it'll generate a watchdog interrupt // every second. The idea is that if things mess up, // the watchdog will kickstart us. WDTCSR = _BV(WDE) | _BV(WDP2) | _BV(WDP1); // 1 second // Initialize the various pins of the ATMEL, and set up // the interrupts. ioinit(); // test the copy from our PROGMEM string // We saved the reason for the reset of the chip. If // it's a power-on, then we run a test pattern through // the LEDs. Note that because we've set a 1-second // watchdog timer (in ioinit), if this test sequence // takes more than a second, the chip will reset. But // since we'll know it isn't a power-on, the test // sequence won't run... if ((cmd & _BV(PORF)) != 0) test_leds(); // display the reason for the reset on the LEDs. set_led(cmd+2); // enable the interrupts. I think this is not needed // since it'll immediately be done by the loop, below. sei(); // Loop until we timeout, at which point the ATMEL is // put to sleep. If the communications routine timed // out, or the user pressed the button for >500ms, // then sensor_timer will be 0xFFFF and we'll immediately // sleep. while (1) { // Reset the watchdog Timer. // // QUESTION: What's with toggling the PD0 output line here? // it doesn't seem to be connected to anything according to // the circuit diagram... // *** PORTD |= 0x1; asm("wdr"); // *** PORTD &= ~0x1; // If the sensor_timer (incremented by TIMER0) maxes out // (in about 3 minutes), then sleep everything. if (sensor_timer == 0xFFFF) { // Avoid pesky interruptions cli(); // Turn off all LEDs - I guess LED 0 is one of the "invisible ones" set_led(0); // Turn off power to the Hall Effect sensor. SENSOR_PORT &= ~_BV(SENSORPOWER); // Deselect EEPROM SPIEE_CS_PORT |= _BV(SPIEE_CS); // pull CS high to deselect // Turn off Watchdog (must be restarted when we get the wakeup) // Wakeup will be via the button interrupt. WDTCSR |= _BV(WDCE) | _BV(WDE); WDTCSR = 0; MCUCR |= _BV(SM1) | _BV(SM0) | _BV(SE); // Re-enable interrupts so we can get the wakeup! sei(); // Go into sleep mode asm("sleep"); } } // *** PORTD |= 0x2;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -