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

PIC16F877A SD file not found

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



Joined: 27 Jan 2025
Posts: 4

View user's profile Send private message

PIC16F877A SD file not found
PostPosted: Mon Jan 27, 2025 9:36 pm     Reply with quote

Hello everybody i'm currently doing a school project with PIC16F877A, currently i'm trying to make a WAV player with the processor, but i'm having troubles with the sd card reading part. In the main code I associate a file that is in the SD Card the name is the same and the extension is correct, but when I'm testing It appears as if there is no file in the SD. Some of the modifications i made are the presence of output_highs in some parts of the code to help me track where was the problem.

So the main problem occours in
Code:

ok |= fat_open_file(wav);                      // Open the wave file


and then in in which return as File not found
Code:

int1 fat_open_file(int8* fname){
  int1 ok_find = 0;
  int1 ok_read = 0;
  int8 target_file[13], s_name[12], fname_parse_pos = 0, name_size;
  int32 addr;
  
  while(fname[fname_parse_pos] != '\0' && fname_parse_pos < 13){
    target_file[fname_parse_pos] = tolower(fname[fname_parse_pos]);
    fname_parse_pos++;
  }
  
  target_file[fname_parse_pos] = '\0';
  name_size = strlen(target_file);
  if(name_size > 12)
    return 1;                                   // Return error (long file name is not supported)
    
  addr = Root_Dir;
  sdcard_select();
  if(sdcard_stop_transmission() != 0){
    sdcard_deselect();
    return 1;
  }
  
  while(addr < Data_Start && ok_find == 0){
    if(get_file_name(addr, s_name) != 0)
      return 1;                           // Error reading file name from the SD card or file not found
    if(strcmp(s_name, target_file) == 0){
      ok_find = 1;
     }
    if(ok_find == 0)
      addr += 32;
  }
  
  if(ok_find == 0){
    output_high(PIN_A2); // Erro é encontrado aqui
    return 1;  // File not found ==> Error
  }



This is the whole code i'm building, it uses the FAT_Lib.c library I modified a bit to help me find where was the error.
Main code.

Code:

//#include <audiosdv2.h>
 
 
#include <16F877A.h>
#device ADC=16
 
 
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
 
 
#use delay(crystal=6000000)
 
 
// SD Card module connections
#define   SDCARD_SPI_HW
#define   SDCARD_PIN_SELECT  PIN_D3
// End SD card module connections
 
 
#use fast_io(D)
#include <FAT_Lib.c>
 
 
 
 
const int8 *wav = "1.wav";
int1 ok = 0;
int8 i, j, data[16], channel_count;
 
 
 
 
void play(){
  sdcard_read_byte(address_pointer + 22, &channel_count);       // Read number of channels
  while(fat_read_data(16, data) == 0){
    for(i = 0; i < 16; i++){
      set_timer1(0);
      j = data[i];
      set_pwm1_duty((int16)j);                   // Update PWM1 duty cycle
      
      while(get_timer1() < 500);                 // Wait some time (about 125us) to update the duty cycle
    }
  }
}
 
 
void main(){
  //delay_ms(2000);
 
 
  setup_ccp1(CCP_PWM);                           // Configure CCP1 as a PWM
  set_pwm1_duty(0);                              // set PWM1 duty cycle to 0
  setup_timer_2(T2_DIV_BY_1, 63, 1);             // Set PWM frequency to maximum with 8-bit resolution
  setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 );
  
  // Configura os pinos
  //set_tris_b(0x01);
 
 
  ok |= sdcard_init();                           // Initialize the SD card module
  ok |= fat_init();                              // Initialize FAT library
  
  ok |= fat_open_file(wav);                      // Open the wave file
    if(ok == 0){
       output_high(PIN_A3);
 
 
  }
  
 
 
  while(true){
 
 
   //if(input(PIN_B0) == 0) {
      
      
      // Espera até que o botão seja pressionado
      
      if(ok == 0){
        // output_low(PIN_A4);
        // output_high(PIN_A5);
         play();                                      // Inicia a reprodução do áudio
      }
    
   //}
   else{
       // output_low(PIN_A5);
       // output_high(PIN_A4);
        
   }
  
  // Após a reprodução, desliga os PWM
  set_pwm1_duty(0);                              // set PWM1 duty cycle to 0
 
 
  }
}
 


This is the FAT_Lib.c, I will put the full code in the attachments in case anyone needs

Code:



/////////////////////////////////////////////////////////////////////////
////                                                                 ////
////  FAT file system library (FAT16 & FAT32) for CCS PIC C compiler ////
////                                                                 ////
////  This library is for reading files only.                        ////
////  Long file name (LFN) is not supported (maximum file name is 12 ////
////  character)                                                     ////
////                    ----- User Functions -----                   ////
////                                                                 ////
////  sdcard_init();                                                 ////
////   Initializes the media. Returns 0 if OK, non-zero if error.    ////
////   Must be called before any other function.                     ////
////                                                                 ////
////  sdcard_read_byte(int32 addr, int8* data);                      ////
////   Reads a byte from the MMC/SD card at address 'addr', saves to ////
////   pointer 'data'. Returns 0 if OK, non-zero if error.           ////
////                                                                 ////
////  fat_init();                                                    ////
////   Initializes the FAT library. Returns 0 if OK, 1 if error.     ////
////                                                                 ////
////  fat_file(int8* fname);                                    ////
////   Opens file stream. fname is file name. Returns 0 if OK and 1  ////
////   if the opening process faced problem (file not found, long    ////
////   file name ...........).                                       ////
////                                                                 ////
////  fat_read_data(int32 size, int8* data);                         ////
////   Reads an array (data) of size 'size' from the opened file.    ////
////   Returns 0 if OK, 1 if error or end of file is reached.        ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////                                                                 ////
////  http://ccspicc.blogspot.com                                   ////
////  electronnote@gmail.com                                         ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
 
 
#include <string.h>
#ignore_warnings    240
// SD card definitions
int16 timeout;
enum _card_type{MMC, SDSC, SDHC} g_card_type;
enum sdcard_err{
  sdcard_goodec = 0, sdcard_idle = 0x01, sdcard_timeout = 0x80,
  sdcard_illegal_cmd = 0x04 };
#define GO_IDLE_STATE 0
#define SEND_IF_COND 8
#define SET_BLOCKLEN 16
#define SEND_APP_OP_COND 41
#define APP_CMD 55
#define READ_OCR 58
void send_sdcard_command(int8 command, int32 sd_data, int8 sd_crc);
sdcard_err sdcard_init();
int1 sdcard_read_byte(int32 addr, int8* data);
sdcard_err sdcard_get_r1();
sdcard_err sdcard_get_r7();
sdcard_err sdcard_go_idle_state();
sdcard_err sdcard_send_if_cond();
sdcard_err sdcard_send_app_cmd();
sdcard_err sdcard_sd_send_op_cond();
sdcard_err sdcard_read_ocr(int8* _ocr_byte_3);
sdcard_err sdcard_set_blocklen();
int1 sdcard_stop_transmission();
void sdcard_select();
void sdcard_deselect();
 
 
// FAT System definitions
int1 read_in_progress = 0, fat_type = 0;
int8 Sectors_Per_Cluster;
int16 Bytes_Per_Cluster; 
int32 fat_Start, fat_Length, Root_Dir, Data_Start, address_pointer, file_pointer,
      file_size = 0, file_start_cluster, file_sector_number;
int1 fat_init();
int1 fat_open_file(int8* fname);
int1 fat_read_data(int32 size, int8* data);
int1 get_file_name(int32 file_entry_addr, int8 sname[]);
int32 get_next_cluster(int32 my_cluster);
int8 sdcard_wait_for_token();
 
 
// SD Card functions
sdcard_err sdcard_init(){
  int8 i, resp, ocr_byte_3;
#if defined(SDCARD_SPI_HW)
  SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 | SPI_XMIT_L_TO_H);
  #define sdcard_xfer(x)    spi_read(x)
#else
  #if defined(SDCARD_PIN_SCL)
   output_drive(SDCARD_PIN_SCL);
  #endif
  #if defined(SDCARD_PIN_SDO)
   output_drive(SDCARD_PIN_SDO);
  #endif
  #if defined(SDCARD_PIN_SDI)
   output_float(SDCARD_PIN_SDI);
  #endif
    #use spi(MASTER, DI=SDCARD_PIN_SDI, DO=SDCARD_PIN_SDO, CLK=SDCARD_PIN_SCL, BITS=8, MSB_FIRST, MODE=3, baud=400000)
    #define sdcard_xfer(x)    spi_xfer(x)
#endif
  output_high(SDCARD_PIN_SELECT);
  output_drive(SDCARD_PIN_SELECT);
  delay_ms(250);
  for(i = 0; i < 10; i++)                        // Send 80 cycles
    sdcard_xfer(0xFF);
  i = 0;
  while((resp != sdcard_idle) && (i < 5)){
    i++;
    sdcard_select();
    resp = sdcard_go_idle_state();               // Send CMD0
    sdcard_deselect();
  }
  if(resp != sdcard_idle)
    return resp;
  sdcard_select();
  resp = sdcard_send_if_cond();                  // Send CMD8
  sdcard_deselect();
  if(resp != sdcard_idle)
    return resp;
  i = 0;
  do{
    sdcard_select();
    resp = sdcard_send_app_cmd();                // Send CMD58
    if((resp != sdcard_idle) && (resp != sdcard_illegal_cmd) && (resp != 0)){
      sdcard_deselect(); return resp;}
    resp = sdcard_sd_send_op_cond();             // Send ACMD41
    sdcard_deselect();
    delay_ms(100);
    i++;
  } while((resp == 0x01) && (i < 254));
  sdcard_deselect();
  if((resp != 0 || i >= 254) && (resp != sdcard_illegal_cmd))
    return sdcard_timeout;
  if(resp == 0x04) g_card_type =  MMC;           // MMC type
  else             g_card_type = SDSC;           // SDSC or SDHC type
  if(g_card_type == SDSC){
    sdcard_select();
    resp = sdcard_read_ocr(&ocr_byte_3);
    sdcard_deselect();
    if(resp != sdcard_idle && resp != sdcard_illegal_cmd)
      return resp;
    if(resp != sdcard_illegal_cmd){
      if(bit_test(ocr_byte_3, 6))                  // If bit 30 of the OCR register is 1 (CCS is 1) ==> SDHC type
        g_card_type =  SDHC;
    }
  }
  sdcard_select();
  resp = sdcard_set_blocklen();
  sdcard_deselect();
  if(resp != 0)
    return sdcard_timeout;
#if defined(SDCARD_SPI_HW)
  // Speed up the SPI bus
  SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_XMIT_L_TO_H);
#else
   #use spi(MASTER, DI=SDCARD_PIN_SDI, DO=SDCARD_PIN_SDO, CLK=SDCARD_PIN_SCL, BITS=8, MSB_FIRST, MODE=3)
#endif
  return sdcard_goodec;
}
int1 sdcard_read_byte(int32 addr, int8* data){
  int16 i, byte_addr;
  int32 sector_number;
  sector_number = addr/512;
  if(g_card_type != SDHC)
    sector_number = sector_number << 9;
  byte_addr = addr % 512;
  sdcard_select();
  send_sdcard_command(17, sector_number, 0xFF);
  if(sdcard_wait_for_token() == 0xFE){
    for(i = 0; i < byte_addr; i++)
      sdcard_xfer(0xFF);
    *data = sdcard_xfer(0xFF);
    byte_addr++;
    for(i = byte_addr; i < 512; i++)
      sdcard_xfer(0xFF);
    for(i = 0; i < 2; i++)
      sdcard_xfer(0xFF);
    sdcard_deselect();
    return sdcard_goodec;
  }
  sdcard_deselect();
  return 1; 
}
void send_sdcard_command(int8 command, int32 sd_data, int8 sd_crc){
  int8 i;
  sdcard_xfer(0x40 | command);
  for(i = 0; i < 4; i++)
    sdcard_xfer(sd_data >> (3 - i) * 8);
  sdcard_xfer(sd_crc);
}
sdcard_err sdcard_go_idle_state(){
  send_sdcard_command(GO_IDLE_STATE, 0, 0x95);
  return sdcard_get_r1();
}
sdcard_err sdcard_send_if_cond(){
  send_sdcard_command(SEND_IF_COND, 0x1AA, 0x87);
  return sdcard_get_r7(); 
}
sdcard_err sdcard_send_app_cmd(){
  send_sdcard_command(APP_CMD, 0, 0xFF);
  return sdcard_get_r1();
}
sdcard_err sdcard_sd_send_op_cond(){
  send_sdcard_command(SEND_APP_OP_COND, 0x40000000, 0xFF);
  return sdcard_get_r1();
}
sdcard_err sdcard_read_ocr(int8* _ocr_byte_3){
  unsigned int8 i, response;
  timeout = 0xFFFF;
  send_sdcard_command(READ_OCR, 0, 0xFF);
  while(timeout){
    response = sdcard_xfer(0xFF);
    if(response != 0xFF){
      if(response == 0x04) return response;
      *_ocr_byte_3 = sdcard_xfer(0xFF);
      for(i = 0; i < 3; i++)
        sdcard_xfer(0xFF);
      return sdcard_idle;
    timeout--;
    }
  }
  return sdcard_timeout;
}
sdcard_err sdcard_set_blocklen(){
  send_sdcard_command(SET_BLOCKLEN, 512, 0xFF);
  return sdcard_get_r1();
}
sdcard_err sdcard_get_r1(){
  int8 response = 0;
  timeout = 0xFFFF;
  while(timeout){
    response = sdcard_xfer(0xFF);
    if(response != 0xFF){
      return response;
    }
    timeout--;
  }
  return sdcard_timeout;
}
sdcard_err sdcard_get_r7(){
  int8 i, response = 0;
  timeout = 0xFFFF;
  while(timeout){
    response = sdcard_xfer(0xFF);
    if(response != 0xFF){
      for(i = 0; i < 4; i++)
        sdcard_xfer(0xFF);
      return sdcard_idle;
    }
    timeout--;
  }
  return sdcard_timeout;
}
int1 sdcard_stop_transmission(){
  int8 resp=0xFF;
  timeout = 0xFFFF;
  while((resp != sdcard_idle) && (resp != sdcard_illegal_cmd) && (resp != 0) && timeout){
    send_sdcard_command(12, 0, 0xFF);            // Send stop transmission command
    resp = sdcard_get_r1();
    timeout--;
  }
  if(timeout)
    return 0;
  else
    return 1;
}
void sdcard_select(){
  output_low(SDCARD_PIN_SELECT);
}
void sdcard_deselect(){
  output_high(SDCARD_PIN_SELECT);
}
 
 
// FAT Functions
int1 fat_init(){
  int1   ec = 0;
  int8   FATs;
  int16  Bytes_Per_Sector, Reserved_Sectors, Root_Entries, Small_Sectors;
  int32  Hidden_Sectors, Total_Sectors, Sectors_Per_FAT = 0;
  ec |= sdcard_read_byte(0x0C, &Bytes_Per_Sector);
  Bytes_Per_Sector <<= 8;
  ec |= sdcard_read_byte(0x0B, &Bytes_Per_Sector);
  ec |= sdcard_read_byte(0x0D, &Sectors_Per_Cluster);
  ec |= sdcard_read_byte(0x0F, &Reserved_Sectors);
  Reserved_Sectors <<= 8;
  ec |= sdcard_read_byte(0x0E, &Reserved_Sectors);
  ec |= sdcard_read_byte(0x10, &FATs);
  ec |= sdcard_read_byte(0x14, &Small_Sectors);
  Small_Sectors <<= 8;
  ec |= sdcard_read_byte(0x13, &Small_Sectors);
  ec |= sdcard_read_byte(0x1F, &Hidden_Sectors);
  Hidden_Sectors <<= 8;
  ec |= sdcard_read_byte(0x1E, &Hidden_Sectors);
  Hidden_Sectors <<= 8;
  ec |= sdcard_read_byte(0x1D, &Hidden_Sectors);
  Hidden_Sectors <<= 8;
  ec |= sdcard_read_byte(0x1C, &Hidden_Sectors);
  ec |= sdcard_read_byte(0x23, &Total_Sectors);
  Total_Sectors <<= 8;
  ec |= sdcard_read_byte(0x22, &Total_Sectors);
  Total_Sectors <<= 8;
  ec |= sdcard_read_byte(0x21, &Total_Sectors);
  Total_Sectors <<= 8;
  ec |= sdcard_read_byte(0x20, &Total_Sectors);
  if((Total_Sectors/Sectors_Per_Cluster) > 65525)
    fat_type = 1;                                // FAT32
  if(fat_type == 0){                             // If FAT16
    ec |= sdcard_read_byte(0x12, &Root_Entries);
    Root_Entries <<= 8;
    ec |= sdcard_read_byte(0x11, &Root_Entries);
    ec |= sdcard_read_byte(0x17, &Sectors_Per_FAT);
    Sectors_Per_FAT <<= 8;
    ec |= sdcard_read_byte(0x16, &Sectors_Per_FAT);
  }
  else{                                          // If FAT32
    ec |= sdcard_read_byte(0x27, &Sectors_Per_FAT);
    Sectors_Per_FAT <<= 8;
    ec |= sdcard_read_byte(0x26, &Sectors_Per_FAT);
    Sectors_Per_FAT <<= 8;
    ec |= sdcard_read_byte(0x25, &Sectors_Per_FAT);
    Sectors_Per_FAT <<= 8;
    ec |= sdcard_read_byte(0x24, &Sectors_Per_FAT);
  }
  if(ec != 0)
    return 1;
  Bytes_Per_Cluster = Sectors_Per_Cluster * Bytes_Per_Sector;
  fat_Length = Sectors_Per_FAT * Bytes_Per_Sector;
  fat_Start = (int32)Reserved_Sectors * Bytes_Per_Sector;
  Root_Dir = fat_Start + (FATs * fat_Length);
  if(fat_type == 0){                             // If FAT16
    Data_Start = (Root_Entries * 0x20) + (Bytes_Per_Sector - 1);
    Data_Start /= Bytes_Per_Sector;
    Data_Start += Reserved_Sectors + (FATs * Sectors_Per_FAT);
    Data_Start *= Bytes_Per_Sector;
  }
  else                                           // If FAT32
    Data_Start = Bytes_Per_Cluster + Root_Dir;
  return 0;
}
 
 
int1 fat_open_file(int8* fname){
  int1 ok_find = 0;
  int1 ok_read = 0;
  int8 target_file[13], s_name[12], fname_parse_pos = 0, name_size;
  int32 addr;
  
  while(fname[fname_parse_pos] != '\0' && fname_parse_pos < 13){
    target_file[fname_parse_pos] = tolower(fname[fname_parse_pos]);
    fname_parse_pos++;
  }
  
  target_file[fname_parse_pos] = '\0';
  name_size = strlen(target_file);
  if(name_size > 12)
    return 1;                                   // Return error (long file name is not supported)
    
  addr = Root_Dir;
  sdcard_select();
  if(sdcard_stop_transmission() != 0){
    sdcard_deselect();
    return 1;
  }
  
  while(addr < Data_Start && ok_find == 0){
    if(get_file_name(addr, s_name) != 0)
      return 1;                           // Error reading file name from the SD card or file not found
    if(strcmp(s_name, target_file) == 0){
      ok_find = 1;
     }
    if(ok_find == 0)
      addr += 32;
  }
  
  if(ok_find == 0){
    output_high(PIN_A2); // Erro é encontrado aqui
    return 1;  // File not found ==> Error
  }
    
  ok_read = 0;
  ok_read |= sdcard_read_byte(addr + 0x1F, &file_size);
  file_size <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x1E, &file_size);
  file_size <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x1D, &file_size);
  file_size <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x1C, &file_size);
  ok_read |= sdcard_read_byte(addr + 0x15, &file_start_cluster);
  file_start_cluster <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x14, &file_start_cluster);
  file_start_cluster <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x1B, &file_start_cluster);
  file_start_cluster <<= 8;
  ok_read |= sdcard_read_byte(addr + 0x1A, &file_start_cluster);
  if(fat_type == 0)
    address_pointer = Data_Start + (file_start_cluster - 2) * Bytes_Per_Cluster;
  else
    address_pointer = Root_Dir + (file_start_cluster - 2) * Bytes_Per_Cluster;
    
  file_pointer = 0;
  read_in_progress = 0;
  file_sector_number = address_pointer/512;
  return ok_read;
}
 
 
int1 fat_read_data(int32 fsize, int8* fdata){
  int8 j;
  int16 i;
  sdcard_select();
  if(file_pointer == file_size){
    sdcard_stop_transmission();
    sdcard_deselect();
    return 1;
  }
  if(file_pointer + fsize > file_size){
    fsize = file_size - file_pointer;
    fdata[fsize] = '\0';
  }
  if(read_in_progress == 1){
    for(i = 0; i < fsize; i++){
      fdata[i] = sdcard_xfer(0xFF);
      file_pointer++;
      if((file_pointer % 512) == 0){
        for(j = 0; j < 2; j++)
          sdcard_xfer(0xFF);
        #if defined (FILE_CLUSTER_CHECK)
        if((file_pointer % Bytes_Per_Cluster) == 0){
          sdcard_stop_transmission();
          file_start_cluster = get_next_cluster(file_start_cluster);
          if(fat_type == 0)
            file_sector_number = (Data_Start + (file_start_cluster - 2) * Bytes_Per_Cluster)/512;
          else
            file_sector_number = (Root_Dir + (file_start_cluster - 2) * Bytes_Per_Cluster)/512;
          if(g_card_type != SDHC)
            file_sector_number = file_sector_number << 9;
          sdcard_select();
          send_sdcard_command(18, file_sector_number, 0xFF);     // Send multi-block read command
        }
        #endif
        if(sdcard_wait_for_token() != 0xFE){
          sdcard_deselect();
          return 1;
        }
      }
    }
    sdcard_deselect();
    return 0;
  }
  if(read_in_progress == 0){
    read_in_progress = 1;
    if(g_card_type != SDHC)
      file_sector_number = file_sector_number << 9;
    send_sdcard_command(18, file_sector_number, 0xFF);     // Send multi-block read command
    if(sdcard_wait_for_token() == 0xFE){
      for(i = 0; i < fsize; i++){
        file_pointer++;
        fdata[i] = sdcard_xfer(0xFF);
        if((file_pointer % 512) == 0){
          for(j = 0; j < 2; j++)
            sdcard_xfer(0xFF);
          if(sdcard_wait_for_token() != 0xFE){
            sdcard_deselect();
            return 1;
          }
        }
      }
      sdcard_deselect();
      return 0;
    }
    sdcard_deselect();
    return 1;
  }
}
int1 get_file_name(int32 file_entry_addr, int8 sname[]){
  int8 buf, i, j = 0;
  for(i = 0; i < 11; i++){
    if(sdcard_read_byte(i + file_entry_addr, &buf) != 0)
      return 1;
    if((i == 0) && (buf == 0xE5))
      return 0;                                  // Deleted file ==> Skip reading (go to next address)
    if((i == 0) && (buf == 0))
      return 1;                                  // End of file names in the root directory
    if(buf != ' '){
      sname[j] = tolower(buf);
      j += 1;
    }
  }
  sname[j] = '\0';
  
  
  if(strlen(sname) > 3){
    j = strlen(sname);
    for(i=j; i > j-3; --i)
      sname[i] = sname[i-1];
    sname[i] = '.';
  }
  sname[j+1] = '\0';
  return 0;
}
#if defined (FILE_CLUSTER_CHECK)
int32 get_next_cluster(int32 my_cluster){
  int32 next_cluster = 0;
  if(fat_type == 0){
    sdcard_read_byte((my_cluster << 1) + FAT_Start + 1, &next_cluster);
    next_cluster <<= 8;
    sdcard_read_byte((my_cluster << 1) + FAT_Start,     &next_cluster);
  }
  else{
    sdcard_read_byte((my_cluster << 2) + FAT_Start + 3, &next_cluster);
    next_cluster <<= 8;
    sdcard_read_byte((my_cluster << 2) + FAT_Start + 2, &next_cluster);
    next_cluster <<= 8;
    sdcard_read_byte((my_cluster << 2) + FAT_Start + 1, &next_cluster);
    next_cluster <<= 8;
    sdcard_read_byte((my_cluster << 2) + FAT_Start,     &next_cluster);
  }
  return next_cluster;
}
#endif
int8 sdcard_wait_for_token(){
  int8 response = 0xFF;
  timeout = 0xFFFF;
  while(timeout && (response != 0xFE)){
    response = sdcard_xfer(0xFF);
    timeout--;
  }
  return response;
}
// End.


Couple of things i forgot to mencioned the SD card is already formatted as FAT. and made based in this
https://simple-circuit.com/wav-player-pic16f877a-sd-card-ccs-c/[/code]
Ttelmah



Joined: 11 Mar 2010
Posts: 19634

View user's profile Send private message

PostPosted: Mon Jan 27, 2025 11:25 pm     Reply with quote

Seriously, have you searched the forum?. 90% of problems with SD's
are not getting the pull-up resistors correct on the connections, then
the capacitor needed by the SD, the voltages involved between the
PIC and the SD, These are discussed in the sticky at the top of the forum
here about interfacing the SD to a 5v PIC.
You need much more basic, and make sure you can read and write a
single sector in the card, and get this working right, _before_ worrying
about handling FAT.
Once you have this working, then go to the code library here There is
an updated set of FAT drivers there, that solves a lot of issues with modern
cards.

However looking at what you post, one critical line:

const int8 *wav = "1.wav";

Either get rid of the 'const' there, or add
#DEVICE PASS_STRINGS=IN_RAM

Otherwise a pointer cannot be constructed to a constant string.
QueroPizzadePao



Joined: 27 Jan 2025
Posts: 4

View user's profile Send private message

PostPosted: Tue Jan 28, 2025 3:19 pm     Reply with quote

Ttelmah wrote:


However looking at what you post, one critical line:

const int8 *wav = "1.wav";

Either get rid of the 'const' there, or add
#DEVICE PASS_STRINGS=IN_RAM

Otherwise a pointer cannot be constructed to a constant string.


Ok so I remove the const from the code and the problem appears to keep happening,
so my sdcard module is connected as

CS -> RD3
SCK -> RC3
MOSI -> RC5
MISO -> RC4
VCC -> 5V
GND -> GND

if it has something off can you point me out?

Do you have any links from other similar problems that can help me out? I need this ready until friday
Ttelmah



Joined: 11 Mar 2010
Posts: 19634

View user's profile Send private message

PostPosted: Wed Jan 29, 2025 2:42 am     Reply with quote

AAArgh!........

Quote:

VCC -> 5V


An SD is a 3.3v device.
Since your PIC can run at 3.3v, I assumed you were running at 3.3v.
If you have connected 5v signals to the SD you will probably have killed
it.
You need level shifting hardware between a 5v PIC and the SD card.
The SD needs a 3.3v supply.

Read the sticky at the top of the forum about how to do the connections.
temtronic



Joined: 01 Jul 2010
Posts: 9342
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 29, 2025 8:34 am     Reply with quote

curious... found this.....
some interesting information....

https://www.sdcard.org/developers/sd-standard-overview/

Seems not all SD cards are created equal........ Crying or Very sad
QueroPizzadePao



Joined: 27 Jan 2025
Posts: 4

View user's profile Send private message

PostPosted: Wed Jan 29, 2025 8:56 am     Reply with quote

Ttelmah wrote:
AAArgh!........

Quote:

VCC -> 5V


An SD is a 3.3v device.
Since your PIC can run at 3.3v, I assumed you were running at 3.3v.
If you have connected 5v signals to the SD you will probably have killed
it.
You need level shifting hardware between a 5v PIC and the SD card.
The SD needs a 3.3v supply.



sorry i forgot to mentioned that I'm using an SD Card module so it's not directly connected to the SD, does this still applies for 3.3v? because the SD card is fine I can safely connect to the computer to edit the files
temtronic



Joined: 01 Jul 2010
Posts: 9342
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 29, 2025 9:18 am     Reply with quote

You really NEED to post the make/model/link to what you're using.
Not all 'modules' are the same !!
QueroPizzadePao



Joined: 27 Jan 2025
Posts: 4

View user's profile Send private message

PostPosted: Wed Jan 29, 2025 9:40 am     Reply with quote

Ok so here is everything I been using for this project

PIC16F877A
MicroSD Card (SD-C02G)
[img]
https://photos.enjoei.com.br/public/1200x1200/czM6Ly9waG90b3MuZW5qb2VpLmNvbS5ici9wcm9kdWN0cy8zMzYxNjM1OC83ZWQ3YTRkZmNiNGQ5MGJiYTM3YzYwYjAyZGMzZDU3Zi5qcGc
[/img]

Micro SD Card Adapter Module

[img]
https://hobbycomponents.com/1427-large_default/microsd-card-adapter-with-level-shifters.jpg
[/img]

PICK 150 for PIC Programing
[img]
https://cdn.snsimg.carview.co.jp/minkara/parts/000/009/261/626/9261626/p1.jpg?ct=bf743b426496
[/img]

And for the PIC powering TS8900-PIC:
[img]
https://ae01.alicdn.com/kf/H1c4cee47902040a5be57a7b12708ee7fD.jpg
[/img]

So this every major thing I been using in this project
temtronic



Joined: 01 Jul 2010
Posts: 9342
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 30, 2025 1:51 pm     Reply with quote

GREAT, looked up the SD interface and it does have level shifters, so should be fine BUT.....you do need the correct pullups and maybe a pulldown resistor?
There's 4 resistors on the PCB, but no schematic.

Check the 'sticky' up top, good info in it !

Also not all SD cards are the same, so some 'timings' may need to be changed.
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