當設計中使用I2C的數量多于1個時,其底層I2C的代碼邏輯都是一樣的,只有IO口變了,為此還要復制粘貼、修改IO,不僅費時,而且還浪費片內資源,因此可以使用指標來重復執行IIC底層代碼,實作一個代碼,多個IO使用,
Stm32f30x_SMLT_I2C.h
#ifndef __Stm32f30x_SMLT_I2C_H
#define __Stm32f30x_SMLT_I2C_H
//========================= Include ===============================================================
#include "stm32f30x.h"
//========================= Variable ==============================================================
typedef struct
{
GPIO_TypeDef* I2C_GPIO;
uint32_t SDA_SBIT;
uint32_t SDA_RBIT;
uint32_t SCL_SBIT;
uint32_t SCL_RBIT;
uint32_t SDA_MODER;
uint32_t SDA_MODER_0;
uint32_t SDA_MODER_1;
}I2C_Struct;
extern I2C_Struct S_I2C_IO[2];//------------------------------------- 需修改
//(想要3個的話,陣列添加,下面的define也要添加,初始化函式S_I2C_Init中也要添加)
//========================= I2C1_Define ================================================================
//--------------------------SDA-------------------------------------- 需修改
#define S_I2C1_SDA_GPIO GPIOB
#define S_I2C1_SDA_BIT 9
//--------------------------SCL-------------------------------------- 需修改
#define S_I2C1_SCL_GPIO GPIOB
#define S_I2C1_SCL_BIT 8
//還需配置結構體初始化函式 S_I2C_Init
//========================= I2C2_Define ================================================================
//--------------------------SDA-------------------------------------- 需修改
#define S_I2C2_SDA_GPIO GPIOE
#define S_I2C2_SDA_BIT 1
//--------------------------SCL-------------------------------------- 需修改
#define S_I2C2_SCL_GPIO GPIOE
#define S_I2C2_SCL_BIT 0
//還需配置結構體初始化函式 S_I2C_Init
// //========================= I2Cx_Define ================================================================
// //--------------------------SDA-------------------------------------- 需修改
// #define S_I2Cx_SDA_GPIO GPIOx
// #define S_I2Cx_SDA_BIT x
// //--------------------------SCL-------------------------------------- 需修改
// #define S_I2Cx_SCL_GPIO GPIOx
// #define S_I2Cx_SCL_BIT x
// //添加在此添加
//========================= Function ==============================================================
void S_I2C_Init(void);//--------------------------------------------- 需修改
void S_I2C_Start(I2C_Struct I2C_temp);
void S_I2C_Stop(I2C_Struct I2C_temp);
uint8_t S_I2C_SendByte(I2C_Struct I2C_temp, uint8_t byte);
uint8_t S_I2C_ReceiveByte(I2C_Struct I2C_temp, uint8_t nack);
uint8_t S_I2C_WriteRegister(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t data);
uint8_t S_I2C_ReadRegister(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t amount, uint8_t* data, uint8_t typ);
uint8_t S_I2C_PointerRead(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t amount, uint8_t* data, uint8_t typ);
//========================= Define ================================================================
//通過宏定義將IO結構體直接放入基礎函式中
//-------------------------- I2C1 -----------------------------------
#define S_I2C1_Start() S_I2C_Start(S_I2C_IO[0])
#define S_I2C1_Stop() S_I2C_Stop(S_I2C_IO[0])
#define S_I2C1_SendByte(byte) S_I2C_SendByte(S_I2C_IO[0], byte)
#define S_I2C1_ReceiveByte(nack) S_I2C_ReceiveByte(S_I2C_IO[0], nack)
#define S_I2C1_WriteRegister(addr, regAddr, data) S_I2C_WriteRegister(S_I2C_IO[0], addr, regAddr, data)
#define S_I2C1_ReadRegister(addr, regAddr, amount, data, typ) S_I2C_ReadRegister(S_I2C_IO[0], addr, regAddr, amount, data, typ)
#define S_I2C1_PointerRead(addr, regAddr, amount, data, typ) S_I2C_PointerRead(S_I2C_IO[0], addr, regAddr, amount, data, typ)
//-------------------------- I2C2 -----------------------------------
#define S_I2C2_Start() S_I2C_Start(S_I2C_IO[1])
#define S_I2C2_Stop() S_I2C_Stop(S_I2C_IO[1])
#define S_I2C2_SendByte(byte) S_I2C_SendByte(S_I2C_IO[1], byte)
#define S_I2C2_ReceiveByte(nack) S_I2C_ReceiveByte(S_I2C_IO[1], nack)
#define S_I2C2_WriteRegister(addr, regAddr, data) S_I2C_WriteRegister(S_I2C_IO[1], addr, regAddr, data)
#define S_I2C2_ReadRegister(addr, regAddr, amount, data, typ) S_I2C_ReadRegister(S_I2C_IO[1], addr, regAddr, amount, data, typ)
#define S_I2C2_PointerRead(addr, regAddr, amount, data, typ) S_I2C_PointerRead(S_I2C_IO[1], addr, regAddr, amount, data, typ)
//-------------------------- Another --------------------------------
#define ACK 0
#define NACK 1
#define FAILED 0
#define SUCCEED 1
//=================================================================================================
#endif
Stm32f30x_SMLT_I2C.c
#include "Stm32f30x_SMLT_I2C.h"
I2C_Struct S_I2C_IO[2];//IO口結構體,通過.h檔案定義使用的引腳
/**
* @description: I2C結構體IO初始化
* @param {I2C_Struct*} I2C_temp I2C的IO結構體
* @param {uint8_t} SDA_BIT I2C SDA引腳號(1~16)
* @param {uint8_t} SCL_BIT I2C SCL引腳號(1~16)
* @return {*}
*/
void S_I2C_IO_Init(I2C_Struct* I2C_temp, GPIO_TypeDef* I2C_GPIO, uint8_t SDA_BIT, uint8_t SCL_BIT)
{
I2C_temp->I2C_GPIO = I2C_GPIO;
I2C_temp->SDA_SBIT = (SBIT0 << SDA_BIT);
I2C_temp->SDA_RBIT = (RBIT0 << SDA_BIT);
I2C_temp->SCL_SBIT = (SBIT0 << SCL_BIT);
I2C_temp->SCL_RBIT = (RBIT0 << SCL_BIT);
I2C_temp->SDA_MODER = 0x03 << (SDA_BIT << 1);
I2C_temp->SDA_MODER_0 = 0x01 << (SDA_BIT << 1);
I2C_temp->SDA_MODER_1 = 0x02 << (SDA_BIT << 1);
}
/**
* @description: I2C結構體總初始化,將計算的各個變數存入結構體中
* @param {*}
* @return {*}
*/
void S_I2C_Init(void)
{
S_I2C_IO_Init(&S_I2C_IO[0], S_I2C1_SDA_GPIO, S_I2C1_SDA_BIT, S_I2C1_SCL_BIT);
S_I2C_IO_Init(&S_I2C_IO[1], S_I2C2_SDA_GPIO, S_I2C2_SDA_BIT, S_I2C2_SCL_BIT);
// S_I2C_IO_Init(&S_I2C_IO[x], S_I2Cx_SDA_GPIO, S_I2Cx_SDA_BIT, S_I2Cx_SCL_BIT); //添加在此添加
}
/**
* @description: I2C時鐘延時,調整波特率
* @param {*}
* @return {*}
*/
void Delay(void)
{
for (uint8_t i = 0; i < 0x10; i++)
{
/* code */
}
}
/**
* @description: 產生起始信號
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @return {*}
*/
void S_I2C_Start(I2C_Struct I2C_temp)
{
I2C_temp.I2C_GPIO->MODER |= I2C_temp.SDA_MODER_0;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_SBIT;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
Delay();
}
/**
* @description: 產生停止信號
* @param {I2C_Struct} I2C_temp
* @return {*}
*/
void S_I2C_Stop(I2C_Struct I2C_temp)
{
I2C_temp.I2C_GPIO->MODER |= I2C_temp.SDA_MODER_0;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_SBIT;
Delay();
}
/**
* @description: 發送一個位元組資料
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @param {uint8_t} byte 資料
* @return {uint8_t} NACK
* ACK
*/
uint8_t S_I2C_SendByte(I2C_Struct I2C_temp, uint8_t byte)
{
uint8_t temp = 0x00;
for(uint8_t i = 0x80; i; i >>= 1)
{
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
if(byte & i)
{
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_SBIT;
}
else
{
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
}
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
Delay();
}
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
I2C_temp.I2C_GPIO->MODER &= ~I2C_temp.SDA_MODER;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
if(I2C_temp.I2C_GPIO -> IDR & I2C_temp.SDA_SBIT)
{
temp = NACK;
}
else
{
temp = ACK;
}
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
I2C_temp.I2C_GPIO->MODER |= I2C_temp.SDA_MODER_0;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
Delay();
return temp;
}
/**
* @description: 接收一個位元組資料
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @param {uint8_t} nack 接收后結束要發送的應答信號
* NACK
* ACK
* @return {uint8_t} 接收到的位元組
*/
uint8_t S_I2C_ReceiveByte(I2C_Struct I2C_temp, uint8_t nack)
{
uint8_t byte = 0x00;
I2C_temp.I2C_GPIO->MODER &= ~I2C_temp.SDA_MODER;
for (uint8_t i = 8; i; i--)
{
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
Delay();
byte <<= 1;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
if(I2C_temp.I2C_GPIO -> IDR & I2C_temp.SDA_SBIT)
{
byte |= 0x01;
}
Delay();
}
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
I2C_temp.I2C_GPIO->MODER |= I2C_temp.SDA_MODER_0;
if(nack)
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_SBIT;
else
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SDA_RBIT;
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_RBIT;
Delay();
I2C_temp.I2C_GPIO->BSRR |= I2C_temp.SCL_SBIT;
Delay();
return byte;
}
/**
* @description: 寫一個位元組的暫存器
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @param {uint8_t} addr 器件地址(0x00~0x7f)
* @param {uint8_t} regAddr 暫存器地址(0x00~0xff)
* @param {uint8_t} data 資料
* @return {uint8_t} FAILED 失敗
* SUCCEED 成功
*/
uint8_t S_I2C_WriteRegister(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t data)
{
addr <<= 1;
S_I2C_Start(I2C_temp);
if(S_I2C_SendByte(I2C_temp, addr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
if(S_I2C_SendByte(I2C_temp, regAddr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
if(S_I2C_SendByte(I2C_temp, data) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
S_I2C_Stop(I2C_temp);
return SUCCEED;
}
/**
* @description: 讀取器件,讀取格式:器件讀-暫存器地址-讀取資料
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @param {uint8_t} addr 器件地址(0x00~0x7f)
* @param {uint8_t} regAddr 暫存器地址(0x00~0xff)
* @param {uint8_t} amount 讀取資料數量(0x00~0xff)
* @param {uint8_t*} data 回傳資料指標
* @param {uint8_t} typ 資料指標型別
* ARRAY
* STRUCT
* @return {*} FAILD 失敗
* SUCCEED 成功
*/
uint8_t S_I2C_ReadRegister(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t amount, uint8_t* data, uint8_t typ)
{
S_I2C_Start(I2C_temp);
addr = (addr << 1) | 0x01;
if(S_I2C_SendByte(I2C_temp, addr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
if(S_I2C_SendByte(I2C_temp, regAddr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
for(amount -= 1;amount;amount --)
{
*data = S_I2C_ReceiveByte(I2C_temp, ACK);
if(typ == ARRAY)
data ++;
else
data --;
}
*data = S_I2C_ReceiveByte(I2C_temp, NACK);
S_I2C_Stop(I2C_temp);
return SUCCEED;
}
/**
* @description: 讀取器件,讀取格式:器件寫-暫存器地址-器件讀-讀取資料
* @param {I2C_Struct} I2C_temp I2C_IO結構體
* @param {uint8_t} addr 器件地址(0x00~0x7f)
* @param {uint8_t} regAddr 暫存器地址(0x00~0xff)
* @param {uint8_t} amount 讀取資料數量(0x00~0xff)
* @param {uint8_t*} data 回傳資料指標
* @param {uint8_t} typ 資料指標型別
* ARRAY
* STRUCT
* @return {uint8_t} FAILD 失敗
* SUCCEED 成功
*/
uint8_t S_I2C_PointerRead(I2C_Struct I2C_temp, uint8_t addr, uint8_t regAddr, uint8_t amount, uint8_t* data, uint8_t typ)
{
addr <<= 1;
S_I2C_Start(I2C_temp);
if(S_I2C_SendByte(I2C_temp, addr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
if(S_I2C_SendByte(I2C_temp, regAddr) == NACK)
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
S_I2C_Stop(I2C_temp);
addr |= 0x01;
S_I2C_Start(I2C_temp);
if(S_I2C_SendByte(I2C_temp, addr) == NACK) //NACK
{
S_I2C_Stop(I2C_temp);
return FAILED;
}
for(amount -= 1;amount;amount --)
{
*data = S_I2C_ReceiveByte(I2C_temp, ACK);
if(typ == ARRAY)
data ++;
else
data --;
}
*data = S_I2C_ReceiveByte(I2C_temp, NACK);
S_I2C_Stop(I2C_temp);
return SUCCEED;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276266.html
標籤:其他
