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
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