Skip to content

Commit 66ca83a

Browse files
committed
Revert "SPI library to new format"
1 parent 0dcd4b2 commit 66ca83a

File tree

5 files changed

+386
-0
lines changed

5 files changed

+386
-0
lines changed

libraries/SPI/SPI.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
3+
* SPI Master library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#include "pins_arduino.h"
12+
#include "SPI.h"
13+
14+
SPIClass SPI;
15+
16+
void SPIClass::begin() {
17+
18+
// Set SS to high so a connected chip will be "deselected" by default
19+
digitalWrite(SS, HIGH);
20+
21+
// When the SS pin is set as OUTPUT, it can be used as
22+
// a general purpose output port (it doesn't influence
23+
// SPI operations).
24+
pinMode(SS, OUTPUT);
25+
26+
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
27+
// automatically switches to Slave, so the data direction of
28+
// the SS pin MUST be kept as OUTPUT.
29+
SPCR |= _BV(MSTR);
30+
SPCR |= _BV(SPE);
31+
32+
// Set direction register for SCK and MOSI pin.
33+
// MISO pin automatically overrides to INPUT.
34+
// By doing this AFTER enabling SPI, we avoid accidentally
35+
// clocking in a single bit since the lines go directly
36+
// from "input" to SPI control.
37+
// http://code.google.com/p/arduino/issues/detail?id=888
38+
pinMode(SCK, OUTPUT);
39+
pinMode(MOSI, OUTPUT);
40+
}
41+
42+
43+
void SPIClass::end() {
44+
SPCR &= ~_BV(SPE);
45+
}
46+
47+
void SPIClass::setBitOrder(uint8_t bitOrder)
48+
{
49+
if(bitOrder == LSBFIRST) {
50+
SPCR |= _BV(DORD);
51+
} else {
52+
SPCR &= ~(_BV(DORD));
53+
}
54+
}
55+
56+
void SPIClass::setDataMode(uint8_t mode)
57+
{
58+
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
59+
}
60+
61+
void SPIClass::setClockDivider(uint8_t rate)
62+
{
63+
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
64+
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
65+
}
66+

libraries/SPI/SPI.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2010 by Cristian Maglie <[email protected]>
3+
* SPI Master library for arduino.
4+
*
5+
* This file is free software; you can redistribute it and/or modify
6+
* it under the terms of either the GNU General Public License version 2
7+
* or the GNU Lesser General Public License version 2.1, both as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#ifndef _SPI_H_INCLUDED
12+
#define _SPI_H_INCLUDED
13+
14+
#include <stdio.h>
15+
#include <Arduino.h>
16+
#include <avr/pgmspace.h>
17+
18+
#define SPI_CLOCK_DIV4 0x00
19+
#define SPI_CLOCK_DIV16 0x01
20+
#define SPI_CLOCK_DIV64 0x02
21+
#define SPI_CLOCK_DIV128 0x03
22+
#define SPI_CLOCK_DIV2 0x04
23+
#define SPI_CLOCK_DIV8 0x05
24+
#define SPI_CLOCK_DIV32 0x06
25+
//#define SPI_CLOCK_DIV64 0x07
26+
27+
#define SPI_MODE0 0x00
28+
#define SPI_MODE1 0x04
29+
#define SPI_MODE2 0x08
30+
#define SPI_MODE3 0x0C
31+
32+
#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR
33+
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
34+
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
35+
36+
class SPIClass {
37+
public:
38+
inline static byte transfer(byte _data);
39+
40+
// SPI Configuration methods
41+
42+
inline static void attachInterrupt();
43+
inline static void detachInterrupt(); // Default
44+
45+
static void begin(); // Default
46+
static void end();
47+
48+
static void setBitOrder(uint8_t);
49+
static void setDataMode(uint8_t);
50+
static void setClockDivider(uint8_t);
51+
};
52+
53+
extern SPIClass SPI;
54+
55+
byte SPIClass::transfer(byte _data) {
56+
SPDR = _data;
57+
while (!(SPSR & _BV(SPIF)))
58+
;
59+
return SPDR;
60+
}
61+
62+
void SPIClass::attachInterrupt() {
63+
SPCR |= _BV(SPIE);
64+
}
65+
66+
void SPIClass::detachInterrupt() {
67+
SPCR &= ~_BV(SPIE);
68+
}
69+
70+
#endif
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
SCP1000 Barometric Pressure Sensor Display
3+
4+
Shows the output of a Barometric Pressure Sensor on a
5+
Uses the SPI library. For details on the sensor, see:
6+
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
7+
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
8+
9+
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
10+
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
11+
12+
Circuit:
13+
SCP1000 sensor attached to pins 6, 7, 10 - 13:
14+
DRDY: pin 6
15+
CSB: pin 7
16+
MOSI: pin 11
17+
MISO: pin 12
18+
SCK: pin 13
19+
20+
created 31 July 2010
21+
modified 14 August 2010
22+
by Tom Igoe
23+
*/
24+
25+
// the sensor communicates using SPI, so include the library:
26+
#include <SPI.h>
27+
28+
//Sensor's memory register addresses:
29+
const int PRESSURE = 0x1F; //3 most significant bits of pressure
30+
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
31+
const int TEMPERATURE = 0x21; //16 bit temperature reading
32+
const byte READ = 0b11111100; // SCP1000's read command
33+
const byte WRITE = 0b00000010; // SCP1000's write command
34+
35+
// pins used for the connection with the sensor
36+
// the other you need are controlled by the SPI library):
37+
const int dataReadyPin = 6;
38+
const int chipSelectPin = 7;
39+
40+
void setup() {
41+
Serial.begin(9600);
42+
43+
// start the SPI library:
44+
SPI.begin();
45+
46+
// initalize the data ready and chip select pins:
47+
pinMode(dataReadyPin, INPUT);
48+
pinMode(chipSelectPin, OUTPUT);
49+
50+
//Configure SCP1000 for low noise configuration:
51+
writeRegister(0x02, 0x2D);
52+
writeRegister(0x01, 0x03);
53+
writeRegister(0x03, 0x02);
54+
// give the sensor time to set up:
55+
delay(100);
56+
}
57+
58+
void loop() {
59+
//Select High Resolution Mode
60+
writeRegister(0x03, 0x0A);
61+
62+
// don't do anything until the data ready pin is high:
63+
if (digitalRead(dataReadyPin) == HIGH) {
64+
//Read the temperature data
65+
int tempData = readRegister(0x21, 2);
66+
67+
// convert the temperature to celsius and display it:
68+
float realTemp = (float)tempData / 20.0;
69+
Serial.print("Temp[C]=");
70+
Serial.print(realTemp);
71+
72+
73+
//Read the pressure data highest 3 bits:
74+
byte pressure_data_high = readRegister(0x1F, 1);
75+
pressure_data_high &= 0b00000111; //you only needs bits 2 to 0
76+
77+
//Read the pressure data lower 16 bits:
78+
unsigned int pressure_data_low = readRegister(0x20, 2);
79+
//combine the two parts into one 19-bit number:
80+
long pressure = ((pressure_data_high << 16) | pressure_data_low) / 4;
81+
82+
// display the temperature:
83+
Serial.println("\tPressure [Pa]=" + String(pressure));
84+
}
85+
}
86+
87+
//Read from or write to register from the SCP1000:
88+
unsigned int readRegister(byte thisRegister, int bytesToRead ) {
89+
byte inByte = 0; // incoming byte from the SPI
90+
unsigned int result = 0; // result to return
91+
Serial.print(thisRegister, BIN);
92+
Serial.print("\t");
93+
// SCP1000 expects the register name in the upper 6 bits
94+
// of the byte. So shift the bits left by two bits:
95+
thisRegister = thisRegister << 2;
96+
// now combine the address and the command into one byte
97+
byte dataToSend = thisRegister & READ;
98+
Serial.println(thisRegister, BIN);
99+
// take the chip select low to select the device:
100+
digitalWrite(chipSelectPin, LOW);
101+
// send the device the register you want to read:
102+
SPI.transfer(dataToSend);
103+
// send a value of 0 to read the first byte returned:
104+
result = SPI.transfer(0x00);
105+
// decrement the number of bytes left to read:
106+
bytesToRead--;
107+
// if you still have another byte to read:
108+
if (bytesToRead > 0) {
109+
// shift the first byte left, then get the second byte:
110+
result = result << 8;
111+
inByte = SPI.transfer(0x00);
112+
// combine the byte you just got with the previous one:
113+
result = result | inByte;
114+
// decrement the number of bytes left to read:
115+
bytesToRead--;
116+
}
117+
// take the chip select high to de-select:
118+
digitalWrite(chipSelectPin, HIGH);
119+
// return the result:
120+
return(result);
121+
}
122+
123+
124+
//Sends a write command to SCP1000
125+
126+
void writeRegister(byte thisRegister, byte thisValue) {
127+
128+
// SCP1000 expects the register address in the upper 6 bits
129+
// of the byte. So shift the bits left by two bits:
130+
thisRegister = thisRegister << 2;
131+
// now combine the register address and the command into one byte:
132+
byte dataToSend = thisRegister | WRITE;
133+
134+
// take the chip select low to select the device:
135+
digitalWrite(chipSelectPin, LOW);
136+
137+
SPI.transfer(dataToSend); //Send register location
138+
SPI.transfer(thisValue); //Send value to record into register
139+
140+
// take the chip select high to de-select:
141+
digitalWrite(chipSelectPin, HIGH);
142+
}
143+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Digital Pot Control
3+
4+
This example controls an Analog Devices AD5206 digital potentiometer.
5+
The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
6+
A - connect this to voltage
7+
W - this is the pot's wiper, which changes when you set it
8+
B - connect this to ground.
9+
10+
The AD5206 is SPI-compatible,and to command it, you send two bytes,
11+
one with the channel number (0 - 5) and one with the resistance value for the
12+
channel (0 - 255).
13+
14+
The circuit:
15+
* All A pins of AD5206 connected to +5V
16+
* All B pins of AD5206 connected to ground
17+
* An LED and a 220-ohm resisor in series connected from each W pin to ground
18+
* CS - to digital pin 10 (SS pin)
19+
* SDI - to digital pin 11 (MOSI pin)
20+
* CLK - to digital pin 13 (SCK pin)
21+
22+
created 10 Aug 2010
23+
by Tom Igoe
24+
25+
Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
26+
27+
*/
28+
29+
30+
// inslude the SPI library:
31+
#include <SPI.h>
32+
33+
34+
// set pin 10 as the slave select for the digital pot:
35+
const int slaveSelectPin = 10;
36+
37+
void setup() {
38+
// set the slaveSelectPin as an output:
39+
pinMode (slaveSelectPin, OUTPUT);
40+
// initialize SPI:
41+
SPI.begin();
42+
}
43+
44+
void loop() {
45+
// go through the six channels of the digital pot:
46+
for (int channel = 0; channel < 6; channel++) {
47+
// change the resistance on this channel from min to max:
48+
for (int level = 0; level < 255; level++) {
49+
digitalPotWrite(channel, level);
50+
delay(10);
51+
}
52+
// wait a second at the top:
53+
delay(100);
54+
// change the resistance on this channel from max to min:
55+
for (int level = 0; level < 255; level++) {
56+
digitalPotWrite(channel, 255 - level);
57+
delay(10);
58+
}
59+
}
60+
61+
}
62+
63+
void digitalPotWrite(int address, int value) {
64+
// take the SS pin low to select the chip:
65+
digitalWrite(slaveSelectPin, LOW);
66+
// send in the address and value via SPI:
67+
SPI.transfer(address);
68+
SPI.transfer(value);
69+
// take the SS pin high to de-select the chip:
70+
digitalWrite(slaveSelectPin, HIGH);
71+
}

libraries/SPI/keywords.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#######################################
2+
# Syntax Coloring Map SPI
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SPI KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
transfer KEYWORD2
17+
setBitOrder KEYWORD2
18+
setDataMode KEYWORD2
19+
setClockDivider KEYWORD2
20+
21+
22+
#######################################
23+
# Constants (LITERAL1)
24+
#######################################
25+
SPI_CLOCK_DIV4 LITERAL1
26+
SPI_CLOCK_DIV16 LITERAL1
27+
SPI_CLOCK_DIV64 LITERAL1
28+
SPI_CLOCK_DIV128 LITERAL1
29+
SPI_CLOCK_DIV2 LITERAL1
30+
SPI_CLOCK_DIV8 LITERAL1
31+
SPI_CLOCK_DIV32 LITERAL1
32+
SPI_CLOCK_DIV64 LITERAL1
33+
SPI_MODE0 LITERAL1
34+
SPI_MODE1 LITERAL1
35+
SPI_MODE2 LITERAL1
36+
SPI_MODE3 LITERAL1

0 commit comments

Comments
 (0)