Subject:
|
Play tones with legOS !
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Mon, 29 Nov 1999 03:28:47 GMT
|
Viewed:
|
1065 times
|
| |
| |
Because dsound_play currently doesn't work for me, and because i wanted
to make some noise under legOS with little memory and CPU footprint, i
wrote a little function that plays arbitrary tones without using
interrupts. I allows to play any frequency (in Hz) from 31 to 65535.
The sound timer is programmed just the same way as the infrared carrier
timer. The only tricky thing is to find the best prescaler mode for the
frequency to produce and the correct compare match value. My solution
uses a table for the first task and some rounding arithmetics for the
second.
If you want to play melodies by just copying from your old beatles
songbook, please wait until tomorrow... i'm going to build a header file
containing macros with mode/match settings for all keys on my piano.
This will be even faster than the frequency approach because nothing has
to be computed, just load the timer and go.
#include <sys/h8.h>
typedef struct modeTableEntry
{
unsigned minFreq;
unsigned char CKSmask;
unsigned long base;
} modeTableEntry ;
modeTableEntry modeTable[] =
{
{ 3907, 0x01, 10000000UL },
{ 977, 0x82, 2500000UL },
{ 489, 0x02, 1250000UL },
{ 123, 0x83, 312500UL },
{ 31, 0x03, 78125UL },
};
void playTone(unsigned frequency)
{
modeTableEntry *entry = modeTable;
unsigned long quot10,quot,reminder;
if (frequency < 31) frequency=31;
// find best mode for this freqency
while (frequency < entry->minFreq) ++entry;
// compute compare match Value
quot10 = entry->base / frequency;
quot = quot10 / 10;
reminder = quot10 - (quot*10);
if (reminder < 5) quot-=1;
// start timer
T0_CR = 0x00; // timer off
T0_CNT = 0x00; // counter reset
if (entry->CKSmask & 0x80)
STCR |= 0x01; // ICKS0 = 1
else
STCR &= ~0x01; // ICKS0 = 0
T0_CORA = quot; // set compare match A
T0_CSR = CSR_TOGGLE_ON_A; // Output toggles on compare Match A
T0_CR = CR_CLEAR_ON_A | (entry->CKSmask &0x3); // Go
}
void quiet()
{
T0_CR = 0x00; // timer 0 off
}
|
|
Message has 1 Reply: | | Re: Play tones with legOS !
|
| I can't think of any possible reason why I would ever use this, but it is still cool... it's great to come back and see that people (other than Markus) have been working on stuff. My two cents on all of that tomorrow- -Luis (...) ###...### Profanity (...) (25 years ago, 29-Nov-99, to lugnet.robotics.rcx.legos)
|
2 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|