EasyC 4 sound library for cortex

So I’m posting a project for everyone to BETA TEST as it’s not really production code.

This project is a demo for a sound library I hacked together today that allows tones to be played both synchronously and asynchronously from EasyC on the cortex. The demo is tested using EasyC V and plays mission impossible (thanks ROBOTC) while driver control is running.

User functions are as follows.

void PlayFrequency( int freq, int amplitude, int timems );
void PlayFrequencyWait( int freq, int amplitude, int timems );
void ClearSounds( void );

PlayFrequency does as it says, freq is in hertz, amplitude can vary between 0 (off) and 255 (full, may be too much). timems is the time this sound should play in milliseconds, it runs on a repeating timer for duration. The first time this is called the initialization of the DAC happens.

PlayFrequencyWait is the same but blocks until the sound is done.

ClearSounds just stop the currently playing sound.

There are also alternative functions to play tones in the background. Probably best not to mix these with the above, use one or the other.

void	InitChipToneMusic( int len );
void	SetNextTone( int freq, int amplitude, int timems );
void	StartChipTone( int repeat );

InitChipToneMusic allocates storage for “len” samples each of which is a triplet of freq, amplitude and time.

See the included example on how to use but it goes basically like this.

// play mission impossible asynchronously

  InitChipToneMusic( 66 );

  SetNextTone(  880,  128,  70);  // Note(D, Duration(32th))
  SetNextTone(  933,  128,  70);  // Note(D#, Duration(32th))
  SetNextTone(  880,  128,  70);  // Note(D, Duration(32th))
  // skipped code

  SetNextTone(  932,  128, 140);  // Note(A#5, Duration(16th))
  SetNextTone(  784,  128, 140);  // Note(C, Duration(16th))
  SetNextTone(    0,  128, 500);  // 
  StartChipTone(1); // 1 means repeat


The sound library uses Timer7 and DMA2 channel 3, I don’t think these things clash with anything else in EasyC but have not tested all the sensors so use this code at you own risk for now. The code is based on examples in the STM32 standard peripheral library, I included some of the DAC code in vsl.c to simplify things but there is still a folder of header files.

Hopefully Intelitek will have some official code soon.


1 Like

I just downloaded your zip file. Im going to try this out tomorrow.

Actually, here is an update, now you can play ring tones directly in RTTTL format such as.

char tune4] = "Beethoven:d=4,o=6,b=125:8e,8d,8e,8d,8e,8b5,8d,8c,a5,8p,8c5,8e5,8a5,b5,8p,8e5,8g5,8b5,c,8p,8e5,8e,8d,8e,8d,8e,8b5,8d,8c,a5,8p,8c5,8e5,8a5,b5,8p,8e5,8c,8b5,a5";

See the demo for details, one new function added. (an example)

PlayRtttl( tune5, 128, 1 );

This plays the ring tone stored in the array “tune5” at amplitude 128 and repeats in a loop (the final 1).

The demo has 5 tunes included, change using joystick button 8R and 8L.

There are dozens of RTTTL tunes on the internet, anything you want.


1 Like

Do I just download the code straight into my cortex and have the speaker in the speaker port?
Edit: is there any way I can use a speaker on a microcontroller? I am working on a project currently and im using an old brain that we have and I would like to use my new speaker.

It’s a complete EasyC project so just open and download. It will write some information on the LCD if that is connected, the project I posted today can use the joystick 8R and 8L to change tunes.

If you want to add this to a new project the following files should be moved into the project directory.

the vsl_include folder with everything in it.

vsl.c and vslrtttl.c need to be added as user source files and the following added to the UserInclude.h file along with any functions you use to call them.

void PlayFrequency( int freq, int amplitude, int timems );
void ClearSounds( void );
void InitChipToneMusic( int len );
void SetNextTone( int freq, int amplitude, int timems );
void StartChipTone( int repeat  );
char *PlayRtttl( char *song , int amplitude, int repeat );

I seem to remember that Quazar posted something for sound on the PIC last year, I was looking for it yesterday but could not find it, I will look again.

1 Like

I will definately give the code a try tomorrow. I will look also to see if I can use it on PIC.

The code I posted will only work on a cortex, it accesses the cortex hardware directly, it will not work on a PIC. Let me look again for Quazar’s code.

1 Like

I don’t recall doing audio support for the PIC, though it shouldn’t be too challenging if you don’t mind dedicating a motor port (not sure if the new Vex speaker would survive the voltage of a motor port!). This whitepaper describes the timer gymnastics, and if you switch the motor port to user processor control, the timer can drive it directly.

If you can chain into an interrupt handler under EasyC, then you could get it to work with one of the I/O ports.

Instead of PIC support, I can offer a utility function for ROBOTC+Cortex that lets you pass in a music string and play it as a series of notes/rests. Here is the wiki page.


  • Dean
1 Like

That’s what I remember you posted before, guess I was wrong about the PIC sound support. I was going to port the RTTTL code I posted yesterday to ROBOTC but ran into the 20 char limit on strings so gave up for now. It was going to be hard without pointers anyway and ROBOTC has a utility to convert RTTTL to a function with suitable PlayTone commands.

1 Like

I tried your code out today. it worked perfectly. it was really weird hearing super mario and pac man coming out of my robot. I really hope that the speaker is not legal in competition because it got annoying just after a few seconds.

1 Like

Glad it worked for you, I agree that it can be really annoying, it’s not something I’m going to encourage on team 8888’s robot but it may be fun for the demonstrations we do at elementary schools.

I’m posting another version with the code cleaned up a bit and some null pointer and invalid parameter checking. There is no change in functionality, I just wanted to clean it up a little (the old versions have been removed). I will also email Intelitek and hopefully they can incorporate some official functions soon, when they do that this code should not be used as it will conflict with their code.

Functions available are as follows, see the source and demo code for explanations.

void PlayFrequency( int freq, int amplitude, int timems );
void PlayFrequencyWait( int freq, int amplitude, int timems );
void ClearSounds( void );

int  InitChipToneSong( int len );
void DeinitChipToneSong( void );
void StartChipToneSong( int repeat );
void SetNextChipTone( int freq, int amplitude, int timems );
void DebugChipTone( void );

char *PlayRtttl( char *song , int amplitude, int repeat );

sounddemo.zip (110 KB)

1 Like

I’m just bumping this as it has some explanation of the sound library that I wrote back in April. The code attached to Post #11 is the same that Kingof1337 attached to the sticky in the EasyC tech forum

1 Like