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

Music Player using PIC16F887

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



Joined: 07 Jun 2022
Posts: 19

View user's profile Send private message

Music Player using PIC16F887
PostPosted: Tue Jun 07, 2022 9:14 am     Reply with quote

I'm currently working on a project that uses PIC16F887 to play music. The wav file is stored in a MicroSD card (FAT32 or FAT16 formatted), there will be a SD card reader. The code I'm using keep giving me this error: A numeric expression must appear here (at line 47) and a warning: String truncated (line 17)

Error
ok |= fat_open_file(wav);

Warning
const int8 *wav = "mywav.wav";

Code:

/*
  WAV Audio Player using PIC16F887 microcontroller and SD card CCS C code.
  FAT Library for CCS C compiler must be installed
*/

// SD Card module connections
#define   SDCARD_SPI_HW
#define   SDCARD_PIN_SELECT  PIN_D3
// End SD card module connections

#include <16F887.h>
#fuses NOMCLR, HS, NOBROWNOUT, NOLVP
#use delay(clock = 20MHz)
#use fast_io(D)
#include <FAT_Lib.c>

const int8 *wav = "mywav.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
      if(channel_count == 2){                    // If 2-channel wave file (stereo)
        i++;                                     // increment i
        j = data[i];
        set_pwm2_duty((int16)j);                 // Update PWM2 duty cycle
      }
      while(get_timer1() < 500);                 // Wait some time (about 125us) to update the duty cycles
    }
  }
}
void main(){
  delay_ms(2000);
  setup_ccp1(CCP_PWM);                           // Configure CCP1 as a PWM
  setup_ccp2(CCP_PWM);                           // Configure CCP2 as a PWM
  set_pwm1_duty(0);                              // set PWM1 duty cycle to 0
  set_pwm2_duty(0);                              // set PWM2 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 );    // Timer1 configuration
  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){
    play();
  }
  set_pwm1_duty(0);                              // set PWM1 duty cycle to 0
  set_pwm2_duty(0);                              // set PWM2 duty cycle to 0
}   // End
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 07, 2022 11:25 am     Reply with quote

Quote:
const int8 *wav = "mywav.wav";

Change that line to look like this:
Code:
int8 wav[] = "mywav.wav";

Then it compiled with no errors or warnings.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Jun 08, 2022 3:05 am     Reply with quote

I'm astounded that he is likely to have enough RAM with that chip.
Efficient handling of FAT requires a 512byte buffer. Without this the
speed will be terrible. Probably not good enough for audio.

He might be able to use:

const int8 wav[] = "mywav.wav";

With #DEVICE PASS_STRINGS=IN_RAM

There is also the issue of voltage. The 887, only supports 3.3v
operation up to 10MHz. He is trying to clock at 20MHz. If he is
running the PIC above 3.3v, then he is going to need voltage
level translators to work with the SD card.

Seriously 'look again' at the chip choice. Much better to use a chip that
is rated to work at 3.3v...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 08, 2022 11:32 am     Reply with quote

The project is from here:
https://simple-circuit.com/wav-player-pic16f877a-sd-card-ccs-c/
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Jun 08, 2022 11:47 am     Reply with quote

Which won't work.... Sad

SDI on a 877, is an ST input. At 5v, it requires 4v for Vih. The SD card
only produces just under 3.3v.

Duh.

If the poster had it working, he was running off less than 5v and getting
some overshoot when the signals switched. Or had an example of the
chip that had particularly low Vih.
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