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

Open .wav file with PIC18F4550

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



Joined: 27 Oct 2023
Posts: 5

View user's profile Send private message

Open .wav file with PIC18F4550
PostPosted: Fri Oct 27, 2023 12:13 pm     Reply with quote

Hello everyone,

I am trying to do a music player with a PIC18F4550 and a SD CARD. I am trying to simulate this in proteus and it doesn't work.
I have searched in this forum (like https://www.ccsinfo.com/forum/viewtopic.php?t=59793&highlight=open+wave+file) for similar questions but I didn't manage to find the solution for me.

I am coding this project step by step. What am I pretending to do first, is to initialize first all the sort of things and commands that I will need to read and open files. Here is my code:

Code:

// WAV Player using PIC16F877A microcontroller and SD card CCS C code.
#include <18f4550.h>
 
#define   SDCARD_SPI_HW                         
#define   SDCARD_PIN_SELECT  PIN_D0             
#define   SDCARD_PIN_SDI     PIN_C7                     
#define   SDCARD_PIN_SDO     PIN_B0         
#define   SDCARD_PIN_SCL     PIN_B1           
 
 
#define   DRAW_WAV_FROM_MMCSD_CARD
 

#fuses nowdt

#fuses HSPLL PLL2 CPUDIV1
#use delay(clock = 48MHz)                 

                       
#use fast_io(C)       
#use fast_io(D)

#include <sdcard.c>             
#include <fat16.c>                 
 
 
const int8 *wav = "miej.wav";
int1 ok = 0;
int8 i, j, data[16], channel_count;
 

void play() {
     output_high(PIN_D5);  //It has read well all the 3 inits and has entered "play" function and light the BLUE led.
}


void main() {
  output_low(PIN_D7);
  output_low(PIN_D6);
  output_low(PIN_D5);

  SET_TRIS_B(0B01);           //MISO (1) y SCK(0)
  SET_TRIS_D(0B00000000);            //SD_CS (0)
  SET_TRIS_C(0B0000000);      //MOSI (0) SPEAKER1(0) SPEAKER2(0)
 

  SETUP_ADC_PORTS(0B1111);     
  SETUP_COMPARATOR(0B111);     
   
  delay_ms(1000);             

  if(sdcard_init() ==0)
  {
    ok=0;
    if(fat16_init() ==0)
    {
      ok=0;
      if(fat16_open_file(wav) ==0)
      {
         ok=0;
      } else {
         ok=1;
      }
    } else {
      ok=1;
    }
  } else {
    ok=1;
  }
 
 
  if(ok ==0){
    output_high(PIN_D6); //green LED on. It has read correctly all three inits.
    play();
  } else {
    output_high(PIN_D7); //red LED on. It hasn't read well at least one of three inits
  }

}



I try first to initialize the sd_card module (I have tried this nitialization alone and it works), then I initialize the fat16 library (I also have tried initializating both of those libraries and they work). Then I try opening the wav file.

This code compiles perfectly on CCS C. I have tested it on proteus.
The idea is initializating all those 3 "libraries" , lighting the green LED, and then entering the void play function, lighting the blue LED. When I simulate this on proteus, anything happens. No led is lighted, although doesn't give any error.

As I said, the previous code that I have tried, was without the "fat16_open_file" command, and it worked fine, lighting the green led.

Any idea on how to open the wav file? Where am I wrong?


Thank you a lot in advance!
Regards,
vis
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 27, 2023 2:31 pm     Reply with quote

hmm..you said..... "I am trying to simulate this in proteus and it doesn't work. "


It's well known that Proteus is NOT a good 'simulator', full of bugs and errors and NONE of the 'schematics' can actually WORK in the real world. Post yours and I'll tell you why.

Need to know what hardware you're using.
So you're using a 5 volt PIC with a 3 volt SD card, what interface do you have between them ???

edited to show that the original posted' said 'it doesn't work'....
sorry for the confusion


Last edited by temtronic on Fri Oct 27, 2023 5:17 pm; edited 1 time in total
vissarion17



Joined: 27 Oct 2023
Posts: 5

View user's profile Send private message

PostPosted: Fri Oct 27, 2023 3:25 pm     Reply with quote

Quote:
hmm.. I am trying to simulate this in proteus and it doesn't work.


Thanks for your reply!

When you say that it doesn't work, do you mean that an error appears, or that nothing happens, no led is lighted (like my proteus)

PD: Remember that, in order to make it work in proteus, you should ensamble the circuit and the ports the same way as I did, with the ".wav" file inside the SD card, the 48MHz defined in the PIC...etc


Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Oct 28, 2023 8:08 am     Reply with quote

There is a sticky about Proteus at the top of the forum. A lot of working
code, Proteus will tell you doesn't work, while a lot of faulty code Proteus
merrily accepts and will tell you will run.
Not a lot of use... :(

On your device. Jay has already asked about the interface to the SD.
Vital. The 5v PIC cannot talk to a 3.3v SD without a suitable interface
between. Again there is a sticky about this.
Now it looks as if you have an 8MHz crystal?.
Your declaration of the file is wrong.

First you need the option pass_strings=IN_RAM

Then declare the file as:

char wav[] = "miej.wav";

That the declaration is wrong is pointed out in the thread from which
you got the original code.

The problem with the signal levels to the SD is also pointed out there. Sad
vissarion17



Joined: 27 Oct 2023
Posts: 5

View user's profile Send private message

PostPosted: Sat Oct 28, 2023 9:45 am     Reply with quote

Quote:
On your device. Jay has already asked about the interface to the SD.
Vital. The 5v PIC cannot talk to a 3.3v SD without a suitable interface
between. Again there is a sticky about this.


Thanks for the response!
In fact, in the past, I have been able to simulate in proteus readings from a SD card to a PIC microcontroller without any problem...this is the only time I have not been able to read a file from an sd card (coincidentally, a .wav file).

Quote:
Your declaration of the file is wrong.

First you need the option pass_strings=IN_RAM

Then declare the file as:

char wav[] = "miej.wav";


Thanks! I just tried it and nothing happens on the proteus neither. :(

Quote:
That the declaration is wrong is pointed out in the thread from which
you got the original code.


Where exactly? Can you pass the link please?

Actually, I think you started a forum thread abput this exact same issue. Did you managed to make it work?

Thanks,
temtronic



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

View user's profile Send private message

PostPosted: Sat Oct 28, 2023 11:05 am     Reply with quote

re: In fact, in the past, I have been able to simulate in proteus readings from a SD card to a PIC microcontroller without any problem..

Obviously Proteus is BROKEN as it is NOT properly 'simulating' real hardware of 5 volt PIC and 3V SD card. In the real world that CANNOT work

It should give several error messages about incompatible hardware,a swe all know you CANNOT connect a 5 v PIC to a 3v SD card and have it work. it can't.
vissarion17



Joined: 27 Oct 2023
Posts: 5

View user's profile Send private message

PostPosted: Sat Oct 28, 2023 11:17 am     Reply with quote

Quote:
re: In fact, in the past, I have been able to simulate in proteus readings from a SD card to a PIC microcontroller without any problem..

Obviously Proteus is BROKEN as it is NOT properly 'simulating' real hardware of 5 volt PIC and 3V SD card. In the real world that CANNOT work


Thanks for your reply!
However, whether the proteus is physically loyal or not, is not the main point of this thread, the main point of this thread is how to open and read a .wav file in CCS C.
Leaving alone the issue of voltages with PIC and SDCard, and imaging that the voltages miraclelly work in the Proteus (as they did for me in previous PIC projects of SDcards), how can I open and read wave files? That same code works perfectly (the Leds lights perfectly) with initializing the sdcard and the fat16 system. The point is that it doesn’t happen anything when trying to open the .wav file[/code][/quote]
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Oct 28, 2023 12:03 pm     Reply with quote

The thread was here:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=59793&highlight=fat
[/url]

You do understand that with the filename declared like this the corresponding
file must be in the directory where the simulation is running. It won't be
found anywhere else (the PIC library does not know about PATH's etc.).

This is a classic example of Proteus being flawed. The PIC has inputs on
some signals that are TTL: compatible (So would see a 3v signal as high),
and on others (like the SPI input), that need higher signal levels (4v on
this one). Proteus does not know this, so accepts the 3.3v signal. A real
PIC will not.....
vissarion17



Joined: 27 Oct 2023
Posts: 5

View user's profile Send private message

PostPosted: Mon Oct 30, 2023 12:01 pm     Reply with quote

Thanks for your reply!

Quote:
The thread was here:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=59793&highlight=fat
[/url]

You do understand that with the filename declared like this the corresponding
file must be in the directory where the simulation is running. It won't be
found anywhere else (the PIC library does not know about PATH's etc.).

This is a classic example of Proteus being flawed. The PIC has inputs on
some signals that are TTL: compatible (So would see a 3v signal as high),
and on others (like the SPI input), that need higher signal levels (4v on
this one). Proteus does not know this, so accepts the 3.3v signal. A real
PIC will not.....


I have checked the thread but in the thread is treated the PIC16F877, which doesn't have enough ROM to run wave files. I am using the PIC18F4550 which does has much more ROM than the PIC16F877 to run .wav files. However, I am not even trying to run .wav files, but just to open them... and I still can't (I also tried to put the .wav file in the correct directories of proteus) Sad

What am I doing wrong exactly? Would it be better to use even another more powerful PIC micro?
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Mon Oct 30, 2023 12:45 pm     Reply with quote

What the others have been strongly hinting is that proteus is a complete waste of not only your time, but ours as well. There is a 'pinned post' near the top of this forum concerning proteus. You should read it.

When you get to the point of having real hardware in front of you, by all means come back for help if you can't get things working. Stop wasting time with proteus.
temtronic



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

View user's profile Send private message

PostPosted: Mon Oct 30, 2023 2:18 pm     Reply with quote

really 'doesn't work'....
OK.. What error message are you getting from trying to open the file ?

A 'file not found' ?
A 'wrong format' ?
A 'checksum error' ?
probably 10-12 other 'errors' you could have......

curious
is the wave file ACTUALLY properly created ?

https://docs.fileformat.com/audio/wav/ is one hit that shows the format.
I see several possible ways to 'corrupt' the file and not be playable.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Tue Oct 31, 2023 7:09 am     Reply with quote

The one I was worried about was the most likely. File Not found.

Point is that the drivers are written on the assumption they are used to access
a SD card. As posted the filename will access the file in the root of the
SD filesystem. However when running in Proteus, you are not looking at
the root of the filesystem. Instead you actually access a SD card simulation
file, and the file you want to access has to be inside this. The actual
simulation is of an MMC card, not an SD card as well.
You have to setup the winimage file you want to use in the Proteus
configuration, and this has to be configured correctly to work.
The driver being used has to support SD/MMC or it won't work at all.
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