DRV8830/Texas Instruments H-Bridge Voltage-Controlled Motor Driver library for Brushed DC Motor
Dependents: NucleoF401_motor_test_simple Frequency_Counter_w_GPS_1PPS Nucleo_ACM1602_I2C_DC_Angle Frequency_Cntr_1PPS_F746ZG
DRV8830.cpp
- Committer:
- kenjiArai
- Date:
- 2014-08-23
- Revision:
- 0:05db098cf4f8
- Child:
- 1:a9b58bc85be0
File content as of revision 0:05db098cf4f8:
/* * mbed library program * Texas Instruments / DRV8830 H-Bridge Voltage-Controlled Motor Driver * http://www.ti.com/product/drv8830 * * Copyright (c) 2014 Kenji Arai / JH1PJL * http://www.page.sannet.ne.jp/kenjia/index.html * http://mbed.org/users/kenjiArai/ * Created: August 6th, 2014 * Revised: August 16th, 2014 * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "mbed.h" #include "DRV8830.h" // motor status #define DRV8830_FREE 0x00 #define DRV8830_CW 0x01 #define DRV8830_CCW 0x02 #define DRV8830_BREAK 0x03 // register address #define DRV8830_CONTROL 0x00 #define DRV8830_FAULT 0x01 // voltage data #define DRV8830_V_R48 0x06 #define DRV8830_V_R56 0x07 #define DRV8830_V_R64 0x08 #define DRV8830_V_R72 0x09 // (no definition from 0.82V to 4.74V) #define DRV8830_V_4R82 0x3c #define DRV8830_V_4R90 0x3d #define DRV8830_V_4R98 0x3e #define DRV8830_V_5R06 0x3f #define DRV8830_V_MIN 0x06 #define DRV8830_V_MAX 0x3f #define DRV8830_V_RANGE (DRV8830_V_MAX - DRV8830_V_MIN) // fault status #define DRV8830_F_CLEAR 0x80 DRV8830::DRV8830 (PinName p_sda, PinName p_scl, uint8_t addr) : i2c(p_sda, p_scl){ DRV8830_addr = (char)addr; } DRV8830::DRV8830 (I2C& p_i2c, uint8_t addr) : i2c(p_i2c){ DRV8830_addr = (char)addr; } void DRV8830::speed(float speed) { uint8_t direction = 0; uint8_t pwm_rate = 0; uint8_t dt[2]; if (speed == 0.0 ){ pwm_rate = DRV8830_V_MIN; direction = DRV8830_FREE; } else if (speed > 0.0){ pwm_rate = (uint8_t)(DRV8830_V_RANGE * speed) + DRV8830_V_MIN; direction = DRV8830_CW; } else if (speed < 0.0){ speed *= -1; pwm_rate = (uint8_t)(DRV8830_V_RANGE * speed) + DRV8830_V_MIN; direction = DRV8830_CCW; } if (pwm_rate > DRV8830_V_MAX){ pwm_rate = DRV8830_V_MAX; } else if (pwm_rate < DRV8830_V_MIN){ pwm_rate = DRV8830_V_MIN; } dt[0] = DRV8830_CONTROL; dt[1] = (pwm_rate << 2) + direction; i2c.write((int)DRV8830_addr, (char *)dt, 2); } uint8_t DRV8830::status() { uint8_t dt[2]; dt[0] = DRV8830_FAULT; i2c.write((int)DRV8830ADDR_00, (char *)dt, 1); // write register address i2c.read((int)DRV8830ADDR_00, (char *)dt, 1); // read register content return dt[0]; } void DRV8830::reset() { uint8_t dt[2]; dt[0] = DRV8830_FAULT; dt[1] = DRV8830_F_CLEAR; i2c.write((int)DRV8830ADDR_00, (char *)dt, 2); }