STM32F407使用PB9和PB10的I2C初始化并使用DMA进行数据收发的完整代码,不使用HAL库,使用标准库
时间: 2024-05-06 16:15:27 浏览: 97
以下是在STM32F407平台上使用PB9和PB10的I2C初始化并使用DMA进行数据收发的完整代码,不使用HAL库,使用标准库。
首先,需要包含头文件:
```c
#include "stm32f4xx.h"
#include <stdio.h>
```
接着,定义I2C的时钟频率和从机地址:
```c
#define I2C_SPEED 100000 // I2C时钟频率为100KHz
#define I2C_SLAVE_ADDRESS 0x27 // 从机地址为0x27
```
然后,定义初始化I2C的函数:
```c
void I2C_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 使能GPIOB时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
// 配置GPIOB的PB9和PB10引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // 复用模式
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // GPIO速度为50MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // 开漏输出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // 上拉电阻
GPIO_Init(GPIOB, &GPIO_InitStruct);
// 配置GPIOB的PB9和PB10引脚为I2C功能
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C1);
// 使能I2C1时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// I2C配置
I2C_InitStruct.I2C_ClockSpeed = I2C_SPEED; // I2C时钟频率
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C模式
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 占空比为2
I2C_InitStruct.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS; // 从机地址
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; // 确认信号使能
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // 从机地址长度
I2C_Init(I2C1, &I2C_InitStruct);
// 使能I2C DMA传输
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_InitStruct.DMA_Channel = DMA_Channel_1; // DMA通道1
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t) &(I2C1->DR); // I2C数据寄存器地址
DMA_InitStruct.DMA_Memory0BaseAddr = 0; // 内存地址
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; // DMA传输方向,从外设到内存
DMA_InitStruct.DMA_BufferSize = 0; // DMA缓存大小
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 禁止外设地址自增
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; // 允许内存地址自增
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; // 外设数据宽度为1字节
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // 内存数据宽度为1字节
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal; // 普通模式
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh; // 优先级为最高
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; // 禁止FIFO模式
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; // FIFO阈值
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; // 内存突发传输
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; // 外设突发传输
DMA_Init(DMA1_Stream0, &DMA_InitStruct);
// 使能DMA传输完成中断
DMA_ITConfig(DMA1_Stream0, DMA_IT_TC, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Stream0_IRQn; // DMA1 Stream0中断
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级为0
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; // 子优先级为0
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // 使能DMA1 Stream0中断
NVIC_Init(&NVIC_InitStruct);
// 使能I2C1
I2C_Cmd(I2C1, ENABLE);
}
```
接下来,定义I2C数据传输的函数:
```c
void I2C_Transfer(uint8_t *data, uint16_t length, uint8_t direction)
{
// 关闭DMA1 Stream0
DMA_Cmd(DMA1_Stream0, DISABLE);
// 设置DMA1 Stream0的外设地址和内存地址
DMA_SetCurrDataCounter(DMA1_Stream0, length);
DMA_MemoryTargetConfig(DMA1_Stream0, (uint32_t) data, direction);
// 使能DMA1 Stream0
DMA_Cmd(DMA1_Stream0, ENABLE);
}
```
最后,定义DMA1 Stream0的中断处理函数:
```c
void DMA1_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA1_Stream0, DMA_IT_TCIF0))
{
// 清除DMA传输完成标志位
DMA_ClearITPendingBit(DMA1_Stream0, DMA_IT_TCIF0);
}
}
```
完整代码如下:
阅读全文
相关推荐
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)