cx32l003_i2c.c 函数
时间: 2024-03-15 08:44:38 浏览: 32
下面是CX32L003官方提供的标准外设库中cx32l003_i2c.c文件中常用的几个函数的示例代码:
```
#include "cx32l003_i2c.h"
void I2C_DeInit(I2C_TypeDef* I2Cx)
{
// 复位I2C外设
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
}
void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
{
uint32_t tmpreg = 0;
uint32_t freqrange = 0;
uint32_t pclk1 = 0;
// 使能I2C外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// 根据频率计算I2C时钟周期和I2C预设的分频系数
freqrange = RCC_GetPCLK1Freq() / 1000000;
tmpreg = (uint16_t)(freqrange);
I2Cx->IC_TPR = tmpreg & 0xFFFF;
// 配置I2C总线时钟速度
pclk1 = RCC_GetPCLK1Freq();
tmpreg = (pclk1 / (I2C_InitStruct->I2C_ClockSpeed << 1));
I2Cx->IC_CCR = tmpreg & 0xFFF;
// 配置I2C工作模式、ACK、地址等参数
tmpreg = 0;
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_Mode);
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_DutyCycle);
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_Ack);
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_AcknowledgedAddress);
I2Cx->IC_CON = tmpreg;
// 配置I2C自身的地址
tmpreg = 0;
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_OwnAddress1) << 1;
tmpreg |= (uint32_t)(I2C_InitStruct->I2C_AcknowledgedAddress);
I2Cx->IC_OAR1 = tmpreg;
// 使能I2C总线
I2C_Cmd(I2C1, ENABLE);
}
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
if (NewState != DISABLE)
{
// 使能I2C总线
I2Cx->IC_CON |= I2C_CON_EN;
}
else
{
// 禁用I2C总线
I2Cx->IC_CON &= ~I2C_CON_EN;
}
}
void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
{
// 将数据写入I2C数据寄存器
I2Cx->IC_TxData = Data;
}
uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
// 从I2C数据寄存器读取数据
return (uint8_t)(I2Cx->IC_RxData);
}
```
在上面的代码中,我们定义了几个常用的函数,包括I2C外设的复位、初始化、使能/禁用、发送数据和接收数据。这些函数可以方便地进行I2C总线的操作,具体使用方法可以参考CX32L003官方提供的标准外设库手册。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)