当前位置:网站首页>Software I2C based on Hal Library
Software I2C based on Hal Library
2022-07-06 11:22:00 【Clear glass, brilliant orange】

I2C Transmission timing
#include <stdio.h>
#include "main.h"
#include "driver_i2c.h"
#include "driver_timer.h"
#include "driver_usart.h"
#define I2C_Delay() us_timer_delay(5) // Clock Pulse Width >5us
/* * Function name :void I2C_Init(void) * Input parameters : * Output parameters : nothing * Return value : nothing * Function function : Initialize simulation I2C The pin of is in the output state and SCL/SDA Are initially high level */
void I2C_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
SCL_PIN_CLK_EN(); /* __HAL_RCC_GPIOB_CLK_ENABLE() */
SDA_PIN_CLK_EN(); /* __HAL_RCC_GPIOB_CLK_ENABLE() */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = SCL_PIN; /* SCL_PIN by GPIO_PIN_6 */
HAL_GPIO_Init(SCL_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SDA_PIN; /* SDA_PIN by GPIO_PIN_7 */
HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);
SCL_H(); /* Set up SCL_PIN High level HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET) */
SDA_H(); /* Set up SDA_PIN High level HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_SET) */
}
/*
- Function name :static void I2C_SDA_OUT(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function : To configure SDA Pin is output
*/
static void I2C_SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = SDA_PIN;
HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);
}
/*
- Function name :static void I2C_SDA_IN(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function : To configure SDA Pin is input
*/
static void I2C_SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {
0};
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = SDA_PIN;
HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);
}
/*
- Function name :void I2C_Start(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Start signal
*/
void I2C_Start(void)
{
I2C_SDA_OUT();
SCL_H(); /* Pull up the clock line HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET) */
I2C_Delay();
SDA_H();
I2C_Delay();
SDA_L();
I2C_Delay();
SCL_L();
I2C_Delay();
}
/*
- Function name :void I2C_Stop(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Stop signal
*/
void I2C_Stop(void)
{
I2C_SDA_OUT();
SDA_L();
I2C_Delay();
SCL_H();
I2C_Delay();
SDA_H();
I2C_Delay();
}

/*
- Function name :void I2C_ACK(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a reply signal
*/
void I2C_ACK(void)
{
I2C_SDA_OUT(); /* Set up SDA For export */
SCL_L();
I2C_Delay();
SDA_L();
I2C_Delay();
SCL_H(); /* Pull the clock cable high , Generate rising edge data transmission */
I2C_Delay();
SCL_L();
I2C_Delay();
}
/*
- Function name :void I2C_NACK(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a non reply signal
*/
void I2C_NACK(void)
{
I2C_SDA_OUT();
SCL_L();
I2C_Delay();
SDA_H();
I2C_Delay();
SCL_H();
I2C_Delay();
SCL_L();
I2C_Delay();
}
/*
- Function name :uint8_t I2C_GetACK(void)
- Input parameters :
- Output parameters : nothing
- Return value :1 No response ,0 There's a response
- Function function :I2C Wait for the response signal from the slave
*/
uint8_t I2C_GetACK(void)
{
uint8_t time = 0;
I2C_SDA_IN();
SCL_L();
I2C_Delay();
SDA_H();
I2C_Delay();
SCL_H();
I2C_Delay();
while(SDA_INPUT())
{
time++;
if(time>250)
{
SCL_L();
return 1;
}
}
SCL_L();
return 0;
}
/*
- Function name :void I2C_SendByte(uint8_t data)
- Input parameters :data-> Data sent
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a byte
*/
void I2C_SendByte(uint8_t data)
{
uint8_t cnt = 0;
I2C_SDA_OUT();
for(cnt=0; cnt<8; cnt++)
{
SCL_L();
I2C_Delay();
if(data & 0x80)
{
SDA_H();
}
else
{
SDA_L();
}
data = data<<1;
SCL_H();
I2C_Delay();
}
SCL_L();
I2C_Delay();
I2C_GetACK();
}
/*
- Function name :uint8_t I2C_ReadByte(uint8_t ack)
- Input parameters :ack-> Sent reply flag ,1 The reply ,0 Non response
- Output parameters : nothing
- Return value : Return the read bytes
- Function function :I2C Read a byte
*/
uint8_t I2C_ReadByte(uint8_t ack)
{
uint8_t cnt;
uint8_t data = 0xFF;
SCL_L();
I2C_Delay();
for(cnt=0; cnt<8; cnt++)
{
SCL_H(); //SCL high ( Reading data )
I2C_Delay();
data <<= 1;
if(SDA_INPUT())
{
data |= 0x01; //SDA high ( The data is 1)
}
SCL_L();
I2C_Delay();
}
// Send reply signal , Answer for low , High representative non response
if(ack == 0)
{
I2C_ACK();
}
else
{
I2C_NACK();
}
return data; // Return the data
}
i2c.h file
#ifndef __DRIVER_I2C_H
#define __DRIVER_I2C_H
#include "stm32f1xx_hal.h"
/************************* I2C Hardware related definitions *************************/
#define ACK (0)
#define NACK (1)
#define SCL_PIN GPIO_PIN_6
#define SCL_PORT GPIOB
#define SCL_PIN_CLK_EN() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SDA_PIN GPIO_PIN_7
#define SDA_PORT GPIOB
#define SDA_PIN_CLK_EN() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SCL_H() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET)
#define SCL_L() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_RESET)
#define SCL_INPUT() HAL_GPIO_ReadPin(SCL_PORT, SCL_PIN)
#define SDA_H() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_SET)
#define SDA_L() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_RESET)
#define SDA_INPUT() HAL_GPIO_ReadPin(SDA_PORT, SDA_PIN)
/************************* I2C End of hardware related definitions ************************/
/
- Function name :void I2C_Init(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function : Initialize simulation I2C The pin of is in the output state and SCL/SDA Are initially high level
*/
extern void I2C_Init(void);
/*
- Function name :void I2C_Start(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Start signal
*/
extern void I2C_Start(void);
/*
- Function name :void I2C_Stop(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Stop signal
*/
extern void I2C_Stop(void);
/*
- Function name :void I2C_ACK(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a reply signal
*/
extern void I2C_ACK(void);
/*
- Function name :void I2C_NACK(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a non reply signal
*/
extern void I2C_NACK(void);
/*
- Function name :uint8_t I2C_GetACK(void)
- Input parameters :
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Wait for the response signal from the slave
*/
extern uint8_t I2C_GetACK(void);
/*
- Function name :void I2C_SendByte(uint8_t data)
- Input parameters :data-> Data sent
- Output parameters : nothing
- Return value : nothing
- Function function :I2C Send a byte
*/
extern void I2C_SendByte(uint8_t data);
/*
- Function name :uint8_t I2C_ReadByte(uint8_t ack)
- Input parameters :ack-> Sent reply flag ,1 The reply ,0 Non response
- Output parameters : nothing
- Return value : Return the read bytes
- Function function :I2C Read a byte
*/
extern uint8_t I2C_ReadByte(uint8_t ack);
#endif
边栏推荐
- [蓝桥杯2020初赛] 平面切分
- Software testing and quality learning notes 3 -- white box testing
- AcWing 179. Factorial decomposition problem solution
- Some notes of MySQL
- Test objects involved in safety test
- AcWing 1298. Solution to Cao Chong's pig raising problem
- ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
- MySQL主從複制、讀寫分離
- 基于apache-jena的知识问答
- 软件测试与质量学习笔记3--白盒测试
猜你喜欢
![[number theory] divisor](/img/ec/036d7e76cc566c08d336444f2898e1.jpg)
[number theory] divisor

PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘

Classes in C #
![[recommended by bloggers] C WinForm regularly sends email (with source code)](/img/5d/57f8599a4f02c569c6c3f4bcb8b739.png)
[recommended by bloggers] C WinForm regularly sends email (with source code)

Learning question 1:127.0.0.1 refused our visit

Why can't I use the @test annotation after introducing JUnit

QT creator shape

Summary of numpy installation problems

【博主推荐】SSM框架的后台管理系统(附源码)

QT creator specifies dependencies
随机推荐
FRP intranet penetration
MySQL completely uninstalled (windows, MAC, Linux)
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
误删Path变量解决
[AGC009D]Uninity
记某公司面试算法题:查找一个有序数组某个数字出现的次数
Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
[蓝桥杯2021初赛] 砝码称重
Django running error: error loading mysqldb module solution
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
Summary of numpy installation problems
SSM integrated notes easy to understand version
解决安装Failed building wheel for pillow
Some notes of MySQL
虚拟机Ping通主机,主机Ping不通虚拟机
自动机器学习框架介绍与使用(flaml、h2o)
QT creator create button
Swagger、Yapi接口管理服务_SE
QT creator shape
How to build a new project for keil5mdk (with super detailed drawings)