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

Function to generate square waves at desired frequencies

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



Joined: 11 Mar 2007
Posts: 4

View user's profile Send private message AIM Address

Function to generate square waves at desired frequencies
PostPosted: Sun Mar 11, 2007 6:13 pm     Reply with quote

Hello all,

Here is an example program I made to demonstrate how to play sounds with a PIC microcontroller. It utilizes a function I wrote, "playtone()" to create a square wave of a set frequency (Hz) and set duration (ms).

Usage: playsound(float frequency, float duration);

Code:
//////////////////////////////////////////////////////////////////
////                           Playsound.C                    ////
////                                                          ////
////   Author: Kyle J. Morin                                  ////
////   Date: March 11, 2007                                   ////
////                                                          ////
////   Description: This example program shows how to         ////
////   manually generate sounds for a specified amount of     ////
////   time. The function playsound() was created as a        ////
////   a viable, more precise alternative to generate_tone()  ////
//////////////////////////////////////////////////////////////////

#INCLUDE <12F683.h>         // Include the standard header file for the PIC12F683                                   
#USE delay(clock=4000000)   // Tell the delay functions the processor speed (4 MHz) so delay timing is correct   
#FUSES nomclr, nowdt        // Master clear pin used for I/O, no watch dog timer                       


//***************************************************************//
// DESCRIPTION: Plays a specified sound for a specified duration //
// PARAMETERS: Frequency in hertz, duration in milliseconds      //
//***************************************************************//

int16   freq_truncated,   playtime_truncated;   // Declare 16-bit variables for frequency, playtime and delaytime     
int16   i;                                      // Declare i as a 16-bit counter variable (it will be used later in loops)   

void playsound(float frequency, float duration)
{
   frequency = 1000000 / frequency;    // This is to figure out how many microseconds each period will take to complete
   frequency = frequency - 72;         //  Offset this # by -72. This is because the instructions in the sound loop take
                                       //  time to complete and henceforth lower the actual frequency heard. We want precise tones.
   freq_truncated = frequency;         // Convert the 32-bit floating point frequency to a 16-bit number delay functions can understand
      duration = ( duration*1000 )/freq_truncated;  // Figure out how many cycles are required to play sound
   playtime_truncated = duration;      // This number is stored in the 16-bit "playtime_truncated" variable
   shift_right( &freq_truncated,2,0 ); // Divide freq_truncated by 2 using a "shift right" bit-shifting operation
                             
   for ( i=1 ;i <= playtime_truncated; ++i )  // This funciton creates the square wave audio output signal   
   {                                                                                 
      output_high( PIN_A2 );           // Turn PIN_A2 on                                   
      delay_us( freq_truncated );      // Delay for "freq_truncated" microseconds                 
      output_low( PIN_A2 );            // Turn PIN_A2 off                                   
      delay_us( freq_truncated );      // Delay for "freq_truncated" microseconds                 
   }
}

//**************************************************************//
// DESCRIPTION: Main loop                                       //
//**************************************************************//
void main()
{
   while(TRUE)                          // Loop forever                                                                     
   {                                                                                                   
      playsound( 500.0, 1000.0 );       // Create a 500 Hz square wave for 1 second
      delay_ms( 1000 );                 // Delay for 1 second
   }
}
longmenok



Joined: 23 Oct 2006
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 18, 2007 4:57 am     Reply with quote

Thanks !!!
emelyjose



Joined: 13 Nov 2016
Posts: 5

View user's profile Send private message

PostPosted: Wed Nov 16, 2016 10:10 am     Reply with quote

How do you implement the code to use it?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Nov 22, 2016 5:18 pm     Reply with quote

1- what is the reason for using floats in the call to playsound() , when you immediately move them to 16 bit space.

do you know what happens when you ask for a frequency of 75000 hz ?

2-WHAT happens to playtime_truncated
if you attempt an assignment greater then 0xFFFF ??

3- do you know CCS has an unsigned int32 ??
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