LibDriver PCA9685  1.0.0
PCA9685 full-featured driver
driver_pca9685_write_test.c
Go to the documentation of this file.
1 
38 
39 static pca9685_handle_t gs_handle;
51 uint8_t pca9685_servo_write_test(pca9685_address_t addr, pca9685_channel_t channel, uint32_t times)
52 {
53  uint8_t res;
54  uint8_t reg;
55  uint16_t on_count, off_count;
56  uint32_t i;
57  pca9685_info_t info;
58 
59  /* link interface function */
70 
71  /* get information */
72  res = pca9685_info(&info);
73  if (res != 0)
74  {
75  pca9685_interface_debug_print("pca9685: get info failed.\n");
76 
77  return 1;
78  }
79  else
80  {
81  /* print chip info */
82  pca9685_interface_debug_print("pca9685: chip is %s.\n", info.chip_name);
83  pca9685_interface_debug_print("pca9685: manufacturer is %s.\n", info.manufacturer_name);
84  pca9685_interface_debug_print("pca9685: interface is %s.\n", info.interface);
85  pca9685_interface_debug_print("pca9685: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
86  pca9685_interface_debug_print("pca9685: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
87  pca9685_interface_debug_print("pca9685: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
88  pca9685_interface_debug_print("pca9685: max current is %0.2fmA.\n", info.max_current_ma);
89  pca9685_interface_debug_print("pca9685: max temperature is %0.1fC.\n", info.temperature_max);
90  pca9685_interface_debug_print("pca9685: min temperature is %0.1fC.\n", info.temperature_min);
91  }
92 
93  /* start write test */
94  pca9685_interface_debug_print("pca9685: start write test.\n");
95 
96  /* set addr pin */
97  res = pca9685_set_addr_pin(&gs_handle, addr);
98  if (res != 0)
99  {
100  pca9685_interface_debug_print("pca9685: set addr pin failed.\n");
101 
102  return 1;
103  }
104 
105  /* pca9685 init */
106  res = pca9685_init(&gs_handle);
107  if (res != 0)
108  {
109  pca9685_interface_debug_print("pca9685: init failed.\n");
110 
111  return 1;
112  }
113 
114  /* inactive */
115  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_FALSE);
116  if (res != 0)
117  {
118  pca9685_interface_debug_print("pca9685: set active failed.\n");
119  (void)pca9685_deinit(&gs_handle);
120 
121  return 1;
122  }
123 
124  /* set sleep mode */
125  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_TRUE);
126  if (res != 0)
127  {
128  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
129  (void)pca9685_deinit(&gs_handle);
130 
131  return 1;
132  }
133 
134  /* set 50Hz */
136  if (res != 0)
137  {
138  pca9685_interface_debug_print("pca9685: output frequency convert to register failed.\n");
139  (void)pca9685_deinit(&gs_handle);
140 
141  return 1;
142  }
143 
144  /* set pre scale */
145  res = pca9685_set_prescaler(&gs_handle, reg);
146  if (res != 0)
147  {
148  pca9685_interface_debug_print("pca9685: set pre scale failed.\n");
149  (void)pca9685_deinit(&gs_handle);
150 
151  return 1;
152  }
153 
154  /* disable external clock pin */
156  if (res != 0)
157  {
158  pca9685_interface_debug_print("pca9685: set external clock pin failed.\n");
159  (void)pca9685_deinit(&gs_handle);
160 
161  return 1;
162  }
163 
164  /* enable auto increment */
166  if (res != 0)
167  {
168  pca9685_interface_debug_print("pca9685: set register auto increment failed.\n");
169  (void)pca9685_deinit(&gs_handle);
170 
171  return 1;
172  }
173 
174  /* disable respond sub address 1 */
176  if (res != 0)
177  {
178  pca9685_interface_debug_print("pca9685: set respond sub address 1 failed.\n");
179  (void)pca9685_deinit(&gs_handle);
180 
181  return 1;
182  }
183 
184  /* disable respond sub address 2 */
186  if (res != 0)
187  {
188  pca9685_interface_debug_print("pca9685: set respond sub address 2 failed.\n");
189  (void)pca9685_deinit(&gs_handle);
190 
191  return 1;
192  }
193 
194  /* disable respond sub address 3 */
196  if (res != 0)
197  {
198  pca9685_interface_debug_print("pca9685: set respond sub address 3 failed.\n");
199  (void)pca9685_deinit(&gs_handle);
200 
201  return 1;
202  }
203 
204  /* disable respond all call */
206  if (res != 0)
207  {
208  pca9685_interface_debug_print("pca9685: set respond all call failed.\n");
209  (void)pca9685_deinit(&gs_handle);
210 
211  return 1;
212  }
213 
214  /* disable output invert */
216  if (res != 0)
217  {
218  pca9685_interface_debug_print("pca9685: set output invert failed.\n");
219  (void)pca9685_deinit(&gs_handle);
220 
221  return 1;
222  }
223 
224  /* stop output change */
226  if (res != 0)
227  {
228  pca9685_interface_debug_print("pca9685: set output change failed.\n");
229  (void)pca9685_deinit(&gs_handle);
230 
231  return 1;
232  }
233 
234  /* totem pole driver */
236  if (res != 0)
237  {
238  pca9685_interface_debug_print("pca9685: set output driver failed.\n");
239  (void)pca9685_deinit(&gs_handle);
240 
241  return 1;
242  }
243 
244  /* high impedance */
246  if (res != 0)
247  {
248  pca9685_interface_debug_print("pca9685: set output disable type failed.\n");
249  (void)pca9685_deinit(&gs_handle);
250 
251  return 1;
252  }
253 
254  /* set sleep mode */
255  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_FALSE);
256  if (res != 0)
257  {
258  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
259  (void)pca9685_deinit(&gs_handle);
260 
261  return 1;
262  }
263 
264  /* active */
265  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_TRUE);
266  if (res != 0)
267  {
268  pca9685_interface_debug_print("pca9685: set active failed.\n");
269  (void)pca9685_deinit(&gs_handle);
270 
271  return 1;
272  }
273 
274  /* output */
275  for (i = 1; i < times + 1; i++)
276  {
277  /* convert data */
278  res = pca9685_pwm_convert_to_register(&gs_handle, 0.0f, 2.5f + (float)(i) / (float)(times) * 10.0f,
279  (uint16_t *)&on_count, (uint16_t *)&off_count);
280  if (res != 0)
281  {
282  pca9685_interface_debug_print("pca9685: convert to register failed.\n");
283  (void)pca9685_deinit(&gs_handle);
284 
285  return 1;
286  }
287 
288  /* write channel */
289  res = pca9685_write_channel(&gs_handle, channel, on_count, off_count);
290  if (res != 0)
291  {
292  pca9685_interface_debug_print("pca9685: write channel failed.\n");
293  (void)pca9685_deinit(&gs_handle);
294 
295  return 1;
296  }
297 
298  /* output data */
299  pca9685_interface_debug_print("pca9685: set channel %d %0.2f degrees.\n", channel, (float)(i) / (float)(times) * 180.0f);
300 
301  /* delay 1000 ms */
303  }
304 
305  /* output */
306  for (i = 1; i < times + 1; i++)
307  {
308  /* convert data */
309  res = pca9685_pwm_convert_to_register(&gs_handle, 0.0f, 2.5f + (float)(i) / (float)(times) * 10.0f,
310  (uint16_t *)&on_count, (uint16_t *)&off_count);
311  if (res != 0)
312  {
313  pca9685_interface_debug_print("pca9685: convert to register failed.\n");
314  (void)pca9685_deinit(&gs_handle);
315 
316  return 1;
317  }
318 
319  /* write all channel */
320  res = pca9685_write_all_channel(&gs_handle, on_count, off_count);
321  if (res != 0)
322  {
323  pca9685_interface_debug_print("pca9685: write all channel failed.\n");
324  (void)pca9685_deinit(&gs_handle);
325 
326  return 1;
327  }
328 
329  /* output data */
330  pca9685_interface_debug_print("pca9685: set all channel %0.2f degrees.\n", (float)(i) / (float)(times) * 180.0f);
331 
332  /* delay 1000 ms */
334  }
335 
336  /* inactive */
337  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_FALSE);
338  if (res != 0)
339  {
340  pca9685_interface_debug_print("pca9685: set active failed.\n");
341  (void)pca9685_deinit(&gs_handle);
342 
343  return 1;
344  }
345 
346  /* finish write test */
347  pca9685_interface_debug_print("pca9685: finish write test.\n");
348  (void)pca9685_deinit(&gs_handle);
349 
350  return 0;
351 }
driver pca9685 write test header file
uint8_t pca9685_pwm_convert_to_register(pca9685_handle_t *handle, float delay_percent, float high_duty_cycle_percent, uint16_t *on_count, uint16_t *off_count)
convert the pwm to the register raw data
uint8_t pca9685_set_prescaler(pca9685_handle_t *handle, uint8_t prescaler)
set the clock pres cale
uint8_t pca9685_write_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t on_count, uint16_t off_count)
write led channels
uint8_t pca9685_write_all_channel(pca9685_handle_t *handle, uint16_t on_count, uint16_t off_count)
write all led channels
pca9685_address_t
pca9685 address enumeration definition
uint8_t pca9685_set_respond_all_call(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond all call
uint8_t pca9685_init(pca9685_handle_t *handle)
initialize the chip
uint8_t pca9685_set_active(pca9685_handle_t *handle, pca9685_bool_t enable)
set the chip active
uint8_t pca9685_set_addr_pin(pca9685_handle_t *handle, pca9685_address_t addr_pin)
set the address pin
uint8_t pca9685_set_output_driver(pca9685_handle_t *handle, pca9685_output_driver_t driver)
set the output driver type
uint8_t pca9685_set_respond_subaddress_2(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 2
uint8_t pca9685_set_output_disable_type(pca9685_handle_t *handle, pca9685_output_disable_type_t type)
set the output disable type
uint8_t pca9685_output_frequency_convert_to_register(pca9685_handle_t *handle, uint32_t oscillator, uint16_t output_freq, uint8_t *reg)
convert the output frequency to the register raw data
uint8_t pca9685_set_register_auto_increment(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the register auto increment
uint8_t pca9685_set_respond_subaddress_3(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 3
uint8_t pca9685_info(pca9685_info_t *info)
get chip's information
uint8_t pca9685_set_output_invert(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable output invert
pca9685_channel_t
pca9685 channel enumeration definition
#define PCA9685_OSCILLATOR_INTERNAL_FREQUENCY
pca9685 internal oscillator frequency
uint8_t pca9685_set_sleep_mode(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the sleep mode
uint8_t pca9685_set_external_clock_pin(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the external clock pin
uint8_t pca9685_deinit(pca9685_handle_t *handle)
close the chip
uint8_t pca9685_set_respond_subaddress_1(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 1
uint8_t pca9685_set_output_change(pca9685_handle_t *handle, pca9685_output_change_t change)
set the output change type
@ PCA9685_BOOL_FALSE
@ PCA9685_BOOL_TRUE
@ PCA9685_OUTPUT_DISABLE_TYPE_HIGH_IMPEDANCE
@ PCA9685_OUTPUT_DRIVER_TOTEM_POLE
@ PCA9685_OUTPUT_CHANGE_STOP
void pca9685_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t pca9685_interface_oe_deinit(void)
interface oe deinit
uint8_t pca9685_interface_oe_init(void)
interface oe init
uint8_t pca9685_interface_iic_deinit(void)
interface iic bus deinit
uint8_t pca9685_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t pca9685_interface_iic_init(void)
interface iic bus init
void pca9685_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pca9685_interface_oe_write(uint8_t value)
interface oe write
uint8_t pca9685_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t pca9685_servo_write_test(pca9685_address_t addr, pca9685_channel_t channel, uint32_t times)
servo write test
pca9685 handle structure definition
pca9685 information structure definition
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]