Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ic2_test_bus by
PCA9685.cpp
00001 #include "PCA9685.h" 00002 #include "mbed.h" 00003 PCA9685::PCA9685(PinName sda, PinName scl, int addr) : i2c(sda, scl), _i2caddr(addr) {} 00004 00005 void PCA9685::begin(void) 00006 { 00007 reset(); 00008 } 00009 00010 void PCA9685::frequencyI2C(int freq) 00011 { 00012 i2c.frequency(freq); 00013 } 00014 void PCA9685::write8(uint8_t address, uint8_t data) 00015 { 00016 char cmd[2]; 00017 cmd[0] = address; 00018 cmd[1] = data; 00019 i2c.write(_i2caddr, cmd, 2); 00020 } 00021 00022 char PCA9685::read8(char address) 00023 { 00024 i2c.write(_i2caddr, &address, 1); 00025 char rtn; 00026 i2c.read(_i2caddr, &rtn, 1); 00027 return rtn; 00028 } 00029 00030 void PCA9685::reset(void) 00031 { 00032 write8(PCA9685_MODE1, 0x0); 00033 } 00034 void PCA9685::setPrescale(uint8_t prescale) { 00035 uint8_t oldmode = read8(PCA9685_MODE1); 00036 uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep 00037 write8(PCA9685_MODE1, newmode); // go to sleep 00038 wait_ms(5); 00039 write8(PCA9685_PRESCALE, prescale); // set the prescaler 00040 write8(PCA9685_MODE1, oldmode); 00041 wait_ms(5); 00042 write8(PCA9685_MODE1, oldmode | 0xa1); 00043 } 00044 void PCA9685::setPWMFreq(float freq) 00045 { 00046 float prescaleval = 25000000; 00047 prescaleval /= 4096; 00048 prescaleval /= freq; 00049 uint8_t prescale = floor(prescaleval + 0.5) - 1; 00050 setPrescale(prescale); 00051 } 00052 00053 void PCA9685::setPWM(uint8_t num, uint16_t on, uint16_t off) 00054 { 00055 char cmd[5]; 00056 cmd[0] = LED0_ON_L + 4 * num; 00057 cmd[1] = on; 00058 cmd[2] = on >> 8; 00059 cmd[3] = off; 00060 cmd[4] = off >> 8; 00061 i2c.write(_i2caddr, cmd, 5); 00062 } 00063 00064 void PCA9685::setPWM_ALL(uint16_t on, uint16_t off) 00065 { 00066 char cmd[5]; 00067 00068 for (uint8_t num = 0; num < 16; num++) 00069 { 00070 cmd[0] = LED0_ON_L + 4 * num; 00071 cmd[1] = on; 00072 cmd[2] = on >> 8; 00073 cmd[3] = off; 00074 cmd[4] = off >> 8; 00075 i2c.write(_i2caddr, cmd, 5); 00076 wait(0.0005); 00077 } 00078 00079 }
Generated on Thu Jul 14 2022 02:59:55 by
1.7.2
