Moving SPI pin initialization from constructor to begin() function. That way, the SPI library won't alter any pin states unless / until you call begin().

This commit is contained in:
David A. Mellis 2010-08-07 21:24:49 +00:00
parent 08feacbb40
commit b89d8a9c11
5 changed files with 20 additions and 23 deletions

View File

@ -60,6 +60,9 @@ long pressure = 0;
long lastReadingTime = 0;
void setup() {
// start the SPI library:
SPI.begin();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
@ -68,8 +71,6 @@ void setup() {
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
// start the SPI library:
SPI.begin();
Serial.begin(9600);
//Configure SCP1000 for low noise configuration:

View File

@ -25,8 +25,11 @@ W5100Class W5100;
void W5100Class::init(void)
{
initSS();
delay(300);
SPI.begin();
initSS();
writeMR(1<<RST);
writeTMSR(0x55);
writeRMSR(0x55);

View File

@ -13,8 +13,7 @@
SPIClass SPI;
SPIClass::SPIClass()
{
void SPIClass::begin() {
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// When the SS pin is set as OUTPUT, it can be used as
@ -29,12 +28,15 @@ SPIClass::SPIClass()
digitalWrite(MOSI, LOW);
digitalWrite(SS, HIGH);
SPCR = _BV(SPE) | _BV(MSTR);
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR = _BV(SPE) | _BV(MSTR);
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
}
void SPIClass::end() {
SPCR &= ~_BV(SPE);
}
void SPIClass::setBitOrder(uint8_t bitOrder)

View File

@ -35,8 +35,6 @@
class SPIClass {
public:
SPIClass();
inline static byte transfer(byte _data);
// SPI Configuration methods
@ -44,8 +42,8 @@ public:
inline static void attachInterrupt();
inline static void detachInterrupt(); // Default
inline static void begin(); // Default
inline static void end();
static void begin(); // Default
static void end();
static void setBitOrder(uint8_t);
static void setDataMode(uint8_t);
@ -69,12 +67,4 @@ void SPIClass::detachInterrupt() {
SPCR &= ~_BV(SPIE);
}
void SPIClass::begin() {
SPCR |= _BV(SPE);
}
void SPIClass::end() {
SPCR &= ~_BV(SPE);
}
#endif

View File

@ -36,13 +36,14 @@ const int chipSelectPin = 7;
void setup() {
Serial.begin(9600);
// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
// start the SPI library:
SPI.begin();
// initalize the data ready and chip select pins:
pinMode(dataReadyPin, INPUT);
pinMode(chipSelectPin, OUTPUT);
//Configure SCP1000 for low noise configuration:
writeRegister(0x02, 0x2D);
writeRegister(0x01, 0x03);