stm32f103c8t6 iic24c16
时间: 2023-12-13 10:00:40 浏览: 134
STM32F103C8T6是一种基于ARM Cortex-M3内核的单片机,拥有丰富的外设和高性能的处理能力。它具有诸如GPIO、USART、SPI、I2C等多种通信接口,以及计时器、PWM、ADC等功能模块,适用于各种嵌入式应用场景。
24C16是一种串行EEPROM芯片,容量为16K bits。它采用I2C总线协议进行通信,可以通过I2C接口与STM32F103C8T6进行数据交互。通过I2C总线,STM32F103C8T6可以读取或写入24C16芯片中的数据,实现数据存储和传输的功能。
在连接STM32F103C8T6和24C16时,首先需要将I2C总线的SCL和SDA引脚分别连接到对应的引脚上。然后在STM32F103C8T6的软件代码中配置I2C外设,并设置好通信参数,比如通信速度、设备地址等。接下来,可以使用I2C接口的相应函数读取或写入24C16芯片中的数据。
总之,STM32F103C8T6和24C16是一对常用的嵌入式系统组合,通过I2C总线实现数据交互。通过编程控制STM32F103C8T6与24C16之间的通信,可以实现数据的读取、存储和传输等功能。
相关问题
stm32f103c8t6 硬件iic驱动及at24c02读写
1. 硬件IIC驱动
STM32F103C8T6内置硬件I2C接口(IIC),可以通过配置寄存器来实现IIC通讯。以下是一个简单的硬件IIC驱动程序示例,用于向一个从设备(例如AT24C02 EEPROM)写入和读取一个字节。
首先需要配置I2C的时钟和GPIO引脚,如下所示:
```
//配置I2C时钟和GPIO引脚
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
```
然后需要初始化I2C,设置通讯速率、地址等参数:
```
//初始化I2C
I2C_InitTypeDef I2C_InitStructure;
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);
```
接下来就可以使用I2C驱动程序向AT24C02 EEPROM写入和读取数据了。以下是一个简单的示例:
```
//向AT24C02 EEPROM写入一个字节
void I2C_WriteByte(uint8_t deviceAddr, uint8_t regAddr, uint8_t data)
{
//发送开始信号
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//发送设备地址(写模式)
I2C_Send7bitAddress(I2C1, deviceAddr, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//发送寄存器地址
I2C_SendData(I2C1, regAddr);
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);
}
//从AT24C02 EEPROM读取一个字节
uint8_t I2C_ReadByte(uint8_t deviceAddr, uint8_t regAddr)
{
uint8_t data = 0;
//发送开始信号
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
//发送设备地址(写模式)
I2C_Send7bitAddress(I2C1, deviceAddr, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//发送寄存器地址
I2C_SendData(I2C1, regAddr);
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, deviceAddr, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//读取数据
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
data = I2C_ReceiveData(I2C1);
//发送停止信号
I2C_GenerateSTOP(I2C1, ENABLE);
return data;
}
```
2. AT24C02读写
AT24C02是一种2Kbit(256字节)I2C EEPROM,可以通过I2C接口进行读写。以下是一个简单的示例,用于向AT24C02写入和读取一个字节。
首先需要定义AT24C02的设备地址和寄存器地址:
```
#define AT24C02_ADDR 0xA0 //设备地址
#define AT24C02_REG_ADDR 0x00 //寄存器地址
```
然后可以使用上面的I2C驱动程序来向AT24C02写入和读取数据了。以下是一个简单的示例:
```
//向AT24C02写入一个字节
void AT24C02_WriteByte(uint8_t data)
{
I2C_WriteByte(AT24C02_ADDR, AT24C02_REG_ADDR, data);
}
//从AT24C02读取一个字节
uint8_t AT24C02_ReadByte(void)
{
return I2C_ReadByte(AT24C02_ADDR, AT24C02_REG_ADDR);
}
```
stm32f103c8t6 模拟iIC驱动at24c02 和模拟spi 驱动的w25q64代码
以下是stm32f103c8t6模拟iIC驱动at24c02和模拟spi驱动的w25q64的代码示例:
模拟iIC驱动at24c02代码:
```
#include "stm32f10x.h"
#define ADDR_AT24C02 0xA0
void I2C1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
}
void I2C1_Mode_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_DeInit(I2C1);
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);
}
uint8_t I2C1_WriteByte(uint8_t dev_addr, uint8_t reg_addr, uint8_t data)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, dev_addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg_addr);
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);
return 0;
}
uint8_t I2C1_ReadByte(uint8_t dev_addr, uint8_t reg_addr)
{
uint8_t data;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, dev_addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg_addr);
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, dev_addr, 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;
}
void AT24C02_Write(uint8_t addr, uint8_t data)
{
I2C1_WriteByte(ADDR_AT24C02, addr, data);
}
uint8_t AT24C02_Read(uint8_t addr)
{
return I2C1_ReadByte(ADDR_AT24C02, addr);
}
int main()
{
I2C1_GPIO_Config();
I2C1_Mode_Config();
uint8_t data = 0x55;
AT24C02_Write(0x00, data);
data = AT24C02_Read(0x00);
while (1);
}
```
模拟spi驱动的w25q64代码:
```
#include "stm32f10x.h"
#include "spi.h"
#include "w25q64.h"
void W25Q64_Init(void)
{
SPI2_Init();
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
uint8_t W25Q64_ReadSR1(void)
{
uint8_t cmd = W25Q64_CMD_READ_SR1;
uint8_t sr1;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
sr1 = SPI2_ReadByte();
GPIO_SetBits(GPIOB, GPIO_Pin_12);
return sr1;
}
void W25Q64_WriteEnable(void)
{
uint8_t cmd = W25Q64_CMD_WRITE_ENABLE;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void W25Q64_EraseChip(void)
{
uint8_t cmd = W25Q64_CMD_ERASE_CHIP;
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
while (W25Q64_ReadSR1() & 0x01);
}
void W25Q64_WritePage(uint32_t addr, uint8_t *data, uint16_t len)
{
uint8_t cmd[4];
uint32_t i;
cmd[0] = W25Q64_CMD_PAGE_PROGRAM;
cmd[1] = (addr >> 16) & 0xFF;
cmd[2] = (addr >> 8) & 0xFF;
cmd[3] = addr & 0xFF;
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
for (i = 0; i < 4; i++) {
SPI2_WriteByte(cmd[i]);
}
for (i = 0; i < len; i++) {
SPI2_WriteByte(data[i]);
}
GPIO_SetBits(GPIOB, GPIO_Pin_12);
while (W25Q64_ReadSR1() & 0x01);
}
void W25Q64_ReadData(uint32_t addr, uint8_t *data, uint16_t len)
{
uint8_t cmd[4];
uint32_t i;
cmd[0] = W25Q64_CMD_READ_DATA;
cmd[1] = (addr >> 16) & 0xFF;
cmd[2] = (addr >> 8) & 0xFF;
cmd[3] = addr & 0xFF;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
for (i = 0; i < 4; i++) {
SPI2_WriteByte(cmd[i]);
}
for (i = 0; i < len; i++) {
data[i] = SPI2_ReadByte();
}
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
int main()
{
W25Q64_Init();
uint8_t data[256];
uint32_t i;
for (i = 0; i < 256; i++) {
data[i] = i;
}
W25Q64_EraseChip();
for (i = 0; i < 65536; i += 256) {
W25Q64_WritePage(i, data, 256);
}
for (i = 0; i < 65536; i += 256) {
W25Q64_ReadData(i, data, 256);
/* do something with data */
}
while (1);
}
```
阅读全文