Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

IR Controlled PGA2313 Code

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
danward79
member
member


Joined: 02 Jan 2006
Posts: 21
Location: Hove

uk.gif
PostPosted: Sat Mar 29, 2008 2:27 pm    Post subject: IR Controlled PGA2313 Code Reply with quote

Hi All,

Just thought I would share some code I have put together, it is for controlling a PGA2313 Volume Controller from Texas Instruments.

It also has some code for control via IR and RS232.

I would be interested in any criticism (constructive!) If you have any. I think I have managed to get quite a bit on a AT90S2313...

There may be a few bits that appear "odd" they are probably my attempt at saving memory.

I must add I would not have managed this with out snippets found on this forum.

Code:

'------------------------------------------------------------------------------
'Amp Control Board
'RS-232 Protocol

'Normal operation
'a - Adjust to value
'd - Volume down
'f - Off
'm - Mute
'o - On
'r - Restore/Unmute
's - Status
'u - Volume up
'x - Settings

'In settings mode:-
'c - Current settings
'm - Set max gain
's - Set start gain
'x - Exit settings mode

'Port allocations
'Port D
'0 - RS232 RX
'1 - RS232 TX
'2 -
'3 - IR Indicator LED
'4 - DOUT (SDI)  Pin 8 to Pin 3 - PGA2313
'5 - Clock (SCK) Pin 9 to Pin 6 - PGA2313
'6 - CS Pin 11 to Pin 2 - PGA2313

'Port B
'0 -
'1 -
'2 - IR Input
'3 - PWR Relay Pin 15
'4 - Vol+ Button Pin 16
'5 - Vol- Button Pin 17
'6 - PWR Button Pin 18
'7 - Mute Button Pin 19

'Gain Calulation
'For N = 1 to 255:
'Gain (dB) = 31.5 - [0.5  (255 - N)]

'----------------------------------------------------------------------------
'Config Stuff
$regfile = "2313def.dat"
$crystal = 8000000
$baud = 9600
$lib "mcsbyte.lbx"

'Config SPI Interface
Config Spi = Soft , Dout = Portd.4 , Clock = Portd.5
Spiinit

'Config RC5 Interface
Config Rc5 = Pinb.2
Config Pinb.2 = Input
Enable Interrupts

'Config debounce time in ms
Config Debounce = 30

'Declare Sub's and Func's
Declare Sub Get_gain
Declare Sub Set_gain
Declare Sub Inc_vol_up
Declare Sub Inc_vol_dwn
Declare Sub Mute
Declare Sub Unmute
Declare Sub Toggle_mute
Declare Sub Pwr_off
Declare Sub Pwr_on
Declare Sub Toggle_pwr
Declare Sub Print_off
Declare Sub Wait_some

'Declare Variables
Dim Volume As Byte
Dim Mute_state As Bit
Dim Max_gain As Byte
Dim Premute_lvl As Byte
Dim Address As Byte , Command As Byte
Dim Estartvol As Eram Byte , Emaxvol As Eram Byte           'Eprom Variables

'Aliases and Ports
Config Portb.3 = Output
Pwr_relay Alias Portb.3

Config Pinb.4 = Input
Volup_button Alias Pinb.4

Config Pinb.5 = Input
Voldwn_button Alias Pinb.5

Config Pinb.6 = Input
Pwr_button Alias Pinb.6

Config Portd.3 = Output
Ir_indicator Alias Portd.3

Config Portd.6 = Output
Cs Alias Portd.6

Config Pinb.7 = Input
Mute_button Alias Pinb.7

'Constants
Const Swversion = "1.0"
Const Startvol = 130
Const Maxvol = 220
Const Standardwait = 100


'------------------------------------------------------------------------------
'Main Code

   Print Swversion                                          '; ", " ; Version()

   Volume = Estartvol
   Max_gain = Emaxvol

   If Volume = 255 Then

      Estartvol = Startvol

      Emaxvol = Maxvol

      Volume = Estartvol

      Max_gain = Emaxvol

   End If

   Premute_lvl = Volume

   Pwr_off

'Main Loop
   Do

      'Button handling
      Debounce Volup_button , 0 , Inc_vol_up_sub , Sub
      Debounce Voldwn_button , 0 , Inc_vol_dwn_sub , Sub
      Debounce Pwr_button , 0 , Toggle_pwr_sub , Sub
      Debounce Mute_button , 0 , Toggle_mute_sub , Sub

      'Serial input handling
      Select Case Inkey()

         'ascii "a"
         Case 097

            Input "Gain? " , Volume                         ' (Max " ; Max_gain ; ")

            Set_gain

         'ascii "d"
         Case 100

            Inc_vol_dwn

         'ascii "f"
         Case 102

            Pwr_off

         'ascii "m"
         Case 109

            Mute

         'ascii "o"
         Case 111

            Pwr_on

         'ascii "r"
         Case 114

            Unmute
         'ascii "s"
         Case 115

            Get_gain

         'ascii "u"
         Case 117

            Inc_vol_up

         'ascii x
         Case 120

            Print "Settings"
            Gosub Settings

      End Select

      'RC5 IR Handling
      Getrc5(address , Command)

         If Address < 255 Then

            Command = Command And &B01111111

            Set Ir_indicator

            Select Case Command

               Case 12

                  Toggle_pwr

               Case 86

                  Inc_vol_up

               Case 85

                  Inc_vol_dwn

               Case 13

                  Toggle_mute

               Case Else

                  Wait_some

            End Select

         End If

      Reset Ir_indicator

   Loop

End


'------------------------------------------------------------------------------
'Sub Routines Code

'Get gain not in dB due to memory constraints!!
Sub Get_gain

   Print "Gain=" ; Volume ; ", Mute=" ; Mute_state

End Sub

'Set PGA gain
Sub Set_gain

   If Volume > Max_gain Then

      Volume = Max_gain

   End If

   Reset Cs

   Spiout Volume , 1

   Spiout Volume , 1

   Set Cs

   Get_gain

End Sub

'Increment Volume Up
Sub Inc_vol_up

   If Pwr_relay = 1 Then

      If Mute_state = 1 Then

         Unmute

      Else

         Volume = Volume + 1

         Print "V+"

         Set_gain

      End If

      Wait_some

   Else

      Print_off

   End If

End Sub

'Increment Volume Down
Sub Inc_vol_dwn

   If Pwr_relay = 1 Then

      If Mute_state = 1 Then

         Unmute

      Else

         If Volume > 1 Then

            Volume = Volume - 1

            Print "V-"

            Set_gain

         End If

      End If

      Wait_some

   Else

      Print_off

   End If

End Sub

'Mute Routine
Sub Mute

   'If Pwr_relay = 1 Then

      If Mute_state = 0 Then

         Premute_lvl = Volume

         Volume = 0

         Set Mute_state

         Set_gain

      Else

         If Pwr_relay = 0 Then

            Print_off

         End If

      End If

      Wait_some

End Sub


'UnMute Routine
Sub Unmute

   If Pwr_relay = 1 Then

      If Mute_state = 1 Then

         Volume = Premute_lvl

         Reset Mute_state

         Set_gain

      End If

      Wait_some

   Else

      Print_off

   End If

End Sub

'Helper sub - mute toggle
Sub Toggle_mute

   If Mute_state = 1 Then

      Unmute

   Else

      Mute

   End If

End Sub

'Power Off
Sub Pwr_off

   Volume = Estartvol

   Print_off

   Mute

   Reset Ir_indicator

   Reset Pwr_relay

   Wait 5

End Sub

'Power On
Sub Pwr_on

   Set Pwr_relay

   Wait_some

   Reset Ir_indicator

   Print "PwrOn"

   Unmute

   Wait 1

End Sub

'Helper sub - Toggle pwr state
Sub Toggle_pwr

   If Pwr_relay = 0 Then

      Pwr_on

   Else

      Pwr_off

   End If

End Sub

'Prints "PwrOff" this is just to save memory(1%)!
Sub Print_off

   Print "PwrOf"

End Sub

'Wait sub to save memory
Sub Wait_some

   Waitms Standardwait

End Sub


'Labels for debouce calls
Inc_vol_up_sub:
   Inc_vol_up
Return

Toggle_pwr_sub:
   Toggle_pwr
Return

Toggle_mute_sub:
   Toggle_mute
Return

Inc_vol_dwn_sub:
   Inc_vol_dwn
Return


'Settings routine
Settings:
Do

   'Serial input handling
   Select Case Inkey()

      'ascii "c"
      Case 099

         Volume = Estartvol

         Print "Start V " ; Volume

         Print "Max V " ; Max_gain

      'ascii "m"
      Case 109

         Input "Max? " , Max_gain

         If Max_gain > 254 Then

            Max_gain = 255

         End If

         Emaxvol = Max_gain

      'ascii "s"
      Case 115

         Input "Start? " , Volume

         If Volume > 254 Then

            Volume = 255

         End If

         Estartvol = Volume

      'ascii "x"
      Case 120

         Pwr_off

         Return

   End Select
Loop
Back to top
View user's profile
PaulC
member
member


Joined: 09 Jan 2008
Posts: 122
Location: Ireland

ireland.gif
PostPosted: Sun Mar 30, 2008 7:54 am    Post subject: Reply with quote

Hi danward79
i am new to bascom & avr
question is ?
does 'Serial input handling
Select Case Inkey()
wait for input from rs232 & do nothing until it gets something..
or can it carry on doing something while it scans rs232..
i would like to use this in my routine if thats o.k.
PaulC
Back to top
View user's profile
danward79
member
member


Joined: 02 Jan 2006
Posts: 21
Location: Hove

uk.gif
PostPosted: Sun Mar 30, 2008 9:14 am    Post subject: Reply with quote

Hi Paul,

In the main loop it continues looping checking for a key input and ir input, when it receives something it branches off does it then continues.

So you could do other stuff while it waits.

I am new to bascom, so this may not be the best was to do it.

Feel free to use what ever you like.
Back to top
View user's profile
PaulC
member
member


Joined: 09 Jan 2008
Posts: 122
Location: Ireland

ireland.gif
PostPosted: Sun Mar 30, 2008 9:36 am    Post subject: Reply with quote

loads of thanks..
better than the way i was trying to do it..
thankyou
PaulC
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum