STM32 C++编程 005 I2c(Soft)类

使用 C++ 语言给 STM32 编写一个 I2c(Soft)类

我使用的STM32芯片:STM32F103ZET6
我们使用的STM32库版本:V3.5.0



注意:

  • 想学习本套 STM32 C++编程 的专栏是有点门槛的。你需要有一点点 STM32 基础 和 一点点 C++ 语言基础。

  • 完整的STM32 C++ I2c(Soft)类 的下载地址可以在本篇博客的最下面找到。


I2cSoft.h

#ifndef __AOBO_stm32f10x_I2c_H_
#define     __AOBO_stm32f10x_I2c_H_

#include "stm32f10x.h"
#include "Gpio.h"

namespace stm32f10x{

class I2cSoft{
    public:
         I2cSoft(Gpio *sda, Gpio *scl);
        void initialize(void);
         int singleWrite(u8 SlaveAddress,u8 REG_Address,u8 REG_data);   
         int singleRead(u8 SlaveAddress,u8 REG_Address);
         int multRead(u8 SlaveAddress,u8 REG_Address,u8 * ptChar,u8 size);

    private:
        Gpio *SDA, *SCL;
        void delay(void);
         int start(void);
        void stop(void);
        void ack(void); 
        void noAck(void);
         int waitAck(void);      //返回为:=1有ACK,=0无ACK
        void sendByte(u8 SendByte);
          u8 readByte(void);  //数据从高位到低位//  
};

}

#endif

i2cSoft.cpp

#include "I2cSoft.h"

using namespace stm32f10x;

I2cSoft::I2cSoft(Gpio *sda, Gpio *scl):SDA(sda), SCL(scl){
//  initialize();
}

//模拟IIC初始化
void I2cSoft::initialize(void){
}

void I2cSoft::delay(void)
{
   /*u8 i=0; 
   while(i) 
   { 
     i--; 
   } 
        */  
}

int I2cSoft::start(void)
{
    SDA->high();    SCL->high();    delay();
    if(SDA->islow())return 0;   //SDA线为低电平则总线忙,退出
    SDA->low(); delay();
    if(SDA->ishigh()) return 0; //SDA线为高电平则总线出错,退出
    SDA->low(); delay();
    return 1;   
}

void I2cSoft::stop(void)
{
    SCL->low(); delay();
    SDA->low(); delay();
    SCL->high();    delay();
    SDA->high();    delay();
} 

void I2cSoft::ack(void)
{   
    SCL->low(); delay();
    SDA->low(); delay();
    SCL->high();    delay();
    SCL->low(); delay();
}   

void I2cSoft::noAck(void)
{   
    SCL->low(); delay();
    SDA->high();    delay();
    SCL->high();    delay();
    SCL->low(); delay();
} 

int I2cSoft::waitAck(void)   //返回为:=1有ACK,=0无ACK
{
    SCL->low(); delay();
    SDA->high();    delay();
    SCL->high();    delay();
    if(SDA->ishigh()){
        SCL->low(); delay();    return 0;
    }
    SCL->low(); delay();
    return 1;
}

void I2cSoft::sendByte(u8 SendByte) //数据从高位到低位//
{
    u8 i=8;
    while(i--){
        SCL->low(); delay();
        if(SendByte&0x80)   SDA->high();  
        else    SDA->low();   
        SendByte<<=1;   delay();
        SCL->high();        delay();
    }
    SCL->low();
}  

u8 I2cSoft::readByte(void)  //数据从高位到低位//
{ 
    u8 i=8;
    u8 ReceiveByte=0;

    SDA->high();                
    while(i--){
        ReceiveByte<<=1;      
        SCL->low(); delay();
        SCL->high();    delay();    
        if(SDA->ishigh()){
            ReceiveByte|=0x01;
        }
    }
    SCL->low();
    return ReceiveByte;
} 

//单字节写入*******************************************
int I2cSoft::singleWrite(u8 SlaveAddress,u8 REG_Address,u8 REG_data)    {
    if(!start())return 0;
    sendByte(SlaveAddress);   //发送设备地址+写信号//sendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 
    if(!waitAck())  {stop(); return 0;}
    sendByte(REG_Address );   //设置低起始地址      
    waitAck();  
    sendByte(REG_data);
    waitAck();   
    stop(); 
    return 1;
}

//单字节读取*****************************************
int I2cSoft::singleRead(u8 SlaveAddress,u8 REG_Address){   
    unsigned char REG_data;         
    if(!start())return 0;
    sendByte(SlaveAddress); //sendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 
    if(!waitAck()){
        stop(); return 0;
    }
    sendByte((u8) REG_Address);   waitAck(); /*设置低起始地址  */   
    start();
    sendByte(SlaveAddress+1);   waitAck();

    REG_data= readByte();
    noAck();
    stop();
    //return TRUE;
    return REG_data;

}   

/******************************************************************************
多字节读取
******************************************************************************/
int I2cSoft::multRead(u8 SlaveAddress,u8 REG_Address,u8 * ptChar,u8 size){
    uint8_t i;

    if(size < 1)    return 0;
    if(!start())    return 0;
    sendByte(SlaveAddress);
    if(!waitAck()){
        stop(); return 0;
    }
    sendByte(REG_Address);      waitAck();

    start();
    sendByte(SlaveAddress+1);   waitAck();

    for(i=1;i<size; i++){
        *ptChar++ = readByte();
        ack();
    }
    *ptChar++ = readByte();
    noAck();
    stop();
    return 1;    
}   


/******************* (C) COPYRIGHT 2014 ANO TECH *****END OF FILE************/

main.cpp

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "Gpio.h"
#include "I2cSoft.h"

using namespace stm32f10x;
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

int main(void)
{
    Gpio scl(PA,5);
    Gpio sda(PA,6);
    I2cSoft  i2c(&sda, &scl);
    while(true)
    {
    }   
}

搞定


你可以到这里下载我已经做好的 STM32 C++ I2c(Soft)类
百度云 链接:http://pan.baidu.com/s/1bpbZ2MV 密码:esam
也可以在CSDN里面下载:http://download.csdn.net/detail/github_35160620/9626553



小结:
下一讲,我们来使用 C++ 语言,创建一个 STM32硬件 I2c 类。

原文地址:https://www.cnblogs.com/aobosir/p/5928553.html