several months ago i bought some plant sensor on ali express called "chirp plant watering alarm 2.4". https://wemakethings.net/chirp/
I tried the sample code of them to read and control the sensors over a d1 mini.
Sadly i only get the same value everytime. 4294967295 for every i2c read on every sensor. I connect the SDA/SDC Pins like in the pinout on there website.
The Sample Code:
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
}
void writeI2CRegister8bit(int addr, int value) {
Wire.beginTransmission(addr);
Wire.write(value);
Wire.endTransmission();
}
unsigned int readI2CRegister16bit(int addr, int reg) {
Wire.beginTransmission(addr);
Wire.write(reg);
Wire.endTransmission();
delay(1100);
Wire.requestFrom(addr, 2);
unsigned int t = Wire.read() << 8;
t = t | Wire.read();
return t;
}
void loop() {
Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register
writeI2CRegister8bit(0x20, 3); //request light measurement
delay(9000); //this can take a while
Serial.print(", ");
Serial.println(readI2CRegister16bit(0x20, 4)); //read light register
delay(500);
}
after a long pause I started to program AVRs again. I was able to setup a UART communication between my Linux computer and an Atmega8. Unfortunately the Atmega8 seems to receive the wrong bytes when I send bytes greater than 0x1f.
The UART is running with 9600 BAUD and the data format 8N1.
// clock frequency 1Mhz
#define F_CPU 1000000UL
// baud rate
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
void uart_init (void);
void uart_putc (unsigned char data);
void uart_puts (unsigned char * str, uint8_t size);
// interrupt service routine for UART receiver interupt
ISR(USART_RXC_vect) {
// get received char
unsigned char received_char = UDR;
uart_puts("received=", 9);
if((received_char & (1<<7)) == (1<<7)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<6)) == (1<<6)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<5)) == (1<<5)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<4)) == (1<<4)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<3)) == (1<<3)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<2)) == (1<<2)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<1)) == (1<<1)) {
uart_putc('1');
} else {
uart_putc('0');
}
if((received_char & (1<<0)) == (1<<0)) {
uart_putc('1');
} else {
uart_putc('0');
}
uart_puts("\n\r",2);
}
// function to initialize UART
// dataformat 8N1
void uart_init (void) {
// shift the register right by 8 bits
UBRRH = (BAUDRATE>>8);
// set baud rate
UBRRL = BAUDRATE;
// enable receiver, transmitter and receiver interrupt
UCSRB|= (1<<TXEN)|(1<<RXEN)|(1<<RXCIE);
// 8bit data format
UCSRC|= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
}
// sends a single char over the UART
void uart_putc (unsigned char data) {
// wait while register is free
while (!( UCSRA & (1<<UDRE)));
// load data in the register
UDR = data;
}
// sends a string over the UART
void uart_puts (unsigned char * str, uint8_t size) {
uint8_t i;
for(i = 0; i < size; i++) {
uart_putc(str[i]);
}
}
// receives a single char over the UART
unsigned char uart_getc (void) {
// wait while data is being received
while(!(UCSRA) & (1<<RXC));
// return 8-bit data
return UDR;
}
uint8_t main (void) {
// enable interrupts
sei();
// enable uart
uart_init();
uart_puts("ready\n\r", 7);
while(1) {
}
return 0;
}
In GtkTerm I send the following byte sequence: 0x01, 0x02, 0x03, 0x1d, 0x1e, 0x1f, 0x20 and interrupt service routine ISR responds with the following sequence.
However, I should receive received=00100000 for 0x20
Whats wrong?
From my comment:
Maybe the baud rate is off due to using a slow clock rate / internal RC oszillator? The slower the baud rate, the greater the offset from the "middle" of each bit. This can lead to skipping bits or the controller seeing a bit twice.
everyone, I want to write and store my string at spi eeprom, then read back from spi eeprom and display in terminal through uart. I already follow the step in [1]: http://ww1.microchip.com/downloads/en/DeviceDoc/21822E.pdf . But it seem that it can only display one letter. I don't know if the other letter is save in spi eeprom or not. I hope someone can help me.
I am using:
chip:Atmega8a
software:avr studio 5
terminal: Bray terminal.
#include <avr/io.h>
#include <util/delay.h>
void serial_init(void)
{
UBRRH = 0x00;
UBRRL = 95;
UCSRB = (1 << RXEN) | (1 << TXEN) | (1<<RXCIE);
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)|(1 << UCSZ1);
}
void SPI_MasterInit(void)
{
DDRB = 0b00101100;
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);
SPCR = 0b01010000;
SPSR = 0b00000001;
}
char spi_transfer(volatile char data)
{
SPDR = data;
while(!(SPSR & (1<<SPIF)));
{
}
return SPDR;
}
void SPI_MasterTransmit(unsigned long data)
{
unsigned long address;
DDR_SPI &= ~(1<<DD_SS); //ss goes low
spi_transfer(WREN); //enable write operation
DDR_SPI |= (1<<DD_SS); //ss goes high
_delay_ms(10);
DDR_SPI &= ~(1<<DD_SS); //ss goes low
spi_transfer(WRITE); // write data to memory
spi_transfer(address>>8); // address MSB first
spi_transfer(address);
spi_transfer(data); // send lsb
DDR_SPI |= (1<<DD_SS); //ss goes high
}int resetEEPROM()
{
DDR_SPI &= ~(1<<DD_SS); // Select EEPROM
spi_transfer(WREN); // Send WRITE_ENABLE command
DDR_SPI |= (1<<DD_SS); // Release EEPROM
DDR_SPI &= ~(1<<DD_SS); // Select EEPROM again after WREN cmd
spi_transfer(WRDI); // send CHIP_ERASE command
DDR_SPI |= (1<<DD_SS); // Release EEPROM
return 0;
} // END eraseEEPROM()
unsigned long SPI_MasterReceive(unsigned long address) //terima data //read address
{
unsigned long data;
DDR_SPI &= ~(1<<DD_SS); //ss goes low
spi_transfer(READ); //enable write operation
spi_transfer(address>>8); // address MSB first
spi_transfer(address);
data = spi_transfer(0xff);
DDR_SPI |= (1<<DD_SS); //goes high
return data;
}
int main(void)
{
long int data;
unsigned long address;
serial_init();
SPI_MasterInit();
resetEEPROM();
data = Usart_Receive();
while (1)
{
if (Usart_Receive() == '.')
{
USART_Print("\r\nStore\r\n");
SPI_MasterTransmit(data); //store in spi eeprom
}
if (Usart_Receive() == '>')
{
USART_Print("\nout \r\n");
data = SPI_MasterReceive(address); //read data from the memory
Usart_Transmit(data);
}
}
return 0;
}
There is a way to write more than one byte to the EEPROM at once, but your code does not do that. Instead, you are writing one byte per write operation, and always at the same address. You are overwriting any previous bytes with each new one.
If you want to store more than one byte, you need to change the address as you write, or change the way you are writing to more than one byte at a time. (Note that you can only write multiple bytes if they are the same page of EEPROM memory.)
Perhaps a circular buffer?
Here are my Circular Buffer code. Based on this http://www.rn-wissen.de/index.php/UART_mit_avr-gcc
#include <avr/io.h>
#include <fifo.h>
#define FIFOBUF_SIZE 128
uint8_t fifobuf[FIFOBUF_SIZE];
fifo_t fifo;
ISR (USART_RXC_vect)
{
_inline_fifo_put(&fifo, UDR);
}
void serial_init(void)
{
cli();
UBRRH = 0x00;
UBRRL = 95;
UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
sei();
}
void fifo_init (fifo_t *f, uint8_t * buffer, const uint8_t size)
{
f-> count = 0;
f-> pread = f-> pwrite = buffer;
f-> read2end = f-> write2end = f-> size = size;
}
static inline int Usart_Transmit (const uint8_t c)
{
PORTD= 0b00000100; //RTS Enable
while ((UCSRA & (1 << UDRE)) ==0) {};
UDR = c;
PORTD= 0b00000000; //RTS Enable
return 1;
}
int main(void)
{
unsigned long data;
unsigned long address;
fifo_init(&fifo, fifobuf, FIFOBUF_SIZE);
serial_init();
while (1)
{
SPI_MasterInit();
resetEEPROM();
SPI_MasterTransmit(Usart_Receive());
_delay_ms(100);
if (fifo.count > 0) //; fifo.count >8 ; fifo.count
{
Usart_Transmit(_inline_fifo_get(&fifo));
}
data = SPI_MasterReceive(address); //read data from the memory
_delay_ms(100);
Usart_Transmit(data);
}
return 0;
}
it Came out all of the letter, but not follow the sequence. Example like this " bfabeaabbfcabf ", I am only type " abcdef "
And can you show me how to set the EEPROM address in spi EEPROM. Like e.g. show me some link or example about this spi EEPROM address. I ask for your Kindness to help me about this because I have been about 2 months searching on the internet, there only few examples on how to handle spi EEPROM address. Mostly I just found about ATmega EEPROM, LTD. And all of Them are not give me a good result. Thank in advance for your time. :)
I'm having some very weird issues using the following hardware elements:
Arduino Uno
Wi-Fi shield
GPS receiver
Accelerometer
Barometer
I wanted to off-load the sensor readings to an SD card as needed, but before I can even code the SD functions, the mere inclusion of the SD.h library renders my code useless.
My code is as follows:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SD.h>
/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/
//For baraometer
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 2; // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// So ...Temperature(...) must be called before ...Pressure(...).
long b5;
//End of baraometer
//ACcelerometer
// These constants describe the pins. They won't change:
const int xpin = A1; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A3; // z-axis (only on 3-axis models)
//end of accel
TinyGPS gps;
SoftwareSerial nss(3, 4);
static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
void setup()
{
//Make sure the analog-to-digital converter takes its reference voltage from
// the AREF pin
analogReference(EXTERNAL);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
//Barometer
Wire.begin();
bmp085Calibration();
//GPS
Serial.begin(115200);
nss.begin(57600);
Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
Serial.println("by Mikal Hart");
Serial.println();
Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
Serial.println();
Serial.println("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum");
Serial.println(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail");
Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
}
void loop()
{
//Accelerometer
Serial.print( analogRead(xpin));
Serial.print("\t");
//Add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
Serial.print( analogRead(ypin));
Serial.print("\t");
//add a small delay between pin readings. I read that you should
//do this but haven't tested the importance
delay(1);
Serial.print( analogRead(zpin));
Serial.print("\n"); // delay before next reading:
bool newdata = false;
unsigned long start = millis();
// Every second we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
//barometer
float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
float pressure = bmp085GetPressure(bmp085ReadUP());
float atm = pressure / 101325; // "standard atmosphere"
float altitude = calcAltitude(pressure); //Uncompensated caculation - in Meters
Serial.print("Temperature: ");
Serial.print(temperature, 2); //display 2 decimal places
Serial.println(" C");
Serial.print("Pressure: ");
Serial.print(pressure, 0); //whole number only.
Serial.println(" Pa");
Serial.print("Standard Atmosphere: ");
Serial.println(atm, 4); //display 4 decimal places
Serial.print("Altitude: ");
Serial.print(altitude, 2); //display 2 decimal places
Serial.println(" M");
Serial.println();//line break
//end of barometer
gpsdump(gps);
}
static void gpsdump(TinyGPS &gps)
{
float flat, flon;
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
gps.f_get_position(&flat, &flon, &age);
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
print_date(gps);
print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2);
print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);
gps.stats(&chars, &sentences, &failed);
print_int(chars, 0xFFFFFFFF, 6);
print_int(sentences, 0xFFFFFFFF, 10);
print_int(failed, 0xFFFFFFFF, 9);
Serial.println();
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, "*******");
else
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
Serial.print(sz);
feedgps();
}
static void print_float(float val, float invalid, int len, int prec)
{
char sz[32];
if (val == invalid)
{
strcpy(sz, "*******");
sz[len] = 0;
if (len > 0)
sz[len-1] = ' ';
for (int i=7; i<len; ++i)
sz[i] = ' ';
Serial.print(sz);
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1);
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(" ");
}
feedgps();
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
Serial.print("******* ******* ");
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
Serial.print(sz);
}
print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
feedgps();
}
static void print_str(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str[i] : ' ');
feedgps();
}
static bool feedgps()
{
while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}
// Stores all of the bmp085's calibration values into global variables
// Calibration values are required to calculate temp and pressure
// This function should be called at the beginning of the program
void bmp085Calibration()
{
Serial.write("\n\nCalibrating ... ");
ac1 = bmp085ReadInt(0xAA);
ac2 = bmp085ReadInt(0xAC);
ac3 = bmp085ReadInt(0xAE);
ac4 = bmp085ReadInt(0xB0);
ac5 = bmp085ReadInt(0xB2);
ac6 = bmp085ReadInt(0xB4);
b1 = bmp085ReadInt(0xB6);
b2 = bmp085ReadInt(0xB8);
mb = bmp085ReadInt(0xBA);
mc = bmp085ReadInt(0xBC);
md = bmp085ReadInt(0xBE);
Serial.write("Calibrated\n\n");
}
// Calculate temperature in deg C
float bmp085GetTemperature(unsigned int ut){
long x1, x2;
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = x1 + x2;
float temp = ((b5 + 8)>>4);
temp = temp /10;
return temp;
}
// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085GetPressure(unsigned long up){
long x1, x2, x3, b3, b6, p;
unsigned long b4, b7;
b6 = b5 - 4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
long temp = p;
return temp;
}
// Read 1 byte from the BMP085 at 'address'
char bmp085Read(byte address)
{
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 1);
while(!Wire.available()) {};
return Wire.read();
}
// Read 2 bytes from the BMP085
// First byte will be from 'address'
// Second byte will be from 'address'+1
int bmp085ReadInt(byte address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BMP085_ADDRESS, 2);
while(Wire.available()<2)
;
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
// Read the uncompensated temperature value
unsigned int bmp085ReadUT(){
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write((byte)0xF4);
Wire.write((byte)0x2E);
Wire.endTransmission();
// Wait at least 4.5 ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
return ut;
}
// Read the uncompensated pressure value
unsigned long bmp085ReadUP(){
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Write 0x34+(OSS<<6) into register 0xF4
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BMP085_ADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (OSS<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on OSS
delay(2 + (3<<OSS));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
msb = bmp085Read(0xF6);
lsb = bmp085Read(0xF7);
xlsb = bmp085Read(0xF8);
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
return up;
}
void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // Start transmission to device
Wire.write(address); // Send register address
Wire.write(val); // Send value to write
Wire.endTransmission(); // End transmission
}
int readRegister(int deviceAddress, byte address){
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // Register to read
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 1); // Read a byte
while(!Wire.available()) {
// waiting
}
v = Wire.read();
return v;
}
float calcAltitude(float pressure){
float A = pressure/101325;
float B = 1/5.25588;
float C = pow(A,B);
C = 1 - C;
C = C /0.0000225577;
return C;
}
Granted, right now, it is merely a conglomeration of multiple example sketches, but they work. I get a sampled reading from the accelerometer, the GPS unit and the barometer once a second. However once I simply add the line #include <SD.h> to the sketch, it fails to run correctly. The serial monitor does not display anything. I have similar versions of the above sketch (omitted as they are much lengthier), but I get the same result: either jumbled text or nothing on the Serial monitor. If I comment out the line that include the SD.h library, everything works fine....
Are there known issues with the SD.h library or conflicts? And yes, I am NOT using the necessary pins for the SD access (digital pin #4) for my sensor connections....
UPDATE:
I at least figured out it has something to do with the SoftSerial (SoftSerial.h) library and the use of the SoftSerial object (which I called nss). I can load all libraries and get everything to work if I do not call nss.begin. Is there a reason why that would conflict?
Turns out I was out of memory. Having the Serial go unresponsive like that is a common symptom. This link ultimately is what I used to trace and conclude my memory issue.
First thing would be to check the Arduino site, on the SD documentation (here) there's a mention that the communication between the microcontroller and the SD card uses SPI (documentation here) which takes place on digital pins 11, 12 and 13. I wouldn't be surprised if this was the source of your problems with the Serial monitor.
Reading some comments in Sd2Card.h, it might be tricky to get your setup to work properly:
/**
* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
Even if you put MEGA_SOFT_SPI to a non 0 value, you'd probably still fail to pass the (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__)) check.
I would suggest trying your same sketch without the TinyGPS to try to pinpoint the issue.
Also, check out this sketch it seems to be doing something similar to what you're doing, maybe you can fix yours based on what's done here.
Use pin 4 for CS and change the MOSI, MISO and SCK pins in the library SD in Sd2card.h, hope you will get rid of the problem