STM32控制AT24C02代码
时间: 2023-12-20 22:37:40 浏览: 52
下面是一个使用STM32控制AT24C02 EEPROM的示例代码。该代码使用I2C总线进行通信,通过读写AT24C02的寄存器来实现数据存储和读取。
```c
#include "stm32f4xx.h"
#define EEPROM_ADDRESS 0xA0
#define I2C_TIMEOUT 10000
void I2C1_Init(void);
void I2C1_Start(void);
void I2C1_Stop(void);
void I2C1_Write(uint8_t data);
uint8_t I2C1_Read(uint8_t ack);
int main(void) {
uint8_t data[] = "Hello, AT24C02!";
uint8_t readData[16];
// 初始化I2C总线
I2C1_Init();
// 启动I2C总线并发送写命令和EEPROM地址
I2C1_Start();
I2C1_Write(EEPROM_ADDRESS);
I2C1_Write(0x00); // 写入数据的起始地址
// 写入数据
for (int i = 0; i < sizeof(data); i++) {
I2C1_Write(data[i]);
}
// 停止I2C总线
I2C1_Stop();
// 延时等待写入完成
for (int i = 0; i < 1000000; i++) {
__NOP();
}
// 启动I2C总线并发送读命令和EEPROM地址
I2C1_Start();
I2C1_Write(EEPROM_ADDRESS);
I2C1_Write(0x00); // 读取数据的起始地址
// 重启I2C总线并发送读命令
I2C1_Start();
I2C1_Write(EEPROM_ADDRESS | 0x01); // I2C总线地址后面加上读位
// 读取数据
for (int i = 0; i < sizeof(readData); i++) {
readData[i] = I2C1_Read(i == sizeof(readData) - 1 ? 0 : 1);
}
// 停止I2C总线
I2C1_Stop();
// 输出读取的数据
for (int i = 0; i < sizeof(readData); i++) {
printf("%c", readData[i]);
}
while (1) {
}
}
void I2C1_Init(void) {
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
// 配置GPIOB引脚为I2C模式
GPIOB->MODER |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1;
GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7;
GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7;
GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_0 | GPIO_PUPDR_PUPDR7_0;
GPIOB->AFR[0] |= (4 << (6 * 4)) | (4 << (7 * 4));
// 配置I2C1
I2C1->CR1 = 0;
I2C1->CR2 = 42; // APB1 clock = 42 MHz
I2C1->CCR = 210; // 100 kHz SCL frequency
I2C1->TRISE = 43;
I2C1->CR1 |= I2C_CR1_PE;
}
void I2C1_Start(void) {
I2C1->CR1 |= I2C_CR1_START;
while (!(I2C1->SR1 & I2C_SR1_SB))
;
(void)I2C1->SR1;
}
void I2C1_Stop(void) {
I2C1->CR1 |= I2C_CR1_STOP;
while (I2C1->CR1 & I2C_CR1_STOP)
;
}
void I2C1_Write(uint8_t data) {
I2C1->DR = data;
while (!(I2C1->SR1 & I2C_SR1_TXE))
;
}
uint8_t I2C1_Read(uint8_t ack) {
if (ack) {
I2C1->CR1 |= I2C_CR1_ACK;
} else {
I2C1->CR1 &= ~I2C_CR1_ACK;
}
while (!(I2C1->SR1 & I2C_SR1_RXNE))
;
return I2C1->DR;
}
```
这个示例代码使用了STM32的I2C模块(I2C1)来与AT24C02 EEPROM进行通信。首先,代码通过I2C总线向EEPROM写入数据,然后再从EEPROM中读取数据。请注意,代码中的I2C时钟频率和EEPROM地址需要根据实际情况进行调整。
这只是一个简单的示例代码,实际应用中可能需要根据具体情况进行修改和优化。另外,请确保正确连接STM32和AT24C02 EEPROM,并正确配置I2C总线的引脚和时钟频率。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)