Hello,
I log pressure sensor reading via SPI to SD memory card. I used example code to run VT1000 barometer with SPI(Chip Select Pin 7). It is OK. and I can read/write SD memory card with arduino default lib for SD based SPI(ship select Pin 10). My final code is to log pressure data into SD Memory. I shared MISO, MOSI, SCK(13,12,11) and seperated chip select pin for two device. But Final writing is OK but can't communicate with VT1000 sensor. How to use two SPI module in one master uno board?
Any comments welcome!
#include "Wire.h"
// the sensor communicates using SPI, so include the library:
#include <SPI.h>
#include <SD.h>
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
//Sensor's memory register addresses:
const int PRESSURE = 0x1F; //3 most significant bits of pressure
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
const int TEMPERATURE = 0x21; //16 bit temperature reading
const byte READ = 0b11111100; // SCP1000's read command
const byte WRITE = 0b00000010; // SCP1000's write command
// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int dataReadyPin = 6;
const int chipSelectPin = 7;
const int SD_chipSelectPin = 10;
#define redLEDpin 2
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup() {
Wire.begin();
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);
writeRegister(0x03, 0x02);
// give the sensor time to set up:
delay(100);
Serial.begin(9600);
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SD_chipSelectPin, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(SD_chipSelectPin)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
}
void loop() {
float real_temp = 0.0;
long pressure = 0;
char buffer[30];
Serial.write(PrintDateTime());
Serial.print(", ");
// print_BaroTemp();
get_BaroTemp(&real_temp, &pressure);
/* Serial.print("Temp[C]=");
Serial.print(real_temp, DEC);
Serial.print(" ");
Serial.print("Pressure[Pa]=");
Serial.println(pressure, DEC);
*/
int sub = 0;
sub = (int)(100.0*(real_temp - int(real_temp)));
sprintf(buffer,"Temp[C]=%2d.%2d, Pressure[Pa]= %4d\n",int(real_temp), sub,pressure);
Serial.print(buffer);
logfile.print(String(buffer)); //file writing
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// log milliseconds since starting
uint32_t m = millis();
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
digitalWrite(redLEDpin, HIGH);
logfile.flush();
digitalWrite(redLEDpin, LOW);
setup_time();
}