;------------------------------------------------- ;Program: MOD-SYSTEM.ASM ;Update: 13 November 2002 ; ;By: Dr. Marcus O. Durham, PhD, PE ; Tulsa, OK, USA ; mod@superb.org ; www.ThewayCorp.com ;Copyright (c)1991, 2002. All rights reserved ; ;Purpose: ; A set of routines are provided to perform ; infrared receive functions. The code can be ; easily modified to add features based on the ; received signal. Note, all this test code has ; not been verified since the latest updates. ;################################################# ; PROGRAM ;################################################# ORG 00H START: ;------------------------------------------------- LJMP INITIAL ;------------------------------------------------- ORG 002BH ;Address past vectors DB 0 ;null address = 2Ch DB 18, 1, 'Copyright (C) 2002' DB 25, 1, 'Marcus O. Durham, PhD, PE' DB 14, 1, 'Tulsa, OK USA' DB 0 ;null address = 6Eh ;************************************************* ; INITIAL & MAIN ;************************************************* ORG 0070H ;Addres past reserve INITIAL: ;------------------------------------------------- ;INITIALIZE MOV SP,#5Fh ;start stack @ 5f+1 ;------------------------------------------------- MAIN: ;------------------------------------------------- ;PROCESS LCALL IRRECEIV ;infrared MAN9: LJMP MAIN ;Repeat ;************************************************* ; ; INFRARED REMOTE ; ;************************************************* ; ; PHILIPS RC5 remote receiver. ; The circuit uses an infrared receiver LT1029-ND ; Original code by Wagner Lipnharski 11/99 ; www.ustr.net ; wagner@ustr.net ; Thanks to Sarah Billingsley for debug of my code. ; ;------------------------------------------------- ; ; The carrier is 40 kHz square wave. So each ; cycle is 27 microsec. Then this is modulated ; with data bits. Therefore, a 40 kHz signal is ; seen during ON(0) & dc is seen during OFF(1). ; ; Each bit is 1.728 ms long. ; Each IR string is 14 bits long. ; The bit stream is repeated every 130 ms, if the ; key is held. This gives a long interval to ; resync if there is a receive error. ; ; Bit 1 is MSB. ; Bit 1-2 Automatic gain control/AGC, 10 =good ; Bit 3 Check, it flips at each new xmission ; Bit 4-8 Address, 00 = television ; Bit 9-14 Command, instructions out on P1 ; ; A data bit is both positive and negative. The ; first half cycle is setup, the second half is ; the state. On is 0 in the second half, while ; Off is 1 in the second half. ; ; --__ 1 BIT ; RX DATA ; __-- 0 BIT ; ; When the first trailing edge is detected, this ; is used as the timer frame. Wait 3/ 4 bit time ; to sync in middle of next bit. ; ; The transmitter carrier frequency is turned on/ ; off by the digital signal. The receiver gets ; this modulated signal which is a series of ; carrier frequencies that are turned on/off. The ; detector removes the carrier to leave the on/ ; off pulses. ; ; The carrier frequency causes 32 pulses for each ; half cycle or 64 pulses per bit. The computer ; does not see these pulses. ; ; This program has a wait cycle at the start. It ; is looking for the first transition in the bit ; stream. A better approach would be to activate ; the interrupt on INT1. ; ; The first or start bit is always on (0). The ; second is also on (0). These are the automatic ; gain control (AGC). ; ; The third bit is a check (CHK). It toggles each ; time a new key is pushed. ; ; Next follows 5 address and 6 command bits. ; The output should remain on P1 for a short time ; to prevent glitches in data. ; ; Check the first two bits for on. If not, wait ; more than 15 bit times to begin a new sample. ; This will assure not ; ; The television remote is address 00. However, ; that is a code 10 at AV6 universal programmable ; remote. ; ; There are addresses associated with other ; devices. ; ; ADDRESS, EQUIPMENT ; 0, tv set ; 2, teletext ; 5, video recorder ; 7, experimental ; 16, preamplifier ; 17, receiver / tuner ; 18, tape / cassete recorder ; 19, experimental ; ; The software generates a Scope pulse. This ; triggers with each new bit. Therefore a scope ; can look at the incoming timing. This will ; assure you are sensing at mid bit. ; ; ---___---___------______---___---___--- P33 ; _|_____|_____|_____|_____|_____|_____|_ P34 ; ; P35 is used to show IR is being received. ; ; The Receiver configuration uses this assignment ; P1 -90H -all 8 bit result out drive to gnd ; P33 -0B3h -3- IR input ; P34 -0B4h -4- Scope, software pulse @ get bit ; P35 -0B5h -5- LED shows receiving data ;------------------------------------------------- ;Ir Receiver Assignments ;------------------------------------------------- IrInp BIT 0B3h ;IR serial input stream IrScope BIT 0B4h ;soft generate pulse for each bit IrComd DATA 41h ;IR code received ;------------------------------------------------- IRRECEIV: ;------------------------------------------------- ; ; Receive IR string of 14 bits. ;INITIAL IRRE0: MOV P1,#0FFh ;Reset Decoder Output High SETB P35 ;Turn off IR Indicator SETB IrInp ;Input bit ;WAIT FOR FIRST BIT IRRE1: MOV R3,#122 ; IRRE1A: MOV R2,#000h ;Time loop IRRE2: JNB IrInp,IRRE3 ;Incoming bit is low DJNZ R2,IRRE2 ;time loop to keep DJNZ R3,IRRE1A ;command at P1 LJMP IRRE0 ;in case press key again ;------------------------------------------------- ; ; Interrupt entry on first falling edge. ; The code must be activated and the processor ; set up to jump to here. ; ANL IE,#11111011b ;turn off ExtInt1 ;START BITS (AGC) IRRE3: CLR P35 ;IR Indicator on 1st LCALL DLYPHI34 ;3/4 bittime=1.296ms SETB IrScope ;scope pulse CLR IrScope ;scope pulse JB IrInp,IRRE31 ;2nd AGC,1st half hi SJMP IRRE8 ;error, resync ;ADDRESS STREAM IRRE31: LCALL DLYPHI ;1 bit time delay CLR A ;IR Rx first low lev MOV B,#6 ;6 more bits IRRE4: SETB IrScope ;scope pulse MOV C,IrInp ;IR state to Carry CLR IrScope ;scope pulse RLC A ;Insert IR into A LCALL DLYPHI ;1 bit time delay DJNZ B,IRRE4 ;Rotate 6 bits in A ;CHKbit & 5 ADDRESS ;DECODE ADDRESS ANL A,#00011111b ;mask CHK (flip) bit CJNE A,#0h,IRRE8 ;<> Address 00 ;COMMAND STREAM MOV B,#6 ;6bit command stream IRRE5: SETB IrScope ;scope pulse MOV C,IrInp ;IR state to Carry CLR IrScope ;scope pulse RLC A ;Insert IR into A LCALL DLYPHI ;1 bit delay DJNZ B,IRRE5 ;Rotate command to A ;SAVE COMMAND MOV IrComd,A ;Save Command ;CONVERT HEX TO 8Bit IRRE6: MOV DPTR,#IRRC5TAB MOVC A,@A+DPTR ;convert table ;VALIDITY TEST CJNE A,#0FFh,IRRE7 ;command<>FF, valid LJMP IRRE8 ;invalid command ;OUTPUT RESULTS IRRE7: MOV P1,A ;out Command to P1 LJMP IRRE1 ;Job done, resync ;WAIT TO SYNC NEXT IRRE8: LCALL DLYPHI15 ;invalid, wait 15 LJMP IRRE0 ;bit time, restart RET ;IRRE8 for Interrupt ;************************************************* ; ; DELAY ; ;************************************************* ; ; Delay routine using NOPS and a nested loop. ; Registers R2 & R3 are used for counting loops. ; ; Count the machine cycles for each instruction. ; NOP = 1 ; MOV = 1 ; DJNZ = 2 ; LCALL= 2 ; RET = 2 ; ; The time for each loop is calculated ; MOV = 1 ; JNB = 2*count ; NOP = 1*count ; DJNZ = 2*count ; inside = (incount *2) + 1 ; outside= [(inside + 2)outcout] + 1 ; Plus = 4 for call ; ; A smaller number on the inside loop gives ; more precise counting. ; ; Each machine cycle requires time to execute. ; This is based on the oscillator frequency. ; Time= (sec/11.059E6 cycle)(12 period/mach cy) ; = 1.08509 E-6 sec / machine cycle ; ;------------------------------------------------- DLYPHI: ;------------------------------------------------- ; For time interval at 1 bit cycle, need ; time interval of 1.728 ms, so need ; .001728 * 11059000/12 = 1592.5 machine cycles ; Use 1.72, need 1585 machine cycles. MOV R3,#70 ;Outer loop counter ZDLR1: MOV R2,#10 ;Nested loop counter ZDLR2: DJNZ R2,ZDLR2 ;Nested loop, 256 x DJNZ R3,ZDLR1 ;Outer loop RET ;Return to call ;------------------------------------------------- DLYPHI34: ;------------------------------------------------- ; For interval at 3/ 4 of a bit cycle, need ; time interval of 1.728 * .75= 1.296 ms, so need ; .001296 * 11059000/12 = 1194.3 machine cycles ; Use .75 * 1.72 gives 1188 machine cycles. ; MOV R3,#110 ;Outer loop counter ZDLR3: MOV R2,#4 ;Nested loop counter ZDLR4: DJNZ R2,ZDLR4 ;Nested loop, 256 x DJNZ R3,ZDLR3 ;Outer loop NOP RET ;Return to call ;------------------------------------------------- DLYPHI15: ;------------------------------------------------- ; For time interval at 15 bit cycle, need ; time interval of 1.728 * 15= 25.92 ms, so need ; .02592 * 11059000/12= 23,887.44 machine cycle ; MOV R3,#220 ;Outer loop counter ZDLR5: MOV R2,#50 ;Nested loop counter ZDLR6: DJNZ R2,ZDLR2 ;Nested loop, 256 x DJNZ R3,ZDLR1 ;Outer loop RET ;Return to call ;------------------------------------------------- IRRC5TAB: ;------------------------------------------------- ; The following table bit values at the first column, means ; the translation from remote control key to the value to be ; be post at P1. The program ignores values of "FF". ; Change those bits according to your needs. ; ; Bit #7 is "0" when a valid key is pressed, otherwise is "1" ; It can be used to recognize values as key "zero" pressed, ; when all bits UP means command ZERO. ; ; This TV remote uses only Address = "00" ; PHILIPS TV REMOTE CONTROL DECODING ; ; VALUE TO P1 REMOTE KEY COMMAND ; ----------- ---------- ------- DB 01111111b ; 0 ; 0 DB 01111110b ; 1 ; 1 DB 01111101b ; 2 ; 2 DB 01111100b ; 3 ; 3 DB 01111011b ; 4 ; 4 DB 01111010b ; 5 ; 5 DB 01111001b ; 6 ; 6 DB 01111000b ; 7 ; 7 DB 01110111b ; 8 ; 8 DB 01110110b ; 9 ; 9 DB 11111111b ; ; A DB 11111111b ; ; B DB 01110011b ; ON/OFF ; C DB 01110010b ; MUTE ; D DB 01110001b ; PP ; E DB 01110000b ; OSD ; F DB 01101111b ; Volume+ ; 10 DB 01101110b ; Volume- ; 11 DB 01101101b ; Bright+ ; 12 DB 01101100b ; Bright- ; 13 DB 01101011b ; Color+ ; 14 DB 01111010b ; Color- ; 15 DB 11111111b ; ; 16 DB 11111111b ; ; 17 DB 11111111b ; ; 18 DB 11111111b ; ; 19 DB 11111111b ; ; 1A DB 11111111b ; ; 1B DB 01100011b ; Contrast+ ; 1C DB 01100010b ; Contrast- ; 1D DB 11111111b ; ; 1E DB 11111111b ; ; 1F DB 01011111b ; Program+ ; 20 DB 01011110b ; Program- ; 21 DB 11111111b ; ; 22 DB 11111111b ; ; 23 DB 01011011b ; Timer ; 24 DB 01010111b ; Special 1 ; 25 DB 01000001b ; Special 2 ; 26 DB 01000111b ; Special 3 ; 27 DB 01001110b ; Special 4 ; 28 DB 01000101b ; Special 5 ; 29 DB 01010010b ; Special 6 ; 2A ;------------------------------------------------- END ;Program end