'-------------------------------------------------------------------
' learningRC5.BAS
' (c) 4/2000 J.Vincent-Newson
' Bascom RC5in based on Atmel AVR410 application note
'-------------------------------------------------------------------


Ddrb = &B11100111                                   'setup i/o dir: o/o/o/i/i/o/o/o

                                                    'Define pins to use for the IR rx ip and prog led op:

Config Rc5 = Pinb.3                                 'rc5 RX ip

Prog Alias Portb.0                                  'prog led op and motor/EN


                                                    'turn off motor first
Reset Portb.1                                       'motor -ve OFF
Reset Portb.2                                       'motor +ve OFF
Reset Prog                                          'motor /en OFF

Enable Interrupts                                   'the interrupt routine is inserted automatic for GetRC5
                                                    'but we need to make it occur so enable the interrupts


Dim Address As Byte , Command As Byte               'reserve variable space
Dim Volup As Byte , Voldn As Byte , Addr As Byte


If Pinb.4 = 1 Then Goto Main Else Goto Getvoldn     'now start by checking to see if prog jumper is set (low)
                                                    'if not set, jump to main and use eeprom values
                                                    'if set, learn address and commands before saving in eeprom:

Getvoldn:                                           'first get the volume down button:

   Set Prog                                         'turn on Prog led and disable motor
   Reset Portb.1                                    'turn motor (-) led off
   Waitms 150                                       'wait
   set Portb.1                                      'then turn it on (ie will flash every time we loop until key pressed)

   Getrc5(address , Command)                        'read RC5 data if any present - store in variables

   If Address = 255 Then Goto Getvoldn              'check for a valid system address (ie not 255) loop till key pressed
                                                    'OK, valid address (ie key pressed) so carry on
      Command = Command And &B10111111              'clear the toggle bit as it toggles on each received command
      Addr = Address : Voldn = Command              'put received RC5 values into variables
      Wait 1                                        'leave motor (-) led on for 1 sec
      Reset Portb.1                                 'turn motor (-) led off
      Wait 1                                        'wait another sec for new key
      Writeeeprom Addr , 1                          'now save Addr and Voldn down in eeprom locations 1,2
      Writeeeprom Voldn , 2

   Goto Getvolup                                    'and go to get the Volup command:

Getvolup:

   Reset Portb.2                                    'turn motor (+) led off
   Waitms 150                                       'wait
   set Portb.2                                      'then turn it on (ie will flash every time we loop until key pressed)

   Getrc5(address , Command)                        'read RC5 data if any present - store in variables

   If Address = 255 Then Goto Getvolup              'check for a valid system address (ie not 255) loop till key pressed
                                                    'OK, valid address (ie key pressed) so carry on
      Command = Command And &B10111111              'clear the toggle bit as it toggles on each received command
      Volup = Command                               'put received RC5 value into variable
      Wait 1                                        'leave motor (+) led on for 1 sec
      Reset Portb.2                                 'turn motor (+) led off
      Wait 1.5                                      'wait another 1.5 sec
      Writeeeprom Volup , 3                         'now save "Volup" in eeprom
      Reset Prog                                    'turn off prog led and enable motor

   Goto Main                                        'goto main program



Main:                                               'main program starts

Readeeprom Addr , 1                                 'check to see if there is already valid data in eeprom loc 1

If Addr = 255 Then Goto Getvoldn                    'if not go & get RC5 values & store them (failsafe)

Readeeprom Addr , 1                                 'otherwise go as usual & load in 3 vars from eeprom first
Readeeprom Voldn , 2
Readeeprom Volup , 3

Do                                                  'then loop to drive motor UP/DOWN using eeprom values

Rc5in:

   Getrc5(address , Command)                        'read RC5 data if any
   Command = Command And &B10111111                 'make sure we ignore toggle bit

   Reset Portb.1 : Reset Portb.2                    'turn off motor before starting just in case

   If Address = Addr Then                           'RC5 sys address matches eeprom Addr so we'll use commands

      If Command = Voldn Then                       'if RC5 command is Voldn..
         Reset Portb.2 : Set Portb.1                'Drive motor DOWN
         Elseif Command = Volup Then                'if RC5 command is Volup..
         Set Portb.2 : Reset Portb.1                'Drive motor UP
         Else                                       'otherwise must be another key..
         Reset Portb.1 : Reset Portb.2              'so turn motor OFF
      End If

   End If

Loop                                                'keep on looping reading RC5 data

End