CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SD file error

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
2pipo



Joined: 07 Mar 2017
Posts: 2

View user's profile Send private message

SD file error
PostPosted: Sat Apr 22, 2017 8:57 am     Reply with quote

Hi there

I'm creating a project in which I save data into a SD card. I'm using PIC 18f2685 and 4.104 version of the CCS.

I also use a system to communicate from the 5V pic to the 3.3V SD.

I'm using the libraries from CCS (fat.c and mmcsd.c)

The problem that i have is that I have a maximum size of file which is about 450 characters. Do you know how can I solve it?

Here is the code:

Code:

/*
Coses a fer:
Preparar per rebre dades des de portàtil==> Fet
Crear arxiu nou diferent cada cop==> Fet
Optimització sistema guardar dades
*/


#include "C:\Users\pau_v\Dropbox\Pau\Estudis\Formula Student 2016-2017\TFG\programa_final\main.h"
#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>

#use rs232(baud=115200, UART1)

char mystring[20];
char myss[15];
int i = 0,alta = 1,alta_ant=0,transmetre = 0;
int k = 0;
int kk=0;
int16 num = 0;
int8 index=0,indexx=0;
unsigned int16 cobid=0;
unsigned int8 lengthCAN=0;
//char filleName[15] = "file.txt";
long long mylong;
char *fileName;
unsigned int8 array[10][150];
unsigned int Total_v=0,Min_v=0,Max_v=0,Min_t=0,Max_t=0,Min_l=0,Max_l=0,Throtle_1=0,Throtle_2=0,Brake=0,Steering=0,Sus_d_e=0,Sus_d_d=0,Sus_r_e=0,Sus_r_d=0,Shut_1=0,Vel_d_d=0,Vel_d_e=0;
char g_CWD[2] = "/";
int m = 0;
char nume;
int1 variable;
int t=0;
int ttt = 0;
int counter_msg=0;
int1 can0x80=0;
int1 can0x91=0;

#int_canrx0
void canrx0_int ( ) {
   k++;
   cobid=((unsigned int16)RXB0SIDH << 3)|((RXB0SIDL & 0xE0)>>5);
   lengthCAN =  (unsigned int8)RXB0DLC & 0xF;
   array[0][k]=cobid;
   array[1][k]=lengthCAN;
   array[2][k]=RXB0D0;
   array[3][k]=RXB0D1;
   array[4][k]=RXB0D2;
   array[5][k]=RXB0D3;
   array[6][k]=RXB0D4;
   array[7][k]=RXB0D5;
   array[8][k]=RXB0D6;
   array[9][k]=RXB0D7;
   if(cobid == 0x80){
      ttt=1;
      can0x80=1;
      kk=1;
      index++;
      indexx++;
      if(indexx >= 20){
         indexx =0;
      }
      if(index >= 10){
         index = 0;
      }
      variable =  RXB0D0;
      //delay_ms(8);
   }
   if(cobid == 0x91){//Votatge
      can0x91=1;
      Min_v=make16(RXB0D0,RXB0D1);
      Max_v=make16(RXB0D2,RXB0D3);
      Total_v=make32(RXB0D4,RXB0D5,RXB0D6,RXB0D7);
   }
   if(cobid == 0x92){//Sensors pedals i direcció
      Throtle_1 = make16(RXB0D0,RXB0D1);
      Throtle_2 = make16(RXB0D2,RXB0D3);
      Brake = make16(RXB0D4,RXB0D5);
      Steering = make16(RXB0D6,RXB0D7);
   }
   if(cobid == 0x93){//Sensors suspensió i velocitat
      Sus_d_d = make16(RXB0D0,RXB0D1);
      Sus_d_e = make16(RXB0D2,RXB0D3);
      Vel_d_d = RXB0D4;
      Vel_d_e = RXB0D5;
   }
   if(cobid == 0x94){//Shutdown i info inversors
      Shut_1 = RXB0D0;
   }
   //if(cobid == 0x95)//Accelerometre, giro, susp darrera
   if(cobid == 0x97){//Temp package, current
      Min_t = make16(RXB0D0,RXB0D1);
      Max_t = make16(RXB0D2,RXB0D3);
      Min_l = make16(RXB0D4,RXB0D5);
      Max_l = make16(RXB0D6,RXB0D7);
   }
   if(cobid == 0x90)//engegar alta,guardar o transmetre
   if(cobid == 0x96){//Susp rera
      Sus_r_d = make16(RXB0D0,RXB0D1);
      Sus_r_e = make16(RXB0D2,RXB0D3);
   }
   
   RXB0CON.RXFUL = 0;
}

#include <stdlib.h> // for atoi32

//meda library, a compatable media library is required for FAT.
#use fast_io(c)

#include "mmcsd.c"

#include "fat.c"
#include <input.c>

//////////////////////
///                ///
/// Useful Defines ///
///                ///
//////////////////////

#define COMMAND_SIZE 10
#define NUM_COMMANDS 13*/

////////////////////////
///                  ///
/// Global Variables ///
///                  ///
////////////////////////

//Make file
void MakeFile(char *fileName)
{
   printf("\r\nMaking file '%s': ", fileName);
   if(mk_file(fileName) != GOODEC)
   {
      printf("Error creating file");
      return;
   }
   printf("OK");
}

// Print file
void PrintFile(char *fileName){
   
   FILE stream;
   if(fatopen(fileName, 'r', &stream) != GOODEC)
   {
      printf("\r\nError opening file");
      return;
   }

   printf("\r\n");

   fatprintf(&stream);
   fatclose(&stream);
}

//Display la info
void DisplayDirectory(char *dir)
{
   disp_folder_contents(dir);
}

void main(){

   disable_interrupts(GLOBAL);
   can_init();
   can_set_mode(CAN_OP_CONFIG);
   
   BRGCON1.brp=1;
   BRGCON1.sjw=1;
   BRGCON2.prseg=2;
   BRGCON2.seg1ph=7;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=6;
   BRGCON3.wakfil=FALSE;
   CIOCON = 0x20;
     
   can_set_mode(CAN_OP_NORMAL);
   
   delay_ms(1000);
   
   char fileName[25];
   
   int i;   // pointer to the buffer

   i = fat_init();
   if (i){
      printf("\r\n\nERROR INITIALIZING FAT\r\n\n");
      output_low(PIN_A5);
   }
   else{
      printf("\r\n\n FAT INITIALIZED\r\n\n");
      output_high(PIN_A5);
   }
   
   DisplayDirectory(g_CWD);
   
   //m=23;
   
   sprintf(nume,"%u",m);
   
   //fileName = "file";   
   strcpy(mystring,g_CWD);
   //strcat(mystring,fileName);
   strcat(mystring,nume);
   /*fileName=".txt";
   strcat(mystring,fileName);*/
   
   FILE stream;
   
   MakeFile(mystring);
   
   enable_interrupts(int_canrx0);
   enable_interrupts(GLOBAL);
         
   while(true){     
      output_bit(PIN_A2,variable);
      if(transmetre == 1){
         PrintFile(mystring);
      }
      if(indexx=8 && can0x80==1 && can0x91==1){
            can0x80=0;
            can0x91=0;
      }
      alta_ant=alta;
      if(index == 9 & alta == 1 & kk==1){//en l'instant 9 guarda les dades que han passat durant tot el mig segon
         kk=0;
         can0x80=0;
         can0x91=0;
         t=1;
         counter_msg++;
         if(fatopen(mystring, "a", &stream) != GOODEC){
            printf("Error opening file");
            return;
         }
         else{
            printf("Open");
         }
         for(i=0;i<k;i++){
            for(num = 0; num<=9; num++){
               sprintf(myss,"%u",array[num][i]);
               fatputs(myss, &stream);
               fatputs(",", &stream);
               array[num][i]=0;
            }
            fatputs("\r\n", &stream);
         }
         sprintf(myss,"%u",counter_msg);
         fatputs(myss, &stream);
         fatputs("\r\n", &stream);
         k=0;
         /*fileName = "file";   
         strcpy(mystring,g_CWD);
         strcat(mystring,fileName);
         strcat(mystring,nume);
         fileName=".txt";
         strcat(mystring,fileName);*/
      }
      if((index == 0 | (alta==0 & alta_ant==1 )) & kk==1 & t==1){//al començar el següent segon tanca l'arxiu. Aixó es fa perque duri menys de 50 ms tota l'operació de guardar dades.
            kk=0;
            can0x80=0;
            can0x91=0;
            if(fatclose(&stream) != GOODEC)
            {
               printf("Error closing file");
               return;
            }
            printf("Close");
      } 
      if(ttt==1){
         printf("%u",alta);
         printf(",");
         printf("%u",k);
         printf(",");
         printf("%u",index);
         printf(",");
         printf("%u",kk);
         printf(",");
         printf("%u",can0x80);
         printf(",");
         printf("%u",can0x91);
         printf(",");         
         printf("%u",array[2][k]);
         printf("\r\n");
         ttt=0;
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 22, 2017 5:44 pm     Reply with quote

There is a sticky in the code library forum with all the bug fixes in it.
Try using that version instead.
2pipo



Joined: 07 Mar 2017
Posts: 2

View user's profile Send private message

PostPosted: Tue Apr 25, 2017 10:04 am     Reply with quote

I finally got the error, the library you told me had some errors and didn't work in my project.

The problem i had was that i formatted the SD in a wrong way.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group