Files
linear-Slide/MyDriver/SoftI2C.h

44 lines
1.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef __DRIVER_I2C_H__
#define __DRIVER_I2C_H__
#include <stdint.h>
#include "stm32f407xx.h"
#include "stm32f4xx.h"
#include "main.h"
#include "Com_debug.h"
#include "DWT.h"
// PA8是SDAPA9是SCL
// 板子改进意见PE9和PE11用作定时器1的CH1和CH2然后PA8和PA9留出来用作I2C的通信引脚
#define I2C_SCL_PIN GPIO_PIN_9
#define I2C_SDA_PIN GPIO_PIN_8
#define I2C_GPIO_PORT GPIOA
// 宏定义 SDA拉高或拉低
#define SDA_HIGH I2C_GPIO_PORT->BSRR = I2C_SDA_PIN
#define SDA_LOW I2C_GPIO_PORT->BSRR = (uint32_t)I2C_SDA_PIN << 16U
// 宏定义 SCL拉高或拉低
#define SCL_HIGH I2C_GPIO_PORT->BSRR = I2C_SCL_PIN
#define SCL_LOW I2C_GPIO_PORT->BSRR = (uint32_t)I2C_SCL_PIN << 16U
// 宏定义 读SDA
#define SDA_READ (I2C_GPIO_PORT->IDR & I2C_SDA_PIN)
// 宏定义 延时5us
#define SCL_DELAY DWT_delay_us(5)
void Dri_I2C_Init(void); // 初始化函数
void Dri_I2C_Start(void); // 发送起始信号
void Dri_I2C_Stop(void); // 发送停止信号
void Dri_I2C_TransmitByte(uint8_t byte); // 发送一个字节的数据
uint8_t Dri_I2C_ReceiveByte(void); // 接收一个字节的数据
void Dri_I2C_TransmitACK(uint8_t ack); // 发送一个ACK信号, 参数是0(ACK)或1(NACK)
uint8_t Dri_I2C_ReceiveACK(void); // 接收一个ACK信号并返回
void Dri_I2C_WriteAddr(uint8_t slave_addr); // 写从机地址
void Dri_I2C_WriteReg(uint8_t reg, uint8_t data); // 写一个寄存器
uint8_t Dri_I2C_ReadReg(uint8_t slave_addr_read, uint8_t reg); // 读一个寄存器
#endif /* __DRIVER_I2C_H__ */