Lookup table or Math?


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91

    Default Lookup table or Math?

    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.
    Code:
    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
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Thanks Ace!

    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?
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  4. #4
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Smile

    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.
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  5. #5
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Talking Thanks,

    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.
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  6. #6
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default It works!!!!

    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.
    Code:
    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.
    "It will never happen here!" just happened here.
    My thoughts and prayers for Sandy Hook victims and families.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Dec 2003
    Location
    Storrs, Ct.
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Very pleased.

    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.

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default

    OK. Thanks, can't wait to test it. Samples are comming!

    Ioannis

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ronjodu View Post
    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11
    Join Date
    Aug 2006
    Location
    In a world of german electrons
    Posts
    102


    Did you find this post helpful? Yes | No

    Default

    What did you learn from him?
    Be well - whoever you are.

Similar Threads

  1. Lookup table syntax.....
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th May 2009, 04:45
  2. Lookup Table
    By yasser hassani in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th March 2008, 10:38
  3. problem with USART
    By leemin in forum Serial
    Replies: 4
    Last Post: - 11th December 2006, 17:56
  4. Need help with lookup table or direct calculation
    By nverma in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 11th October 2006, 18:27
  5. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts