Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revisionBoth sides next revision | ||
tinyidiot [2018/12/19 16:23] – [TinyIdiot] yair | tinyidiot [2018/12/20 21:44] – [code] yair | ||
---|---|---|---|
Line 33: | Line 33: | ||
==== code ==== | ==== code ==== | ||
- | < | + | https:// |
- | /* | ||
- | TinyIdiot | ||
- | tiytony esp8266 | ||
- | drv8830 code from https:// | ||
- | pdf > http:// | ||
- | ##wiring | ||
- | ESP DRV 8830 | ||
- | GPIO 4 | SDA | ||
- | GPIO 5 | SCL | ||
- | VIN | Vcc | ||
- | GND | Gnd | ||
- | * **connect to the ESP only with I2C pulled up with 3.3v.** | ||
- | * Motor (+) and OUT1, motor (-) and OUT2 | ||
- | * A 1 nF ceramic capacitor between + and - of the motor | ||
- | * A resistance of 0.2 Ω between ISENSE and GND of the DRV 8830 | ||
- | * 0.1 μF ceramic capacitor between VCC and GND of the DRV 8830 | ||
- | * A 10 kΩ resistor between SDA and 3.3 v | ||
- | * A 10 kΩ resistor between SCL and 3.3 v | ||
- | * 5 V output such as USB serial and VIN of ESP - WROOM - 02 board | ||
- | * GND of USB serial etc. and GND of ESP - WROOM - 02 board | ||
- | */ | ||
- | |||
- | #include < | ||
- | #include < | ||
- | |||
- | uint8_t readMotorStatus(); | ||
- | void resetMotorStatus(); | ||
- | void runMotor(int inVector); | ||
- | void writeToDriver(byte inDirection, | ||
- | void brakeMotor(); | ||
- | |||
- | const int kDrv8830Address = 0x64; | ||
- | |||
- | const int kBitClear | ||
- | const int kBitILimit = 0x10; | ||
- | const int kBitOTS | ||
- | const int kBitUVLO | ||
- | const int kBitOCP | ||
- | const int kBitFault | ||
- | |||
- | |||
- | // | ||
- | // Setup | ||
- | // | ||
- | |||
- | void setup() { | ||
- | Serial.begin(115200); | ||
- | Wire.begin(); | ||
- | } | ||
- | |||
- | |||
- | // | ||
- | // Loop | ||
- | // | ||
- | |||
- | float r = 0; | ||
- | |||
- | void loop() { | ||
- | // | ||
- | // Motor | ||
- | float s = sin(r) * 64.0; | ||
- | r += 0.1; | ||
- | if (r > 6.28) r = 0.0; | ||
- | |||
- | int out = (s > 0) ? 0x01 : 0x02; | ||
- | int speed = s; | ||
- | |||
- | runMotor(speed); | ||
- | |||
- | int status = readMotorStatus(); | ||
- | if (status & kBitFault) { | ||
- | Serial.print(" | ||
- | Serial.println(status, | ||
- | resetMotorStatus(); | ||
- | } | ||
- | | ||
- | delay(100); | ||
- | } | ||
- | |||
- | |||
- | // | ||
- | // DRV8830 Controll | ||
- | // | ||
- | uint8_t readMotorStatus() { | ||
- | uint8_t result = 0x00; | ||
- | | ||
- | Wire.beginTransmission(kDrv8830Address); | ||
- | Wire.write(0x01); | ||
- | Wire.endTransmission(); | ||
- | |||
- | Wire.requestFrom(kDrv8830Address, | ||
- | if (Wire.available()) { | ||
- | result = Wire.read(); | ||
- | } else { | ||
- | Serial.println(" | ||
- | } | ||
- | |||
- | return result; | ||
- | } | ||
- | |||
- | void resetMotorStatus() { | ||
- | Wire.beginTransmission(kDrv8830Address); | ||
- | Wire.write(0x01); | ||
- | Wire.write(0x80); | ||
- | Wire.endTransmission(true); | ||
- | } | ||
- | |||
- | |||
- | void runMotor(int inVector) { | ||
- | int direction; | ||
- | int voltage; | ||
- | |||
- | if (inVector > 0) { | ||
- | direction = 0x01; | ||
- | voltage = inVector; | ||
- | } else if (inVector == 0) { | ||
- | direction = 0x00; | ||
- | voltage = 0; | ||
- | } else { | ||
- | direction = 0x02; | ||
- | voltage = -inVector; | ||
- | } | ||
- | |||
- | writeToDriver(direction, | ||
- | } | ||
- | |||
- | void brakeMotor() { | ||
- | writeToDriver(0x03, | ||
- | } | ||
- | |||
- | |||
- | void writeToDriver(byte inDirection, | ||
- | if (inVoltage <= 0x05) inVoltage = 0x06; // minimum voltage value is 0x06. | ||
- | | ||
- | int outData = (inVoltage & 0x3f) << 2 | (inDirection & 0x03); | ||
- | Wire.beginTransmission(kDrv8830Address); | ||
- | Wire.write(0x00); | ||
- | Wire.write(outData); | ||
- | Wire.endTransmission(true); | ||
- | |||
- | delay(12); | ||
- | } | ||
- | </ |