format spaces to tabs
This commit is contained in:
+66
-66
@@ -4,33 +4,33 @@
|
|||||||
|
|
||||||
/* PCA9685 I2C address */
|
/* PCA9685 I2C address */
|
||||||
/* 0x40 is the 7-bit address. HAL requires it shifted left by 1 (0x80) */
|
/* 0x40 is the 7-bit address. HAL requires it shifted left by 1 (0x80) */
|
||||||
#define PCA9685_ADDR (0x41 << 1)
|
#define PCA9685_ADDR (0x41 << 1)
|
||||||
|
|
||||||
/* Register Addresses */
|
/* Register Addresses */
|
||||||
#define MODE1 0x00
|
#define MODE1 0x00
|
||||||
#define PRESCALE 0xFE
|
#define PRESCALE 0xFE
|
||||||
#define LED0_ON_L 0x06
|
#define LED0_ON_L 0x06
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper: Write a single byte to a register.
|
* Helper: Write a single byte to a register.
|
||||||
*/
|
*/
|
||||||
#define pca9685_write(reg, data) do \
|
#define pca9685_write(reg, data) do \
|
||||||
{ \
|
{ \
|
||||||
if (pca9685_write_i2c(reg, data) == false) \
|
if (pca9685_write_i2c(reg, data) == false) \
|
||||||
{ \
|
{ \
|
||||||
return false; \
|
return false; \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
static bool pca9685_write_i2c(uint8_t reg, uint8_t data)
|
static bool pca9685_write_i2c(uint8_t reg, uint8_t data)
|
||||||
{
|
{
|
||||||
return HAL_I2C_Mem_Write(&hi2c2,
|
return HAL_I2C_Mem_Write(&hi2c2,
|
||||||
PCA9685_ADDR,
|
PCA9685_ADDR,
|
||||||
reg,
|
reg,
|
||||||
I2C_MEMADD_SIZE_8BIT,
|
I2C_MEMADD_SIZE_8BIT,
|
||||||
&data,
|
&data,
|
||||||
1,
|
1,
|
||||||
50) == HAL_OK;
|
50) == HAL_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,20 +39,20 @@ static bool pca9685_write_i2c(uint8_t reg, uint8_t data)
|
|||||||
*/
|
*/
|
||||||
static void pca9685_write4(uint8_t reg, uint16_t on, uint16_t off)
|
static void pca9685_write4(uint8_t reg, uint16_t on, uint16_t off)
|
||||||
{
|
{
|
||||||
uint8_t buf[4];
|
uint8_t buf[4];
|
||||||
/* Split 12-bit values into Low and High bytes */
|
/* Split 12-bit values into Low and High bytes */
|
||||||
buf[0] = on & 0xFF; // ON_L
|
buf[0] = on & 0xFF; // ON_L
|
||||||
buf[1] = on >> 8; // ON_H
|
buf[1] = on >> 8; // ON_H
|
||||||
buf[2] = off & 0xFF; // OFF_L
|
buf[2] = off & 0xFF; // OFF_L
|
||||||
buf[3] = off >> 8; // OFF_H
|
buf[3] = off >> 8; // OFF_H
|
||||||
|
|
||||||
HAL_I2C_Mem_Write(&hi2c2,
|
HAL_I2C_Mem_Write(&hi2c2,
|
||||||
PCA9685_ADDR,
|
PCA9685_ADDR,
|
||||||
reg,
|
reg,
|
||||||
I2C_MEMADD_SIZE_8BIT,
|
I2C_MEMADD_SIZE_8BIT,
|
||||||
buf,
|
buf,
|
||||||
4,
|
4,
|
||||||
10);
|
10);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,32 +61,32 @@ static void pca9685_write4(uint8_t reg, uint16_t on, uint16_t off)
|
|||||||
*/
|
*/
|
||||||
bool PCA9685_Init(void)
|
bool PCA9685_Init(void)
|
||||||
{
|
{
|
||||||
HW_I2C2_Init();
|
HW_I2C2_Init();
|
||||||
|
|
||||||
/* 1. Reset device (Wake up) */
|
/* 1. Reset device (Wake up) */
|
||||||
pca9685_write(MODE1, 0x00);
|
pca9685_write(MODE1, 0x00);
|
||||||
HAL_Delay(10);
|
HAL_Delay(10);
|
||||||
|
|
||||||
/* 2. Setup PWM Frequency ~1000Hz */
|
/* 2. Setup PWM Frequency ~1000Hz */
|
||||||
/* Formula: prescale = 25MHz / (4096 * update_rate) - 1 */
|
/* Formula: prescale = 25MHz / (4096 * update_rate) - 1 */
|
||||||
/* For 1000Hz: 25000000 / (4096 * 1000) - 1 ~= 5 */
|
/* For 1000Hz: 25000000 / (4096 * 1000) - 1 ~= 5 */
|
||||||
uint8_t prescale = 5;
|
uint8_t prescale = 5;
|
||||||
|
|
||||||
/* To change pre-scaler, we must be in SLEEP mode */
|
/* To change pre-scaler, we must be in SLEEP mode */
|
||||||
pca9685_write(MODE1, 0x10); // Sleep mode
|
pca9685_write(MODE1, 0x10); // Sleep mode
|
||||||
HAL_Delay(1);
|
HAL_Delay(1);
|
||||||
|
|
||||||
pca9685_write(PRESCALE, prescale); // Write pre-scaler
|
pca9685_write(PRESCALE, prescale); // Write pre-scaler
|
||||||
|
|
||||||
/* 3. Wake up and set Auto-Increment */
|
/* 3. Wake up and set Auto-Increment */
|
||||||
/* 0xA1: 1010 0001
|
/* 0xA1: 1010 0001
|
||||||
* Bit 5 (0x20): Auto-Increment enabled (allows writing 4 bytes at once)
|
* Bit 5 (0x20): Auto-Increment enabled (allows writing 4 bytes at once)
|
||||||
* Bit 0 (0x01): ALLCALL enabled
|
* Bit 0 (0x01): ALLCALL enabled
|
||||||
*/
|
*/
|
||||||
pca9685_write(MODE1, 0xA1);
|
pca9685_write(MODE1, 0xA1);
|
||||||
HAL_Delay(5);
|
HAL_Delay(5);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,28 +97,28 @@ bool PCA9685_Init(void)
|
|||||||
*/
|
*/
|
||||||
void PCA9685_SetPWM(uint8_t channel, uint16_t on, uint16_t off)
|
void PCA9685_SetPWM(uint8_t channel, uint16_t on, uint16_t off)
|
||||||
{
|
{
|
||||||
/* Safety check */
|
/* Safety check */
|
||||||
if (channel > 15)
|
if (channel > 15)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* 12-bit limit check (4095 is max) */
|
/* 12-bit limit check (4095 is max) */
|
||||||
if (off > 4095)
|
if (off > 4095)
|
||||||
off = 4095;
|
off = 4095;
|
||||||
|
|
||||||
/* Calculate register address for this channel */
|
/* Calculate register address for this channel */
|
||||||
/* Each channel has 4 registers, starting at LED0_ON_L (0x06) */
|
/* Each channel has 4 registers, starting at LED0_ON_L (0x06) */
|
||||||
pca9685_write4(LED0_ON_L + 4 * channel, on, off);
|
pca9685_write4(LED0_ON_L + 4 * channel, on, off);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PCA9685_SetPin(uint8_t channel, uint8_t val)
|
void PCA9685_SetPin(uint8_t channel, uint8_t val)
|
||||||
{
|
{
|
||||||
if (channel > 15) return;
|
if (channel > 15) return;
|
||||||
|
|
||||||
if (val) {
|
if (val) {
|
||||||
// Full ON: Bit 4 в ON_H 1 - 4096, OFF в 0
|
// Full ON: Bit 4 в ON_H 1 - 4096, OFF в 0
|
||||||
pca9685_write4(LED0_ON_L + 4 * channel, 0x1000, 0x0000);
|
pca9685_write4(LED0_ON_L + 4 * channel, 0x1000, 0x0000);
|
||||||
} else {
|
} else {
|
||||||
// Full OFF: ON в 0, Bit 4 в OFF_H 1 - 4096
|
// Full OFF: ON в 0, Bit 4 в OFF_H 1 - 4096
|
||||||
pca9685_write4(LED0_ON_L + 4 * channel, 0x0000, 0x1000);
|
pca9685_write4(LED0_ON_L + 4 * channel, 0x0000, 0x1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user