Hallo zusammen,
ich wollte den Arduino Sketch: DCControl.ino umschreiben für Bascom. Komme nicht weiter.
Der Basiccode wird zwar ohne Fehler übersetzt, aber die Motorplatine reagiert nicht.
Der Arduino Sketch läuft auf einem Uno-Board einwandfrei.
Ich vermute das hex Adressen der I2C Motorplatine nicht stimmen.
Ein Arduino I2C_Scanner gibt für I2cmotordriveradd x0F aus.
Ein Bascom I2C_Scanner gibt für I2cmotordriveradd H1E aus.
CodeBox BascomAVR
Könnt Ihr mir da weiter helfen. Viele Grüße und Danke
Wolfgang
ich wollte den Arduino Sketch: DCControl.ino umschreiben für Bascom. Komme nicht weiter.
Der Basiccode wird zwar ohne Fehler übersetzt, aber die Motorplatine reagiert nicht.
Der Arduino Sketch läuft auf einem Uno-Board einwandfrei.
Code:
[/
/*
Grove- i2C motor driver demo v1.0
by: http://www.seeedstudio.com
// Author:LG
//
//
// This demo code is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
*/
#include <Wire.h>
#define MotorSpeedSet 0x82
#define PWMFrequenceSet 0x84
#define DirectionSet 0xaa
#define MotorSetA 0xa1
#define MotorSetB 0xa5
#define Nothing 0x01
#define EnableStepper 0x1a
#define UnenableStepper 0x1b
#define Stepernu 0x1c
#define I2CMotorDriverAdd 0x0f // Set the address of the I2CMotorDriver
// set the steps you want, if 255, the stepper will rotate continuely;
void SteperStepset(unsigned char stepnu)
{
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(Stepernu); // Send the stepernu command
Wire.write(stepnu); // send the steps
Wire.write(Nothing); // send nothing
Wire.endTransmission(); // stop transmitting
}
///////////////////////////////////////////////////////////////////////////////
// Enanble the i2c motor driver to drive a 4-wire stepper. the i2c motor driver will
//driver a 4-wire with 8 polarity .
//Direction: stepper direction ; 1/0
//motor speed: defines the time interval the i2C motor driver change it output to drive the stepper
//the actul interval time is : motorspeed * 4ms. that is , when motor speed is 10, the interval time
//would be 40 ms
//////////////////////////////////////////////////////////////////////////////////
void StepperMotorEnable(unsigned char Direction, unsigned char motorspeed)
{
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(EnableStepper); // set pwm header
Wire.write(Direction); // send pwma
Wire.write(motorspeed); // send pwmb
Wire.endTransmission(); // stop transmitting
}
//function to uneanble i2C motor drive to drive the stepper.
void StepperMotorUnenable()
{
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(UnenableStepper); // set unenable commmand
Wire.write(Nothing);
Wire.write(Nothing);
Wire.endTransmission(); // stop transmitting
}
//////////////////////////////////////////////////////////////////////
//Function to set the 2 DC motor speed
//motorSpeedA : the DC motor A speed; should be 0~100;
//motorSpeedB: the DC motor B speed; should be 0~100;
void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB) {
MotorSpeedA=map(MotorSpeedA,0,50,0,100);
MotorSpeedB=map(MotorSpeedB,0,50,0,100);
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(MotorSpeedSet); // set pwm header
Wire.write(MotorSpeedA); // send pwma
Wire.write(MotorSpeedB); // send pwmb
Wire.endTransmission(); // stop transmitting
}
//set the prescale frequency of PWM, 0x03 default;
void MotorPWMFrequenceSet(unsigned char Frequence) {
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(PWMFrequenceSet); // set frequence header
Wire.write(Frequence); // send frequence
Wire.write(Nothing); // need to send this byte as the third byte(no meaning)
Wire.endTransmission(); // stop transmitting
}
//set the direction of DC motor.
void MotorDirectionSet(unsigned char Direction) { // Adjust the direction of the motors 0b0000 I4 I3 I2 I1
Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd
Wire.write(DirectionSet); // Direction control header
Wire.write(Direction); // send direction control information
Wire.write(Nothing); // need to send this byte as the third byte(no meaning)
Wire.endTransmission(); // stop transmitting
}
void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB) { //you can adjust the driection and speed together
MotorDirectionSet(Direction);
MotorSpeedSetAB(MotorSpeedA,MotorSpeedB);
}
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
delayMicroseconds(10000);
Serial.begin(9600);
Serial.println("setup begin");
}
void loop() {
// the following code sent commands to motor driver to drive DC motor
while(1) {
Serial.println("sent DC speed 0 , 0 ");
Serial.println("sent Dir 0b00001010");
MotorSpeedSetAB(0,0);//defines the speed of motor 1 and motor 2;
MotorDirectionSet(0b00001010);
delay(2000);
Serial.println("sent Dir 0b0101");
delay(2000);
MotorDirectionSet(0b0101);
delay(2000);
Serial.println("sent Dir 0b0110");
delay(2000);
MotorDirectionSet(0b0110);
delay(2000);
Serial.println("sent DC speed 80 , 80");
MotorSpeedSetAB(80,80);//defines the speed of motor 1 and motor 2;
delay(10); //this delay needed
MotorDirectionSet(0b00001010); //"0b1010" defines the output polarity, "10" means the M+ is "positive" while the M- is "negtive"
// make sure M+ and M- is different polatity when driving DC motors.
delay(2000);
Serial.println("sent DC speed 40 , 40");
MotorSpeedSetAB(40,40);//defines the speed of motor 1 and motor 2;
delay(100); //this delay needed
MotorDirectionSet(0b0101); //0b0101 Rotating in the opposite direction
delay(2000);
Serial.println("sent DC speed 100 , 40");
MotorSpeedSetAB(40,40);//defines the speed of motor 1 and motor 2;
delay(100); //this delay needed
MotorDirectionSet(0b1001); //0b0101 Rotating in the opposite direction
delay(2000);
Serial.println("sent Dir 0b00000000");
delay(2000);
MotorDirectionSet(0b0000);
delay(2000);
}
}
Ich vermute das hex Adressen der I2C Motorplatine nicht stimmen.
Ein Arduino I2C_Scanner gibt für I2cmotordriveradd x0F aus.
Ein Bascom I2C_Scanner gibt für I2cmotordriveradd H1E aus.
CodeBox BascomAVR
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ' ' Project Name : Grove I2c Motor Driver ' ' Files : Grove I2c Motor Driver.cbas ' Writer : ' Date : ' Function : Demonstrates The Grove I2c Motor Driver 1.3 ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' $regfile = "m328pdef.dat" $crystal = 16000000 $hwstack = 132 ' default use 32 for the hardware stack $swstack = 132 'default use 10 for the SW stack $framesize = 164 'default use 40 for the frame space ' '$lib „i2c_twi.lbx“ ' '( 'Controller Pins für LCD Ansteuerung Konfigurieren Config Lcdpin = Pin , Rs = Portb.2 , E = Portb.3 , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 Config Lcd = 20 * 4 'Type des LCD´s 16 Char und 2 Zeilen Config Portb.1 = Output 'LCD Beleuchtung ein / Aus Lcd_display Alias Pinb.1 Initlcd 'LCD high level Initzialisierung Cursor Off 'Cursor ausschalten Cls 'LCD löschen ') ' '################################################################# '############# Grove I2c Motor Driver 1.3 ########################### '################################################################# ' Declare Sub Motorspeedsetab(byval Motorspeeda As Byte , Byval Motorspeedb As Byte) Declare Sub Motorpwmfrequenceset(byval Frequence As Byte) Declare Sub Motordirectionset(byval Directionset As Byte , Byval Direction As Integer) Declare Sub Motordirection ' Const Motorspeedset = &H82 Const Pwmfrequenceset = &H84 Const Directionset = &HAA Const Motorseta = &HA1 Const Motorsetb = &HA5 Const Nothing = &H01 Const Enablestepper = &H1A Const Unenablestepper = &H1B Const Stepernu = &H1C Const I2cmotordriveradd = &H1E ' 8bit I2C address Grove I2c Motor Driver 1.3 (Atmega8) ' Const Dirvor = &B00001010 Const Dirruck = &B00000101 Const Dirstop = &B00000000 ' Dim Somedata As String * 10 Dim Frequence As Byte Dim Direction As Integer ' Frequence = &H03 ' '------------------------------------------------------------------------------------------------------ 'TWI konfigurieren 'I²C - Bus Pinbelegung (Hardware I²C - Bus) Config Sda = Portc.4 'SDA und SCL als Hardware-TWI definieren Config Scl = Portc.5 Config I2cdelay = 5 I2cinit ' ' '============================================================ ' ' Motorspeed Setab ' Function To Set The 2 Dc Motor Speed ' Motor_A : The Dc Motor Links Speed ; Should Be 0~255; ' Motor_B : The Dc Motor Rechts Speed ; Should Be 0~255 ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' Sub Motorspeedsetab(byval Motorspeeda As Byte , Byval Motorspeedb As Byte) ' 'Motor rechts , Motor links I2cstart I2cwbyte I2cmotordriveradd I2cwbyte Motorspeedset I2cwbyte Motorspeeda I2cwbyte Motorspeedb I2cstop ' Waitms 15 End Sub ' '============================================================ ' ' Motorpwmfrequenceset ' Set The Prescale Frequency Of Pwm , 0x03 Default ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' Sub Motorpwmfrequenceset(byval Frequence As Byte) ' I2cstart I2cwbyte I2cmotordriveradd I2cwbyte Pwmfrequenceset I2cwbyte Frequence I2cwbyte Nothing I2cstop ' End Sub ' '============================================================ ' Motordirectionset ' Set The Direction Of Dc Motor '============================================================ 'Sub Motordirection ' Adjust the direction of the motors 0b0000 I4 I3 I2 I1 Sub Motordirection ' I2cstart I2cwbyte I2cmotordriveradd I2cwbyte Directionset I2cwbyte &B1010 I2cwbyte Nothing I2cstop ' End Sub ' '=========================================================== ' 'Sub Motordirectionset(byval Directionset As Byte , Byval Direction As Integer) ' Adjust the direction of the motors 0b0000 I4 I3 I2 I1 Sub Motordirectionset(byval Directionset As Byte , Byval Direction As Integer) ' I2cstart I2cwbyte I2cmotordriveradd I2cwbyte Directionset I2cwbyte &B0001010 I2cwbyte Direction I2cwbyte Nothing I2cstop ' End Sub ' '============================================================ ' Motordriectionandspeedset '============================================================ ' 'Sub Motordirectionandspeedset(byval Direction As Byte , Byval Motorspeed_l As Byte , Byval Motorspeed_r As Byte) ' you can adjust the driection and speed together ' Motordirectionset(direction) ' Motorspeedsetab(motorspeeda , Motorspeedb) 'End Sub ' '################################################################# ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' Main Program ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' Do ' Call Motorspeedsetab(50 , 50) Waitms 50 Call Motordirection Wait 2 Loop ' '################################################################# End
Könnt Ihr mir da weiter helfen. Viele Grüße und Danke
Wolfgang
Zuletzt bearbeitet: