Musical dsPIC Example

This example for the dsPIC30F4011 plays a little tune via a speaker connected to pin RD0 via a transistor:

//
// A transistor example for dsPIC
// Written by Ted Burke
// Last updated 26-9-2013
//

#include <xc.h>
#include <libpic30.h>
 
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

void notefreq(double f);

int main()
{
	int n;
    TRISD = 0;
 
    while(1)
    {
		notefreq(659.26);
		notefreq(587.33);
		notefreq(523.25);
		notefreq(587.33);
		notefreq(659.26);
		notefreq(659.26);
		notefreq(659.26);
		notefreq(587.33);
		notefreq(587.33);
		notefreq(587.33);
		notefreq(659.26);
		notefreq(783.99);
		notefreq(783.99);
		__delay32(15000000);
    }
 
    return 0;
}

void notefreq(double f)
{
	double T;
	int n;
	
	T = 1/f;
	
	for(n=0 ; n<f/3 ; ++n)
	{
		_LATD0 = 1;
		__delay32((T/2) * 30000000.0);
		_LATD0 = 0;
		__delay32((T/2) * 30000000.0);
	}
	__delay32(1000000);
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s