zoukankan      html  css  js  c++  java
  • 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 类。

  • 相关阅读:
    批量清理java源码的target目录
    前端移动node_modules到其他位置
    oracle物化视图和视图的数据不一致
    百词斩英语单词素材提取、听力练习
    2048自动游戏AI, 最高可以玩出一二十个2048
    switcheroo: Alt+Tab的替代工具、窗口搜索
    为知笔记wiz.editor.md增强
    腾讯北极星 Polaris 试用
    [分布式] 分布式事务、seata
    Mysql查询所有的表名和查询表中所有的字段名
  • 原文地址:https://www.cnblogs.com/aobosir/p/5928553.html
Copyright © 2011-2022 走看看