DEFSEG DSEG,START=08H,CLASS=DATA,ABSOLUTE
DEFSEG CSEG,START=00H,CLASS=CODE,ABSOLUTE
DEFSEG BSEG,START=00H,CLASS=BIT,ABSOLUTE
DEFSEG XDSEG,START=0000H,CLASS=XDATA,ABSOLUTE
DEFSEG XDSEG1,START=0000H,CLASS=XDATA,ABSOLUTE
*************************************************************************
*
* pattern definition for TIVO remotes:
* 1 start bit followed by 12 data bits
*
*
---_____- __- --- in 1 ms time frames
*
*
start '1' '0'
* low for 9ms
1 ms 1 ms
*
*
* Define equates for this pattern.
Set valid window for each duration.
*
*
start_min
equ 4200 ;start if high for between 4200us and 4400us
start_max
equ 4500
bitsize equ 1000 ;< 1ms bit is low, > 1ms bit is high
start_conf equ 5000 ;must be
asserted low for at least this time to qualify IR signal
bit_length equ 32
;no. of data bits in pattern
start_timeout %macro time
clr tf0
clr tr0
mov th0,# high (0ffffh-time)
mov tl0,# low (0ffffh-time)
setb tr0
%endm
start_timeout1 %macro time
clr tf1
clr tr1
mov th1,# high (0ffffh-time)
mov tl1,# low (0ffffh-time)
setb tr1
%endm
window_compare %macro var,first,second
%gensym win2
clr c
mov a,var+1
subb a,#low first
mov a,var
subb a,#high first
jc win2
mov a,var+1
subb a,#low second
mov a,var
subb a,#high second
cpl c
win2
%endm
rr0 equ 0
rr1
equ 1
rr2
equ 2
rr3
equ 3
rr4
equ 4
rr5
equ 5
rr6
equ 6
rr7
equ 7
iand equ b.4
ior
equ b.5
ixor
equ b.6
seg bseg
org 0
seg dseg
org 8
time ds 2
result ds 1
p0_image ds 1
p1_image ds 1
p2_image ds 1
p3_image ds 1
lightpattern ds 1
new_command ds 1
seg cseg
org 00h
jmp main
;reset vector
org 03h
;ex0
jmp changestate
org 0bh
;t0
jmp main
org 13h
;ex1
jmp IRreceive
org 1bh
;t1
jmp main
org 23h
;ser
jmp main
org 2bh
;t2
jmp main
*****************************************************************
* main routine
*****************************************************************
main
mov p0,#0ffh ;switch off open collectors
mov p0_image,#0ffh
mov p1,#0ffh
mov p1_image,#0ffh
mov p2,#0
mov p2_image,#0
mov p3,#0ffh ;input mode
mov p3_image,#0ffh
main2
mov sp,#40h
mov ie,#0
;disable interrupts
mov ip,#00000100b
;all intpts same priority
mov tmod,#00010001b
;16-bit timer/counter
mov tcon,#00000101b
;INT1/0 edge triggered
mov th1,#0
mov tl1,#0
*****************************************************************
* Configure start status
*****************************************************************
mov lightpattern,#0
;initialise light pattern #0
mov new_command,#0
setb ex0
setb ex1
setb ea
loop
mov pcon,#1b ;idle mode
mov a,new_command
jz loop
got_new
mov new_command,#0
call do_command
jmp loop
*****************************************************************
* Change State
*****************************************************************
changestate
clr ex0
;clear int 0
mov a,lightpattern
inc a
mov lightpattern,a
mov p1_image,a
mov p1,a
setb ex0
reti
*****************************************************************
* IR Receiver Interrupt service routines
*****************************************************************
IRreceive
clr ex1 ;disable INT1
push acc
push b
start_timeout start_conf ; -____
qualifyIR
jb p3.3,j_falsealarm ;not start bit, false alarm
jnb tf0,qualifyIR
jnb p3.3,$ ;wait for start
bit
start_timeout start_max
wait0 jb p3.3,foundstartbit ;wait for bit
to negate __-
jnb tf0,wait0
j_falsealarm jmp falsealarm ;signal did
not negate within start_out, false alarm again
foundstartbit
clr tr0 ;stop timer 0 and
note timing
mov a,tl0
xrl a,#11111111b
clr c
subb a,#low start_min
mov a,th0
xrl a,#11111111b
subb a,#high start_min
jnc got_start
jmp falsealarm
;error condition
got_start
mov r0,#0 ;data in r3:r2:r1:r0
mov r1,#0
mov r2,#0
mov r3,#0
mov r4,#bit_length
waitstartframe
jnb p3.3,readnextbit
jnb tf0,waitstartframe
jmp falsealarm ;signal did not assert
within start_out, false alarm again
readnextbit jnb p3.3,readnextbit ;wait to read first bit
clr tf1
clr tr1
mov th1,#0
mov tl1,#0
setb tr1 ;start timer1
wait_low jnb p3.3,found_low
jnb tf1,wait_low ;wait for signal
to assert
jmp falsealarm ;65ms passed with
no signal
found_low
clr tr1 ;stop timer1
clr c
mov a,tl1
subb a,#low bitsize
mov a,th1
subb a,#high bitsize
cpl c ;carry set if signal
high > bitsize
mov a,r0
rlc a ;rotate left through
carry (carry set if bit is asserted)
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,r2
rlc a
mov r2,a
mov a,r3
rlc a
mov r3,a
djnz r4,readnextbit
; Check table for valid IR command
;
mov dptr,#remote_table
mov b,#(end_remote_table-remote_table)/4
cmp_loop
mov a,#0
movc a,@a+dptr
inc dptr
xrl a,r3
jnz next_command1
;mov p1, #11111110b ;*test
LED1
movc a,@a+dptr
inc dptr
xrl a,r2
jnz next_command2
;mov p1, #11111101b ;*test LED2
movc a,@a+dptr
inc dptr
xrl a,r1
jnz next_command3
;mov p1, #11111011b ;*test LED3
movc a,@a+dptr
xrl a,r0
jnz next_command3
;mov p1, #11110111b ;*test LED4
match
mov a,#(end_remote_table-remote_table)/4
clr c
subb a,b ;reg a contains output
table offset
inc a
mov new_command,a
falsealarm
pop b
pop acc
setb ex1
reti
next_command1 inc dptr
next_command2 inc dptr
next_command3 inc dptr
djnz b,cmp_loop
sjmp falsealarm
*****************************************************************
* Do command
* Input ACC=command code
*****************************************************************
do_command
cjne a,#5,not_cycling
;
; cycle light pattern
;
mov a,lightpattern
inc a
cjne a,#(end_cycle_pattern-cycle_pattern),not_end_pattern
clr a
not_end_pattern mov lightpattern,a
mov dptr,#cycle_pattern
movc a,@a+dptr
sjmp output_port
not_cycling
cjne a,#6,not_auto_pattern
jmp auto_pattern_start
not_auto_pattern
cjne a,#7,not_power
;
; POWER BUTTON
;
mov a,p1_image
cjne a,#(00001111b xor 11111111b),all_on
mov a,#(00000000b xor 11111111b)
sjmp output_port
all_on mov a,#(00001111b xor 11111111b)
sjmp output_port
not_power:
cjne a,#8,not_flash
mov a,p1_image
flashloop xrl a,p1_image
xrl a,#11111111b
mov p1,a
mov r7,#8
flashdelay1 clr tf0
jnb tf0,$
djnz r7,flashdelay1
mov r6,new_command
cjne r6,#0,exitflash
sjmp flashloop
exitflash mov a,p1_image
sjmp output_port
not_flash
mov dptr,#output_table
movc a,@a+dptr ;output in reg a
mov b,a
mov a,p1_image ;get original p1 output
xrl a,b ;toggle related bit
output_port
mov p1_image,a
mov p1,a
ret
auto_pattern_start
mov a,#0 ;starting pattern #0
clr tr0
mov th0,a
mov tl0,a
setb tr0
auto_pattern_loop
mov r6,a
mov dptr,#auto_pattern
movc a,@a+dptr
mov p1_image,a
mov p1,a
;
; Cycle the display light patten with the following
timer
;
mov r7,#18
delay clr tf0
jnb tf0,$
djnz r7,delay
mov r5,new_command
cjne r5,#0,exit_auto_pattern
inc r6
mov a,r6
cjne a,#(end_auto_pattern-auto_pattern),auto_pattern_loop
sjmp auto_pattern_start
exit_auto_pattern
ret
*****************************************************************
* Output pattern table
*****************************************************************
output_table
db 00000001b ;dummy
db 00000001b ;1 - toggle
db 00000010b ;2 - toggle
db 00000100b ;3 - toggle
db 00001000b ;4 - toggle
end_output_table
*****************************************************************
* Special function table
*****************************************************************
cycle_pattern ;5
- cycle pattern
db 00000001b xor 11111111b
db 00000101b xor 11111111b
db 00000111b xor 11111111b
db 00001010b xor 11111111b
db 00001110b xor 11111111b
db 00001111b xor 11111111b
db 00000110b xor 11111111b
db 00000100b xor 11111111b
end_cycle_pattern
auto_pattern ;6
- auto pattern
db 00000001b xor 11111111b
db 00000011b xor 11111111b
db 00000111b xor 11111111b
db 00001111b xor 11111111b
db 00001110b xor 11111111b
db 00001100b xor 11111111b
db 00001000b xor 11111111b
end_auto_pattern
*****************************************************************
* Table for remote controller
*****************************************************************
remote_table
db 02h,0fdh,80h,7fh ;1
db 02h,0fdh,40h,0bfh ;2
db 02h,0fdh,0C0h,03fh ;3
db 02h,0fdh,20h,0dfh ;4
db 20h,0dfh,00h,0ffh ;CH UP Cycle pattern
db 20h,0dfh,80h,7fh ;CH DN Auto Cycle
pattern
db 20h,0dfh,10h,0EFh ;POWER
db 20h,0dfh,38h,0C7h ;MTS
; db 20h,0dfh,0A8h,57h ;5
; db 20h,0dfh,68h,97h ;6
end_remote_table
end