,,,; Firmware for Welder Synchron Timer 1-99 Halfwaves
,,,; AT89C2051
,,,; Konstantin Gilsenberg
,,,; slowolitus@rambler.ru
,,,; 20.07.2011
,,,
,,,; ---------constants-----------
,,,MAXTIME         EQU 99          ;
,,,MINTIME         EQU  1      ;
,,,PEDDB       EQU 3               ;pedal debounce constante
,,,KEYDB       EQU 5               ;key debounce delay    (x10mS)
,,,KEYDA           EQU 70          ;key autorepeat delay  (x10mS)
,,,KEYARR      EQU 15              ;autorepeat rate       (x10mS)
,,,KEYFARR         EQU 7           ;fast autorepeat rate  (x10mS)
,,,ARCOUNT         EQU 10          ;autorepeat to fast autorepeat count
,,,
,,,; ----------ports--------------
000B,,,SEG_A   EQU P1.2                ;indicators segment A
000B,,,SEG_B   EQU P1.3                ;indicators segment B
000B,,,SEG_C   EQU P1.4                ;indicators segment C
000B,,,SEG_D   EQU P1.5                ;indicators segment D
000B,,,SEG_E   EQU P1.6                ;indicators segment E
000B,,,SEG_F   EQU P1.7                ;indicators segment F
000B,,,SEG_G   EQU P3.7                ;indicators segment G
,,,
000B,,,SCAN0   EQU TXD                 ;display scan digit 1
000B,,,SCAN1   EQU RXD                 ;display scan digit 2
000B,,,KEY_UP  EQU T0                  ;UP   key
000B,,,KEY_DN  EQU INT1                ;DOWN key
000B,,,PED_IN  EQU T1          ;Pedal input
000B,,,FIRE    EQU INT0                ;Fire out
000B,,,CP      EQU P3.6                ;comparator
,,,;---------- Bit addressing memory
,,,FLAGS   EQU 020H                ;flags
,,,TIME    EQU 021H            ;time set
,,,COUNT   EQU 022H                ;time counter
,,,ZERO    EQU 023H                ;zero crossing register
,,,COMP    EQU 024H        ;comparator state
,,,PED_ST  EQU 025H                ;pedal state prev/curr
,,,KEYDBT  EQU 026H                ;key debounce timer
,,,KEYTM   EQU 027H                ;key timer
,,,PREV    EQU 028H                ;previous key code
,,,REPCNT  EQU 029H                ;repeat counter
,,,;---------- Byte addressing memory
,,,UNITS   EQU 030H
,,,TENS    EQU 031H
,,,;---------- Flags-----------------
000B,,,ON_OFF  EQU FLAGS.0     ;timer ON/OFF bit
000B,,,PRESS   EQU FLAGS.1     ;keyboard press bit
000B,,,DIGIT   EQU FLAGS.2 ;digit
,,,;PEDAL   EQU FLAGS.3 ;pedal press bit
000B,,,HWAVE   EQU FLAGS.4 ;previous halfwave polarity
,,,;----------- Work registers------
,,,PEDDBT  EQU R6
,,,INTEG   EQU R7
,,,;--------- Vectors Area ------------
,,,org     0000h
0000,02 00 2B,,                jmp Begin           ;reset vector
,,,org     0003h
0003,32,,        reti
,,,org     000Bh
000B,32,,        reti
,,,org     0013h
0013,32,,                reti
,,,org     001Bh
001B,32,,                reti
,,,org     0023h
0023,32,,                reti
,,,;---------- Main Program -----------
,,,;--------- Initialisation ----------
,,,using           0
,,,org             002Bh
002B,C2 AF,Begin,Begin:          clr EA                          ; interruptions disabled
002D,75 90 FF,,                mov P1,#0FFh
0030,75 B0 FF,,                        mov P3,#0FFh
0033,75 21 01,,                        mov TIME,#MINTIME
0036,85 21 22,,                        mov COUNT,TIME
0039,90 01 BA,,                        mov DPTR,#FONT
003C,E4,,                        clr A
003D,F5 28,,                        mov PREV,A
003F,F5 24,,                        mov COMP,A
0041,F5 25,,                        mov PED_ST,A
0043,F5 20,,                        mov FLAGS,A
0045,D2 00,,                        setb ON_OFF
,,,;------------------------------------
0047,A2 B6,MAIN,MAIN:           mov C,CP                  ;zero crossing detection
0049,E5 24,,                        mov A,COMP
004B,33,,                        rlc A
004C,F5 24,,                        mov COMP,A                ;save comparator state in COMP
004E,30 20 05,,                        jnb COMP.0,COM_0
0051,30 21 05,,                        jnb COMP.1,FRONT
0054,80 F1,,                        jmp MAIN
0056,30 21 EE,COM_0,        COM_0:  jnb COMP.1,MAIN
,,,
0059,30 00 31,FRONT,        FRONT:  jnb ON_OFF,T_OFF  ;timer is on/off?
,,,
005C,E5 25,,                        mov A,PED_ST      ;pedal processing
005E,B4 00 03,,                        cjne A,#00h,REF_1
0061,02 00 93,,                        jmp SUBR
0064,B4 01 03,REF_1,        REF_1:  cjne A,#01h,REF_2
0067,02 00 77,,                        jmp REF_4
006A,B4 02 06,REF_2,        REF_2:  cjne A,#02h,REF_3
006D,85 21 22,,                        mov COUNT,TIME          ;Timer reload
0070,02 00 93,,                        jmp SUBR
0073,E5 22,REF_3,        REF_3:  mov A,COUNT                     ;time over?
0075,60 1C,,                        jz      SUBR
,,,
0077,30 20 06,REF_4,        REF_4:  jnb COMP.0,NEG          ;check edges (polarity of halfwave)
007A,30 04 16,,                        jnb HWAVE,SUBR
007D,02 00 83,,                        jmp REF_5
0080,20 04 10,NEG,        NEG:    jb HWAVE,SUBR           ;negadiv edge detected!
0083,12 00 BB,REF_5,        REF_5:  call PULSE                      ;thyristor fire
0086,B2 04,,                        cpl HWAVE
0088,15 22,,                        dec COUNT
008A,02 00 93,,                        jmp SUBR
,,,
008D,30 28 03,T_OFF,T_OFF:          jnb PED_ST.0,SUBR     ;pedal is pressed?
0090,12 00 BB,,                        call PULSE                       ;thyristor fire
0093,12 00 9E,SUBR,SUBR:           call CK_PEDAL            ;if timer is off
0096,12 00 CC,,                        call CK_KEY
0099,12 01 68,,                        call INDIC
009C,80 A9,,                        jmp MAIN
,,,
,,,;--------- Pedal check----------
009E,D2 B5,CK_PEDAL,CK_PEDAL:   setb PED_IN                                 ;debounce first step, high frequency
00A0,7E 14,,                        mov     PEDDBT,#20
00A2,74 00,,                        mov A,#0
00A4,A2 B5,DEB0,DEB0:           mov C,PED_IN
00A6,34 00,,                        addc A,#0
00A8,DE FA,,                        djnz PEDDBT,DEB0
00AA,94 0A,,                        subb A,#10
,,,
00AC,40 04,,                        jc DEB3
00AE,C3,,                        clr C
00AF,02 00 B3,,                        jmp DEB4
,,,
,,,;                       jc  DEB2
,,,;                       cjne INTEG,#0,DEB1                      ;debounce second step, integration
,,,;                       clr C
,,,;                       jmp DEB4
,,,;  DEB1:    dec INTEG
,,,;                       ret
,,,;  DEB2:        mov A,#PEDDB
,,,;                   subb A,INTEG
,,,;                       jc      DEB3
,,,;                       inc INTEG
,,,;                       ret
,,,
00B2,D3,DEB3,        DEB3:   setb C
00B3,E5 25,DEB4,        DEB4:   mov A,PED_ST
00B5,33,,                rlc A
00B6,54 03,,                        anl A,#03h
00B8,F5 25,,                        mov PED_ST,A                            ;PED_ST.0 current pedal state, PED_S
00BA,22,,                        ret
,,,;-------- 5 ms pulse ------------
00BB,C2 B2,PULSE,PULSE:      clr FIRE
00BD,12 00 C3,,                        call WAIT_5ms
00C0,D2 B2,,                        setb  FIRE
00C2,22,,                        ret
,,,;-------- Wait 5 ms -------------
00C3,7C 09,WAIT_5ms,WAIT_5ms:   mov R4,#9
00C5,7B FF,REF2,          REF2: mov R3,#255
00C7,DB FE,REF1,          REF1: djnz R3,REF1
00C9,DC FA,,                        djnz R4,REF2
00CB,22,,                        ret
,,,;======== Keyboard check ========
,,,
00CC,A2 B4,CK_KEY,CK_KEY:         MOV C,KEY_UP            ;keyboard check
00CE,82 B3,,                        ANL C,KEY_DN
00D0,40 2E,,                        JC  NP                          ;jump if no press
00D2,74 FF,,                        MOV A,#0FFH
00D4,A2 B4,,                        MOV C,KEY_UP
00D6,33,,                        RLC A
00D7,A2 B3,,                        MOV C,KEY_DN
00D9,33,,                        RLC A
00DA,F4,,                        CPL A                           ;ACC.1 - UP, ACC.0 - DN
00DB,F8,,                        MOV R0,A
00DC,65 28,,                        XRL A,PREV
00DE,70 1E,,                        JNZ ND
00E0,E5 26,,                        MOV A,KEYDBT             ; Time over?
00E2,60 04,,                        JZ DOV
00E4,15 26,,                        DEC KEYDBT
00E6,80 1D,,                        SJMP W1                         ;jump if debounce delay is not over
00E8,B8 01 03,DOV,        DOV:    CJNE R0,#1,NDN
00EB,12 01 33,,                        CALL DO_DN                      ;DOWN pressed
00EE,B8 02 03,NDN,        NDN:    CJNE R0,#2,NUP
00F1,12 01 0C,,                        CALL DO_UP                      ;UP pressed
00F4,B8 03 03,NUP,        NUP:    CJNE R0,#3,NUD
00F7,12 01 5A,,                        CALL DO_UD                      ;UP+DOWN pressed
00FA,D2 01,NUD,        NUD:    SETB PRESS
00FC,80 07,,                        SJMP W1
00FE,88 28,ND,        ND:             MOV PREV,R0                     ;edge detected!
0100,C2 01,NP,        NP:             CLR PRESS
0102,75 26 05,,                        MOV KEYDBT,#KEYDB       ;debounce timer load
,,,
0105,E5 27,W1,        W1:             MOV A,KEYTM
0107,60 02,,                        JZ W2
0109,15 27,,                DEC KEYTM               ;advance key timer
010B,22,W2,        W2:             RET
,,,
,,,;UP key processing:
,,,
010C,20 01 08,DO_UP,DO_UP:          JB PRESS,UPH    ;jump if key hold
010F,75 27 46,,                        MOV KEYTM,#KEYDA        ;first press, autorepeat delay load
0112,75 29 00,,                        MOV REPCNT,#0   ;autorepeat counter clear
0115,80 11,,                        SJMP UP_DO
0117,E5 27,UPH,        UPH:    MOV A,KEYTM     ;KEYTM check
0119,70 17,,                        JNZ UP_RET
011B,C3,,                        CLR C
011C,75 27 0F,,                        MOV KEYTM,#KEYARR       ;load normal repeat rate
011F,E5 29,,                        MOV A,REPCNT
0121,94 0A,,                        SUBB A,#ARCOUNT ;REPCNT check
0123,40 03,,                        JC UP_DO                ;jump if REPCNT < ARCOUNT
0125,75 27 07,,                        MOV KEYTM,#KEYFARR      ;load fast repeat rate
0128,E5 21,UP_DO,        UP_DO:  MOV A,TIME
012A,64 63,,                        XRL A,#MAXTIME
012C,60 04,,                        JZ UP_RET               ;jump if limit
012E,05 21,,                        INC TIME                ;time inc.
0130,05 29,,                        INC REPCNT      ;autorepeat counter inc.
0132,22,UP_RET,        UP_RET: RET
,,,
,,,;DOWN key processing:
,,,
0133,20 01 08,DO_DN,DO_DN:          JB PRESS,DNH    ;jump if key hold
0136,75 27 46,,                        MOV KEYTM,#KEYDA        ;first press, autorepeat delay load
0139,75 29 00,,                        MOV REPCNT,#0   ;autorepeat counter clear
013C,80 11,,                        SJMP DN_DO
013E,E5 27,DNH,        DNH:    MOV A,KEYTM     ;KEYTM check
0140,70 17,,                        JNZ DN_RET
0142,C3,,                        CLR C
0143,75 27 0F,,                        MOV KEYTM,#KEYARR       ;load normal repeat rate
0146,E5 29,,                        MOV A,REPCNT
0148,94 0A,,                        SUBB A,#ARCOUNT ;REPCNT check
014A,40 03,,                        JC DN_DO                ;jump if REPCNT < ARCOUNT
014C,75 27 07,,                        MOV KEYTM,#KEYFARR      ;load fast repeat rate
014F,E5 21,DN_DO,        DN_DO:  MOV A,TIME
0151,64 01,,                        xrl A,#MINTIME
0153,60 04,,                        jz  DN_RET
0155,15 21,,                        DEC TIME                ;time dec.
0157,05 29,,                        INC REPCNT      ;autorepeat counter inc.
0159,22,DN_RET,        DN_RET: RET
,,,
,,,;UP+DOWN key processing:
,,,
015A,20 01 0A,DO_UD,DO_UD:          JB PRESS,UD_RET ;no hold mode
015D,20 00 05,,                        jb ON_OFF,UD_OFF
0160,D2 00,,                        setb ON_OFF
0162,02 01 67,,                        jmp UD_RET
0165,C2 00,UD_OFF,        UD_OFF: clr ON_OFF
0167,22,UD_RET,        UD_RET: RET
,,,
,,,;------------------Indication-----------------------
0168,20 00 09,INDIC,INDIC:          jb ON_OFF,IND1
016B,75 30 0A,,                        mov UNITS,#0Ah  ; "-"
016E,75 31 0A,,                        mov TENS, #0Ah  ; "-"
0171,02 01 87,,                        jmp IND2
0174,E5 21,IND1,IND1:           mov A,TIME              ; BCD decoding
0176,75 F0 0A,,                        mov B,#10
0179,84,,                        div AB
017A,70 06,,                        jnz NZ
017C,75 31 0B,,                        mov TENS,#0Bh           ; blank code
017F,02 01 84,,                        jmp UN
0182,F5 31,NZ,                NZ: mov TENS,A
0184,85 F0 30,UN,                UN:     mov UNITS,B
0187,20 02 0B,IND2,IND2:       jb DIGIT,IND3
018A,E5 30,,            mov A,UNITS          ; indicate units
018C,93,,                        movc A,@A+DPTR
018D,12 01 A0,,                        call IND
0190,C2 B1,,                        clr  SCAN0
0192,02 01 9D,,                        jmp IND4
0195,E5 31,IND3,        IND3:   mov A,TENS               ; indicate tens
0197,93,,                        movc A,@A+DPTR
0198,12 01 A0,,                        call IND
019B,C2 B0,,                        clr  SCAN1
019D,B2 02,IND4,        IND4:   cpl DIGIT
019F,22,,                        ret
,,,
01A0,D2 B1,IND,IND:            setb SCAN0
01A2,D2 B0,,                        setb SCAN1
01A4,13,,                        RRC A
01A5,92 92,,                        MOV SEG_A,C
01A7,13,,                        RRC A
01A8,92 93,,                        MOV SEG_B,C
01AA,13,,                        RRC A
01AB,92 94,,                        MOV SEG_C,C
01AD,13,,                        RRC A
01AE,92 95,,                        MOV SEG_D,C
01B0,13,,                        RRC A
01B1,92 96,,                        MOV SEG_E,C
01B3,13,,                        RRC A
01B4,92 97,,                        MOV SEG_F,C
01B6,13,,                        RRC A
01B7,92 B7,,                        MOV SEG_G,C
01B9,22,,                        RET
,,,
,,,;Font table
,,,;                   GFEDCBA
01BA,,FONT,FONT:
01BA,C0,,                        DB 11000000B    ;code 00H, character 0
01BB,F9,,                        DB 11111001B    ;code 01H, character 1
01BC,A4,,                        DB 10100100B    ;code 02H, character 2
01BD,B0,,                        DB 10110000B    ;code 03H, character 3
01BE,99,,                        DB 10011001B    ;code 04H, character 4
01BF,92,,                        DB 10010010B    ;code 05H, character 5
01C0,82,,                        DB 10000010B    ;code 06H, character 6
01C1,F8,,                        DB 11111000B    ;code 07H, character 7
01C2,80,,                        DB 10000000B    ;code 08H, character 8
01C3,90,,                        DB 10010000B    ;code 09H, character 9
01C4,BF,,                        DB 10111111B    ;"-"
01C5,FF,,                        DB 11111111B    ;character blank
,,,
000E,,,END
