📄 stm32f10x_i2c.lst
字号:
221 }
222 /* Write to I2Cx CCR */
223 I2Cx->CCR = tmpreg;
224
225 /* Enable I2Cx */
226 I2C_Cmd(I2Cx, ENABLE);
227
228 /*---------------------------- I2Cx CR1 Configuration ------------------------*/
229 /* Get the I2Cx CR1 value */
230 tmpreg = I2Cx->CR1;
231 /* Clear ACK, SMBTYPE and SMBUS bits */
232 tmpreg &= CR1_CLEAR_Mask;
233 /* Configure I2Cx: mode and acknowledgement */
234 /* Set SMBTYPE and SMBUS bits according to I2C_Mode value */
235 /* Set ACK bit according to I2C_Ack value */
236 tmpreg |= (u16)((u32)I2C_InitStruct->I2C_Mode | I2C_InitStruct->I2C_Ack);
237 /* Write to I2Cx CR1 */
238 I2Cx->CR1 = tmpreg;
239
240 /*---------------------------- I2Cx OAR1 Configuration -----------------------*/
241 /* Set I2Cx Own Address1 and acknowledged address */
242 I2Cx->OAR1 = (I2C_InitStruct->I2C_AcknowledgedAddress | I2C_InitStruct->I2C_OwnAddress1);
243 }
244
245 /*******************************************************************************
246 * Function Name : I2C_StructInit
247 * Description : Fills each I2C_InitStruct member with its default value.
248 * Input : - I2C_InitStruct: pointer to an I2C_InitTypeDef structure
249 * which will be initialized.
250 * Output : None
251 * Return : None
252 *******************************************************************************/
253 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
254 {
255 /*---------------- Reset I2C init structure parameters values ----------------*/
256 /* Initialize the I2C_Mode member */
257 I2C_InitStruct->I2C_Mode = I2C_Mode_I2C;
258
259 /* Initialize the I2C_DutyCycle member */
260 I2C_InitStruct->I2C_DutyCycle = I2C_DutyCycle_2;
261
262 /* Initialize the I2C_OwnAddress1 member */
263 I2C_InitStruct->I2C_OwnAddress1 = 0;
264
265 /* Initialize the I2C_Ack member */
266 I2C_InitStruct->I2C_Ack = I2C_Ack_Disable;
267
268 /* Initialize the I2C_AcknowledgedAddress member */
269 I2C_InitStruct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
270
271 /* initialize the I2C_ClockSpeed member */
272 I2C_InitStruct->I2C_ClockSpeed = 5000;
273 }
274
275 /*******************************************************************************
276 * Function Name : I2C_Cmd
277 * Description : Enables or disables the specified I2C peripheral.
278 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
279 * - NewState: new state of the I2Cx peripheral. This parameter
280 * can be: ENABLE or DISABLE.
281 * Output : None
282 * Return : None
283 *******************************************************************************/
284 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
285 {
286 /* Check the parameters */
287 assert_param(IS_FUNCTIONAL_STATE(NewState));
288
289 if (NewState != DISABLE)
290 {
291 /* Enable the selected I2C peripheral */
292 I2Cx->CR1 |= CR1_PE_Set;
293 }
294 else
295 {
296 /* Disable the selected I2C peripheral */
297 I2Cx->CR1 &= CR1_PE_Reset;
298 }
299 }
300
301 /*******************************************************************************
302 * Function Name : I2C_DMACmd
303 * Description : Enables or disables the specified I2C DMA requests.
304 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
305 * - NewState: new state of the I2C DMA transfer.
306 * This parameter can be: ENABLE or DISABLE.
307 * Output : None
308 * Return : None
309 *******************************************************************************/
310 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
311 {
312 /* Check the parameters */
313 assert_param(IS_FUNCTIONAL_STATE(NewState));
314
315 if (NewState != DISABLE)
316 {
317 /* Enable the selected I2C DMA requests */
318 I2Cx->CR2 |= CR2_DMAEN_Set;
319 }
320 else
321 {
322 /* Disable the selected I2C DMA requests */
323 I2Cx->CR2 &= CR2_DMAEN_Reset;
324 }
325 }
326
327 /*******************************************************************************
328 * Function Name : I2C_DMALastTransferCmd
329 * Description : Specifies that the next DMA transfer is the last one.
330 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
331 * - NewState: new state of the I2C DMA last transfer.
332 * This parameter can be: ENABLE or DISABLE.
333 * Output : None
334 * Return : None
335 *******************************************************************************/
336 void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
337 {
338 /* Check the parameters */
339 assert_param(IS_FUNCTIONAL_STATE(NewState));
340
341 if (NewState != DISABLE)
342 {
343 /* Next DMA end of transfer is the last transfer */
344 I2Cx->CR2 |= CR2_LAST_Set;
345 }
346 else
347 {
348 /* Next DMA end of transfer is not the last transfer */
349 I2Cx->CR2 &= CR2_LAST_Reset;
350 }
351 }
352
353 /*******************************************************************************
354 * Function Name : I2C_GenerateSTART
355 * Description : Generates I2Cx communication START condition.
356 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
357 * - NewState: new state of the I2C START condition generation.
358 * This parameter can be: ENABLE or DISABLE.
359 * Output : None
360 * Return : None.
361 *******************************************************************************/
362 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
363 {
364 /* Check the parameters */
365 assert_param(IS_FUNCTIONAL_STATE(NewState));
366
367 if (NewState != DISABLE)
368 {
369 /* Generate a START condition */
370 I2Cx->CR1 |= CR1_START_Set;
371 }
372 else
373 {
374 /* Disable the START condition generation */
375 I2Cx->CR1 &= CR1_START_Reset;
376 }
377 }
378
379 /*******************************************************************************
380 * Function Name : I2C_GenerateSTOP
381 * Description : Generates I2Cx communication STOP condition.
382 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
383 * - NewState: new state of the I2C STOP condition generation.
384 * This parameter can be: ENABLE or DISABLE.
385 * Output : None
386 * Return : None.
387 *******************************************************************************/
388 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
389 {
390 /* Check the parameters */
391 assert_param(IS_FUNCTIONAL_STATE(NewState));
392
393 if (NewState != DISABLE)
394 {
395 /* Generate a STOP condition */
396 I2Cx->CR1 |= CR1_STOP_Set;
397 }
398 else
399 {
400 /* Disable the STOP condition generation */
401 I2Cx->CR1 &= CR1_STOP_Reset;
402 }
403 }
404
405 /*******************************************************************************
406 * Function Name : I2C_AcknowledgeConfig
407 * Description : Enables or disables the specified I2C acknowledge feature.
408 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
409 * - NewState: new state of the I2C Acknowledgement.
410 * This parameter can be: ENABLE or DISABLE.
411 * Output : None
412 * Return : None.
413 *******************************************************************************/
414 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
415 {
416 /* Check the parameters */
417 assert_param(IS_FUNCTIONAL_STATE(NewState));
418
419 if (NewState != DISABLE)
420 {
421 /* Enable the acknowledgement */
422 I2Cx->CR1 |= CR1_ACK_Set;
423 }
424 else
425 {
426 /* Disable the acknowledgement */
427 I2Cx->CR1 &= CR1_ACK_Reset;
428 }
429 }
430
431 /*******************************************************************************
432 * Function Name : I2C_OwnAddress2Config
433 * Description : Configures the specified I2C own address2.
434 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
435 * - Address: specifies the 7bit I2C own address2.
436 * Output : None
437 * Return : None.
438 *******************************************************************************/
439 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, u8 Address)
440 {
441 u16 tmpreg = 0;
442
443 /* Get the old register value */
444 tmpreg = I2Cx->OAR2;
445 /* Reset I2Cx Own address2 bit [7:1] */
446 tmpreg &= OAR2_ADD2_Reset;
447 /* Set I2Cx Own address2 */
448 tmpreg |= (u16)(Address & (u16)0x00FE);
449 /* Store the new register value */
450 I2Cx->OAR2 = tmpreg;
451 }
452
453 /*******************************************************************************
454 * Function Name : I2C_DualAddressCmd
455 * Description : Enables or disables the specified I2C dual addressing mode.
456 * Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
457 * - NewState: new state of the I2C dual addressing mode.
458 * This parameter can be: ENABLE or DISABLE.
459 * Output : None
460 * Return : None
461 *******************************************************************************/
462 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
463 {
464 /* Check the parameters */
465 assert_param(IS_FUNCTIONAL_STATE(NewState));
466
467 if (NewState != DISABLE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -