PDA

View Full Version : Lookup table or Math?



ronjodu
- 16th September 2007, 16:22
I'm controlling a TI PGA 2320 volume control chip for a stereo amp I built. Chip works great but I would like to display the Gain in the serial comm window. (eventually on a serial LCD)


I'm wondering if a lookup table is the way to go or can the math be done with PBP2.47.
I'm not looking for the code to be written for me, just a nudge in the right direction.

Here is a blurb from the TI datasheet:

GAIN SETTINGS
The gain for each channel is set by its corresponding 8-bit
code, either R[7:0] or L[7:0]; see Figure 2. The gain code
data is straight binary format. If we let N equal the decimal
equivalent of R[7:0] or L[7:0], then the following
relationships exist for the gain settings:
For N = 0:
Mute Condition. The input multiplexer is connected to
analog ground (AGNDR or AGNDL).
For N = 1 to 255:
Gain (dB) = 31.5 − [0.5 • (255 − N)]
This results in a gain range of +31.5dB (with N = 255) to
−95.5dB (with N = 1).


Here is the code I've come up with to control the chip. Note that the GetGain subroutine doesn't work correctly.


DEFINE LOADER_USED 1
define OSC 4

'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::

INCLUDE "MODEDEFS.BAS" ' Include Shiftin/out modes

DEFINE HSER_RCSTA 90h ' setup receive register
DEFINE HSER_TXSTA 20h ' setup transmit register
DEFINE HSER_BAUD 4800 ' setup baud rate

OPTION_REG.7 = 0

'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::

' Alias pins
RST VAR PORTC.1
IO VAR PORTC.2 'data pin
SCLK VAR PORTC.3 'clock pin
led var PORTB.0 'heartbeat
Butt1 VAR PORTB.4
Butt2 VAR PORTB.5
Butt3 VAR PORTB.6
Mute var PORTB.7
'lcd var PORTC.4
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
' Program Variables
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::

' Allocate variables
leftvolume VAR BYTE
rightvolume VAR BYTE
loops var word
hold var byte
Gain var word
setting var word
leftvolume = 100 '0 = no gain, 255 = max gain
rightvolume = 100 '0 = no gain, 255 = max gain
gain = 0
setting = 0
mute = 0
PortB.2 = 0
loops = 0
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
TRISA = %00111111 'Set PORTA RA0-RA5 to input
TRISB = %00000000 'Set PORTB to all output except RB0/INT
PORTB = %00000000 'Set all outputs to 0 (off)
TRISC = %10000000 'Set PORTC to all output
PORTC = %00000000 'Set all outputs to 0 (off)
'ADCON1 = 7
Low RST ' Reset RTC
Low SCLK
high PortB.2
read 1, leftvolume 'Read from EEprom and initialize variable
read 2, rightvolume
read 3, mute
read 4, PortB.2
pause 500
mainloop:
if loops = 2500 then
gosub heartbeat
Low RST
SHIFTOUT IO,SCLK,1,[leftvolume, rightvolume]
high rst
else
loops = loops + 1
endif
GoTo mainloop 'Do it forever
heartbeat:
toggle led
loops = 0
button Butt1,0,100,10,hold,1,Increasegain
button Butt2,0,100,10,hold,1,Decreasegain
button Butt3,0,254,255,hold,1,Quiet
return
Increasegain:
if PortB.2 = 1 then return
leftvolume = leftvolume + 1
rightvolume = rightvolume + 1
HSerout ["Left = ",dec leftvolume, 13,10]
HSerout ["Right = ",dec rightvolume, 13,10]
gosub getgain
HSerout ["Gain = ",dec gain, 13,10]
write 1, leftvolume
write 2, rightvolume
return
Decreasegain:
if PortB.2 = 1 then return
leftvolume = leftvolume - 1
rightvolume = rightvolume - 1
HSerout ["Left = ",dec leftvolume, 13,10]
HSerout ["Right = ",dec rightvolume, 13,10]
gosub getgain
HSerout ["Gain = ",dec gain, 13,10]
write 1, leftvolume
write 2, rightvolume
return
Quiet:
toggle Mute 'Mute or unmute
PortB.2 =~ mute 'Led on muted
write 3, mute 'save mute status
write 4, PortB.2 'save mute led status
if PortB.2 = 1 then
HSerout ["Muted",13,10]
else
HSerout ["Left = ",dec leftvolume, 13,10]
HSerout ["Right = ",dec rightvolume, 13,10]
endif
return

GetGain:
setting = (255 - leftvolume)* 5 'Gain = 31.5 - [0.5 • (255 - N)]
gain = 315 - setting
return
end

Thanks for any help.

16f876, MCS3.0,Melabs Loader, PBP2.47, LABx2

Acetronics2
- 16th September 2007, 18:25
Hi,

Your Gain calculation is correct ...

BUT can only be used for Displaying ( LCDOUT SDEC Gain ...)

some easy formatting will give you the "real" value ...

Also note Decimals are here only for odd values of N ... and are only 0 or 5 ... !!!

See 18S20 temp. sensor Threads for similar values displaying ...

Alain

ronjodu
- 16th September 2007, 22:22
Thanks for the quick reply. After reading your post I took a closer look at the data I am sending to the serial window. I see that while the volume increases the gain setting is also increasing up to 65531 and then overruns to "0". From there up to full volume the gain numbers track as expected if divided by 10.

Left = 188
Right = 188
Gain = 65516
Left = 189
Right = 189
Gain = 65521
Left = 190
Right = 190
Gain = 65526
Left = 191
Right = 191
Gain = 65531
Left = 192
Right = 192
Gain = 0 HERE!!!!!!!!!!!!!!!!!!!
Left = 193
Right = 193
Gain = 5
Left = 194
Right = 194
Gain = 10
Left = 195
Right = 195
Gain = 15
Left = 196
Right = 196
Gain = 20
/////////////////////////////////////////////////////////
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\
Left = 253
Right = 253
Gain = 305
Left = 254
Right = 254
Gain = 310
Left = 255
Right = 255
Gain = 315

This tells me that I need to send the remainder of (65536 - gain) up to gain = 0 and then send the actual gain value/10.

I think.

Am I on the right track?

BigWumpus
- 16th September 2007, 22:33
Yes,
remember: PBP up to 2.47 couldn't calculate with negative numbers.

Check out the twos-complement for negative numbers,
check the MSB of the result and get the twos-complement of the result, print a "-" in front and print the rest of the result.

ronjodu
- 16th September 2007, 23:02
Thanks for the reply.
My brain is starting to hurt.

I'll work on this for a while and get back with my results.

Was that up to and including PBP2.47 for the negative numbers?
Thanks again for the help.

ronjodu
- 19th September 2007, 22:18
Took me a couple days and some studying of "TFM" but I got it to work. Here is the code snippett I ended up with. Any comments or tips are welcome. Thanks for the help so far.


Decreasegain:
if PortB.2 = 1 then return
leftvolume = leftvolume - 1
rightvolume = rightvolume - 1
HSerout ["Left = ",dec leftvolume, 13,10]
HSerout ["Right = ",dec rightvolume, 13,10]
gosub getgain
if gain.15 then
gain = abs gain
gain2 = gain dig 2
gain1 = gain dig 1
HSerout ["Gain = -",dec gain2,dec gain1, ".", dec (gain.0* 5 ), " Db", 13,10]
else
gain2 = gain dig 2
gain1 = gain dig 1
HSerout ["Gain = ",dec gain2,dec gain1, ".", dec (gain.0* 5 ), " Db", 13,10]
endif
Low RST
SHIFTOUT IO,SCLK,1,[leftvolume, rightvolume]
high rst
write 1, leftvolume
write 2, rightvolume
return


Same idea used for increasegain and Quiet subroutines.

Ioannis
- 20th September 2007, 08:23
How is the PGA working? are you satisfied?

Also I noted that you are writing to memory your last settings every time you change the volume. This might wear the eeprom very soon. Do you need to do this so often?

Ioannis

ronjodu
- 20th September 2007, 11:58
Thanks for the reply. Yes, I am very pleased with the PGA. I know there's a lot of talk about the Wolfson WM8816 in the DIY forums but I was able to get samples of the PGA and for my ears it has worked out very well.
As for the eeprom, no I don't need to write often, probably only once at power down but this was only a test prog to test out the PGA. I eventually will have an off/on switch for the amp connected to the micro and can read/write at that time.
For anyone who cares this is a Chipamp dual mono that I built using 2 LM3668 chips. It's been a really cool project.

Ioannis
- 21st September 2007, 07:56
OK. Thanks, can't wait to test it. Samples are comming!

Ioannis

Archangel
- 21st September 2007, 17:35
For anyone who cares this is a Chipamp dual mono that I built using 2 LM3668 chips. It's been a really cool project.

Hi ronjodu,
Of course we care ! Every post here reveals a little genius in it's poster. Genius = geni-in-us . Your post matters, and I learned from it too. Thank You.
JS

selbstdual
- 2nd May 2008, 17:55
What did you learn from him?