【STM32】IIC学习笔记

学习IIC


前言

最近沉迷手写笔记~

尝试解读江科大的IIC程序,结合笔记更理解IIC


一、基础知识




GPIO_WriteBit 写入高低电平

二、放代码

这个是江科大的软件IIC的设置部分

bash 复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

void MyI2C_W_SCL(uint8_t BitValue)	//IIC_SCL写入
{
	GPIO_WriteBit(GPIOB, GPIO_Pin_10, (BitAction)BitValue);
	Delay_us(10);
}

void MyI2C_W_SDA(uint8_t BitValue)	//IIC_SDA写入
{
	GPIO_WriteBit(GPIOB, GPIO_Pin_11, (BitAction)BitValue);
	Delay_us(10);
}

uint8_t MyI2C_R_SDA(void)			//IIC_SDA读取
{
	uint8_t BitValue;
	BitValue = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11);
	Delay_us(10);
	return BitValue;
}

void MyI2C_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;			//开漏
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_10 | GPIO_Pin_11);
}

void MyI2C_Start(void)
{
	MyI2C_W_SDA(1);
	MyI2C_W_SCL(1);
	MyI2C_W_SDA(0);
	MyI2C_W_SCL(0);
}

void MyI2C_Stop(void)
{
	MyI2C_W_SDA(0);
	MyI2C_W_SCL(1);
	MyI2C_W_SDA(1);
}

void MyI2C_SendByte(uint8_t Byte)
{
	uint8_t i;
	for (i = 0; i < 8; i ++)
	{
		MyI2C_W_SDA(Byte & (0x80 >> i));		//右移操作逐渐得到 Byte 的每一位
		MyI2C_W_SCL(1);							//驱动数据传输,SXL保持高电平才能发送数据
		MyI2C_W_SCL(0);
	}
}

uint8_t MyI2C_ReceiveByte(void)
{
	uint8_t i, Byte = 0x00;
	MyI2C_W_SDA(1);
	for (i = 0; i < 8; i ++)
	{
		MyI2C_W_SCL(1);
		if (MyI2C_R_SDA() == 1) // 读取数据线的状态
		{
			Byte |= (0x80 >> i);
		}
		MyI2C_W_SCL(0);
	}
	return Byte;
}

void MyI2C_SendAck(uint8_t AckBit)
{
	MyI2C_W_SDA(AckBit);
	MyI2C_W_SCL(1);
	MyI2C_W_SCL(0);
}

uint8_t MyI2C_ReceiveAck(void)
{
	uint8_t AckBit;
	MyI2C_W_SDA(1);
	MyI2C_W_SCL(1);
	AckBit = MyI2C_R_SDA();
	MyI2C_W_SCL(0);
	return AckBit;
}

三、逐行细读

开始

SCL高电平,SDA由低电平向高电平跳变

停止

其他的部分在注释里面体现了。


总结

这篇文章依旧没有总结

相关推荐
lkbhua莱克瓦2434 分钟前
Java基础——方法
java·开发语言·笔记·github·学习方法
p66666666681 小时前
STM32-bootloader引导程序跳转机制笔记
笔记·stm32·嵌入式硬件
Fantasydg2 小时前
Servlet学习
学习·servlet
雍凉明月夜2 小时前
Ⅰ人工智能学习的核心概念概述+线性回归(1)
人工智能·学习
智者知已应修善业2 小时前
【c语言蓝桥杯计算卡片题】2023-2-12
c语言·c++·经验分享·笔记·算法·蓝桥杯
2301_783360133 小时前
R语言 | 带重要性相关热图和贡献图如何解释?如何绘制随机森林计算结果重要性及相关性图?[学习笔记]
学习·随机森林·r语言
潲爺3 小时前
Java IDEA学习之路:第九周课程笔记归纳
java·学习·intellij-idea
石像鬼₧魂石3 小时前
192.168.1.4(Windows 靶机)渗透测试练习全流程(详细步骤)
windows·学习
亿道电子Emdoor3 小时前
【Arm】Encountered an improper argument
arm开发·stm32·单片机
GLAB-Mary4 小时前
HCIE最优规划路线:如何系统性学习华为认证?
学习·华为·华为认证·hcie·数通