arduino:spi-test-peripheral-odd
Testing Arduino as SPI with one controller and different Arduino-based peripherals. Use with spi-test-controller and spi-test-peripheral-even counter.
- spi-test-peripheral-odd.ino
/** * spi-peripheral-odd.ino * Mithat Konar * GPL3 * * An SPI peripheral that returns odd number(s). * Commands: * * '0': reset the number to 1 * * 'i': get most recent number, then increment the value by 2 * * 'r': get most recent number * * After you have made this device active by bringing pin SS/CS low, you * can send a transaction using one of these commands. * * See spi-test-peripheral-evens for how to use this. * * Pinout (ATmega328P-based Arduinos): * SCK: 13 * PICO: 12 * POCI: 11 * CS: 10 */ #include <Arduino.h> #include <SPI.h> volatile byte num = 0; // state variable used to hold count void setup (void) { // configure POCI as output pinMode(MISO, OUTPUT); // TODO: what happens when everyone is an output // all at once? SPCR |= bit(SPE); // turn on SPI in peripheral mode SPDR = 1; // initialize SPI data -- works on H/W reset but not // consistently on power up SPI.attachInterrupt(); } // SPI interrupt routine ISR (SPI_STC_vect) { // Before we got here, the old value in SPDR was already automagically // transferred back to the controller. byte cmd = SPDR; switch(cmd) { case '0': // reset num = 1; break; case 'i': // increment num += 2; break; case 'r': // do nothing -- also the default, so 'r' isn't necessary; break; // it makes for semantic code though. } SPDR = num; // set SPDR to num, ready to be transferred to the controller // on the next transfer. } void loop (void) { // nothing to do here :-) }
arduino/spi-test-peripheral-odd.txt · Last modified: 2023/07/26 02:23 by mithat