PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : RS232 Steuerung klappt nicht. Problem an einer Stelle



Snecx
19.11.2007, 01:10
Hallo

ich habe ein Verständnisproblem bei meinem Program. Es soll über die Rs232 ein Steuerbefehl empfangen und dann die Uhr stellen oder wieder zurückgeben. Dazu sende ich zum stellen der Uhr z.B. den Befehl "WB1S12101911071"
Das klappt ohne Probleme. Möchte ich die Uhrzeit wieder auslesen sende ich "WB1G" und hierbei ist das Problem. Egal was ich mache der Code in der Select Case wird nicht ausgeführt.
Kann mir bitte jemand sagen warum??? Wenn ich das gleich Programm in .NET progge geht das. Ich bitte um Tips.
Danke.



$regfile = "m8535.dat"
''$regfile = "m16def.dat"
$crystal = 8000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40


Config Graphlcd = 240 * 128 , Dataport = Porta , Controlport = Portc , Ce = 2 , Cd = 3 , Wr = 0 , Rd = 1 , Reset = 4 , Fs = 6 , Mode = 6
'Config Graphlcd = 128 * 64sed , Dataport = Porta , Controlport = Portc , Ce = 4 , Ce2 = 3 , Cd = 0 , Rd = 1 , Reset = 5 , Enable = 2
'Clear the screen will both clear text and graph display
Cls


Config 1wire = Portd.3 '' 1 Wire Port
Config Pinc.7 = Output '' LCD Invert
Config Pind.4 = Output

Config Scl = Portd.5
Config Sda = Portd.6

'config UART Interrupt
On Urxc Onrxd 'Interrupt-Routine setzen
Enable Urxc 'Interrupt URXC einschalten
Enable Interrupts 'Interrupts global zulassen '' LCD Backlight

Portc.7 = 0 '' LCD Invert ON
Portd.4 = 1 '' LCD Backlight ON

Dim Zaehler As Integer
Zaehler = 0

Dim I As Byte
Dim Settimedate As Byte
Dim Rs232 As Byte
I = 1
Dim Mystring As String * 11
Dim Mybytes(12) As Byte At Mystring Overlay

Dim Ds1307w As Byte
Dim Ds1307r As Byte
Ds1307w = &B11010000
Ds1307r = &B11010001

Dim _day As Byte
Dim _month As Byte
Dim _year As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim _weekday As Byte

Dim Modus As Byte
Dim Test As Byte

Config Portb = Output
Portb = &B11111111



Do
Select Case Modus
Case 1 '' Set Date and Time
Print "Hour:"
Print _hour
Print "Minute:"
Print _min
Print "Sec:"
Print _sec
Print "Day:"
Print _day
Print "Month:"
Print _month
Print "Year:"
Print _year
Print "Weekday:"
Print _weekday
If Test = 1 Then
_sec = Makebcd(_sec)
_min = Makebcd(_min)
_hour = Makebcd(_hour)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cwbyte _sec
I2cwbyte _min
I2cwbyte _hour
I2cstop
_day = Makebcd(_day)
_month = Makebcd(_month)
_year = Makebcd(_year)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 3
I2cwbyte _weekday
I2cwbyte _day
I2cwbyte _month
I2cwbyte _year
I2cstop
End If
Modus = 0
Case 2 '' Get Date and Time
If Test = 1 Then
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cstop
I2cstart
I2cwbyte Ds1307r
I2crbyte _sec , Ack
I2crbyte _min , Ack
I2crbyte _hour , Ack
I2crbyte _weekday , Ack
I2crbyte _day , Ack
I2crbyte _month , Ack
I2crbyte _year , Nack
I2cstop
End If
Print "20332011076"
_day = Makedec(_day)
_month = Makedec(_month)
_year = Makedec(_year)
_sec = Makedec(_sec)
_min = Makedec(_min)
_hour = Makedec(_hour)
Print "Hour:"
Print _hour
Print "Minute:"
Print _min
Print "Sec:"
Print _sec
Print "Day:"
Print _day
Print "Month:"
Print _month
Print "Year:"
Print _year
Print "Weekday:"
Print _weekday
Modus = 0
End Select


Portb = &B00000000
Wait 1
Portb = &B11111111
Wait 1

Loop
End




'UART interrupt routine
Onrxd:
Dim Temp As String * 11
Dim T As Byte
If I = 1 Then
If Chr(udr) = "W"then
I = 2
Else
I = 1
End If
Elseif I = 2 Then
If Chr(udr) = "B" Then
I = 3
Else
I = 1
End If
Elseif I = 3 Then
If Chr(udr) = "1" Then
I = 4
Else
I = 1
End If
Elseif I = 4 Then
Select Case Chr(udr)
Case "S"
I = 5
Case "G"
Modus = 2
Case Else
I = 1
End Select
Elseif I = 5 Then
Incr Rs232
Mybytes(rs232) = Udr
If Rs232 = 11 Then
Rs232 = 0
I = 1
Temp = Mid(mystring , 1 , 2)
T = Val(temp)
_hour = T

Temp = Mid(mystring , 3 , 2)
T = Val(temp)
_min = T
_sec = 00

Temp = Mid(mystring , 5 , 2)
T = Val(temp)
_day = T


Temp = Mid(mystring , 7 , 2)
T = Val(temp)
_month = T

Temp = Mid(mystring , 9 , 2)
T = Val(temp)
_year = T

Temp = Mid(mystring , 11 , 1)
T = Val(temp)
_weekday = T
Modus = 1
End If

End If
Return

PicNick
19.11.2007, 08:33
mmmhh.

Bascom zeigt keinen Fehler an, aber eigentlich sollte bei "SELECT"
immer stehen: "CASE "xx" : (Doppelpunkt)

Probier's mal einfach.

Snecx
19.11.2007, 14:06
Danke. Werde ich ausprobieren. Bin im Moment auf der Arbeit.
Der Code ist very quick and dirty. War wohl gestern zu spät :-)
Ich optimiere das heute abend und teste dann weiter.

Snecx
19.11.2007, 17:46
So. Ich habe den Code ein wenig aufgeräumt. Die Sub für die UART ein wenig geändert. Ein einfach anhängen eines : Zeichen brachte leider nicht den Erfolg.
Ich habe es aber denoch hinbekommen. Beim Eintritt in die Sub weise ich einer Variabel (String*1) sofort udr zu.
Innerhalb der Sub verwende ich nur noch dann die Variabel für die Bedingungen. Meine Vermutung ist das irgendwo im Programm die Typen durcheinanderkommen und deshalb immer nur die erste Bedingung in der Select Case genommen wird.
Kleines Problem ist jetzt aber dabeigekommen. Sende ich nur die Befehle z.B. WB1G <-- zum lesen der Uhrzeit. Dann reagiert das Programm nicht.
Ebenso wenn ich zum schreiben der Uhrzeit den Befehl sende.
Sende ich aber vor dem eigentlichen Befehl noch 1 oder 2 "Initialzeichen" dann reagiert alles ohne Probleme z.B. **WB1G
als wenn am Start etwas verschluckt wird.
Hat da jemand eine Idee?
Hier jetzt mein Code:



$regfile = "m8535.dat"
''$regfile = "m16def.dat"
$crystal = 8000000 ' used crystal frequency
$baud = 9600 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40

Config Graphlcd = 240 * 128 , Dataport = Porta , Controlport = Portc , Ce = 2 , Cd = 3 , Wr = 0 , Rd = 1 , Reset = 4 , Fs = 6 , Mode = 6
Config 1wire = Portd.3 '' 1 Wire Port
Config Pinc.7 = Output '' LCD Invert
Config Pind.4 = Output
Config Scl = Portd.5
Config Sda = Portd.6
Config Portb = Output


On Urxc Onrxd 'Interrupt-Routine setzen
Enable Urxc 'Interrupt URXC einschalten
Enable Interrupts 'Interrupts global zulassen '' LCD Backlight

Dim Temp As String * 11
Dim T As Byte
Dim Rs232in As String * 1

Dim I As Byte

Dim Rs232 As Byte
Dim Mystring As String * 11
Dim Mybytes(12) As Byte At Mystring Overlay
Dim Ds1307w As Byte
Dim Ds1307r As Byte
Dim _day As Byte
Dim _month As Byte
Dim _year As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim _weekday As Byte
Dim Modus As Byte
Dim Test As Byte

Portb = &B11111111
Portc.7 = 0 '' LCD Invert ON
Portd.4 = 1 '' LCD Backlight ON
Ds1307w = &B11010000
Ds1307r = &B11010001

Modus = 0
Test = 0
I = 1

Cls

Do
Select Case Modus
Case 1: '' Set Date and Time
Print "Hour:"
Print _hour
Print "Minute:"
Print _min
Print "Sec:"
Print _sec
Print "Day:"
Print _day
Print "Month:"
Print _month
Print "Year:"
Print _year
Print "Weekday:"
Print _weekday
If Test = 1 Then
_sec = Makebcd(_sec)
_min = Makebcd(_min)
_hour = Makebcd(_hour)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cwbyte _sec
I2cwbyte _min
I2cwbyte _hour
I2cstop
_day = Makebcd(_day)
_month = Makebcd(_month)
_year = Makebcd(_year)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 3
I2cwbyte _weekday
I2cwbyte _day
I2cwbyte _month
I2cwbyte _year
I2cstop
End If
Modus = 0
Case 2: '' Get Date and Time
If Test = 1 Then
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cstop
I2cstart
I2cwbyte Ds1307r
I2crbyte _sec , Ack
I2crbyte _min , Ack
I2crbyte _hour , Ack
I2crbyte _weekday , Ack
I2crbyte _day , Ack
I2crbyte _month , Ack
I2crbyte _year , Nack
I2cstop
End If
Print "20332011076"
_day = Makedec(_day)
_month = Makedec(_month)
_year = Makedec(_year)
_sec = Makedec(_sec)
_min = Makedec(_min)
_hour = Makedec(_hour)
Print "Hour:"
Print _hour
Print "Minute:"
Print _min
Print "Sec:"
Print _sec
Print "Day:"
Print _day
Print "Month:"
Print _month
Print "Year:"
Print _year
Print "Weekday:"
Print _weekday
Modus = 0
End Select


Portb = &B00000000
Wait 1
Portb = &B11111111
Wait 1

Loop
End


'UART interrupt routine
Onrxd:
Rs232in = Chr(udr)

Select Case I
Case 1:
If Rs232in = "W"then
I = 2
Else
I = 1
End If
Case 2:
If Rs232in = "B" Then
I = 3
Else
I = 1
End If
Case 3:
If Rs232in = "1" Then
I = 4
Else
I = 1
End If
Case 4:
Select Case Rs232in
Case "G":
Modus = 2
Case "S":
I = 5
Case Else
I = 1
End Select
Case 5:
Incr Rs232
Mybytes(rs232) = Rs232in
If Rs232 = 11 Then

Rs232 = 0
I = 1
Modus = 1

Temp = Mid(mystring , 1 , 2)
T = Val(temp)
_hour = T

Temp = Mid(mystring , 3 , 2)
T = Val(temp)
_min = T
_sec = 00

Temp = Mid(mystring , 5 , 2)
T = Val(temp)
_day = T

Temp = Mid(mystring , 7 , 2)
T = Val(temp)
_month = T

Temp = Mid(mystring , 9 , 2)
T = Val(temp)
_year = T

Temp = Mid(mystring , 11 , 1)
T = Val(temp)
_weekday = T
End If
Case Else
I = 1
End Select

Return