Skip to content

Instantly share code, notes, and snippets.

@robertinant
Created April 8, 2014 16:18
Show Gist options
  • Save robertinant/10150407 to your computer and use it in GitHub Desktop.
Save robertinant/10150407 to your computer and use it in GitHub Desktop.
DRV8897 Motor Driver Sketch
/*
DRV8897MotorControl
The basic Energia example that illustrates how to control a motor using the DRV8897
Hardware Required:
* LaunchPad + DRV8897_BOOST BoosterPack
This example code is in the public domain.
*/
#define LED P1_1
#define MOTOR_PIN_RIGHT P1_2
#define MOTOR_PIN_LEFT P2_6
#define SPEED_PIN P1_0
#define REF_PIN P1_4
#define DIR_PIN P1_3
void setup() {
/* Make the direction pin input */
pinMode(DIR_PIN, INPUT);
/* Make the LED pin an output */
pinMode(LED, OUTPUT);
/* Set the motor speed reference pin to high */
pinMode(REF_PIN, OUTPUT);
digitalWrite(REF_PIN, HIGH);
analogWrite(MOTOR_PIN_LEFT, 255);
analogWrite(MOTOR_PIN_RIGHT, 255);
/* Set the PMW frequency to 15kHz */
analogFrequency(15000);
}
uint16_t motorSpeed;
uint16_t oldMotorSpeed;
uint32_t then;
uint8_t ledState = false;
void updateLED() {
/* Fary the LED flashing speed depending on the motor speed */
if(millis() - then > motorSpeed) {
digitalWrite(LED, ledState);
ledState = !ledState;
then = millis();
}
}
void loop() {
/* Read the potentiometer */
motorSpeed = analogRead(SPEED_PIN);
/* Map potentiometer position to the PWM signal output */
motorSpeed = map(motorSpeed, 0, 1023, 0, 255);
/* Update motor speed and direction */
if(digitalRead(DIR_PIN)) {
analogWrite(MOTOR_PIN_RIGHT, 255);
analogWrite(MOTOR_PIN_LEFT, motorSpeed);
} else {
analogWrite(MOTOR_PIN_LEFT, 255);
analogWrite(MOTOR_PIN_RIGHT, motorSpeed);
}
/* Remember the previous motor speed */
oldMotorSpeed = motorSpeed;
/* Update the speed indication LED */
updateLED();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment