EmbeddedRelated.com
Forums

Sound with MSP430

Started by gerdknese June 9, 2004
Hi Folks,

does anybody have a simple sourcecode for a sound generator (if 
possible in "C").
Connecting a piezo via a FET to a port of the MSP it's no problem to 
produce a sounds, but I'm searching for a code to produce good, 
music like, sounds folling the scale to play a melody.
The MSP430 is runnig with MCLK=8MHz so it should be no problem for 
higher frequencies.

See you,
Gerd


Beginning Microcontrollers with the MSP430

Hi,

in the archive you should find 1-bit-sound.

Rolf

gerdknese schrieb:

>Hi Folks,
>
>does anybody have a simple sourcecode for a sound generator (if 
>possible in "C").
>Connecting a piezo via a FET to a port of the MSP it's no problem to 
>produce a sounds, but I'm searching for a code to produce good, 
>music like, sounds folling the scale to play a melody.
>The MSP430 is runnig with MCLK=8MHz so it should be no problem for 
>higher frequencies.
>
>See you,
>Gerd
>
>
>
>
>.
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>  
>


Not C asm, but simple. This code generates sine waves in 64 steps. 
rather than use PWM, because I didn't want to use the full count of 
CCRx0, and because I already had a spare DAC channel I wrote this little 
thing. The output is RC filtered then fed to an LM386 via a digital POT 
volume control. It uses quite a decent speaker, and the results are 
quite good. To change the frequency simply change the interval between 
interrupts, or, alternatively chanbge the step size, ie use a fixed 
interval and have different pointers for each tone. this allows multiple 
tones to sound together. For example tone (the lowest tone) 1 will 
incre,ment the step through the table by 1 each time, while tone 2 might 
increment by 2 steps, or even 3/7 steps (in this case a real time table 
helps spread the steps to get a mean of 3/7) It's all easier than it
sounds.

Cheers

Al

;****** Timer_B 0 is tone

TB0_ISR:				
	ADD	&TONE,&CCRB0		;SET NEXT INT TIME
         BIS	#CCIE,&CCTLB0		;COMPARE MODE OUT DISCONNECTED
	PUSH	&P5OUT			;PRESERVE THE ORIGINAL PORT DATA
	PUSH	&P4OUT
	MOV.B	#SINE_TABLE(R10),&P5OUT	;R10 RESERVED AS TONE POINTER
	INC	R10
	AND	#003FH,R10		;64 ENTRIES?
	BIS.B	#A0+A1,&P4OUT		;SELECT CHANNEL 4 ON QUAD DAC
	NOP
	BIC.B	#DACSEL,&P4OUT
	NOP
	POP	&P4OUT
	POP	&P5OUT
	POP	R10
	RETI

	EVEN

SINE_TABLE:
	DB	128,140,153,165,177,188,199,209,218,227,234,241,246,250,253,255
	DB	255,253,250,246,241,234,227,218,209,199,188,177,165,153,140,128
	DB	127,116,103,91,79,68,57,47,38,29,22,15,10,6,3,1
	DB	0,3,6,10,15,22,29,38,47,57,68,79,91,103,116,127


gerdknese wrote:
> Hi Folks,
> 
> does anybody have a simple sourcecode for a sound generator (if 
> possible in "C").
> Connecting a piezo via a FET to a port of the MSP it's no problem to 
> produce a sounds, but I'm searching for a code to produce good, 
> music like, sounds folling the scale to play a melody.
> The MSP430 is runnig with MCLK=8MHz so it should be no problem for 
> higher frequencies.
> 
> See you,
> Gerd
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 


Hi Rolf,

sorry but I couldn't find the archive.
Can you please help me to find it!

Gerd

--- In msp430@msp4..., "Rolf F." <rolf.freitag@d...> wrote:
> Hi,
> 
> in the archive you should find 1-bit-sound.
> 
> Rolf
> 
> gerdknese schrieb:
> 
> >Hi Folks,
> >
> >does anybody have a simple sourcecode for a sound generator (if 
> >possible in "C").
> >Connecting a piezo via a FET to a port of the MSP it's no problem 
to 
> >produce a sounds, but I'm searching for a
code to produce good, 
> >music like, sounds folling the scale to play a melody.
> >The MSP430 is runnig with MCLK=8MHz so it should be no problem 
for 
> >higher frequencies.
> >
> >See you,
> >Gerd
> >
> >
> >
> >
> >.
> >
> > 
> >Yahoo! Groups Links
> >
> >
> >
> > 
> >
> >
> >  
> >


Hi,

with google you can find descriptions with algorithms:

http://centauri.ezy.net.au/~fastvid/picsound.htm

Rolf


gerdknese schrieb:

>Hi Rolf,
>
>sorry but I couldn't find the archive.
>Can you please help me to find it!
>
>Gerd
>
>--- In msp430@msp4..., "Rolf F." <rolf.freitag@d...> wrote:
>  
>
>>Hi,
>>
>>in the archive you should find 1-bit-sound.
>>
>>Rolf
>>
>>gerdknese schrieb:
>>
>>    
>>
>>>Hi Folks,
>>>
>>>does anybody have a simple sourcecode for a sound generator (if 
>>>possible in "C").
>>>Connecting a piezo via a FET to a port of the MSP it's no
problem 
>>>      
>>>
>to 
>  
>
>>>produce a sounds, but I'm searching for a code to produce good,

>>>music like, sounds folling the scale to play a melody.
>>>The MSP430 is runnig with MCLK=8MHz so it should be no problem 
>>>      
>>>
>for 
>  
>
>>>higher frequencies.
>>>
>>>See you,
>>>Gerd
>>>
>>>
>>>
>>>
>>>.
>>>
>>>
>>>Yahoo! Groups Links
>>>
>>>
>>>
>>>
>>>
>>>
>>> 
>>>
>>>      
>>>
>
>
>
>
>.
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>  
>


Ahh,

now everything is clear to me.

Thank you,

Gerd


--- In msp430@msp4..., "Rolf F." <rolf.freitag@d...> wrote:
> Hi,
> 
> with google you can find descriptions with algorithms:
> 
> http://centauri.ezy.net.au/~fastvid/picsound.htm
> 
> Rolf
> 
> 
> gerdknese schrieb:
> 
> >Hi Rolf,
> >
> >sorry but I couldn't find the archive.
> >Can you please help me to find it!
> >
> >Gerd
> >
> >--- In msp430@msp4..., "Rolf F." <rolf.freitag@d...> 
wrote:
> >  
> >
> >>Hi,
> >>
> >>in the archive you should find 1-bit-sound.
> >>
> >>Rolf
> >>
> >>gerdknese schrieb:
> >>
> >>    
> >>
> >>>Hi Folks,
> >>>
> >>>does anybody have a simple sourcecode for a sound generator (if

> >>>possible in "C").
> >>>Connecting a piezo via a FET to a port of the MSP it's no 
problem 
> >>>      
> >>>
> >to 
> >  
> >
> >>>produce a sounds, but I'm searching for a code to produce
good, 
> >>>music like, sounds folling the scale to play a melody.
> >>>The MSP430 is runnig with MCLK=8MHz so it should be no problem 
> >>>      
> >>>
> >for 
> >  
> >
> >>>higher frequencies.
> >>>
> >>>See you,
> >>>Gerd
> >>>
> >>>
> >>>
> >>>
> >>>.
> >>>
> >>>
> >>>Yahoo! Groups Links
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> 
> >>>
> >>>      
> >>>
> >
> >
> >
> >
> >.
> >
> > 
> >Yahoo! Groups Links
> >
> >
> >
> > 
> >
> >
> >  
> >


Go to the MSP430 Yahoo group website and click on FILES. There is a file 
in there which allows both recoridng and playback of sounds at 
reasonable quality. The memory requirement is 2K/second at 16k samples a 
second. As an alternative you can simply create a table of values that 
represent a single cycle of a sine wave (you actually only need a 
quarter cycle, but you must then apply a little logic and maths to 'walk 
the table'.

These values, output sequentially to a DAC, or used as the values for a 
PWM will give a single tone whose frequency is dependant upon the period 
of the PWM signal, or time between outputting each value. You can also 
make the tabel smaller, or, instead of incrementing the table pointer by 
1, you could use a larger value to deal with higher frequencies.

Unlike the recording software this is limited to tones, and 'chords', 
but it requires very little memory to implement.

the following code shows a simple program to generate a single tone 
using a 64 entry sine table. The output frequency at 8MHz is 
8,000,000/(TONE * 64) or 125,000/TONE. Uisng the full table at a minimum 
INT period of 100 clock cycles this gives a maximum frequency of 
1.25kHz, but by using a step size of up to 16, and a simple output 
filTER FREQUENCIES UP TO 20kHz could readily be produced. using a 12 bit 
table and DAC or PWM would give better results. With a dedicated data 
port for the DAC, and DAC operation restricted to the ISR this program 
can be reduced even further, as shown below the first example.

;****** Timer_B 0 is Buzzer tone

TB0_ISR:				
	ADD	&TONE,&CCRB0. 		;TONE holds ISR period
         BIS	#CCIE,&CCTLB0		;make sure int is enabled
	PUSH	&P5OUT			;PRESERVE PORTS IN CASE THEY
	PUSH	&P4OUT			;WERE IN USE
	MOV.B	SINE_TABLE(R10),&P5OUT	;OUTPUT NEXT VALUE
	INC	R10			;R10 IS THE TBLE POINTER
	AND	#003FH,R10		;LIMIT TO 6 BITS
	BIC.B	#DACSEL,&P4OUT		;SELECT THE DAC
	NOP				;
	BIS.B	#DACSEL,&P4OUT		;BY STROBING IT
	POP	&P4OUT
	POP	&P5OUT
	RETI

TB0_ISR:				
	ADD	&TONE,&CCRB0 		;TONE holds ISR period
         BIS	#CCIE,&CCTLB0		;make sure int is enabled
	MOV.B	SINE_TABLE(R10),&P5OUT	;OUTPUT NEXT VALUE
	INC	R10			;R10 IS THE TBLE POINTER
	AND	#003FH,R10		;LIMIT TO 6 BITS
	RETI

SINE_TABLE:
	DB	128,140,153,165,177,188,199,209,218,227,234,241,246,250,253,255
	DB	255,253,250,246,241,234,227,218,209,199,188,177,165,153,140,128
	DB	127,116,103,91,79,68,57,47,38,29,22,15,10,6,3,1
	DB	0,3,6,10,15,22,29,38,47,57,68,79,91,103,116,127

This is, of course, a sinusoidal tone generator. The output is 
surprisingly good, depending upon the speaker quality, however if you 
only wanted square wave signals there are other ways to do it. For 
example you could decide that you wanted a maximum of 4 tones, and 
assign a timer to each (simplest but wasteful of resources) by 
outputting a square wave tone on the timer output pin and OR'ing the 4 
pins you would get the effect of crude polyphony. But, since Fout now 
equals CLOCK/2*period an 8MHz clock gives you a 40kHz tone when the 
period between interrupts is 100 clocks. You could also make use of this 
fact to use  asingle timer to create a similar polyphony. Either using 4 
pins, and OR'ing them (each pin controlled by the baseline timer, or by 
doing a little calculation and controlling a single pin. For clues onn 
how to do this, creating complex polyphony from a single timer study the 
operation of the CVSD like algorithm I first pointed you at on the FTP 
site. This uses a binary pattern to reproduce complex waveforms. 
Calculating the waveforms is simply a case of adding calculations for 
multuiple pure tones together. This method can be almost as good quality 
as a 12 bit sinusoidal table based system, yet it handles higher 
frequencies. It is a bit more compute intensive though, so it is best 
used in systems with plenty of processing averhead.

Well, enough about sounds, the granddaughter is screaming to be fed, so 
I'd better leave the rest to your imagination.

cheers

Al