Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

modbus interfacing with 8051

Status
Not open for further replies.

matthai

Member level 2
Joined
Mar 11, 2010
Messages
52
Helped
13
Reputation
26
Reaction score
12
Trophy points
1,298
Location
KERALA
Activity points
1,584
This asm code may help u for modbus usin 8051.


HTML:
;;;;;;;;;;;;;;;;;;;;;;;;; 
;;     MODBUS.ASM      ;; 
;;;;;;;;;;;;;;;;;;;;;;;;; 
PROC    MODBUS,SP0INT 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
SP0INT: 
        NOP 
        PUSH   PSW 
        PUSH   ACC 
        PUSH   DPH1 
        PUSH   DPL1 
        JB     SP0_RTC,SP0INT_RECEIVE 
        LJMP   SP0INT_SEND 
SP0INT_RECEIVE: 
        JBC    RI,SP0INT_RECEIVE1 
        CLR    TI 
        LJMP   SP0INT_RECE_END 
SP0INT_RECEIVE1: 
        MOV    A,SBUF 
        ;MOV    C,RB8 
        ;JNB   SP0INT_RECE_ERROR 
        MOV    DPH1,#00H 
        MOV    DPL1,SP0RXD_CAL 
        MOVX   @DPTR,A 
        INC    SP0RXD_CAL 
SP0INT_RECE_END: 
        SJMP   SP0INT_END 
;;;;-----------------------;;;;; 
SP0INT_SEND: 
        NOP 
        JBC    TI,SP0INT_SEND1 
        CLR    RI 
        SJMP   SP0INT_SEND_END 
SP0INT_SEND1: 
        DJNZ   SP0_LONG,SP0INT_SEND2 
        ;SETB    SP0_RTC 
        ;MOV     SP0RXD_CAL,#00H 
        SJMP   SP0INT_SEND_END 
SP0INT_SEND2: 
        INC    SP0TXD_CAL 
        MOV    A,SP0TXD_CAL 
        ;ADD    A,#00H 
        MOV    DPL1,A 
        MOV    DPH1,#00H 
        MOVX   A,@DPTR 
        SETB   TB8 
        MOV    SBUF,A 
SP0INT_SEND_END: 
;;      SJMP   SP0INT_END 
;;;---------------------------;;; 
SP0INT_END: 
        MOV    SP0_WD,#00H 
        POP    DPL1 
        POP    DPH1 
        POP    ACC 
        POP    PSW 
        RETI 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
MODBUS: 
        NOP 
        JBC   SP0_REND,MODBUS_CHECK_ADDRESS 
        LJMP  MODBUS_END 
MODBUS_CHECK_ADDRESS: 
        MOV   DPTR,#SP0_SBUF1 
        MOVX  A,@DPTR 
        XRL   A,SLAVE 
        JZ    MODBUS_CHECK_CRC 
MODBUS_ADDRESS_ERROR: 
        NOP 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_CHECK_CRC: 
        MOV   DPTR,#SP0_SBUF1 
        MOV   R2,SP0RXD_CAL 
        DEC   R2 
        DEC   R2 
        LCALL CRC16 
        MOVX  A,@DPTR 
        INC   DPTR 
        XRL   A,R1 
        JNZ   MODBUS_CRC_ERROR 
        MOVX  A,@DPTR 
        XRL   A,R0 
        JZ    MODBUS_CHECK_ORDER 
MODBUS_CRC_ERROR: 
        NOP 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_CHECK_ORDER: 
        NOP 
        MOV   DPTR,#SP0_SBUF2 
        MOVX  A,@DPTR 
        CJNE  A,#07H,MODBUS_CHECK_ORDER1 
MODBUS_CHECK_ORDER1: 
        JC    MODBUS_ORDER_CASE 
        NOP 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_ORDER_CASE: 
        MOV   B,#03H 
        MUL   AB 
        MOV   DPTR,#MODBUS_ORDER_TABLE 
        JMP   @A+DPTR 
MODBUS_ORDER_TABLE: 
        LJMP  MODBUS_ORDER_ERROR 
        LJMP  MODBUS_ORDER_ERROR 
        LJMP  MODBUS_ORDER_ERROR 
        LJMP  MODBUS_ORDER3 
        LJMP  MODBUS_ORDER_ERROR 
        LJMP  MODBUS_ORDER_ERROR 
        LJMP  MODBUS_ORDER6 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_END: 
        RET 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
MODBUS_ORDER_ERROR: 
        SETB  SP0_RTC 
        MOV   SP0RXD_CAL,#00H 
        LJMP  MODBUS_END 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
MODBUS_ORDER3: 
        NOP 
MODBUS_ORDER3_CHECK_ADDRESS: 
        MOV   DPTR,#SP0_SBUF3 
        MOVX  A,@DPTR 
        XRL   A,#00H 
        JNZ   MODBUS_ORDER3_ADDRESS_ERROR 
        INC   DPTR 
        MOVX  A,@DPTR 
        MOV   R7,A 
        CJNE  A,#0BH,MODBUS_ORDER3_1 
MODBUS_ORDER3_1: 
        JC    MODBUS_ORDER3_CHECK_LONG 
MODBUS_ORDER3_ADDRESS_ERROR: 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_ORDER3_CHECK_LONG: 
        INC   DPTR 
        MOVX  A,@DPTR 
        JNZ   MODBUS_ORDER3_LONG_ERROR 
        MOV   A,#0BH 
        CLR   C 
        SUBB  A,R7 
        MOV   B,A 
        INC   DPTR 
        MOVX  A,@DPTR 
        MOV   R6,A 
        XCH   A,B 
        CLR   C 
        SUBB  A,B 
        JNC   MODBUS_ORDER3_READ_DATA 
MODBUS_ORDER3_LONG_ERROR: 
        NOP 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_ORDER3_READ_DATA: 
        MOV   A,R6 
        MOV   R2,A 
        RL    A 
        MOV   SP0_LONG,A 
        MOV   A,R7 
        RL    A 
        ADD   A,#KRL 
        MOV   R0,A 
        MOV   DPTR,#SP0_SBUF4 
        LCALL MOVE_IRAMXRAM2 
         
        MOV   DPTR,#SP0_SBUF1 
        MOV   A,SLAVE 
        MOVX  @DPTR,A 
        INC   DPTR 
        MOV   A,#03H 
        MOVX  @DPTR,A 
        INC   DPTR 
        MOV   A,SP0_LONG 
        MOVX  @DPTR,A 
        INC   SP0_LONG 
        INC   SP0_LONG 
        INC   SP0_LONG 
        LJMP  MODBUS_SEND_CRC 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
MODBUS_ORDER6: 
        NOP 
MODBUS_ORDER6_CHECK_ADDRESS: 
        MOV   DPTR,#SP0_SBUF3 
        MOVX  A,@DPTR 
        JNZ   MODBUS_ORDER6_ADDRESS_ERROR 
        INC   DPTR 
        MOVX  A,@DPTR 
        MOV   R7,A 
        CJNE  A,#02H,MODBUS_ORDER6_1 
MODBUS_ORDER6_1: 
        JC    MODBUS_ORDER6_READ_DATA 
MODBUS_ORDER6_ADDRESS_ERROR: 
        LJMP  MODBUS_ORDER_ERROR 
MODBUS_ORDER6_READ_DATA: 
        MOV   DPTR,#SP0_SBUF5 
        MOVX  A,@DPTR 
        MOV   R3,A 
        INC   DPTR 
        MOVX  A,@DPTR 
        MOV   R2,A 
        MOV   R5,#KU1H_AD 
        MOV   A,R7 
        RL    A 
        ADD   A,#KU1L_AD 
        MOV   R4,A 
        LCALL WWRITE 
 
        MOV    DPTR,#SBUF1 
        MOV    A,SLAVE 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,#06H 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,#00H 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,R7 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,R3 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,R2 
        MOVX   @DPTR,A 
        MOV    SP0_LONG,#06H 
        LJMP   MODBUS_SEND_CRC 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
MODBUS_SEND_CRC: 
        NOP 
        MOV    DPTR,#SP0_SBUF1 
        MOV    R2,SP0_LONG 
        LCALL  CRC16 
        MOV    A,R1 
        MOVX   @DPTR,A 
        INC    DPTR 
        MOV    A,R0 
        MOVX   @DPTR,A 
        INC    SP0_LONG 
        INC    SP0_LONG 
MODBUS_SEND_BEGIN: 
        CLR    SP0_RTC 
        MOV    DPTR,#SP0_SBUF1 
        MOVX   A,@DPTR 
        SETB   TB8 
        MOV    SBUF,A 
        MOV    SP0TXD_CAL,#00H 
        LJMP   MODBUS_END 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;Èë¿Ú: dptr R2¸ö²ÎÓë¼ÆËãµÄ×Ö½Ú 
;³ö¿Ú: R1-CRC½á¹û¸ßλ R0-CRC½á¹ûµÍλ,DPTR 
;Ó°Ï죺A£¬DPH£¬DPL 
;X16+X14+X2+1 
CRC16: 
        NOP 
        MOV    R0,#0FFH 
        MOV    R1,#0FFH 
CRC16_LOOP: 
        MOV    DPS,#01H 
        MOVX   A,@DPTR 
        INC    DPTR 
        XRL    A,R1 
        INC    DPS 
        MOV    DPTR,#CRC16_DATA 
        ADD    A,DPL 
        MOV    DPL,A 
        CLR    A 
        ADDC   A,DPH 
        MOV    DPH,A 
        CLR    A 
        MOVC   A,@A+DPTR 
        XRL    A,R0 
        MOV    R1,A 
        INC    DPH 
        CLR    A 
        MOVC   A,@A+DPTR 
        MOV    R0,A 
        DJNZ   R2,CRC16_LOOP 
        MOV    DPS,#01H 
        RET 
;ORG    0FF9H 
PROGRAM_ERROR_CRCTABL1: 
        NOP 
        NOP 
        NOP 
        NOP 
        NOP 
        SJMP  $ 
;ORG   1000H 
CRC16_DATA: 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H  ;1 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H  ;8 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H  ;16 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H  ;24 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    000H,0C1H,081H,040H,001H,0C0H,080H,041H 
        DB    001H,0C0H,080H,041H,000H,0C1H,081H,040H  ;32 
 
        DB    000H,0C0H,0C1H,001H,0C3H,003H,002H,0C2H 
        DB    0C6H,006H,007H,0C7H,005H,0C5H,0C4H,004H 
        DB    0CCH,00CH,00DH,0CDH,00FH,0CFH,0CEH,00EH 
        DB    00AH,0CAH,0CBH,00BH,0C9H,009H,008H,0C8H 
        DB    0D8H,018H,019H,0D9H,01BH,0DBH,0DAH,01AH 
        DB    01EH,0DEH,0DFH,01FH,0DDH,01DH,01CH,0DCH 
        DB    014H,0D4H,0D5H,015H,0D7H,017H,016H,0D6H 
        DB    0D2H,012H,013H,0D3H,011H,0D1H,0D0H,010H  ;40 
        DB    0F0H,030H,031H,0F1H,033H,0F3H,0F2H,032H 
        DB    036H,0F6H,0F7H,037H,0F5H,035H,034H,0F4H 
        DB    03CH,0FCH,0FDH,03DH,0FFH,03FH,03EH,0FEH 
        DB    0FAH,03AH,03BH,0FBH,039H,0F9H,0F8H,038H 
        DB    028H,0E8H,0E9H,029H,0EBH,02BH,02AH,0EAH 
        DB    0EEH,02EH,02FH,0EFH,02DH,0EDH,0ECH,02CH 
        DB    0E4H,024H,025H,0E5H,027H,0E7H,0E6H,026H 
        DB    022H,0E2H,0E3H,023H,0E1H,021H,020H,0E0H  ;48 
        DB    0A0H,060H,061H,0A1H,063H,0A3H,0A2H,062H 
        DB    066H,0A6H,0A7H,067H,0A5H,065H,064H,0A4H 
        DB    06CH,0ACH,0ADH,06DH,0AFH,06FH,06EH,0AEH 
        DB    0AAH,06AH,06BH,0ABH,069H,0A9H,0A8H,068H 
        DB    078H,0B8H,0B9H,079H,0BBH,07BH,07AH,0BAH 
        DB    0BEH,07EH,07FH,0BFH,07DH,0BDH,0BCH,07CH 
        DB    0B4H,074H,075H,0B5H,077H,0B7H,0B6H,076H 
        DB    072H,0B2H,0B3H,073H,0B1H,071H,070H,0B0H 
        DB    050H,090H,091H,051H,093H,053H,052H,092H  ;56 
        DB    096H,056H,057H,097H,055H,095H,094H,054H 
        DB    09CH,05CH,05DH,09DH,05FH,09FH,09EH,05EH 
        DB    05AH,09AH,09BH,05BH,099H,059H,058H,098H 
        DB    088H,048H,049H,089H,04BH,08BH,08AH,04AH 
        DB    04EH,08EH,08FH,04FH,08DH,04DH,04CH,08CH 
        DB    044H,084H,085H,045H,087H,047H,046H,086H 
        DB    082H,042H,043H,083H,041H,081H,080H,040H  ;64 
PROGRAM_ERROR_CRCTABLE2: 
        NOP 
        NOP 
        NOP 
        NOP 
        NOP 
        SJMP  $ 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
        END
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top