Hallo
Als Bascomlaie habe ich mal ein Grundgerüst angefangen:
Ich verwende 10-bit-PWM, die Funktion Motorpwm() erwartet aber Werte zwischen 0 und 255. Diese werden dann mit 4 multpliziert in die 10-bit Timerregister geschrieben (nicht atomar!).Code:' nibobee mit Bascom 9.12.09 mic' ' Achtung! Nach einer kurzen Verzögerung starten die Motoren! $regfile = "m16def.dat" $crystal = 15000000 $baud = 9600 Config Portb.0 = Output ' LEDs 0-3 Config Portb.1 = Output Config Portb.2 = Output Config Portb.3 = Output Config Portd.4 = Output ' pmw_r Config Portd.5 = Output ' pwm_l Config Portd.6 = Output ' dir_l Config Portd.7 = Output ' dir_r Declare Sub Motorpwm(byval Pwm_l As Byte , Byval Pwm_r As Byte) Declare Sub Motordir(byval Dir_l As Byte , Byval Dir_r As Byte) Declare Sub Setleds(byval Leds As Byte) Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 1 Call Setleds(1) Wait 3 Do Call Setleds(9) Call Motordir(1 , 1) Call Motorpwm(127 , 127) Wait 2 Call Motorpwm(255 , 255) Wait 1 Call Motorpwm(127 , 127) Wait 2 Call Motorpwm(0 , 0) Wait 1 Call Setleds(6) Call Motordir(0 , 0) Call Motorpwm(127 , 127) Wait 2 Call Motorpwm(255 , 255) Wait 1 Call Motorpwm(127 , 127) Wait 2 Call Motorpwm(0 , 0) Wait 1 Loop End Sub Motorpwm(byval Pwm_l As Byte , Byval Pwm_r As Byte) Pwm1a = Pwm_l * 4 Pwm1b = Pwm_r * 4 End Sub Sub Motordir(byval Dir_l As Byte , Byval Dir_r As Byte) If Dir_l = 0 Then Portd.6 = 0 Else Portd.6 = 1 If Dir_r = 0 Then Portb.7 = 1 Else Portb.7 = 0 End Sub Sub Setleds(byval Leds As Byte) If Leds.0 = 1 Then Portb.0 = 1 Else Portb.0 = 0 If Leds.1 = 1 Then Portb.1 = 1 Else Portb.1 = 0 If Leds.2 = 1 Then Portb.2 = 1 Else Portb.2 = 0 If Leds.3 = 1 Then Portb.3 = 1 Else Portb.3 = 0 End Sub
Im Parameter für Setleds() ist den Bits 0 bis 3 jeweils die entsprechende Led zugeordnet.
Die LineLEDs IR3 und IR4 hängen zwischen Vcc und PB4 (LINE_EN). Deshalb schaltet ein Low die Leds an und ein High wieder aus!Code:Sub Linedata() Config Portb.4 = Output Portb.4 = 0 Linesensl = Getadc(5) Linesensc = Getadc(6) Linesensr = Getadc(7) Portb.4 = 1 End Sub
Gruß
mic







Zitieren

Lesezeichen