Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/17/04 07:37
Read: times


 
#66879 - Help me, 8051 timer long.. short control
Last week, I've made the 80s52 control box.

There are 8 LEDs(2*7-segment), 8 Buttons for channel bypass & time

up/down key(interval), so added to channel Holding Button.

Now, No problem the 8 LEDs on/off every 2 second, channel bypass.

But, Not available the time up/down key & channel hold key. Unfortunely,

I'm novice for MCS51, but I'm enjoying killing time for 8051 hardware.

My trainning assemble source here. So, it's open.


;*****************************************************************

***********
;
; Project: Time control for Donkey
; u_com: AT80s52 12MHz
;
;*****************************************************************

***********

;=================================================================

======
; PIN PORT FUNCTION
;-----------------------------------------------------------------

------
; 1 P1.0 (IN)

skip_key0
; 2 P1.1 (IN)

skip_key1
; 3 P1.2 (IN)

skip_key2
; 4 P1.3 (IN)

skip_key3
; 5 P1.4 (IN)

skip_key4
; 6 P1.5 (IN)

skip_key5
; 7 P1.6 (IN)

skip_key6
; 8 P1.7 (IN)

skip_key7
; ......
; 10 P3.0 (IN) up_key
; 11 P3.1 (IN)

down_key
; 12 P3.2 (IN)

hold_key
; ......
;-----------------------------------------------------------------

------
; 21 P2.0 (OUT) LED0
; 22 P2.1 (OUT) LED1
; 23 P2.2 (OUT) LED2
; 24 P2.3 (OUT) LED3
; 25 P2.4 (OUT) LED4
; 26 P2.5 (OUT) LED5
; 27 P2.6 (OUT) LED6
; 28 P2.7 (OUT) LED7
; .......
; 32 P0.7 (OUT)

seven_seg_hi3
; 33 P0.6 (OUT)

seven_seg_hi2
; 34 P0.5 (OUT)

seven_seg_hi1
; 35 P0.4 (OUT)

seven_seg_hi0
; 36 P0.3 (OUT)

seven_seg_lo3
; 37 P0.2 (OUT)

seven_seg_lo2
; 38 P0.1 (OUT)

seven_seg_lo1
; 39 P0.0 (OUT)

seven_seg_lo0
; 40 VCC (IN) VCC
;=================================================================

======

;========================================
; RAM ¼±¾ð
;========================================

zSkipFlags EQU 20H

SkipKey EQU 21H
SkipKey1 EQU 22H
SkipKey2 EQU 23H
SkipKey3 EQU 24H

zOutCount EQU 25H
zTimerCount EQU 26H

zKeyChangeFlags EQU 27H
fSkipKeyChange BIT zKeyChangeFlags.0
fActKeyChange BIT zKeyChangeFlags.1

ActKey EQU 28H
fUp_key BIT ActKey.0
fDown_key BIT ActKey.1
fHold_key BIT ActKey.2

ActKey1 EQU 29H
ActKey2 EQU 2AH
ActKey3 EQU 2BH

SwitchTime EQU 2CH
TimeLeft EQU 2DH



;========================================
; Port def
;========================================
SkipKey_port EQU P1
ActKey_port EQU P3
LED_port EQU P2
SevenSeg_port EQU P0


;========================================
; Interrupt Vector Table
;========================================
org 0 ; power on reset
ajmp PowerOn ; INT0 (IE0)
org 3
sjmp INT0_ISR
org 0bh ; TIMER0 (TF0)
sjmp Timer0_ISR
org 13h ; INT1 (IE1)
sjmp INT1_ISR
org 1bh ; TIMER1 (TF1)
sjmp Timer1_ISR
org 23h ; UART (RI & TI)
sjmp UART_ISR

;---------------------------------------
; Unused interrupt
org 0030h
INT0_ISR:
INT1_ISR:
Timer1_ISR:
UART_ISR:
reti


;========================================
; timer0 interrupt svc (50msec call)
;========================================
org 50h

Timer0_ISR:
push acc
push psw

mov tl0,#0b0h
mov th0,#3ch

acall SkipKey_Check
acall ActKey_Check

inc zTimerCount ; 1 ++
mov a,zTimerCount
clr c ;
subb a, #20 ; 20==1sec
jnz OutReturn

;--------------------------------------------------
; After 1 second
mov zTimerCount, #0

dec TimeLeft ; x--1 second,
jnz Output_TimeLeft ; not 0 jump

mov TimeLeft, SwitchTime
acall Print_Segment

;---------------------------------------------------
; channel switching
mov a, zSkipFlags ; check the

skip flag
clr c ; erase the

carry flag
subb a, #0ffh ; All skip,

return
jz OutReturn

mov a, zOutCount ; count load
rl a
mov dptr, #OUTJUMP_TABLE
jmp @a + dptr

OUTJUMP_TABLE:
sjmp OutPort0
sjmp OutPort1
sjmp OutPort2
sjmp OutPort3
sjmp OutPort4
sjmp OutPort5
sjmp OutPort6
sjmp OutPort7

;---------------------------------
;
Output_TimeLeft:
acall Print_Segment
sjmp Timer0_exit

;--------------------------------
; channel 0 output
OutPort0:
jb zSkipFlags.0, OutPort1
mov LED_port, #01H
mov zOutCount, #1
OutReturn:
sjmp Timer0_exit

;--------------------------------
; channel 1 output
OutPort1:
jb zSkipFlags.1, OutPort2
mov LED_port, #02H
mov zOutCount, #2
sjmp Timer0_exit

;--------------------------------
; channel 2 output
OutPort2:
jb zSkipFlags.2, OutPort3

mov LED_port, #04H mov

zOutCount, #3 sjmp Timer0_exit

;--------------------------------
; channel 3 output
OutPort3:
jb zSkipFlags.3, OutPort4

mov LED_port, #08H
mov zOutCount, #4

sjmp Timer0_exit

;--------------------------------
; channel 4 output
OutPort4:
jb zSkipFlags.4, OutPort5

mov LED_port, #10H mov

zOutCount, #5 sjmp Timer0_exit

;--------------------------------
; channel 5 output
OutPort5:
jb zSkipFlags.5, OutPort6

mov LED_port, #20H
mov zOutCount, #6

sjmp Timer0_exit

;--------------------------------
; channel 6 output
OutPort6:
jb zSkipFlags.6, OutPort7

mov LED_port, #40H mov

zOutCount, #7 sjmp Timer0_exit

;--------------------------------
; channel 7 output
OutPort7:
jb zSkipFlags.7, OutPort0

mov LED_port, #80H mov

zOutCount, #0
Timer0_exit:
pop psw
pop acc
reti

;=====================================
; skip key check routine
;=====================================
SkipKey_Check:
mov a, SkipKey1
clr c

subb a, SkipKey_port
jz checkkey1
mov SkipKey1, SkipKey_port
ret
checkkey1:
mov a,SkipKey2
clr c

subb a,SkipKey_port
jz checkkey2
mov SkipKey2, SkipKey_port
ret
checkkey2:
mov a,SkipKey3
clr c

subb a,SkipKey_port
jz checkkey3
mov SkipKey3, SkipKey_port
ret
checkkey3:
mov a,SkipKey
clr c

subb a,SkipKey_port
jz checkexit
mov SkipKey, SkipKey_port
setb fSkipKeyChange
checkexit:
ret

;=====================================
; Action key check
;=====================================
ActKey_Check:
mov a,ActKey1
clr c

subb a,ActKey_port
jz checkakey1
mov ActKey1, ActKey_port
ret
checkakey1:
mov a,ActKey2
clr c

subb a,ActKey_port
jz checkakey2
mov ActKey2, ActKey_port
ret
checkakey2:
mov a,ActKey3
clr c

subb a,ActKey_port
jz checkakey3
mov ActKey3, ActKey_port
ret
checkakey3:
mov a,ActKey
clr c

subb a,ActKey_port
jz checkaexit
mov ActKey, ActKey_port
setb fActKeyChange
checkaexit:
ret

;---------------------------------
; Remainning time to 7-segment(LEDs) (1~20seccond)
Print_Segment:
mov a,TimeLeft
clr c

subb a,#20
jz print20
mov a,TimeLeft
clr c

subb a,#10
jc SmallOut orl

a,#10h
mov SevenSeg_port,a
ret
SmallOut:
mov SevenSeg_port, TimeLeft
ret
print20:
mov SevenSeg_port, #20H
ret


;=====================================
; Program Main
;=====================================
org 200h

PowerOn:
PortInit:
mov SkipKey_port,#0
SetStack:
mov sp,#0fh ; Stack start at 10H
RamClear:
mov zSkipFlags,#0
mov zOutCount,#0
mov zTimerCount,#0
mov SkipKey,#0
mov SkipKey1,#0
mov SkipKey2,#0
mov SkipKey3,#0
mov zKeyChangeFlags,#0
mov ActKey,#0
mov ActKey1,#0
mov ActKey2,#0
mov ActKey3,#0

InitValue:
mov SwitchTime, #2 ; default 2 second
mov TimeLeft, SwitchTime
acall Print_Segment

InitInterrupt:
mov tmod,#00010001B ; timer 16bit mode

mov tl0,#0B0H ; 50 msec timer inturrupt happen
mov th0,#3CH ; 10000H - 3CB0H = C350H =

50000
setb et0 ; Timer0 overflow

interrupt enable
setb pt0 ; Timer0 interrupt

priority
setb ea ; All Interrupt enable
setb tr0 ; Start Timer0!!

MainLoop:
jb fSkipKeyChange, SkipKey_Action
jb fActKeyChange, ActKey_Action
sjmp MainLoop

;------------------------------------
;
SkipKey_Action:
clr fSkipKeyChange
jb SkipKey.0, toggle0
jb SkipKey.1, toggle1
jb SkipKey.2, toggle2
jb SkipKey.3, toggle3
jb SkipKey.4, toggle4
jb SkipKey.5, toggle5
jb SkipKey.6, toggle6
jb SkipKey.7, toggle7
sjmp MainLoop

toggle0:
jb zSkipFlags.0, clear0
setb zSkipFlags.0
sjmp MainLoop
clear0: clr zSkipFlags.0
sjmp MainLoop

toggle1:
jb zSkipFlags.1, clear1
setb zSkipFlags.1
sjmp MainLoop
clear1: clr zSkipFlags.1
sjmp MainLoop

toggle2:
jb zSkipFlags.2, clear2
setb zSkipFlags.2
sjmp MainLoop
clear2: clr zSkipFlags.2
sjmp MainLoop

toggle3:
jb zSkipFlags.3, clear3
setb zSkipFlags.3
sjmp MainLoop
clear3: clr zSkipFlags.3
sjmp MainLoop

toggle4:
jb zSkipFlags.4, clear4
setb zSkipFlags.4
sjmp MainLoop
clear4: clr zSkipFlags.4
sjmp MainLoop

toggle5:
jb zSkipFlags.5, clear5
setb zSkipFlags.5
sjmp MainLoop
clear5: clr zSkipFlags.5
sjmp MainLoop

toggle6:
jb zSkipFlags.6, clear6
setb zSkipFlags.6
sjmp MainLoop
clear6: clr zSkipFlags.6
sjmp MainLoop

toggle7:
jb zSkipFlags.7, clear7
setb zSkipFlags.7
sjmp MainLoop
clear7: clr zSkipFlags.7
sjmp MainLoop

;----------------------------------
;
ActKey_Action:
clr fActKeyChange
jb fUp_key, time_up
jb fDown_key, time_down
jb fHold_key, hold_channel
ajmp MainLoop

time_up:
mov a,SwitchTime
clr c

subb a,#20 ; Max 20 second
jz timeup_exit
inc SwitchTime
timeup_exit:
ajmp MainLoop

time_down:
mov a,SwitchTime
clr c

subb a,#1 ; min 1 second
jz timeup_exit
dec SwitchTime
ajmp MainLoop

hold_channel:
jb tr0,stop_timer ; Stop when timer

running
mov tl0,#0b0h ; timer counter value

Re-setting
mov th0,#3ch
setb tr0 ; Timer Re-running
stop_timer:
clr tr0
ajmp MainLoop

;----------------------------------
end
; So, sorry long dummy codes !!! ^^; (joseffk@yahoo.com)


List of 4 messages in thread
TopicAuthorDate
Help me, 8051 timer long.. short control            01/01/70 00:00      
   How many LEDs?            01/01/70 00:00      
   novice for MCS51            01/01/70 00:00      
      RE: novice for MCS51            01/01/70 00:00      

Back to Subject List