Skip to content
Browse files

Merge branch 'arduino-1.x'

Conflicts:
	SPI_Bus.cpp
	SPI_Bus.h
  • Loading branch information...
2 parents fbd4dfc + 6f5bb87 commit 205f721ecf508959482a57210b13228eafc9b79f @burjui burjui committed Jul 29, 2012
Showing with 166 additions and 2 deletions.
  1. +1 −2 SPI_Bus.cpp
  2. +38 −0 examples/shreg_demo.pde
  3. +38 −0 examples/shreg_demo_16bit.pde
  4. +48 −0 examples/shreg_demo_2x.pde
  5. +41 −0 examples/shreg_input_demo.pde
View
3 SPI_Bus.cpp
@@ -17,12 +17,11 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include "SPI_Bus.h"
#include <inttypes.h>
#include <stdlib.h>
+#include "SPI_Bus.h"
#include "LineDriver.h"
-
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
View
38 examples/shreg_demo.pde
@@ -0,0 +1,38 @@
+#include <LineDriver.h>
+#include <SPI.h>
+#include <SPI_Bus.h>
+
+/* Use the following declaration for software SPI mode:
+ * SPI_8bit_Bus reg(latchPin, clockPin, dataPin);
+ * Here we use hardware bus mode (the default) with clock divider = 4,
+ * and data transfer mode = most significant bit first.
+ */
+SPI_Bus reg(_8bit, 8);
+
+
+void setup()
+{
+ reg.write(0u);
+}
+
+
+/* Shifts the bits to the left, replacing the low bit by high bit.
+ * In other words, rotates the bits to the left.
+ * For example, 11110000 becomes 11100001.
+ */
+void rotateLeft(uint8_t &bits)
+{
+ bits = (bits << 1) | (bits & 0x80 ? 1 : 0);
+}
+
+
+void loop()
+{
+ static uint8_t nomad = 1;
+
+ reg.write(nomad); // write into shift register
+ rotateLeft(nomad);
+
+ /* Full rotate in one second */
+ delay(1000 / reg.bandwidth() / 8);
+}
View
38 examples/shreg_demo_16bit.pde
@@ -0,0 +1,38 @@
+#include <LineDriver.h>
+#include <SPI.h>
+#include <SPI_Bus.h>
+
+/* Use the following declaration for software SPI mode:
+ * SPI_8bit_Bus reg(latchPin, clockPin, dataPin);
+ * Here we use hardware bus mode (the default) with clock divider = 4,
+ * and data transfer mode = most significant bit first.
+ */
+SPI_Bus reg(_16bit, 8);
+
+
+void setup()
+{
+ reg.write(0u);
+}
+
+
+/* Shifts the bits to the left, replacing the low bit by high bit.
+ * In other words, rotates the bits to the left.
+ * For example, 11110000 becomes 11100001.
+ */
+void rotateLeft(uint16_t &bits)
+{
+ bits = (bits << 1) | (bits & 0x8000 ? 1 : 0);
+}
+
+
+void loop()
+{
+ static uint16_t nomad = 1;
+
+ reg.write(nomad); // write into shift register
+ rotateLeft(nomad);
+
+ /* Full rotate in one second */
+ delay(1000 / reg.bandwidth() / 8);
+}
View
48 examples/shreg_demo_2x.pde
@@ -0,0 +1,48 @@
+#include <LineDriver.h>
+#include <SPI.h>
+#include <SPI_Bus.h>
+
+/* Use the following declaration for software SPI mode:
+ * SPI_8bit_Bus reg(latchPin, clockPin, dataPin);
+ * Here we use hardware bus mode (the default) with clock divider = 4,
+ * and data transfer mode = most significant bit first.
+ */
+SPI_Bus reg1(_8bit, 8), reg2(_8bit, 7);
+
+
+void setup()
+{
+ reg1.write(0u);
+ reg2.write(0u);
+}
+
+
+/* Shifts the bits to the left, replacing the low bit by high bit.
+ * In other words, rotates the bits to the left.
+ * For example, 11110000 becomes 11100001.
+ */
+void rotateLeft(uint8_t &bits)
+{
+ bits = (bits << 1) | (bits & 0x80 ? 1 : 0);
+}
+
+
+void rotateRight(uint8_t &bits)
+{
+ bits = (bits >> 1) | (bits & 1 ? 0x80 : 0);
+}
+
+
+void loop()
+{
+ static uint8_t nomad1 = 1, nomad2 = 0x80;
+
+ reg1.write(nomad1);
+ rotateLeft(nomad1);
+
+ reg2.write(nomad2);
+ rotateRight(nomad2);
+
+ /* Full rotate in one second */
+ delay(1000 / reg1.bandwidth() / 8);
+}
View
41 examples/shreg_input_demo.pde
@@ -0,0 +1,41 @@
+#include <LineDriver.h>
+#include <SPI.h>
+#include <SPI_Bus.h>
+
+SPI_Bus reg(_8bit, 8);
+
+
+void setup()
+{
+ Serial.begin(9600);
+ reg.setSelectionPolicy(SPI_Bus::SELECT_BEFORE);
+}
+
+
+void loop()
+{
+ static uint8_t last_input_states = 0;
+
+ uint8_t states = reg.read8bits();
+
+ /* Print the input states only if they're changed */
+ if (states != last_input_states)
+ {
+ uint8_t changed = states ^ last_input_states; // get changed states only
+ last_input_states = states;
+
+ for (int i = 0; i < 8; ++i)
+ {
+ if (changed & 1)
+ {
+ Serial.print("#");
+ Serial.print(i);
+ Serial.print(" -> ");
+ Serial.println(states & 1);
+ }
+
+ changed >>= 1;
+ states >>= 1;
+ }
+ }
+}

0 comments on commit 205f721

Please sign in to comment.
Something went wrong with that request. Please try again.