【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由低电平向高电平跳变

停止

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


总结

这篇文章依旧没有总结

相关推荐
boneStudent17 小时前
Day35:DMA 原理与架构
stm32·单片机·嵌入式硬件
UVM_ERROR17 小时前
RDMA Scheduler + TX + Completion RTL 开发经验分享
笔记·vscode·ssh·github·芯片
黑客思维者17 小时前
机器学习003:无监督学习(概论)--机器如何学会“自己整理房间”
人工智能·学习·机器学习·无监督学习
Vizio<17 小时前
STM32HAL库开发笔记-GPIO输入
笔记·stm32·单片机·嵌入式硬件
chinalihuanyu17 小时前
蓝牙开发笔记(BlueTooth,BLE,CH592)
笔记
其美杰布-富贵-李17 小时前
tsai 中 Learner 机制深度学习笔记
人工智能·笔记·深度学习
wdfk_prog18 小时前
[Linux]学习笔记系列 -- [fs]dcache
linux·数据库·笔记·学习·ubuntu
小智RE0-走在路上19 小时前
Python学习笔记(7)--集合,字典,数据容器总结
笔记·python·学习
呵呵哒( ̄▽ ̄)"19 小时前
专项智能练习(古代神话)
学习
Big_潘大师19 小时前
十轴IMU模块-AHRS角度姿态、加速度计、磁力计、气压陀螺仪传感器
stm32·单片机·嵌入式硬件·arduino·陀螺仪