PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : UART zwischen Atmega32 und ATmega8



Lunarman
07.06.2007, 15:20
Hallo,
für meinen Bot muss ich eine Kommunikation via UART atmega8 (hardware UART) und atmega 32 (software uart) aufbauen. Nur richtig funktionieren tuts nicht. Ich poste mal den Code beider Seiten. Wenn ich den atmega8 einfach so die Motoren einschalten lasse, funktioniert es aber.



$regfile = "m32def.dat"
$crystal = 16000000

Config Portc.1 = Output
Config Portd.2 = Output
Config Portd.7 = Output
Config Portd.6 = Output

Open "comd.7:9600,8,n,1" For Input As #1
Open "comd.6:9600,8,n,1" For Output As #2

Dim Entfernung As Integer
Dim Motorspeedright As Byte
Dim Motorspeedleft As Byte
Dim Directionright As String * 3
Dim Directionleft As String * 3

Motorspeedright = 100
Motorspeedleft = 100
Directionright = "Rwd"
Directionleft = "Rwd"
Do

Waitms 500
Portc.1 = 1
Waitms 00
Portc.1 = 0


Print #2 , Motorspeedright
Waitms 1
Print "tschacka 1"
Print #2 , Motorspeedleft
Waitms 1
Print "tschacka 2"
'Print #2 , Directionright
Waitms 1
Print "tschacka 3"
'Print #2 , Directionleft
Waitms 1
Print "tschacka 4"

Loop




End


$regfile = "m8def.dat"
$crystal = 1000000

Dim Motorspeedleft As Byte
Dim Motorspeedright As Byte
Dim Directionright As String * 3
Dim Directionleft As String * 3
Dim A As Bit
Dim B As Bit
Dim C As Bit
Dim D As Bit

Config Portc.0 = Output
Config Portc.1 = Output
Config Portc.2 = Output
Config Portc.3 = Output

Config Portb.1 = Output
Config Portb.2 = Output

Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 1


Do
Input Motorspeedright
Input Motorspeedleft
'Input Directionright
'Input Directionleft

'If Directionright = "Fwd" Then
'A = 0
'B = 1
'End If

'If Directionright = "Rwd" Then
'A = 1
'B = 0
'End If

'If Directionright = "Stop" Then
'A = 0
'B = 0
'End If

'If Directionleft = "Fwd" Then
'C = 0
'D = 1
'End If

'If Directionleft = "Rwd" Then
'C = 1
'd = 0
'End If

'If Directionleft = "Stop" Then
'C = 0
'D = 0
'End If



Compare1a = Motorspeedright
Compare1b = Motorspeedleft
Portc.0 = 0 'A
Portc.1 = 1 'B
Portc.2 = 0 'C
Portc.3 = 1 'D
Loop

End

Die Motoren laufen beim UART nicht an, klicken aber einmal und lassen sich per Hand kaum noch drehen. Das klicken führe ich auf eine kleine Bewegung des Ankers zurück, die Bremsung interpretiere ich so, dass Strom durchfließt, aber anscheinend irgendwie nicht genug. Was hab ich denn falsch gemacht?

EDIT: Der Uart funktioniert aber. Wenn ich den Stecker abnehme klicken die Motoren nicht mehr.

robbifan
07.06.2007, 16:37
ich kann dir mal mein musterrobbi-code geben. ich empfange die seriellen daten über interrupt. der code funktioniert auf dem avr32 :



$Regfile = "M32def.dat"
$Crystal = 8000000
$HWStack = 32
$SWStack = 32
$FrameSize = 64
$Baud = 19200

Dim M_wert_a As Byte , M_wert_b As Byte
Dim Text As String * 16

Declare Sub Robby_vor()
Declare Sub Robby_zurueck()
Declare Sub Robby_links()
Declare Sub Robby_rechts()
Declare Sub Robby_links_drehen()
Declare Sub Robby_rechts_drehen()
Declare Sub Robby_aus()
Declare Sub L293_pin()
declare sub empfang()

Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear down , Compare B Pwm = Clear down , Prescale = 256
Start Timer1

config int2=rising

On Int2 Int2_int

Enable Interrupts
Enable Int2

Open "comb.2:9600,8,n,1" For Input As #1
Open "comd.6:9600,8,n,1" For Output As #2

Call L293_pin()

do
waitms 300
loop

End

Int2_int:
Disable Int2
Enable Interrupts
input #1, text
waitms 10
call empfang()
enable int2
Return

sub empfang()
If Text = "vor" Then
M_wert_a = 100
M_wert_b = 100
Call Robby_vor()
End If
If Text = "zurueck" Then
M_wert_a = 100
M_wert_b = 100
Call Robby_zurueck()
End If
If Text = "links" Then
M_wert_a = 60
M_wert_b = 60
Call Robby_links()
End If
If Text = "rechts" Then
M_wert_a = 60
M_wert_b = 60
Call Robby_rechts()
End If
If Text = "stop" Then
Call Robby_aus()
End If
end sub

Sub L293_pin()
Ddrc.2 = 1
Ddrc.3 = 1
Ddrc.4 = 1
Ddrc.5 = 1
Ddrd.4 = 1
Ddrd.5 = 1
End Sub

Sub Robby_vor()
Portc.2 = 0 'rechter motor
Portc.3 = 1
Portd.5 = 1

Portc.4 = 0 'linker motor
Portc.5 = 1
Portd.4 = 1

Pwm1a = M_wert_a
Pwm1b = M_wert_b
End Sub

Sub Robby_zurueck()
Portc.2 = 1 'rechter motor
Portc.3 = 0
Portd.5 = 1

Portc.4 = 1 'linker motor
Portc.5 = 0
Portd.4 = 1
Pwm1a = M_wert_a
Pwm1b = M_wert_b
End Sub

Sub Robby_links()
Portc.2 = 0
Portc.3 = 1
Portd.5 = 1

Portc.4 = 0
Portc.5 = 0
Portd.4 = 0

Pwm1a = M_wert_a
Pwm1b = 0
End Sub

Sub Robby_rechts()
Portc.2 = 0
Portc.3 = 0
Portd.5 = 0

Portc.4 = 0
Portc.5 = 1
Portd.4 = 1
Pwm1a = 0
Pwm1b = M_wert_b
End Sub

Sub Robby_aus()
Portc.2 = 0
Portc.3 = 0
Portd.4 = 0
Portc.4 = 0
Portc.5 = 0
Portd.5 = 0
Pwm1a = 0
Pwm1b = 0
End Sub

Sub Robby_rechts_drehen()
Portc.2 = 1
Portc.3 = 0
Portd.5 = 1

Portc.4 = 0
Portc.5 = 1
Portd.4 = 1
Pwm1a = M_wert_a
Pwm1b = M_wert_b
End Sub

Sub Robby_links_drehen()
Portc.2 = 0
Portc.3 = 1
Portd.5 = 1

Portc.4 = 1
Portc.5 = 0
Portd.4 = 1
Pwm1a = M_wert_a
Pwm1b = M_wert_b
End Sub

Lunarman
07.06.2007, 17:22
ja, das hat mir schon weitergeholfen, auf die idee mit text bin ich gar nicht gekommen. Danke!