PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Problem mit RS485 und URXC



BlaueLed
03.05.2007, 21:08
Hallo,

ich habe einen Sensor, der seine Daten per RS485 an eine Anzeigeeinheit sendet. Wenn ich beide mega´s neu starte, klappt alles einwandfrei. Sobald ich die Anzeigeeinheit jedoch was in Richtung Sensor schicken lasse, kommt die Anzeige durcheinander und zeigt mir nicht mehr die richtigen werte an.

So sollte es aussehen:

118 cm ( funktioniert bis ich von der Anzeige was an den sensor schicke )

So sieht es aus, nachdem ich was gesendet habe.

811 cm ( also die Zahlen verschoben )

hier ist der Code von der Anzeigeeinheit:






'----------------- CPU Config ---------------------------
$regfile = "m16def.dat"
$crystal = 1000000
$baud = 1200
'--------------- Portdefinition ------------------------
Config Porta = Output
Config Portb = Output
Config Portd.7 = Input
Config Portd.6 = Input
Config Portc = Output
Config Portd.2 = Output
Config Debounce = 100
'---------------------------------------- LCD ----------------------------------
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portc.4 , Rs = Portc.5
'----------------------------------- Timer --------------------
Enable Interrupts
'----------------- Sub ------------------------------
Declare Sub Balken
Declare Sub Anzeige
'-------------- Alias -----------------------------------
Led1 Alias Porta.7
Led2 Alias Porta.6
Led3 Alias Porta.5
Led4 Alias Porta.4
Backlight Alias Portb.4
Rs485 Alias Portd.2
Taster2 Alias Pind.6
Taster1 Alias Pind.7
'--------------------------------- Timer ------------------
Config Timer0 = Timer , Prescale = 8
Enable Timer0
On Timer0 Hintergrundzeit
'-------------------------- RS485 -----------------------
Rs485 = 0
On Urxc Onrxd
'-------------------- Dims ---------------------------
Dim Ubertrag As String * 3
Dim Wert As Integer
Dim Xtast As Bit
Dim X1 As Byte
Dim Backtime As Integer
Deflcdchar 1 , 17 , 14 , 17 , 17 , 17 , 17 , 14 , 32
'----------------------------- Variable -------------
X1 = 1
'------------------- Main -------------------------------
Xtast = 0
Cls
Cursor Off Noblink
Backlight = 1
Wait 1
Locate 1 , 1
Lcd " " ; Chr(1) ; "ltank Sensor "
Locate 2 , 1
Lcd " by Kay Pohl "
Wait 2
Cls
Locate 1 , 1
Lcd "-- Tankinhalt --"
Wait 1
Enable Urxc
Led2 = 1
Led3 = 1
Led4 = 1
'-------------------- Hauptprogramm ---------------
_start:
Debounce Taster2 , 0 , Sprungmain
Debounce Taster1 , 0 , Licht
Goto _start
'---------- Einlesen ----------
Onrxd:
Inputbin Ubertrag
Toggle Led4
Call Anzeige
Return
'---------------------------------------- Zeichenanzeige --------------------------------------
Sub Anzeige
Locate 2 , 1
Lcd Ubertrag ; " cm" ; " "
End Sub
'------------------------------------------- Hintergrundzeit --------------------------------
Hintergrundzeit:
Incr Backtime
If Backtime = 30000 Then
Backlight = 0
End If
Return
'------------------------------------------------ Licht -------------------------------------------
Licht:
Backlight = 1
Backtime = 0
Goto _start
'-------------------------------------- Sprungmain ------------------
Sprungmain:
Disable Urxc
Rs485 = 1
Print "1"
Do
Loop Until Ucsra.txc = 1
Set Ucsra.txc

Rs485 = 0
Led2 = 0
Waitms 200
Led2 = 1
Waitms 10
Enable Urxc


Goto _start


und hier der Code vom Sensor :



'------------------------ CPU Config -------------------------
$regfile = "m8def.dat"
$crystal = 1000000
$baud = 1200
$framesize = 42
$swstack = 42
$hwstack = 42
'------------------------ Port Config --------------------------
Config Portb = Output
Config Portd.2 = Output
Config Scl = Portc.5
Config Sda = Portc.4
Const Srf02_slaveid = &HE0
'------------------------ Alias ----------------------------------
Led1 Alias Portb.0
Led2 Alias Portb.1
Led3 Alias Portb.2
Led4 Alias Portb.3
Rs485 Alias Portd.2
'-------------------------- Subs -------------------------------
Led1 = 1
Led2 = 1
Led3 = 1
Led4 = 1
'--------------------------- RS485 -------------------------------
Rs485 = 0
Enable Interrupts
On Urxc Onrxd
Enable Urxc
'----------------------------- Dims ------------------------------
Dim Entfernung As Integer
Dim V As Byte
Dim Wert As String * 3
Dim Ubertrag As String * 3
Dim Sendung As String * 1
Dim Zeit As Byte
Dim Temp1 As String * 3
Zeit = 1
'----------------------- Funcitons -------------------------
Declare Function Srf02_firmware(byval Slaveid As Byte) As Byte
Declare Function Srf02_entfernung(byval Slaveid As Byte) As Integer
Declare Sub Senden()
'------------------------- Boot ---------------------------------
Wait 1
I2cinit
V = 1
Waitms 100
'---------------------------------- Mainprogramm ------------------
Main:
Entfernung = Srf02_entfernung(srf02_slaveid)
Waitms 100
Wert = Str(entfernung)
Ubertrag = Format(wert , "000")
Waitms 100

Rs485 = 1

Print Ubertrag ;
Do
Loop Until Ucsra.txc = 1
Set Ucsra.txc

Rs485 = 0
Waitms 500
Goto Main

'------------- Hilfsfunktionen für SRF02 ----------


Function Srf02_firmware(byval Slaveid As Byte) As Byte
Local Firmware As Byte
Local Slaveid_read As Byte

Slaveid_read = Slaveid + 1

I2cstart
I2cwbyte Slaveid
I2cwbyte 0 'Leseregister festlegen
I2cstop

I2cstart
I2cwbyte Slaveid_read
I2crbyte Firmware , Nack
I2cstop

Srf02_firmware = Firmware
End Function



Function Srf02_entfernung(byval Slaveid As Byte) As Integer
Local Lob As Byte
Local Hib As Byte
Local Firmware As Byte
Local Temp As Byte
Local Slaveid_read As Byte

Slaveid_read = Slaveid + 1

'Messvorgang in starten
I2cstart
I2cwbyte Slaveid
I2cwbyte 0
I2cwbyte 81 'in Zentimetern messen
I2cstop


Warteaufmessung:
Waitms 1
Firmware = Srf02_firmware(slaveid)
If Firmware = 255 Then Goto Warteaufmessung

I2cstart
I2cwbyte Slaveid
I2cwbyte 2 'Leseregister festlegen
I2cstop


I2cstart
I2cwbyte Slaveid_read
I2crbyte Hib , Ack
I2crbyte Lob , Nack
I2cstop


Srf02_entfernung = Makeint(lob , Hib)
End Function

'-------------------------------------------- Empfang -------------------------
Onrxd:
Inputbin Sendung

If Sendung = "1" Then
Toggle Led1
End If

Return
'------------------------ Senden ---------------------------------------
Sub Senden()

End Sub


ich sitze jetzt schon seit 2 Tagen an dem Problem und befürchte, das ich irgendeinen Denkfehler mache. Könnt Ihr mir helfen ?

mfg Kay

Vitis
04.05.2007, 01:18
hier:
Inputbin Ubertrag

und hier:
Print Ubertrag ;

ist der Fehler.
Was Dir fehlt ist n Protokoll, das klare Start- und Stopbedingungen
hat.

BlaueLed
04.05.2007, 11:30
was denn genau ? das Inputbin ubertrag steht ja in dem Empfänger und das Print Ubertrag steht im Sender. mache ich beim Sender das ; weg, dann kommen auch wirre zeichen. Du meinst also mit nem gescheiten protokoll ( Syncbyte, Startbyte, Daten, Stoppbyte ) hätte ich keine probleme ?

mfg Kay

Vitis
04.05.2007, 18:11
exakt, Du kannst beispielsweise das ; weglassen und hast dann
recht einfach n Protokoll, weil dann der CR + LF gesendet wird,
das kannste dann einfach als Stopbedingung nutzen.
CR ist glaub ich ASCII-Code 13,
LF soweit ichs grad da hab ist die 10