帮我写出基于STM32F103 ,AT24C16程序驱动,C语言实现
时间: 2023-05-24 13:04:05 浏览: 352
好的,下面是基于STM32F103和AT24C16的程序驱动的C语言实现:
首先,需要在STM32F103芯片上设置I2C设备和GPIO口,以下是初始化代码:
```
#include "stm32f10x.h"
#include "i2c.h"
#include "at24c16.h"
#define SCL_PIN GPIO_Pin_6
#define SCL_PORT GPIOB
#define SDA_PIN GPIO_Pin_7
#define SDA_PORT GPIOB
void I2C_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Pin = SCL_PIN | SDA_PIN;
GPIO_Init(SCL_PORT, &GPIO_InitStructure);
}
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
```
接着,需要初始化AT24C16芯片并实现相应的读写操作,以下是AT24C16的初始化和读写代码:
```
void AT24C16_Init(void)
{
I2C_Configuration();
I2C_GPIO_Init();
}
void AT24C16_WriteByte(uint16_t addr, uint8_t data)
{
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, AT24C16_ADDRESS, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (uint8_t)(addr >> 8));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(addr & 0xFF));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
uint8_t AT24C16_ReadByte(uint16_t addr)
{
uint8_t data = 0;
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, AT24C16_ADDRESS, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, (uint8_t)(addr >> 8));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(addr & 0xFF));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, AT24C16_ADDRESS, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
data = I2C_ReceiveData(I2C1);
return data;
}
```
最终,在Main函数中,可以进行AT24C16的读写测试,以下是示例代码:
```
int main(void)
{
uint8_t write_data = 0x01;
uint8_t read_data = 0;
uint16_t addr = 0x001;
SysTick_Init();
AT24C16_Init();
AT24C16_WriteByte(addr, write_data);
SysTick_DelayMs(20);
read_data = AT24C16_ReadByte(addr);
while(1)
{
// do something
}
}
```
以上是基于STM32F103和AT24C16的程序驱动的C语言实现,希望对你有帮助。
阅读全文