Code: Select all
#include <xs1.h>
#include <platform.h>
out buffered port:1 speaker = PORT_SPEAKER;
int main (void)
{
unsigned short count;
unsigned short soundOut = 0;
unsigned int soundRate = 0;
unsigned int soundRateCounter = 0;
short goingUp = 1;
short pitchEnvTimes = 0;
speaker <: 0 @ count; // Find out the current port time
while (1) // 12 bits = 0 to 4000 * 25 Khz = 100 Mhz
{
speaker @ count <: 1;
count += soundOut;
speaker @ count <: 0;
count += 4000 - soundOut;
// Calculate the next soundOutTime //
soundOut += soundRate;
if (soundOut > 4000) soundOut -= 4000;
soundRateCounter++;
if (soundRateCounter > 20)
{
soundRateCounter = 0;
if (goingUp) soundRate++; else soundRate--;
if (soundRate > 400 || soundRate < 0)
{
goingUp = !goingUp;
pitchEnvTimes++;
if (pitchEnvTimes > 6) break;
}
}
}
return 0;
}