From: Jon Langseth Date: Wed, 8 Aug 2012 00:18:11 +0000 (+0200) Subject: Storing some SPI example code X-Git-Url: https://git.defcon.no/?p=AVRduinoSPI-example;a=commitdiff_plain;h=HEAD Storing some SPI example code --- 4422daacf5b843dd36d7180d3c7fc5eca743bf3c diff --git a/master/master.ino b/master/master.ino new file mode 100644 index 0000000..8681436 --- /dev/null +++ b/master/master.ino @@ -0,0 +1,65 @@ +// Simple SPI MASTER example +// Counts number of presses on a button connected to D2 (PD2), +// and sends the count and the count+100 over SPI. +// Uses a mix of Arduino and plain AVR code, +// but does not use the Arduino SPI library. + +long int count=1; + +void setup() +{ + Serial.begin(115200); + Serial.println("Hello from MASTER") + + // Declare pushbutton as input using the Arduino-way ;) + pinMode(2, INPUT); + + // Set SCK, MOSI and SS as OUTPUTS using the AVR-way ;) + DDRB |= (1< + +volatile uint8_t buffer[16]; +volatile uint8_t bptr; + + +void setup() +{ + Serial.begin(115200); + Serial.println("Hello from SLAVE") + + // A nice little debug/status pin, set as OUTPUT using the Arduino-way ;) + pinMode(7, OUTPUT); + + // Make sure our buffer-pointer starts at the first buffer byte (pos 0) + bptr = 0; + + // Set MISO as OUTPUT using the AVR-way ;) + DDRB=(1< 15) bptr = 0; +} + +uint8_t spi_read( ){ + // Getting the transferred byte is as imple as reading the Data Register + // each time a byte has completed transfer. This and the ISR, needs to be + // quick, so that the next byte does not overwrite the current one. + + // Sending is done by setting the data register before reading ... + SPDR = 0; + uint8_t d = SPDR; + + // Let's add some fun "things happen because of transfers"... + if ( d == 45 ) digitalWrite(7, ( !digitalRead(7) ) ); + + return d; +} + + +