需要STM32L151 I2C驱动程序,使用V1.3.1库文件
时间: 2024-02-06 07:01:59 浏览: 81
基于STM32单片机的I2C驱动.zip
好的,下面是简单的I2C驱动程序,可以参考:
```c
#include "stm32l1xx.h"
/* I2C GPIO Configuration */
#define I2C_SCL_PIN GPIO_Pin_6
#define I2C_SDA_PIN GPIO_Pin_7
#define I2C_GPIO GPIOB
#define I2C_RCC_AHB RCC_AHBPeriph_GPIOB
/* I2C Configuration */
#define I2C_SPEED 100000
#define I2C_SLAVE_ADDRESS 0xA0
/* Private function prototypes */
static void I2C_GPIO_Configuration(void);
static void I2C_Configuration(void);
static void I2C_Read_Buffer(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
int main(void)
{
uint8_t buffer[10];
/* GPIO Configuration */
I2C_GPIO_Configuration();
/* I2C Configuration */
I2C_Configuration();
/* Read data from I2C device */
I2C_Read_Buffer(buffer, 0x00, 10);
while(1)
{
/* Do something */
}
}
static void I2C_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOB clock */
RCC_AHBPeriphClockCmd(I2C_RCC_AHB, ENABLE);
/* Configure I2C SCL pin */
GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(I2C_GPIO, &GPIO_InitStructure);
/* Configure I2C SDA pin */
GPIO_InitStructure.GPIO_Pin = I2C_SDA_PIN;
GPIO_Init(I2C_GPIO, &GPIO_InitStructure);
/* Connect I2C pins to AF */
GPIO_PinAFConfig(I2C_GPIO, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(I2C_GPIO, GPIO_PinSource7, GPIO_AF_I2C1);
}
static void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
/* Enable I2C clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS;
I2C_InitStructure.I2C_Speed = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStructure);
/* Enable I2C */
I2C_Cmd(I2C1, ENABLE);
}
static void I2C_Read_Buffer(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* Send start condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Wait for EV5 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send slave address */
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction_Transmitter);
/* Wait for EV6 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send register address */
I2C_SendData(I2C1, ReadAddr);
/* Wait for EV8 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send start condition again */
I2C_GenerateSTART(I2C1, ENABLE);
/* Wait for EV5 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send slave address again */
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction_Receiver);
/* Wait for EV6 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* Receive data */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable ACK */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send stop condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/* Wait for EV7 */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
/* Read data */
*pBuffer = I2C_ReceiveData(I2C1);
/* Next byte */
pBuffer++;
NumByteToRead--;
}
/* Enable ACK */
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
```
这是一个简单的I2C读取程序,使用了STM32L1xx标准库,可以读取从机地址为0xA0,寄存器地址为0x00,连续读取10个字节的数据并存储到buffer中。要使用此程序,需要将GPIOB的6、7号引脚配置为I2C模式,然后将I2C1的时钟使能,最后调用I2C_Read_Buffer函数即可。需要注意的是,此程序仅供参考,具体的I2C读写流程和错误处理需要根据具体情况进行调整。
阅读全文