编写程序使用 IIC 通信方式写入 EEPROM AT24C02 一段字符串,然后读出在 在 PC 机的串口调试助手上stm32f103
时间: 2024-01-22 21:19:01 浏览: 171
以下是使用STM32F103与AT24C02之间通过IIC通信的程序,实现向EEPROM中写入字符串,并通过USART串口将读取到的内容发送到PC机上的串口调试助手:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_usart.h"
#include "stdio.h"
#define I2C1_SLAVE_ADDRESS7 0xA0
void I2C_Configuration(void);
void USART_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
uint8_t buff[] = "Hello, World!";
uint8_t read_buff[20];
int i;
USART_Configuration();
I2C_Configuration();
/* Start Transfer */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Write to EEPROM */
for (i = 0; i < sizeof(buff); i++) {
I2C_SendData(I2C1, buff[i]);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send Stop */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Wait for EEPROM to finish writing */
Delay(0xFFFF);
/* Start Transfer */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS7, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* Read from EEPROM */
for (i = 0; i < sizeof(read_buff); i++) {
/* Test on EV7 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
read_buff[i] = I2C_ReceiveData(I2C1);
}
/* Send Stop */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Send received data to USART */
for (i = 0; i < sizeof(read_buff); i++) {
USART_SendData(USART1, read_buff[i]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
while(1) {}
}
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure I2C1 pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable I2C1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* I2C1 configuration */
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;
/* Apply I2C1 configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART1 pins: TX and RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 configuration */
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
/* Apply USART1 configuration after enabling it */
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
```
在串口调试助手中打开串口,波特率设置为115200,连接STM32F103的USART1引脚(PA9和PA10),运行程序,串口调试助手将会输出从EEPROM中读取到的字符串。
阅读全文