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 support@ccsinfo.com

Robot / Pic Project Sound Generator Chip - RS232

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Arizona Chris



Joined: 20 Dec 2014
Posts: 66
Location: Arizona

View user's profile Send private message Visit poster's website

Robot / Pic Project Sound Generator Chip - RS232
PostPosted: Sun Sep 13, 2015 12:47 pm     Reply with quote

This allows you to take the load off your main processor and generate 13 different interesting chirps, peeps, whoops and sirens with your PIC project. I developed these over the years building various robots. The sound chip is a standard PIC16F628a processor and can be addressed with a single byte from any master processor using RS232 protocol and 9600kb speed.

I also have a complete web write up, with schematics and a sound clip:

http://www.schursastrophotography.com/PICprojects/soundchip.html

Here is an example of what the master processor would send over a single wire to the PIC sound chip for a sound:

Code:


#use rs232(baud=9600, xmit=Pin_B2, bits=8, parity=N,STREAM=SOUND)


   s=2  //can be 1 - 13

   fputc(s,SOUND);



Next, here is the actual code for the sound chip - a standard 16F628a. You can see how much ROM space we are saving on our main processor!

Code:


//****************************************************************************
//Chris Schur
//(soundchp 16F628)
//Date:  9/7/15
//****************************************************************************

/*Description of this Program:

This program sends out 13 different robot sounds via serial selection from another processor.

*/


//I/O Designations ---------------------------------------------------
// RA0: 
// RA1: 
// RA2: 
// RA3: 
// RA4:  (Open Collector output)
// RA5:  (MCLR)
// RA6:  Xtal  Output
// RA7:  Xtal  Input

// RB0:  SERIAL INPUT
// RB1: 
// RB2: 
// RB3:  SOUND OUT
// RB4: 
// RB5: 
// RB6: 
// RB7: 


//--------------------------------------------------------------------

//Include Files:
#include <16F628.h>  //Normally chip, math, etc.  used is here.

//Directives and Defines:

#fuses NOPROTECT,HS,NOWDT   //xtal is used
#use delay(crystal=10MHz)   //xtal speed
#use fast_io(ALL)          //must define tris below in main when using this
#use rs232(baud=9600, rcv=Pin_B0, bits=8, parity=N)

#define SPKR Pin_B3

//****************************************************************************
//Global Variables:
int16 n; //counting var
int16 d;  //sound delay var
int8 x;  //counting var

int8 sound = 0;  //selection for sound produced

//****************************************************************************
 
   
//Functions/Subroutines, Prototypes:

void beep1(void); 
void beep2(void);
void beep3(void);
void euro1(void);
void euro2(void);
void rampdn(void);
void rampup(void);

void tripplewhoop(void);
void siren(void);
void cricket(void);
void tripplecricket(void);
void twodutybeeps(void);
void twodutybeeps2(void);

//****************************************************************************
//-- Main Program
//****************************************************************************

void main(void) {

   // Set TRIS I/O directions, define analog inputs, comparators:
      set_tris_A(0b01111110);
      set_tris_B(0b11110001);
     
      //(analog inputs digital by default) 

   //Initialize variables and Outputs:  --------------------------------------
   
   output_low(SPKR);
   
   //Setup for timers, PWM, and other peripherals:
   
   //----------------------------------------------------------------

//MAIN LOOP:



while (true)   {

sound = getc();//select sound

  switch (sound) {
 
  case 0:
 
      output_low(SPKR);
      break;
     
  case 1:
     
      beep1();
      sound = 0;
      break;
 
  case 2:
 
      beep2();
      sound = 0;
      break;
 
  case 3:
 
      beep3();
      sound = 0;
      break;
 
  case 4:
 
      euro1();
      sound = 0;
      break;
 
  case 5:
 
      euro2();
      sound = 0;
      break;
 
  case 6:
 
      rampdn();
      sound = 0;
      break;
 
  case 7:
 
      rampup();
      sound = 0;
      break;
     
  case 8:
 
      tripplewhoop();
      sound = 0;
      break;
     
  case 9:
 
     siren();
     sound = 0;
     break;
     
  case 10:
 
      cricket();
      sound = 0;
      break;
     
  case 11:
 
      tripplecricket();
      sound = 0;
      break;
     
  case 12:
 
      twodutybeeps();
      sound = 0;
      break;
     
  case 13:
 
      twodutybeeps2();
      sound = 0;
      break;
 
    }  //switch
   
  }  //while
 
}  //main

//********* Functions which have prototypes at top of program ****************

//Beep 1:                     //Short single beep
void beep1(void)  {
   for(n=1; n<=50; n=n+1)  {
   output_high(SPKR);
   delay_ms(1);
   output_low(SPKR);
   delay_ms(1);
   }
   return; } 
   
   
//Beep2:                      //double beep
void beep2(void) {
beep1(); 
delay_ms(50);
beep1();
return;  }


//Beep3:                       //Low tone beep
void beep3(void)  {
   for(n=1; n<=25; n=n+1)  {
   output_high(SPKR);
   delay_ms(2);
   output_low(SPKR);
   delay_ms(2); }
   return;  }
   
   
//Euro Beep1:                 //fast Two tone three times
void euro1(void)  {
   beep1();
   beep3();
   beep1();
   beep3();
   beep1();
   beep3();
   return;   }
   
   
//Euro Beep2:                  //standard slow beep
void euro2(void)  {
   for(n=1; n<=400; n=n+1)  {
   output_high(SPKR);
   delay_us(650);
   output_low(SPKR);
   delay_us(650);
   }
   
   for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(1200);
   output_low(SPKR);
   delay_us(1200); }
   
   for(n=1; n<=400; n=n+1)  {
   output_high(SPKR);
   delay_us(650);
   output_low(SPKR);
   delay_us(650);
   }
   
   for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(1200);
   output_low(SPKR);
   delay_us(1200); }
   
   for(n=1; n<=400; n=n+1)  {
   output_high(SPKR);
   delay_us(650);
   output_low(SPKR);
   delay_us(650);
   }
   
   for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(1200);
   output_low(SPKR);
   delay_us(1200); }
   
   return;   }
   
   
//Ramping tones -----------------------------

void rampdn(void)  {    //high tone to low ramp

d=1500;

for(n=1; n<=500; n=n+1)  {
   output_high(SPKR);
   delay_us(d);
   output_low(SPKR);
   delay_us(d);
   d++;   }
   
   return;   }
   
   
   
void rampup(void)  {     //low tone to high ramp

d=1500;

for(n=1; n<=500; n=n+1)  {
   output_high(SPKR);
   delay_us(d);
   output_low(SPKR);
   delay_us(d);
   d--;   }
   
   return;   }
   
   
void tripplewhoop(void)  {  //go  1200 to 600 us

  for(x=1; x<=3; x++)  {
   
   d=1200;
   
   for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(d);
   output_low(SPKR);
   delay_us(d);
   d = d-3;
    }
   
    delay_ms(100);
   
  }
   
   return;   }
   
   
void siren(void)  {  //sort of a sick sounding up down wail

   rampup();
   rampdn();

}

//  CREATURE SOUNDS:

void cricket(void)   {  //single chirp



for (x=0; x<=10; x++)  {
      output_high(SPKR);
      delay_us(250);
      output_low(SPKR);
      delay_us(250);    }
     
      delay_ms(20);
     
      for (x=0; x<=10; x++)  {
      output_high(SPKR);
      delay_us(312);
      output_low(SPKR);
      delay_us(312);    }
     
      delay_ms(20);
     
for (x=0; x<=10; x++)  {
      output_high(SPKR);
      delay_us(312);
      output_low(SPKR);
      delay_us(312);    }
     
      delay_ms(20);
     
for (x=0; x<=10; x++)  {
      output_high(SPKR);
      delay_us(312);
      output_low(SPKR);
      delay_us(312);    }
     
      delay_ms(20);
     
for (x=0; x<=10; x++)  {
      output_high(SPKR);
      delay_us(312);
      output_low(SPKR);
      delay_us(312);    }
     
      delay_ms(20);
   

}


void tripplecricket(void)  {

cricket();
delay_ms(600);
cricket();
delay_ms(600);
cricket();  }



void twodutybeeps(void)  {   //1 khz beeps

//first beep 50% duty:
for(n=1; n<=100; n=n+1)  {
   output_high(SPKR);
   delay_us(500);
   output_low(SPKR);
   delay_us(500);  }
   
//second beep 10% duty:
for(n=1; n<=100; n=n+1)  {
   output_high(SPKR);
   delay_us(25);
   output_low(SPKR);
   delay_us(975);  }

}

void twodutybeeps2(void)  {   //1 khz beeps

//first beep 50% duty:
for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(250);
   output_low(SPKR);
   delay_us(250);  }
   
//second beep 10% duty:
for(n=1; n<=200; n=n+1)  {
   output_high(SPKR);
   delay_us(25);
   output_low(SPKR);
   delay_us(475);  }

}


Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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