Created
June 12, 2016 17:00
-
-
Save sebbdk/2e88145cb79f496071c226eb4b82ab45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#if defined(ARDUINO_SAM_DUE) | |
#define WIRE Wire1 | |
#else | |
#define WIRE Wire | |
#endif | |
//Settings: | |
#define _i2caddr 0x60 | |
//?? | |
#define LED0_ON_L 0x6 | |
#define PCA9685_MODE1 0x0 | |
#define PCA9685_PRESCALE 0xFE | |
void setup() { | |
Serial.begin(9600);// set up Serial library at 9600 bps | |
Serial.println("Adafruit Motorshield v2 - DC Motor test!"); | |
WIRE.begin(); | |
reset(); | |
setPWMFreq(0); | |
//turn off all pins | |
for (uint8_t i=0; i<16; i++) { | |
setPWM(i, 0, 0); | |
} | |
//Stop everything.. | |
setPWM(8, 0, 0); | |
setPin(10, LOW); | |
setPin(9, LOW); | |
} | |
void reset(void) { | |
write8(PCA9685_MODE1, 0x0); | |
} | |
void setPWMFreq(float freq) { | |
//Serial.print("Attempting to set freq "); | |
//Serial.println(freq); | |
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11). | |
float prescaleval = 25000000; | |
prescaleval /= 4096; | |
prescaleval /= freq; | |
prescaleval -= 1; | |
Serial.print("Estimated pre-scale: "); Serial.println(prescaleval); | |
uint8_t prescale = floor(prescaleval + 0.5); | |
Serial.print("Final pre-scale: "); Serial.println(prescale); | |
uint8_t oldmode = read8(PCA9685_MODE1); | |
uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep | |
write8(PCA9685_MODE1, newmode); // go to sleep | |
write8(PCA9685_PRESCALE, prescale); // set the prescaler | |
write8(PCA9685_MODE1, oldmode); | |
delay(5); | |
write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment. | |
// This is why the beginTransmission below was not working. | |
// Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX); | |
} | |
uint8_t read8(uint8_t addr) { | |
WIRE.beginTransmission(_i2caddr); | |
#if ARDUINO >= 100 | |
WIRE.write(addr); | |
#else | |
WIRE.send(addr); | |
#endif | |
WIRE.endTransmission(); | |
WIRE.requestFrom((uint8_t)_i2caddr, (uint8_t)1); | |
#if ARDUINO >= 100 | |
return WIRE.read(); | |
#else | |
return WIRE.receive(); | |
#endif | |
} | |
void write8(uint8_t addr, uint8_t d) { | |
WIRE.beginTransmission(_i2caddr); | |
#if ARDUINO >= 100 | |
WIRE.write(addr); | |
WIRE.write(d); | |
#else | |
WIRE.send(addr); | |
WIRE.send(d); | |
#endif | |
WIRE.endTransmission(); | |
} | |
void setPWM(uint8_t num, uint16_t on, uint16_t off) { | |
WIRE.beginTransmission(_i2caddr); | |
WIRE.write(LED0_ON_L+4*num); | |
WIRE.write(on); | |
WIRE.write(on>>8); | |
WIRE.write(off); | |
WIRE.write(off>>8); | |
WIRE.endTransmission(); | |
} | |
void setPin(uint8_t pin, boolean value) { | |
if (value == LOW) | |
setPWM(pin, 0, 0); | |
else | |
setPWM(pin, 4096, 0); | |
} | |
void loop() { | |
//run it | |
setPWM(8, 150*16, 0); | |
setPin(9, LOW); // take low first to avoid 'break' | |
setPin(10, HIGH); | |
delay(1000);//pause | |
setPin(10, LOW); // take low first to avoid 'break' | |
setPin(9, HIGH); | |
delay(1000);//pause | |
//turn off | |
setPWM(8, 0, 0); | |
setPin(10, LOW); | |
setPin(9, LOW); | |
delay(5000);//pause | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment