PDA

View Full Version : I want to control the volume of the output using Sound command.



billys7
- 12th June 2014, 09:55
I am going to use SOUND command to produce a white noise.

Is there any way to control from the program the volume of the output ?

I want to take a result with max volume and then the volume will decrease to minimum. Something like this |\ |\ |\.

The noise of a steam train engine.

It is here, http://usuaris.tinet.cat/fmco/download/TuShuuu10.asm but i don't know assembly, and here http://usuaris.tinet.cat/fmco/dccfunc_en.html Tushuuu project.

Thank you!

Charlie
- 12th June 2014, 13:33
What specifically are you trying to do? Change the volume in a pattern, or just make it louder / softer?
If it's the latter, changing the final 358 stage to include a variable resistor would be simplest.

billys7
- 12th June 2014, 13:51
I want to make the sound of chuff-chuff.

billys7
- 13th June 2014, 08:28
My issue is that i don't know how to create white noise, without using the SOUND command.
If i knew, it would be easier to me, to make the pattern of the sound i want.

The other choice is to to connect the output of the sound to a transistor and with an interrupt and PWM output to the base of the transistor.

AvionicsMaster1
- 13th June 2014, 20:43
To me, white noise is a random frequency in the audible range. Using the SOUND command gives you a specific frequency. Packing together commands to give you a randomish frequency won't be what I think of as white noise.

To make a specific sound, as the whistle on a train, or music, the chuff-chuff sound, will require you to know the frequency or key of the sound and use the sound command to approximate it.

From experience I've found feeding the output from a 12F683 into a speaker doesn't generate a very "loud" sound. You'll have to amplify it somehow and I don't know why you couldn't use a pin off the the chip to control the volume of the sound. I'm sure someone will shoot a hole in that theory but why not?

I'm thinking if you're going to go through that trouble why don't you get an actual recording of the train whistle or horn signals and put them on a SD card and just have it play into the amplifier? Might be easier and sound better than trying to approximate locomotive horn signals with a PIC.

My nickels worth.

billys7
- 17th June 2014, 12:20
Actually, If i knew the frequency of sound command 255, which is a white noise, it would be easy for me to edit it.
Paco here http://usuaris.tinet.cat/fmco/dccfunc_en.html has made it with Tushuuu and assembly.

He uses TMR2 as PWM generator for sound, Vref module for volume control, TMR1 for Envelope timing, TMR0 for software noise generator, internal comparator for mixing sound and noise.
RB0 is DCC input, RB3 (PWM) conected to RA1 (comparator), RA2 outputs to a buffer OPAMP, RA4 to OPAMP output throught a resistor and to another OPAMP buffer for output to a buzzer.

The size of the circuit is the reason that i need software edit of the sound. The circuit must be very small.

billys7
- 17th June 2014, 12:33
I wrote this, but it is very slow.
The pic is 16F1824, and RA2 is connected to the base of BC547, RC3 to opamp and the output of the opamp to the collector of BC547 and emmiter to speaker.


@ __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
@ __config _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

DEFINE OSC 4

OSCCON = %01101000 '4mhz oscillator

DEFINE CCP3_REG PORTA 'Channel-1 port
DEFINE CCP3_BIT 2 'Channel-1 bit

TRISA = 0
TRISC = 0 ' OUTPUT
ANSELA = 0
ANSELC = 0

OPTION_REG.7=0 ' Weak pullup enabled
OPTION_REG.6=0 ' Interrupt Edg Select bit
OPTION_REG.5=0 ' TMR0CS Timer0 Clock Source select
OPTION_REG.4=0 ' TMR0CS Timer0 Source edge select
OPTION_REG.3=0 ' Prescaller assignemnet (0-> timer0)
OPTION_REG.2=1 ' Prescaller rate
OPTION_REG.1=1 ' Prescaller rate
OPTION_REG.0=1 ' Prescaller rate
'Prescaler 1:256, INTERRUPT every 65,536ms


x var BYTE

ON INTERRUPT GOTO ISR
INTCON = %10100000 ' enable TMR0 and GLOBAL
X = 0

GOTO START

START
sound portc.3, [255, 65535]
goto START


ISR:
disable
X=X+5
IF X>255 THEN
X=0
ENDIF

hpwm 3,x,20000

INTCON.2=0

resume
enable

end

Where is my mistake ?

HenrikOlsson
- 17th June 2014, 13:45
Hi,
First of all, the manual states that the duration part of the sound statement should be 0-255, you're specifying 65535.
Second, and what I think is the main "mistake", is in your interpretation of how ON INTERRUPT works.

ON INTERRUPT isn't a real hardware interrupt. It will only check the interrupt flag between each PBP statement. If you specify a sound duration of 255 the sound command takes 255*12ms=3060ms so the ISR will only execute once ever ~3 seconds even if TMR0 overflows once every 65ms. Had it worked with 65535 as the duration the ISR would only execute every 786th second.

Finally, doing things like IF X>255 THEN when X is declared as a BYTE doesn't do anything because X can never be larger than 255 anyway. You can safely keep adding 5 to it, it will wrap around by itself.

Hope it helps.

/Henrik.

billys7
- 17th June 2014, 16:03
Yes you are right.
How can i make a real interrupt ?

mackrackit
- 17th June 2014, 16:34
You can use
http://darreltaylor.com/DT_INTS-14/intro.html
This is the easy way.

Or you can read in the manual for an explanation on how to write your own.

HenrikOlsson
- 17th June 2014, 18:45
I don't see the need for an interrupt for what you're doing there.

X = 5
Start:
HPWM 3, X, 20000
SOUND PortC.3, [255, 5] ' Make sound for ~60ms
X = X + 5
Goto Start
Though I'd probably setup the CCP module manually and write to the dutycycle register instead of using the HPWM command since I Believe you'll get some discontinuties in the output when constantly changing the dutycycle using HPWM - may be wrong on that and/or it may not matter in your case, try it.

/Henrik.

billys7
- 19th June 2014, 10:00
Totaly, i have tried all the methods you have suggested me, unfortunately the results wasn't so good.
In every case, there was a small interrupt on the sound.

I decided to produce the white noise by an external small circuit, and then to process it with PWM.

Thank you all.