Simple IR (Infrared) Receiver Serial Keypad 08M2

radiosparks

Member
Purpose: Use the IR remote to enter a number from 1 to 65535. Data to be sent via the Sertxd to a terminal.

Originally, this code was written to command a vintage Kenwood TS-680S radio to QSY (change to another frequency). I only needed to be within a kHz from 500 to 53999. Numbers from 1 to 499 selected a preset memory frequency from an EEPROM. Wait about three seconds and the number entered will be executed.

I usually write code by the motto “Git'Er Done”, translated “Chaotic”.
For this Forum presentation, I've stripped out the extraneous stuff and “Glammed Up” my code so that others might benefit by being able to read it. 8)

NOTE:
Entering a number larger than the 16-bit word register will rollover. e.g. 65536 = 0 (You might get some weirdness.) SO, Overflows get cleared and you start over.

There's a lot of DEBUG (Sertxd) code to see what's happening and can be removed to save bytes.


Code:
'   /******************************************/
'  /* Infrared Receiver Keypad - (08M2 chip) */
' /*  radiosparks - rhb.20230827            */
'/******************************************/

#PICAXE 08M2
#Terminal 4800

'  /**********************/
' /*      PORT/PIN      */
'/**********************/

Symbol  LED_OUT = C.4
Symbol IR_INPUT = C.3

'  /**********************/
' /*     Constants      */
'/**********************/

Symbol MAXdigit = 65535

'  /**********************/
' /* Register Variables */
'/**********************/

Symbol commandSTORE = w1    ; b2:b3
Symbol       number = w2    ; b4:b5
Symbol     tempWORD = w3    ; b6:b7

'// temporary variables t1 -> t5

Symbol              t1 = b8
Symbol              t2 = b9
Symbol              t3 = b10
Symbol              t4 = b11
Symbol              t5 = b12

Symbol        ircmd = b13
Symbol      timeout = b14

'// just a habit to zero variables

             number = 0
       commandSTORE = 0        
            timeout = 0
       
'  /**********************/
' /*  MACRO Directives  */
'/**********************/

#MACRO FlashLED(times)
    t1 = times
    gosub LEDflasher
#ENDMACRO
         
'  /**********************/
' /*   Program Start    */
'/**********************/

'// pause until the serial terminal starts up - sometimes slooow
pause 3000    
Sertxd ( "Program Start", CR, LF )
FlashLED(1)
'// REM above when not needed - saves bytes

MAIN:

    '// do some other stuff while waiting for IR input...
    If number <> 0 Then
        INC timeout
        If timeout > 3 Then
            call StoreReset
        End If
    End If

    '// test command store
    Select Case commandSTORE
        Case 123
            '// do something like make a noise
            Tune 0, 3, ( 76,56,66,86 )
           
        Case 99
            '// Convert to individual ASCII characters
            call Number_to_ASCII
            Tune 0, 3, ( 76,86 )
           
        Case 1 to 5
            '// do anything like flash a light
            FlashLED(commandSTORE)
           
        Case 31 to 34
            '// do something that's annoying
            t1 = commandSTORE - 31
            Select Case t1
                case 0: PLAY 0,3
                case 1: PLAY 1,3
                case 2: PLAY 2,3
                case 3: PLAY 3,3
            End Select
            low LED_OUT ;// most times the play leaves the port high
       
    Case > 0 ;// just for debug - REM when not needed - saves bytes
             ;// select/case works top down - display if no match
             Sertxd ( "DEBUG commandSTORE: ", #commandSTORE, CR, LF )
           
    End Select
   
    commandSTORE = 0  ;// force reset so command can only run once
   
    '// wait a second for input from sony remote ... then do other stuff  
    Irin [1000, MAIN], IR_INPUT, ircmd                  

    '/* DEBUG IR remote codes - REM if you don't need it */
    Sertxd ( "DEBUG ircmd key value: ", #ircmd, CR, LF )
    '/* Use to see what codes are being sent */

    Select Case ircmd
       
        '// Keypad Remote Number Input
        Case <=9
             ircmd = ircmd + 1 // 10    ;// correct key value from remote
          tempWORD = MAXdigit - ircmd
               
            '// Test if adding IRCMD causes an Overflow
            If number > 6553 OR number > tempWORD AND ircmd > 0 Then
                Tune 0, 3, ( 66,76,66,76 )
                FlashLED(3)
                number = 0
                Sertxd ( "DEBUG OVERFLOW", CR, LF )
               
            Else ;// not overflow; calc new number
                number = number * 10 + ircmd
               
            End If

             '// TVR010 key-plus 11 [enter] key for simulation
        Case 11, 47  ; << Note: 47 is my remote code for enter testing
           
            '// build string up to 5 digits; MAX value 65535
            If number = 0 Then
                Sertxd ( "DEBUG null entered", CR, LF )    ;// no entry
                Tune 0, 1, ( 66,1 )
            Else
                call StoreReset
            End If
               
            Tune 0, 1, ( 56 )

        '// add extra keypad codes below for an immediate response

        Case 18 ;// right arrow key
            FlashLED(2)
       
        Case 19 ;// left arrow key
            FlashLED(3)

    End Select
   
    Tune 0, 0, ( ircmd )     ;// play tune with value of ircmd
    FlashLED(1)                ;// block to slow down repeating key presses
    timeout = 0             ;// you pressed a key; reset timeout

Goto MAIN

'  /**********************/
' /*    Subroutines     */
'/**********************/

StoreReset:
    '// Command Storage for use later
    commandSTORE = number

    '// number and timeout
      number = 0
     timeout = 0
return

Number_to_ASCII:
    BINtoASCII commandSTORE,t1,t2,t3,t4,t5
    Sertxd ( "Convert to ASCII: ", t1," ",t2," ",t3," ",t4," ",t5, CR, LF )
return

LEDFlasher:
    for t2 = 1 to t1
        High LED_OUT: Pause 125: Low LED_OUT: Pause 125
    next t2
Return

'/*eof*/
AXE091_08M2_IR_KEYPAD_SONY.JPG
 
Last edited:
Top