Sourcecode
Sourcecode

'-----------------------------------------------------------------------------------------

'-----------------------------------------------------------------------------------------

'Titel                    : Robotersteuerung
'Name                     : *.bas
'Autor                    : Arno Schweissinger
'Datum                    : 28.02.2005
'MicroContorller          : AtMega8 4MHz
'Hardware                 : Display HD 44780-kompatibel, 2 Schrittmotoren,
'                           Treiber ULN 2803A, kleiner Lautprecher
'Stromversorgung:         : 5V fuer Logik, 24V fuer Antrieb
'In Planung               : RC5-IR Datenuebertagung zum Roboter
'-----------------------------------------------------------------------------------------

$regfile = "m8def.dat"

'$crystal = 4000000
$crystal = 1500000
'$baud = 9600
Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.0 , Db5 = Portc.1 , Db6 = Portc.2 , Db7 = Portc.3 , E = Portc.5 , Rs = Portc.4

$lib "mcsbyte.lbx"                                          'use byte library for smaller code

'-------------------------
'      Hauptprogramm
'-------------------------
Gosub Init
Do
 Gosub Info_lcd
 Gosub Fahre_gerade
'  Gosub Sensoren
 Loop
End                                                         ' ende Hauptprogramm

'-------------------------
'      Init Programm
'-------------------------
Init:
 ' Display
 Cursor Off
 Cls
 Locate 1 , 1 : Lcd "      A utonomer"
 Locate 2 , 1 : Lcd "      R oboter fuer"
 Locate 3 , 1 : Lcd "      N ull"
 Locate 4 , 1 : Lcd "  eur O"

 ' Rõder Schrittmotoren links PinB.0-3
 '                     rechts PinD.4-7
 Config Pinb.0 = Output
 Config Pinb.1 = Output
 Config Pinb.2 = Output
 Config Pinb.3 = Output
 Config Pind.4 = Output
 Config Pind.5 = Output
 Config Pind.6 = Output
 Config Pind.7 = Output

 'Lautsprecher
 Lsp Alias Pind.0

 'Taster - Kollisions Melder
 Kollision_li Alias Pind.2
 Config Kollision_li = Input
 Kollision_re Alias Pind.3
 Config Kollision_re = Input

 Dim I As Byte                                             ' Allgemeiner Zõhler
 Dim Rad_re As Byte                                        ' Position des Schrittmotors
 Dim Rad_li As Byte
 Dim Schritte_re As Byte                                   ' Statistik
 Dim Schritte_li As Byte                                   ' Statistik

 ' Bitmuster f³r Schrittmotor: Bit 0-3
 Rad_li = &B00010001
 Rad_re = &B10001000

 ' Roboter bereit
 Gosub Beep_lo_hi:
 Gosub Beep_lo_hi:
 Wait 1
 Cls : Lcd "Fahre geradeaus"
Return

'-------------------------
'      Info ausgeben
'-------------------------
Info_lcd:
 Locate 3 , 1
 Lcd "R=" ; Schritte_re ; "  "
 Locate 3 , 7
 Lcd "L=" ; Schritte_li ; "  "
 Locate 4 , 1
 Lcd Bin(pind)
 ' Motor bremsen
 ' Waitms 20
Return

'-------------------------
'      Sensoren Taster
'-------------------------
' abfragen
Sensoren:
 If Kollision_re = 0 Then
   Gosub Angestossen_re
 End If
 If Kollision_li = 0 Then
   Gosub Angestossen_li
 End If
Return

'-------------------------
'      Aktoren Lautsprecher
'-------------------------
' Tiefer Ton
Beep_lo:
 Sound Lsp , 40 , 60                                       'BEEP
Return
                                            '
' Hoher Ton
Beep_hi:
 Sound Lsp , 200 , 20                                      'BEEP
Return

Beep_lo_hi:
 Gosub Beep_lo
 Gosub Beep_hi
Return

'-------------------------
'      Aktoren Motoren
'-------------------------
' Richtungsõnderung nach links
Angestossen_re:
 Cls
 Lcd "Kollision Rechts"
 Gosub Ausweichen_zurueck
 Locate 2 , 11 : Lcd " -> links"
 Gosub Beep_lo
 For I = 1 To 35
   Gosub Info_lcd
   Gosub Drehe_rechts
 Next I
 Gosub Beep_hi
 Cls
Return

' Richtungsõnderung nach rechts
Angestossen_li:
 Cls
 Lcd "Kollision Links"
 Gosub Ausweichen_zurueck
 Locate 2 , 11 : Lcd " -> rechts"
 Gosub Beep_lo
 For I = 1 To 35
   Gosub Info_lcd
   Gosub Drehe_links
 Next I
 Gosub Beep_hi
 Cls
Return

Ausweichen_zurueck:
 Locate 2 , 1 : Lcd "-> zurueck"
 Gosub Beep_lo_hi:
 For I = 1 To 20
   Gosub Info_lcd
   Gosub Fahre_zurueck
 Next I
Return

'-------------------------
'      Motorsteuerung Einzelschritte
'-------------------------
' Beide Motoren Vortrieb
Fahre_gerade:
Incr Schritte_li
Incr Schritte_re
Rotate Rad_li , Right , 1
Rotate Rad_re , Left , 1
Gosub Raeder_drehen
Return

' Beide Motoren r³ckwõrts
Fahre_zurueck:
Decr Schritte_li
Decr Schritte_re
Rotate Rad_li , Left , 1
Rotate Rad_re , Right , 1
Gosub Raeder_drehen
Return

' Rechter Motor Vortrieb
' Linker  Motor R³ckschritt
Drehe_rechts:
Decr Schritte_li
Incr Schritte_re
Rotate Rad_li , Right , 1
Rotate Rad_re , Right , 1
Gosub Raeder_drehen
Return

' Linker  Motor Vortrieb
' Rechter Motor R³ckschritt
Drehe_links:
Incr Schritte_li
Decr Schritte_re
Rotate Rad_li , Left , 1
Rotate Rad_re , Left , 1
Gosub Raeder_drehen
Return

Raeder_drehen:
Portb.0 = Rad_li.0
Portb.1 = Rad_li.1
Portb.2 = Rad_li.2
Portb.3 = Rad_li.3

Portd.4 = Rad_re.0
Portd.5 = Rad_re.1
Portd.6 = Rad_re.2
Portd.7 = Rad_re.3
Return