Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:55 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: Has anyone done anything with the IR reciver.

Author Message
mclout999
Guru

Joined: 05/07/2020
Location: United States
Posts: 490
Posted: 03:56pm 10 Oct 2020
Copy link to clipboard 
Print this post

I was wondering what people had done with the IR that is just sitting there on all of our systems.  I need some help getting started on using it.  I think I can just get an NEC or Sony remote or a universal and set it to either right?  Then I need to record the IR input for the buttons I want(from the manual witch I have read on this).  Then I create a input rutine to use those codes.  NOT sure how to do that but I would like to develop a INC file to use IR input. Maybe It is simplers than I think it is but I would like to know what others may have done with the IR receiver.  I did search for IR here but try that for yourself.  Thanks all.
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1427
Posted: 04:45pm 10 Oct 2020
Copy link to clipboard 
Print this post

The manual shows a really simple example of the IR function making a interrupt happen where the codes are then PRINTed.
Micromites and Maximites! - Beginning Maximite
 
Sasquatch

Guru

Joined: 08/05/2020
Location: United States
Posts: 377
Posted: 07:19pm 10 Oct 2020
Copy link to clipboard 
Print this post

Also, nearly all of the remotes that I have tried work (TV, DVD etc.) what changes are the "Device code" and "Key Code" are different depending on the brand of remote.  

If you do buy a remote, your CMM2 likely has a 38kHz receiver as that is what is specified in the documentation.
Edited 2020-10-11 05:19 by Sasquatch
-Carl
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1427
Posted: 07:26pm 10 Oct 2020
Copy link to clipboard 
Print this post

38 kHz receivers have enough slop that they usually pick up 36-40kHz.
Micromites and Maximites! - Beginning Maximite
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 05:19am 11 Oct 2020
Copy link to clipboard 
Print this post

I tend to only use Sony remotes and they seem to have standardized on 40kHz, so I use the 4840 IRX IC, but as CG says - the 4838's do tend to be good enough for the 40kHz remotes also.

I used remotes like this semi regularly.

An expansion of the demo code in the manual, is a bit of this code from my latest call-bell system:

  Quote  '-------------------------- REMOTE CONTROL CODES: ---------------------------------
CONST POWER=002, TVPWR=170, SET=154, TVIN=026, VOLUP=106, VOLDN=234, SETUP=194
Const APP=240, YEL=008, BLU=024, HOME=136, RET=152, UPB=104, DOWNB=088
Const LEFTB=138, RIGHTB=010, OK=200, MENU=050, MOUSE=000
Const ONE=114, TWO=176, THREE=048, FOUR=082, FIVE=144, SIX=016, SEVEN=098
CONST EIGHT=160, NINE=032, ZERO=128, MUTE=130, DEL=066, DEV=128
'----------------------------------------------------------------------------------


The above code sample sets up all the remote control key values, and DEV is the device code, which never changes for any one specific remote control.

  Quote  IR DevCode,KeyCode,IR_RXD 'Start the background IR remote control decoder


...and that bit of code starts the background interrupt...

  Quote  '------------------------ IR REMOTE CONTROL DECODER INTERRUPT: ---------------------------
SUB IR_RXD
 Device=DevCode
 Code=KeyCode
 IR_FLAG=
1
End Sub


...and this bit of code is the actual interrupt that is executed whenever a button press is detected.  Note the application of one of the ten commandments of writing interrupts: 'Thou shalt not hang around in interrupts.' - keep interrupts very short, and process the results inside your main loop NOT inside the interrupt.

  Quote  Dim Integer NUMS(9)=(ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE) 'IR Remote numbers


...and I use that simple one-dimension array to store the remote key-codes for lookup later on.

The GET_SLOT sub, which accepts two remote control keypresses for the two-digit input between 01 and 99 in the menus:

  Quote  '------------------------ GET THE SLOT NUMBER FOR BTNS: ----------------------------------
SUB GET_SLOT
 
Text MM.Hres/2,175,"00",CM,3,1,RGB(White)
 CODE=
0:DIG=0
 
DO:Loop until CODE<>0 and DEVICE=DEV
 
For X=0 to 9
   Y=NUMS(X)  
   
If Y=CODE Then DIG=X*10
   
Text MM.Hres/2,175," "+STR$(DIG)+" ",CM,3,1,RGB(White)
 
Next X
 CODE=
0
 
DO:Loop until CODE<>0 and DEVICE=DEV
 
For X=0 to 9
   Y=NUMS(X)  
   
If Y=CODE Then DIG=DIG+X
   
Text MM.Hres/2,175," "+STR$(DIG)+" ",CM,3,1,RGB(White)
 
Next X  
End SUb


These bits of code are just some examples of how you can process remote-control keypresses.  There are many ways to do it, and each programmer tends to have their own way of coding, so you may well come up with something else.  I'm just using a simple FOR/NEXT loop for each digit, looking for a match between the code received, and one of the values in the NUMS array.

After you enter the two digits, the result is saved in a global variable(DIG) for analysis by the main loop.

IR remote controls are very useful in your project, and they are easy to code for once you get the hang of how interrupts need to work, and how you process codes received by the interrupt in the background, inside your main loop.

EDIT: One last little bit of code, showing you how I check the value of DIG, as returned by the sub above, for valid numbers:

  Quote          Select Case CODE
         
CASE ONE '------------- LEARN NEW BUTTON: ------------------
           SETUP_BG 'Draw setup screen background
           Text MM.HRES/2,075,"LEARN NEW:",CM,2,1,RGB(Magenta)
           
Text MM.HRES/2,100,"Enter button #:",CM,2,1,RGB(Yellow)
           
Text MM.Hres/2,120,"(two digits)",CM,2,1,RGB(Yellow)
           GET_SLOT
'Get the slot number reference (two digits)
           If DIG<=0 or DIG>30 then
             
Text MM.Hres/2,220,"INVALID!",CM,3,1,RGB(White),RGB(RED)
             
Pause 2000
             PEXIT
'Pre-exit procedures
             EXIT DO 'Jump out of the 2nd level loop. (which takes us to the main loop, and everything starts again...
           Endif
           SETUP_BG
'Draw setup screen background
           Text MM.Hres/2,070,"Please press",CM,2,1,RGB(White)
           
Text MM.Hres/2,090,"new button...",CM,2,1,RGB(White)
           
Text MM.Hres/2,220,"HOME to abort...",CM,2,1,RGB(Cyan)
           
CLose #2 'Close the port to the RX MCU, clearing the buffer
           Open "COM2:2400" as #2 'Re-open the port to the RX MCU ready for the new button code
           CODE=0
           
DO:LOOP until LOC(#2) or (CODE<>0 and DEVICE=DEV)
           
If CODE then 'Abort on ANY button press.
             PEXIT 'Pre-exit procedures
             Exit DO
           
Endif
           D$=
"":DAT$=""
           N=
0:Y=0 'Clear variables
           DO
             D$=
INPUT$(1,#2) 'Suck a byte from the buffer
             DAT$=DAT$+D$ 'Add byte to message string
           Loop until D$=CHR$(27) or LEN(DAT$)>=10
           
If len(DAT$)>=10 then
             ERRORS(
"Code too long.")
             Y=
1 'Set error trap flag
           ELSE
             
Print "LEARN TX CODE: "+DAT$
             
Text mm.hres/2,150,DAT$,CM,2,1,RGB(Yellow)
             
Pause 2000 'Delay to allow human to read the LCD
           Endif
           
If Y then Exit Do 'Error trap exit path back to main-loop
           BTNS(DIG)=VAL(DAT$) 'Save new button code
           SAVEDTA 'Save the current BTNS array of learned codes
           PEXIT 'Pre-exit procedures
           EXIT DO


So, we have the situation where the main loop is calling the GET_SLOT sub, which is then using the IR interrupt for each keypress, updating the global variable DIG and then exiting back to the main loop - where DIG is analysed to see if it is valid or not and taking action based on that.
Edited 2020-10-11 15:47 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
mclout999
Guru

Joined: 05/07/2020
Location: United States
Posts: 490
Posted: 05:41am 11 Oct 2020
Copy link to clipboard 
Print this post

Grogster:

Thanks your responce is like a master series in IR.  It will take some time to figure it out but thanks for the information and examples.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 05:50am 11 Oct 2020
Copy link to clipboard 
Print this post

My examples are perhaps a bit advanced, but they do show you how you handle IR key presses.

Would you like a really simple example code(complete, not bits like above) that just waits for keypresses and shows your choice on the console?  You might be able to more easily learn from that then from my code extracts above where I was doing quite specific things.
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 06:10am 11 Oct 2020
Copy link to clipboard 
Print this post

This is what I used when I was repairing my TV remote and needed to test all keys


' recieve NEC IR codes
dim integer IRdev, IRkey, revIRkey
IR IRdev, IRkey , IRint

do
  if IRrec = 1 then
    revIRkey = bitReverse(IRkey, 8)
    print hex$(IRdev,4);" ";hex$(IRkey,2);" ";hex$(revIRkey,2)
    IRrec = 0
  endif
loop until inkey$ <>""
end

sub IRint
if ((IRdev>>8)and &hFF)  = (inv(IRdev) and &hFF) then ' we have an 8 bit device code.
  IRdev = IRdev>>8 and &hFF
endif
IRrec = 1 ' set a flag to indicate reception of code
end sub

function bitReverse(x!, k!) as integer
local integer n
for n = 1 to k!
  bitReverse = (bitReverse << 1) + ((x! >> n) and 1)
next n
end function


The code works but the TV remote is starting to play up again...

Jim
VK7JH
MMedit
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1427
Posted: 04:10pm 11 Oct 2020
Copy link to clipboard 
Print this post

"The above code sample sets up all the remote control key values, and DEV is the device code, which never changes for any one specific remote control."

True mostly. I have remotes that break that rule. This type of remote has TV keys with one device code and DVR keys that emit a different device code.
Micromites and Maximites! - Beginning Maximite
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025