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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/02/08 09:41
Read: times


 
#151756 - MODBUS RTU for 8051/DS89C450
Hi 8052 expert,

I managed to write a Visual C++ programming to send the MODBUS RTU command and its working fine.
When I am porting to 8051 microcontroller (DS89C450), I am rewriting the whole assembly code.
I completed and tested the code and its not working.
I suspect that it might due to timing and I spent few days but I am still not able to solve it.

This project is to receive command from any PC (serial port 0) with ASCII format.
Then i will translate it to MODBUS RTU and send to transducer (serial port 1) which only accepting RTU command.
Upon receive it, i will process it and storing the flash memory which will be read back later.
I attached my code here and looking forward for any expect out there to help me.


Initialisation...
CLR TR1 ;Stop Timer1 for Serial Baudrate
MOV TMOD,#21h ;Timer 1 is 8 bit with auto-reload
MOV TH1,#Baudrate ;Load serial port 0 & 1 baudrate to timer1 (for serial 0 & 1)
MOV PCON,#80h ;Serial 0 SMOD=1 where Serial Port 0 baud rate is doubled
MOV SCON0,#50h ;Serial 0 in Asynchronous 10 bits, Receiver Enable (Mode 1)
MOV WDCON,#80h ;Serial 1 SMOD=1 where Serial Port 1 baud rate is doubled
MOV SCON1,#50h ;Serial 1 in Asynchronous 10 bits, Receiver Enable (Mode 1)
MOV IE,#50h ;Enable serial port 0 & 1 interupt only
MOV IP,#10h ;Set serial port 0 interupt highest priority
SETB TR1 ;Start Serial Port timer 1
CLR NRTS0 ;Enable receiving data from serial port 0
CLR NRTS1 ;Enable receiving data from serial port 1
SETB EA ;Start/Enable interupt input


The MODBUSCRC routines calculate the CRC value and send the bytes to the transducer.
The CRC calculation is correct.

;******************************************************************************************
; Subroutines - SModbusCRC
; Input : TSlaveAdd,TFunctionCode,TData1-TData20,TCRCn,TCRCi,TCRCj,TCRCDi
; Output : TCRCMSB,TCRCLSB
;******************************************************************************************
SModbusCRC: MOV TCRCMSB,#0FFh ;(1)
MOV TCRCLSB,#0FFh
MOV TCRCi,#0 ;(2)
MOV TCRCDi,TSlaveAdd
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
Goto3: MOV TCRCj,#0 ;(3)
MOV A,TCRCLSB
XRL A,TCRCDi ;(4)
MOV TCRCLSB,A
Goto5: INC TCRCj ;(5)
CLR C
MOV A,TCRCMSB ;(6)
RRC A
JC MSBCarry
MOV TCRCMSB,A
MOV A,TCRCLSB
SJMP RRCLSB
MSBCarry: MOV TCRCMSB,A
MOV A,TCRCLSB
SETB C
RRCLSB: RRC A
JC LSBCarry
MOV TCRCLSB,A ;(6)
SJMP Goto8
LSBCarry: MOV TCRCLSB,A ;(6)
MOV A,TCRCMSB ;(7)
XRL A,#0A0h
MOV TCRCMSB,A
MOV A,TCRCLSB
XRL A,#001h
MOV TCRCLSB,A
Goto8: MOV A,TCRCj ;(8)
CJNE A,#08,Goto5
INC TCRCi ;(9)
MOV A,TCRCi ;(10)
CJNE A,TCRCn,ReloadData
MOV A,TCRCLSB ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
MOV A,TCRCMSB ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
MOV Counter,#0
RET ;(11)

ReloadData: CJNE A,#1,ReloadData1
MOV TCRCDi,TFunctionCode
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData1: CJNE A,#2,ReloadData2
MOV TCRCDi,TData1
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData2: CJNE A,#3,ReloadData3
MOV TCRCDi,TData2
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData3: CJNE A,#4,ReloadData4
MOV TCRCDi,TData3
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData4: CJNE A,#5,ReloadData5
MOV TCRCDi,TData4
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData5: CJNE A,#6,ReloadData6
MOV TCRCDi,TData5
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData6: CJNE A,#7,ReloadData7
MOV TCRCDi,TData6
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData7: CJNE A,#8,ReloadData8
MOV TCRCDi,TData7
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData8: CJNE A,#9,ReloadData9
MOV TCRCDi,TData8
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData9: CJNE A,#10,ReloadData10
MOV TCRCDi,TData9
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData10: CJNE A,#11,ReloadData11
MOV TCRCDi,TData10
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData11: CJNE A,#12,ReloadData12
MOV TCRCDi,TData11
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData12: CJNE A,#13,ReloadData13
MOV TCRCDi,TData12
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData13: CJNE A,#14,ReloadData14
MOV TCRCDi,TData13
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData14: CJNE A,#15,ReloadData15
MOV TCRCDi,TData14
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData15: CJNE A,#16,ReloadData16
MOV TCRCDi,TData15
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData16: CJNE A,#17,ReloadData17
MOV TCRCDi,TData16
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData17: CJNE A,#18,ReloadData18
MOV TCRCDi,TData17
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData18: CJNE A,#19,ReloadData19
MOV TCRCDi,TData18
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData19: CJNE A,#20,ReloadData20
MOV TCRCDi,TData19
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData20: CJNE A,#21,ReloadData21
MOV TCRCDi,TData20
MOV A,TCRCDi ;** Send Serial to Transducer **
CALL SSerialToTrans ;** Send Serial to Transducer **
JMP Goto3
ReloadData21: JMP Goto3

;******************************************************************************************
; Subroutines - SSerialToTrans
; Send the serial value to Transducer
; Input: A
;******************************************************************************************
SSerialToTrans: MOV ModbusData,A ;Copy value received from SPI and send to PC
CALL SModbusOut
RET


;******************************************************************************************
; Subroutines - SModbusOut
; Input : ModbusData
;******************************************************************************************
SModbusOut: CLR EA ;disable interupt input from all
SETB NRTS0 ;Not prepare to receive Serial signal
SETB NRTS1 ;Not prepare to receive Serial signal
;CALL SSerialDelay ;Delay 600us to ensure no on going serial transmission
MOV A,ModbusData ;send data in hex value

;JB NCTS1,$ ;Wait until PC ready to receive serial signal
CLR TI_1 ;Clear send complete flag
MOV SBUF1,A ;Send to PC
JNB TI_1,$ ;Wait until send complete
CLR TI_1 ;CRITICAL ( DO CLEAR AFTER SEND OR FOREVER LOOP )
RET



List of 12 messages in thread
TopicAuthorDate
MODBUS RTU for 8051/DS89C450            01/01/70 00:00      
   How to post source code            01/01/70 00:00      
   Formatted Source Code            01/01/70 00:00      
      Looks like translated PIC code!            01/01/70 00:00      
   So where is the problem?            01/01/70 00:00      
      More details...            01/01/70 00:00      
         How did you test your VC++ app...?            01/01/70 00:00      
         uC -> RTU Slave (OK) ; RTU Slave -> uC (FAIL)            01/01/70 00:00      
            RE:8 bit buffer            01/01/70 00:00      
               Problem?            01/01/70 00:00      
                  RE:8 bit buffer            01/01/70 00:00      
               RTU Communication is working!            01/01/70 00:00      

Back to Subject List