Thanks Ryan
Code: Select all
#include <SPI.h>
#include <SdFat.h>
#include <FlexCAN.h>
#include <kinetis_flexcan.h>
// SD chip select pin. Be sure to disable any other SPI devices such as Enet.
const uint8_t chipSelect = SS;
// Log file base name. Must be six characters or less.
#define FILE_BASE_NAME "Data"
//------------------------------------------------------------------------------
// File system object.
SdFat sd;
// Log file.
SdFile file;
uint16_t Time, Rpm;
float Pw1, Pw2, Baro, Map, Batv, Ego1, Ego2, Mat, Tps, Clt;
float SdValue[12] ;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";
// Specify CAN Baudrate. Currently 125k, 250k, 500k, 1M are supported in teensydrino 1.20
int baud = 500000;
FlexCAN CANbus(baud);
static CAN_message_t rxmsg;
//static CAN_message_t rxmsg.rtr = 1;
static CAN_filter_t filter;
static CAN_filter_t mask;
void setup(void)
{
Serial.begin(115200);
while (!Serial) ; // wait for Arduino Serial Monitor <----------- Revome in final product.**************************************
Serial.println("Teensy 3.2 CANlisten Example and SD");
// Find an unused file name.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
//search known file names and creat a new file name
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
}
}
if (!file.open(fileName, O_CREAT | O_WRITE | O_EXCL)) {
}
writeHeader(); //wite header info to new created file before enabling CAN bus data receive
CANbus.begin(mask); // Strat CAN Bus message stuff
}
void loop(void){
//Poll Rx FIFO
while ( CANbus.read(rxmsg) ) {
//when message available
switch (rxmsg.id) {
case 1520: //change to Base CAN Identifier for offset 0"Zero"
Time=( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8)) ;
Pw1=( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) );
Pw2=( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Rpm=( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) );
break;
case 1522: //offset 2
Baro= ( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8) );
Map= ( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) ) ;
Mat= ( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Clt= ( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) ) ;
break;
case 1523: //offset 3
Tps= ( (rxmsg.buf[1]) | (rxmsg.buf[0] << 8) ) ;
Batv= ( (rxmsg.buf[3]) | (rxmsg.buf[2] << 8) ) ;
Ego1= ( (rxmsg.buf[5]) | (rxmsg.buf[4] << 8) ) ;
Ego2= ( (rxmsg.buf[7]) | (rxmsg.buf[6] << 8) ) ;
break;
}
/////// Change this format to match the SD card. Some items are not needed.
SdValue[0] = Time;
SdValue[1] = Pw1 / 1000.0;
SdValue[2] = Pw2 / 1000.0;
SdValue[3] = Rpm;
SdValue[4] = Baro / 10.0;
SdValue[5] = Map / 10.0;
SdValue[6] = Mat / 10.0;
SdValue[7] = Clt / 10.0;
SdValue[8] = Tps / 10.0;
SdValue[9] = Batv / 10.0;
SdValue[10] = Ego1 / 10.0;
SdValue[11] = Ego2 / 10.0;
//int x;
//int go;
//for (x = 0; x < 12; x++){
// go = SdValue[x];
// //print result
// Serial.print(go);
//
// }
// Serial.println();
logData();
}
}
// Write data header.
void writeHeader() {
file.println("Ryan,Test,SD Card");
file.println("TIME,PW1,PW2,RPM,BARO,MAP,MAT,CLT,TPS,BATV,EGO1,EGO2");
file.close();
}
//------------------------------------------------------------------------------
// Log a data record.
void logData() {
int i;
float go;
// Write CAN data to CSV record.
if (!file.open(fileName, O_WRITE | O_AT_END)){
for (i = 0; i < 12; i++){
go = SdValue[i];
file.print(go);
file.print(",");
}
file.println();
file.close();
}
}