用于燃油喷射和TPS摩托车的慢速SD写入

时间:2018-08-06 05:45:27

标签: c arduino arduino-uno spi

#include <PCD8544.h>

#include <BlockDriver.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SysCall.h>

#include <SPI.h>
#include <stdio.h>
#include <elapsedMillis.h>

static PCD8544 lcd;
float Sensor_fuelinj = A5;
float Sensor_tps = A4;
float fuel_injector_value = 0;
float tps_yellow_value = 0;
float a = 0;
float b = 0;

SdFat SD;
SdFile myFile;
int pinCS = 10;
int i = 0;
char filename[] = "Text00.txt";

elapsedMillis timeElapsed;  
unsigned int interval = 10000;


void fileNaming()
{
  for (i = 0; i < 99; i++)
  {
    filename[4] = i / 10 + '0';
    filename[5] = i % 10 + '0';
    if (! SD.exists(filename))
    {
      myFile.open(filename,  O_CREAT | O_WRITE);
      break;
    }
 }
}

void setup()
{
  pinMode(Sensor_fuelinj, INPUT);
  pinMode(Sensor_tps, INPUT);
  pinMode(pinCS, OUTPUT);

  Serial.begin(9600);

  lcd.begin(84, 48);
  lcd.setContrast(60);

  if (SD.begin(pinCS, SPI_FULL_SPEED))
  {
    Serial.println("Setup is ready.");
    lcd.setCursor(0, 2);
    lcd.print("Ready!");
    delay (1000);
    lcd.clear();
  } else
  {
    Serial.println("Setup is not ready.");
    lcd.setCursor(0, 2);
    lcd.print("Not Ready!");
    delay (2000);
    setup();
  }
  fileNaming();
}


void loop()
{

  a = analogRead(Sensor_fuelinj);
  b = analogRead(Sensor_tps);

  fuel_injector_value = a * 5 / 1024 * 2;
  tps_yellow_value = b * 5 / 1024 * 1220 / 220;

  lcd.setCursor(0, 0);
  lcd.print(filename);

  lcd.setCursor(0, 2);
  lcd.print("Fuel Injector:");
  lcd.setCursor(0, 3);
  lcd.print(fuel_injector_value);

  lcd.setCursor(0, 4);
  lcd.print("TPS          :");
  lcd.setCursor(0, 5);
  lcd.print(tps_yellow_value);


  if (timeElapsed < interval)
  {
    myFile.print(millis());
    myFile.print(",");
    myFile.print(fuel_injector_value);
    myFile.print(",");
    myFile.print(tps_yellow_value);
    myFile.println("");

    Serial.print(millis());
    Serial.print(",");
    Serial.print(fuel_injector_value);
    Serial.print(",");
    Serial.print(tps_yellow_value);
    Serial.println("");

  }
  else
  {
    myFile.close();
    lcd.setCursor(65, 0);
    lcd.print("OK!");
    fileNaming();
    delay(1000);
    lcd.setCursor(65, 0);
    lcd.print("3..");
    delay(1000);
    lcd.setCursor(65, 0);
    lcd.print("2..");
    delay(1000);
    lcd.setCursor(65, 0);
    lcd.print("1..");
    delay(1000);
    lcd.setCursor(65, 0);
    lcd.print("   ");
    timeElapsed = 0;
   } 

}

我构建了上面的代码来记录数据燃油喷射和TPS值以及毫秒,以确定跳过燃油喷射的时间。

假设是燃料将以每rpm的速度喷射:如果我的摩托车以3000rpm巡航,则意味着我的SD必须能够每秒至少记录50个样本或50HZ。但是,即使我将SPI设置为全速,我也只能以每秒25个样本的最高速度进行记录...

我应该如何完善代码...我是机械工程系的学生。

0 个答案:

没有答案