??? 10/26/05 21:33 Read: times |
#102997 - Improved code advice ? |
Hello every one,
After searching here and on net when I did not find the information I needed I thought lets put everything that I have done so that some one can provide helpful reply. I have made a working intercom unit using AT89c52 and with following hardware details, the system is working perfectly with one digit dialing. I code which is pasted below is for four lines mini-exchange and is helpful i guess for any one trying to make a microcontroller based intercom as i found that there is no specific code available here about an intercom exchange. Now I want to use CLI device with each set in such a way that each CLI device, what improvement or addition the code should have in order to send data to the CLI device in such a way that no hardware addition is done as CLI (encoder or decorder nothing), is it possible to use the coding only to send data to a CLI device in my case. Its just a four line intercom unit which is only for pulse dialing telephone sets (its does not mean that its a PBX telephone set act as intercoms) means no Tone Dialing encoding IC is used, controller is used to decode the dialed number from the set since it is pulse dialing. Numbering of the set is : 1 , 2 , 3 , 4 Objective is each telephone set has a CLI device contacted in a normal way now when telephone 1 calls telephone 2 , the CLI attach with telephone 2 shows the calling party's number to the receiving party means telephone 2 user knows that telephone 1 is calling him. Now how what changes should be done in the code in order to get CLI working for this mini-exchange. Hardware Details Crystal :- 11.052 MHz Controller:- AT89c52 Analog Switch: MT8816 Port 2 :- Controller receives High signal for Off Hook , Low for On Hook & Pulse Dialing signals. (So I don’t send P2 00000000 in the start of the code becuase as the power goes on P2 automatically becomes low due to hardware arrangement, now please don’t say that it has be the other way around because hardware is fine and working perfectly only code improvement is needed) Port 1:- Controller sends 8 bit data to MT8816. Port 3:- P 3.7 strobe of MT8816 CS pin of MT8816 is already set high through hardware arrangement so no need to think about any other pin of MT8816 in order to make or break connection of rows and columns. Port 0: Controller sends ringing signals to the 5 volts relays when ever ringing is required on any set. CODE for FOUR Lines Intercom considering the above arrangement: ;Macro definition of timer0 in 16 bit itmer mode ;***********************************MACRO DEFINITION FOR PRODUCING REQUIRED DELAYS************************************* ;The follwing macro implements the logic to produce any amount of required delays in the program. The required delay is ;passed as an argument in the form of a hexadecimal number, which is used as a preset value of timer0 to make the timer ;overflow after a certain interval of time. ;********************************************************************************************************************** TIMER00 MACRO HIGHVALUE0, LOWVALUE0 LOCAL WAIT CLR TF0 ;clear overflow flag CLR TR0 ;stop timer MOV TH0,#HIGHVALUE0 ;load high value MOV TL0,#LOWVALUE0 ;load low value SETB TR0 ;start timer WAIT: JNB TF0,WAIT ;check overflow flag CLR TF0 ;clear overflow flag CLR TR0 ;stop timer ENDM ;end of macro ;*******************************************MACRO DEFINITIONS FOR EACH SET******************************************** ;*******************************************MACRO FOR SET1************************************************************ ;Macro definition for specific operations for each number ;The code is written for Set '1' and this set can call the sets 2,3,4 CON_DIALNUMBER01 MACRO RING_BIT, LABEL_RESTART, DIAL_VALUE,DIAL_BIT, DIAL_VALUE_REM, CON_FLAG LOCAL REPEAT, CONNECT, REMCONNECT, CHECK, AGAIN, AGAIN2, DUMMY,STILLCONNECT JB DIAL_BIT,DUMMY ;send P2.n, which is already offhook JB CON_FLAG,DUMMY ;if this set is already receiving ringing from another set REPEAT: SETB RING_BIT ;sendringing signal to dialed number SETB CON_FLAG ;ensure that the dialed set is in continuing to connect... MOV P1,#090H ;send dial tone form P2.0 MOV R1,#010H ;laod '10' in r1 for continuous loop AGAIN: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN ;decrement and jump if not 0 CPL RING_BIT ;compliment in order to send ringing signal MOV P1,#010H ;send for ringback tone(connect X2,Y0) MOV R1,#010H ;in order to create half second delay AGAIN2: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN2 ;decrement and jump if not zero INC R2 ;increment counter of ringing signal JB DIAL_BIT, CONNECT ;picked up before 8rings JNB P2.0,REMCONNECT ;if dialed set goes to onhook CJNE R2,#008H,REPEAT ;ring continous til 8rings CLR RING_BIT ;remove ringing signal if not picked up before 8 rings CLR CON_FLAG ;ensure that the dialed set is now NOT connected MOV P1,#010H ;remove ring back tone DUMMY: JMP INCORRECT01 ;proceed as incorrect number CONNECT: CLR RING_BIT ;remove ringing signal SETB CON_FLAG ;ensure that the dialed set is already connected MOV P1,#010H ;remove ring back tone MOV P1,#097H ;connect X2 with y7 MOV P1, #DIAL_VALUE ;connect dial set with y7 JNB P2.0, CHECK ;if set1 goes on hook ;JNB DIAL_BIT, CHECK ;if dialed set goes on hook ;LJMP CONNECT ;else continuous connectionis made STILLCONNECT: JB P2.0,$ ;still connection until set1 goes onhook CHECK: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JNB P2.0, REMCONNECT ;if still on hook i.e. valid on hook ;JNB DIAL_BIT, REMCONNECT ;if still on hook i.e.valid on hook LJMP STILLCONNECT ;else invalid on hook REMCONNECT: MOV P1,#017H ;remove connection X2,Y7 CLR CON_FLAG ;ensure that the dialed set is now NOT connected MOV P1,#DIAL_VALUE_REM ;remove connection dialed set MOV R0,#000H ;reinitializethe pulse counter MOV R2,#000H ;reinitializethe ringing counter LJMP WAITOFFHOOK01 ;go back, restart theprogram ENDM ;END OF MACRO ;*********************************MACRO FOR SET2**************************************** CON_DIALNUMBER02 MACRO RING_BIT, LABEL_RESTART, DIAL_VALUE,DIAL_BIT, DIAL_VALUE_REM, CON_FLAG LOCAL REPEAT, CONNECT, REMCONNECT, CHECK, AGAIN, AGAIN2,DUMMY,STILLCONNECT JB DIAL_BIT, DUMMY ;send P2.n, which is already offhook JB CON_FLAG,DUMMY ;if this set is already receiving ringing from anotherset REPEAT: SETB RING_BIT ;sendringing signal to dialed number SETB CON_FLAG ;ensure that the dialed set is in continuing to connect... MOV P1,#098H ;send dial tone form P2.1 MOV R1,#010H ;laod '10' in r1 for continuous loop AGAIN: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN ;decrement and jump if not 0 CPL RING_BIT ;compliment in order to send ringing signal MOV P1,#018H ;send for ringback tone MOV R1,#010H ;in order to create half second delay AGAIN2: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN2 ;decrement and jump if not zero INC R2 ;countringing signal JB DIAL_BIT, CONNECT ;picked up before 8rings JNB P2.1,REMCONNECT ;if dialed set goes to onhook CJNE R2,#008H,REPEAT ;ring continous til 8rings CLR RING_BIT ;remove ringing signal if not picked up before 8 rings CLR CON_FLAG ;ensure that the dialed set in now NOT connected to any set MOV P1,#018H ;remove ring back tone DUMMY: LJMP INCORRECT02 ;proceed as incorrect number CONNECT: CLR RING_BIT ;remove ringing signal SETB CON_FLAG ;ensure that the dialed set is already connected MOV P1,#018H ;remove ring back tone MOV P1,#09EH ;connect X3 with y6 MOV P1, #DIAL_VALUE ;connect dial set with y6 ;JNB P2.1, CHECK ;if set1 goes on hook ;JNB DIAL_BIT, CHECK ;if dialed set goes on hook ;LJMP CONNECT ;else continuous connectionis made STILLCONNECT:JB P2.1,$ ;still connection until set2 goes onhook CHECK: IMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JNB P2.1, REMCONNECT ;if still on hook i.e. valid on hook ;JNB DIAL_BIT, REMCONNECT ;if still on hook i.e.valid on hook LJMP STILLCONNECT ;else invalid on hook REMCONNECT: MOV P1,#01EH ;remove connection X3,Y6 CLR CON_FLAG ;ensure that the dialed set is now NOT connected MOV P1,#DIAL_VALUE_REM ;remove connection dialed set MOV R0,#000H ;reinitializethe pulse counter MOV R2,#000H ;reinitializethe ringing counter LJMP WAITOFFHOOK02 ;go back, restart theprogram ENDM ;END OF MACRO ;*********************************MACRO FOR SET3**************************************** CON_DIALNUMBER03 MACRO RING_BIT, LABEL_RESTART, DIAL_VALUE,DIAL_BIT, DIAL_VALUE_REM, CON_FLAG LOCAL REPEAT, CONNECT, REMCONNECT, CHECK, AGAIN, AGAIN2,DUMMY,STILLCONNECT JB DIAL_BIT, DUMMY ;send P2.n, which is already offhook JB CON_FLAG,DUMMY ;if this set is already receiving ringing from another set REPEAT: SETB RING_BIT ;sendringing signal to dialed number SETB CON_FLAG ;ensure that the dialed set is in continuing to connect... MOV P1,#0A0H ;send dial tone form P2.2 MOV R1,#010H ;laod '10' in r1 for continuous loop AGAIN: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN ;decrement and jump if not 0 CPL RING_BIT ;compliment in order to send ringing signal MOV P1,#020H ;send for ringback tone MOV R1,#010H ;in order to create half second delay AGAIN2: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN2 ;decrement and jump if not zero INC R2 ;countringing signal JB DIAL_BIT, CONNECT ;picked up before 8rings JNB P2.2,REMCONNECT ;if dialed set goes to onhook CJNE R2,#008H,REPEAT ;ring continous til 8rings CLR RING_BIT ;remove ringing signal if not picked up before 8 rings CLR CON_FLAG ;ensure that the dialed set in now NOT connected to any set MOV P1,#020H ;remove ring back tone DUMMY: LJMP INCORRECT03 ;proceed as incorrect number CONNECT: CLR RING_BIT ;remove ringing signal SETB CON_FLAG ;esure that the dialed set is already connected MOV P1,#020H ;remove ring back tone MOV P1,#0A5H ;connect X4 with y5 MOV P1, #DIAL_VALUE ;connect dial set with y5 ;JNB P2.1, CHECK ;if set1 goes on hook ;JNB DIAL_BIT, CHECK ;if dialed set goes on hook ;LJMP CONNECT ;else continuous connectionis made STILLCONNECT:JB P2.2,$ ;sill connection until set3 goes onhook CHECK: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JNB P2.2, REMCONNECT ;if still on hook i.e. valid on hook ;JNB DIAL_BIT, REMCONNECT ;if still on hook i.e.valid on hook LJMP STILLCONNECT ;else invalid on hook REMCONNECT:MOV P1,#025H ;remove connection X4,Y5 CLR CON_FLAG ;ensure that the dialed set is now NOT connected MOV P1,#DIAL_VALUE_REM ;remove connection dialed set MOV R0,#000H ;reinitializethe pulse counter MOV R2,#000H ;reinitialize the ringing counter LJMP WAITOFFHOOK03 ;go back, restart theprogram ENDM ;END OF MACRO ;*********************************MACRO FOR SET4**************************************** CON_DIALNUMBER04 MACRO RING_BIT, LABEL_RESTART, DIAL_VALUE,DIAL_BIT, DIAL_VALUE_REM, CON_FLAG LOCAL REPEAT, CONNECT, REMCONNECT, CHECK, AGAIN, AGAIN2,DUMMY,STILLCONNECT JB DIAL_BIT, DUMMY ;send P2.n, which is already offhook JB CON_FLAG,DUMMY ;if this set is already receiving ringing from another set REPEAT: SETB RING_BIT ;sendringing signal to dialed number SETB CON_FLAG ;ensure that the dialed set is in continuing to connect... MOV P1,#0A8H ;send dial tone form P2.3 MOV R1,#010H ;laod '10' in r1 for continuous loop AGAIN: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN ;decrement and jump if not 0 CPL RING_BIT ;compliment in order to send ringing signal MOV P1,#028H ;send for ringback tone MOV R1,#010H ;in order to create half second delay AGAIN2: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) DJNZ R1, AGAIN2 ;decrement and jump if not zero INC R2 ;countringing signal JB DIAL_BIT, CONNECT ;picked up before 8rings JNB P2.3,REMCONNECT ;if dialed set goes to onhook CJNE R2,#008H,REPEAT ;ring continous til 8rings CLR RING_BIT ;remove ringing signal if not picked up before 8 rings CLR CON_FLAG ;ensure that the dialed set in now NOT connected to any set MOV P1,#028H ;remove ring back tone DUMMY: LJMP INCORRECT04 ;proceed as incorrect number CONNECT: CLR RING_BIT ;remove ringing signal SETB CON_FLAG ;ensure that the dialed set is already connected MOV P1,#028H ;remove ring back tone MOV P1,#0ACH ;connect X5 with y4 MOV P1, #DIAL_VALUE ;connect dial set with y4 ;JNB P2.1, CHECK ;if set1 goes on hook ;JNB DIAL_BIT, CHECK ;if dialed set goes on hook ;LJMP CONNECT ;else continuous connectionis made STILLCONNECT:JB P2.3,$ ;still connection until set3 goes onhook CHECK: TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JNB P2.3, REMCONNECT ;if still on hook i.e. valid on hook ;JNB DIAL_BIT, REMCONNECT ;if still on hook i.e.valid on hook LJMP STILLCONNECT ;else invalid on hook REMCONNECT:MOV P1,#02CH ;remove connection X5,Y4 CLR CON_FLAG ;ensure that the dialed set is now NOT connected MOV P1,#DIAL_VALUE_REM ;remove connection dialed set MOV R0,#000H ;reinitializethe pulse counter MOV R2,#000H ;reinitialize the ringing counter LJMP WAITOFFHOOK04 ;go back, restart theprogram ENDM ;END OF MACRO ;***********************************Start of Code********************************************* ORG 0000H ;reset LJMP MAIN ;jump to main program ORG 0001BH ;Vector address of timer1 LJMP T1ISR ;jump to scheduler ISR ORG 0030H ;start of program CONST EQU 200D CONST2 EQU 100D DELAY_50ms EQU 0CF2CH ;12.5ms+((2.5*15021)/1000)=50ms DELAY_25ms EQU 0E69CH ;6.5+((1.3*15021)/1000)=25ms ;***********************************MAIN PROGRAM****************************** MAIN: MOV P0,#000H ;sothat at start the phones are not ringing MOV P1,#000H ;reset MT8816address and data pin MOV P3,#000H ;reset port3 MOV B,#000H ;reset connection flags MOV TMOD,#011H ;use timer0 and timer1 in mode 1(16 bit timer mode) MOV TH1,#0ECH ;load high byte values of timer1 MOV TL1,#078H ;load low byte value of timer1 SET1 EQU 030H ;starting address of code for set1 SET2 EQU 03CH ;starting address of code for set2 SET3 EQU 048H ;starting address of code for set3 SET4 EQU 054H ;starting address of code for set1 SETB EA ;enavle global interrupt SETB ET1 ;enable timer1 interrupt SETB RS1 ;set register bank select bit1 SETB RS0 ;set register bank select bit2 MOV R0, #SET1 ;have to start execution from set1 CLR RS1 ;clear register bank select bit0 CLR RS0 ;clear register bank select bit1 MOV R0,#000H ;used for pulsecounter MOV R2,#000H ;to count ringing signal SETB P3.7 ;set strobe pin MOV 031H, #HIGH(WAITOFFHOOK01) ;save high byte of address of set1 code MOV 032H , #LOW(WAITOFFHOOK01) ;save low byte of address of set1 code MOV 03DH, #HIGH(WAITOFFHOOK02) ;save high byte of address of set2 code MOV 03EH, #LOW(WAITOFFHOOK02) ;save low byte of address of set2 code MOV 049H ,#HIGH(WAITOFFHOOK03) ;save high byte of address of set3 code MOV 04AH ,#LOW(WAITOFFHOOK03) ;save low byte of address of set3 code MOV 055H ,#HIGH(WAITOFFHOOK04) ;save high byte of address of set4 code MOV 056H ,#LOW(WAITOFFHOOK04) ;save lowbyte of address of set4 code MOV 03FH,#00H ;clear connection flag space SETB TR1 ;start timer1 (use for scheduling) ;*******************************SET1 CODE*************************************** WAITOFFHOOK01: JNB P2.0,$ ;check continuously whether the set is offhook JB B.0,WAITOFFHOOK01 ;if already connected MOV P1,#090H MOV R1,#CONST ;initialize 5 sec counter AGAIN01: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.0,PULSEDIALED01 ;if pulse dialed? DJNZ R1,AGAIN01 ;decrement and jump if no t zero MOV P1,#10H ;remove dial tone if no activity for 5 sec LJMP WAITONHOOK01 ;wait for offhook again PULSEDIALED01: MOV P1,#010H ;remove dialtone LOW_PUSLEWIDTH_COUNTER01: MOV R1,#CONST ;200*250 usec=50 ms AGAIN201: TIMER00 0FFH,006H ;call macro with high and low value of (-250) JB P2.0,HIGH_PULSEWIDTH_COUNTER01 ;jump if valid pulse DJNZ R1,AGAIN201 ;decrement and jump if not zero MOV P1,#010H ;remove dial tone,not valid pulse, set is on hook LJMP WAITOFFHOOK01 ;again check and wait for offhook HIGH_PULSEWIDTH_COUNTER01: INC R0 ;count pulses MOV R1,#CONST ;200*325usec= 65 msec AGAIN301: TIMER00 0FEH,0BBH ;call macro with (-325) JNB P2.0, LOW_PUSLEWIDTH_COUNTER01 ;pulse again DJNZ R1,AGAIN301 ;decrement and jump if not zero ;DIALED NUMBER IS LOADED IN R0.NOW CHECK FOR VALID NUMBER MOV DPTR,#0D00H ;laod base register in DPTR MOV A,R0 ;laod dialed number in accumulator as offset MOVC A,@A+DPTR ;laod the dialed number from lookup table by offset CLR C ;in order to perfomr subtractionsuccesfully SUBB A,R0 ;(A)<---- (A)-(R0)to check whether the dialed number is valid CJNE A,#000H,INCORRECT01 ;incorrect number CJNE R0,#001H,CORRECT01 ;else dialed the same set number LJMP NUMBER101 ;number 1 is dialed CORRECT01: CJNE R0,#002H,NEXT01 ;if dialed number is not 2 LJMP NUMBER201 ;if dialed number is 2 NEXT01: CJNE R0,#003H,NEXT201 ;not 3 LJMP NUMBER301 ;if dialed number is 3 NEXT201: LJMP NUMBER401 ;Therefore ultimate dialed number is 4 INCORRECT01: MOV P1,#090H ;dial tone MOV R1,#010H AGAIN4401: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.0,RESTART01 ;if set onhook DJNZ R1,AGAIN4401 ;decrement and jump if not zero MOV P1,#010H ;set dial tone to generate engage tone MOV R1,#010H AGAIN5501: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.0 ,RESTART01 ;if onhook DJNZ R1,AGAIN5501 ;decrement and jump if not zero LJMP INCORRECT01 ;else onhook RESTART01: MOV P1,#010H ;remove engage tone MOV R0,#000H ;reinitialize the pulse counter LJMP WAITONHOOK01 ;resatart theprogram NUMBER101: MOV R0,#000H ;reinitializepulse counter MOV P1,#010H LJMP INCORRECT01 NUMBER201: CON_DIALNUMBER01 P0.1, RESTART01, 09FH, P2.1, 01FH, B.1 NUMBER301: CON_DIALNUMBER01 P0.2, RESTART01, 0A7H, P2.2, 027H, B.2 NUMBER401: CON_DIALNUMBER01 P0.3, RESTART01, 0AFH, P2.3, 02FH, B.3 WAITONHOOK01: JB P2.0,WAITONHOOK01 ;wait for onhook TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JB P2.0,WAITONHOOK01 ;not valid onhook LJMP WAITOFFHOOK01 ;valid offhook! restart... ;*******************************SET2 CODE*************************************** WAITOFFHOOK02:JNB P2.1,$ ;check continuously whether the set is offhook JB B.1,WAITOFFHOOK02 ;if already connected MOV P1,#098H ;send dialtone MOV R1,#CONST ;initialize 5 sec counter AGAIN02: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.1,PULSEDIALED02 ;if pulse dialed? DJNZ R1,AGAIN02 ;decrement and jump if no t zero MOV P1,#018H ;remove dial tone if no activity for 5 sec LJMP WAITONHOOK02 ;wait for offhook again PULSEDIALED02:MOV P1,#018H ;remove dialtone LOW_PUSLEWIDTH_COUNTER02: MOV R1,#CONST ;200*250 usec=50 ms AGAIN202: TIMER00 0FFH,006H ;call macro with high and low value of (-250) JB P2.1,HIGH_PULSEWIDTH_COUNTER02 ;jump if valid pulse DJNZ R1,AGAIN202 ;decrement and jump if not zero MOV P1,#018H ;remove dial tone,not valid pulse, set is on hook LJMP WAITOFFHOOK02 ;again check and wait for offhook HIGH_PULSEWIDTH_COUNTER02: INC R0 ;count pulses MOV R1,#CONST ;200*325usec= 65 msec AGAIN302: TIMER00 0FEH,0BBH ;call macro with (-325) JNB P2.1, LOW_PUSLEWIDTH_COUNTER02 ;pulse again DJNZ R1,AGAIN302 ;decrement and jump if not zero ;DIALED NUMBER IS LOADED IN R0.NOW CHECK FOR VALID NUMBER MOV DPTR,#0D00H ;laod base register in DPTR MOV A,R0 ;laod dialed number in accumulator as offset MOVC A,@A+DPTR ;laod the dialed number from lookup table by offset CLR C ;in order to perfomr subtractionsuccesfully SUBB A,R0 ;(A)<---- (A)-(R0)to check whether the dialed number is valid CJNE A,#000H,INCORRECT02 ;incorrect number CJNE R0,#002H,CORRECT02 ;else dialed the same set number LJMP NUMBER202 ;number 2 is dialed CORRECT02: CJNE R0,#001H,NEXT02 ;if dialed number is not 1 LJMP NUMBER102 ;if dialed number is 1 NEXT02: CJNE R0,#003H,NEXT202 ;if not 3 LJMP NUMBER302 ;if dialed number is 3 NEXT202: LJMP NUMBER402 ;Therefore ultimate dialed number is 4 INCORRECT02: MOV P1,#098H ;dial tone MOV R1,#010H AGAIN4402: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.1,RESTART02 ;if set onhook DJNZ R1,AGAIN4402 ;decrement and jump if not zero MOV P1,#018H ;set dial tone to generate engage tone MOV R1,#010H AGAIN5502: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.1 ,RESTART02 ;if onhook DJNZ R1,AGAIN5502 ;decrement and jump if not zero LJMP INCORRECT02 ;else onhook RESTART02: MOV P1,#018H ;remove engage tone MOV R0,#000H ;reinitialize the pulse counter LJMP WAITONHOOK02 ;resatart theprogram NUMBER202: MOV R0,#000H ;reinitialize pulse counter MOV P1,#018H LJMP INCORRECT02 NUMBER102: CON_DIALNUMBER02 P0.0, RESTART02, 096H, P2.0, 016H,B.0 NUMBER302: CON_DIALNUMBER02 P0.2, RESTART02, 0A6H, P2.2, 026H,B.2 NUMBER402: CON_DIALNUMBER02 P0.3, RESTART02, 0AEH, P2.3, 02EH,B.3 WAITONHOOK02:JB P2.1,WAITONHOOK02 ;wait for onhook TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JB P2.1,WAITONHOOK02 ;not valid onhook LJMP WAITOFFHOOK02 ;jump to start ;*******************************SET3 CODE*************************************** WAITOFFHOOK03:JNB P2.2,$ ;check continuously whether the set is offhook JB B.2,WAITOFFHOOK03 ;if already connected MOV P1,#0A0H MOV R1,#CONST ;initialize 5 sec counter AGAIN03: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.2,PULSEDIALED03 ;if pulse dialed? DJNZ R1,AGAIN03 ;decrement and jump if no t zero MOV P1,#020H ;remove dial tone if no activity for 5 sec LJMP WAITONHOOK03 ;wait for offhook again PULSEDIALED03:MOV P1,#020H ;remove dialtone LOW_PUSLEWIDTH_COUNTER03: MOV R1,#CONST ;200*250 usec=50 ms AGAIN203: TIMER00 0FFH,006H ;call macro with high and low value of (-250) JB P2.2,HIGH_PULSEWIDTH_COUNTER03 ;jump if valid pulse DJNZ R1,AGAIN203 ;decrement and jump if not zero MOV P1,#020H ;remove dial tone,not valid pulse, set is on hook LJMP WAITOFFHOOK03 ;again check and wait for offhook HIGH_PULSEWIDTH_COUNTER03: INC R0 ;count pulses MOV R1,#CONST ;200*325usec= 65 msec AGAIN303: TIMER00 0FEH,0BBH ;call macro with (-325) JNB P2.2, LOW_PUSLEWIDTH_COUNTER03 ;pulse again DJNZ R1,AGAIN303 ;decrement and jump if not zero ;DIALED NUMBER IS LOADED IN R0.NOW CHECK FOR VALID NUMBER MOV DPTR,#0D00H ;laod base register in DPTR MOV A,R0 ;laod dialed number in accumulator as offset MOVC A,@A+DPTR ;laod the dialed number from lookup table by offset CLR C ;in order to perfomr subtractionsuccesfully SUBB A,R0 ;(A)<---- (A)-(R0)to check whether the dialed number is valid CJNE A,#000H,INCORRECT03 ;incorrect number CJNE R0,#003H,CORRECT03 ;else dialed the same set number LJMP NUMBER303 ;number 3 is dialed CORRECT03: CJNE R0,#001H,NEXT03 ;if dialed number is not 1 LJMP NUMBER103 ;if dialed number is 1 NEXT03: CJNE R0,#002H,NEXT203 ;if not 3 LJMP NUMBER203 ;if dialed number is 2 NEXT203: LJMP NUMBER403 ;Therefore ultimate dialed number is 4 INCORRECT03: MOV P1,#0A0H ;dial tone MOV R1,#010H AGAIN4403: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.2,RESTART03 ;if set onhook DJNZ R1,AGAIN4403 ;decrement and jump if not zero MOV P1,#020H ;set dial tone to generate engage tone MOV R1,#010H AGAIN5503: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.2 ,RESTART03 ;if onhook DJNZ R1,AGAIN5503 ;decrement and jump if not zero LJMP INCORRECT03 ;else onhook RESTART03: MOV P1,#020H ;remove engage tone MOV R0,#000H ;reinitialize the pulse counter LJMP WAITONHOOK03 ;resatart theprogram NUMBER303: MOV R0,#000H ;reinitializepulse counter MOV P1,#020H LJMP INCORRECT03 NUMBER103: CON_DIALNUMBER03 P0.0, RESTART03, 095H, P2.0, 015H,B.0 NUMBER203: CON_DIALNUMBER03 P0.1, RESTART03, 09DH, P2.1, 01DH,B.1 NUMBER403: CON_DIALNUMBER03 P0.3, RESTART03, 0ADH, P2.3, 02DH,B.3 WAITONHOOK03:JB P2.2,WAITONHOOK03 ;wait for onhook TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JB P2.2,WAITONHOOK03 ;not valid onhook LJMP WAITOFFHOOK03 ;valid offhook! restart... ;*******************************SET4 CODE*************************************** WAITOFFHOOK04:JNB P2.3,$ ;check continuously whether the set is offhook JB B.3,WAITOFFHOOK04 ;if already connected MOV P1,#0A8H MOV R1,#CONST ;initialize 5 sec counter AGAIN04: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.3,PULSEDIALED04 ;if pulse dialed? DJNZ R1,AGAIN04 ;decrement and jump if no t zero MOV P1,#028H ;remove dial tone if no activity for 5 sec LJMP WAITONHOOK04 ;wait for offhook again PULSEDIALED04:MOV P1,#028H ;remove dialtone LOW_PUSLEWIDTH_COUNTER04: MOV R1,#CONST ;200*250 usec=50 ms AGAIN204: TIMER00 0FFH,006H ;call macro with high and low value of (-250) JB P2.3,HIGH_PULSEWIDTH_COUNTER04 ;jump if valid pulse DJNZ R1,AGAIN204 ;decrement and jump if not zero MOV P1,#028H ;remove dial tone,not valid pulse, set is on hook LJMP WAITOFFHOOK04 ;again check and wait for offhook HIGH_PULSEWIDTH_COUNTER04: INC R0 ;count pulses MOV R1,#CONST ;200*325usec= 65 msec AGAIN304: TIMER00 0FEH,0BBH ;call macro with (-325) JNB P2.3, LOW_PUSLEWIDTH_COUNTER04 ;pulse again DJNZ R1,AGAIN304 ;decrement and jump if not zero ;DIALED NUMBER IS LOADED IN R0.NOW CHECK FOR VALID NUMBER MOV DPTR,#0D00H ;laod base register in DPTR MOV A,R0 ;laod dialed number in accumulator as offset MOVC A,@A+DPTR ;laod the dialed number from lookup table by offset CLR C ;in order to perfomr subtractionsuccesfully SUBB A,R0 ;(A)<---- (A)-(R0)to check whether the dialed number is valid CJNE A,#000H,INCORRECT04 ;incorrect number CJNE R0,#004H,CORRECT04 ;else dialed the same set number LJMP NUMBER404 ;number 4 is dialed CORRECT04: CJNE R0,#001H,NEXT04 ;if dialed number is not 1 LJMP NUMBER104 ;if dialed number is 1 NEXT04: CJNE R0,#002H,NEXT204 ;if not 2 LJMP NUMBER204 ;if dialed number is 2 NEXT204: LJMP NUMBER304 ;Therefore ultimate dialed number is 3 INCORRECT04: MOV P1,#0A8H ;dial tone MOV R1,#010H AGAIN4404: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.3,RESTART04 ;if set onhook DJNZ R1,AGAIN4404 ;decrement and jump if not zero MOV P1,#028H ;set dial tone to generate engage tone MOV R1,#010H AGAIN5504: TIMER00 HIGH(DELAY_25ms),LOW(DELAY_25ms) ;call macro with high and low value of (-25000) JNB P2.3 ,RESTART04 ;if onhook DJNZ R1,AGAIN5504 ;decrement and jump if not zero LJMP INCORRECT04 ;else onhook RESTART04: MOV P1,#028H ;remove engage tone MOV R0,#000H ;reinitialize the pulse counter LJMP WAITONHOOK04 ;resatart theprogram NUMBER404: MOV R0,#000H ;reinitializepulse counter MOV P1,#028H LJMP INCORRECT04 NUMBER104: CON_DIALNUMBER04 P0.0, RESTART04, 094H, P2.0, 014H,B.0 NUMBER204: CON_DIALNUMBER04 P0.1, RESTART04, 09CH, P2.1, 01CH,B.1 NUMBER304: CON_DIALNUMBER04 P0.2, RESTART04, 0A4H, P2.2, 024H,B.2 WAITONHOOK04:JB P2.3,WAITONHOOK04 ;wait for onhook TIMER00 HIGH(DELAY_50ms),LOW(DELAY_50ms) ;call macro with high and low value of (-50000) JB P2.3,WAITONHOOK04 ;not valid onhook LJMP WAITOFFHOOK04 ;valid offhook! restart... ;********************TIMER1 SERVICE ROUTINE (Scheduler)********************** ;This program assigns time slots to different sets's code ;It first save the status of the currently executing code ;Then laods the state of the next section of the code (for next set) ;This ISR executes in almost 90 cycles ;Requires approx. 7 Micro Secs to execute ;**************************************************************************** T1ISR: ;USING 3 SETB RS1 SETB RS0 CLR TR1 ;stop timer1 CLR TF1 ;clear timer1 overflow falg MOV R1,018H ;R1<---R0 MOV @R1,A ;save accumulator POP ACC ;pop PCH INC R1 ;increment R1 MOV @R1,A ;save high byte of return address POP ACC ;pop PCL INC R1 ;increment R1 MOV @R1,A ;save low byte of return address INC R1 ;increment R1 MOV @R1,IP ;save B INC R1 ;increment R1 MOV @R1,TCON ;save timer control register INC R1 ;increment R1 MOV @R1,TL0 ;save timer2 low byte INC R1 ;increment R1 MOV @R1,TH0 ;save timer2 high byte INC R1 ;increment R1 MOV @R1,00H ;save R0 of bank 0 INC R1 ;increment R1 MOV @R1,01H ;save R1 of bank 0 INC R1 ;increment R1 MOV @R1,02H ;save r2 of bank 0 INC R1 ;increment R1 MOV @R1,DPL ;save data pointer low byte INC R1 ;increment R1 MOV @R1,DPH ;save data pointer high byte MOV A,R0 ;move address of state of currently running set's process in A ADD A,#0CH ;add 12 to jump to address of state of next set's process CJNE A,#060H,LOAD ;if correct address load the state of next process MOV A,#SET1 ;else laod state of first process(SET 1) LOAD: MOV R0,A ;load the address of state of next process in R0. ADD A,#0CH ;add 12 in accumulator MOV R1,A ;load the address in R1 DEC R1 ;decrement R1 MOV DPH,@R1 ;get DPTR high value DEC R1 ;decrement R1 MOV DPL,@R1 ;get DPTR low value DEC R1 ;decrement R1 MOV 02H,@R1 ;get R2 of bank 0 DEC R1 ;decrement R1 MOV 01H,@R1 ;get R1 of bank 0 DEC R1 ;decrement R1 MOV 00H,@R1 ;get R0 of bank0 DEC R1 ;decrement R1 MOV TH0,@R1 ;get high value of timer0 DEC R1 ;decrement R1 MOV TL0,@R1 ;get low value of timer0 DEC R1 ;decrement R1 MOV TCON,@R1 ;get timer control register DEC R1 ;decrement R1 MOV IP,@R1 ;get interrupt priority register DEC R1 ;decrement R1 MOV A,@R1 ;get return address low byte in accumulator PUSH ACC ;put return address low byte on stack DEC R1 ;decrement R1 MOV A,@R1 ;get return adrress high byte in acc. PUSH ACC ;put return address high byte on stack DEC R1 ;decrement R1 MOV A,@R1 ;get accumulator back MOV TH1,#0ECH ;put high value in timer MOV TL1,#078H ;put low value in timer CLR RS1 ;Clear register select bit1 CLR RS0 ;clear register select bit0 SETB TR1 ;start scheduling timer RETI ;return from ISR CSEG AT 0D00H ;reserved space for lookup tables SETNUMBERS: DB 0,1,2,3,4 ;valid set numbers END |
Topic | Author | Date |
Improved code advice ? | 01/01/70 00:00 | |
code advice | 01/01/70 00:00 | |
I second it | 01/01/70 00:00 | |
Thirded![]() | 01/01/70 00:00 |