OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1178|回复: 1

共享一个简单的I2C驱动

[复制链接]

44

主题

187

帖子

0

精华

高级会员

Rank: 4

积分
566
金钱
566
注册时间
2016-9-28
在线时间
158 小时
发表于 2018-8-13 22:25:57 | 显示全部楼层 |阅读模式
本帖最后由 mftang2016 于 2018-8-13 22:36 编辑

说明: 应用MCU端口模拟 I2C 时序
应用实例:

http://www.openedv.com/forum.php?mod=viewthread&tid=277047

http://www.openedv.com/forum.php?mod=viewthread&tid=277102

I2C.c 文件:

[mw_shl_code=c,true]/** \file

$Id: I2C.c 40486 2018-08-08 13:50:21Z tangmingfei2013@126.com $

Copyright (c)tangmingfei2013@126.com Holding B.V.
All Rights Reserved.

This source code and any compilation or derivative thereof is the proprietary
information of mingfei.tang  Holding B.V. and is confidential in nature.
Under no circumstances is this software to be combined with any
Open Source Software in any way or placed under an Open Source License
of any type without the express written permission of mingfei.tang Holding B.V.
*/

/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/
#include "I2C.h"

/******************************************************************************
* LOCAL MACROS AND DEFINITIONS
******************************************************************************/
#define GPIO_PORT_I2C              GPIOA
#define I2C_SCL_PIN                GPIO_Pin_5                        
#define I2C_SDA_PIN                GPIO_Pin_7

#define I2C_SCL_1()      GPIO_SetBits(GPIO_PORT_I2C, I2C_SCL_PIN)             /* SCL = 1 */
#define I2C_SCL_0()      GPIO_ResetBits( GPIO_PORT_I2C, I2C_SCL_PIN)        /* SCL = 0 */

#define I2C_SDA_1()      GPIO_SetBits( GPIO_PORT_I2C, I2C_SDA_PIN)          /* SDA = 1 */
#define I2C_SDA_0()      GPIO_ResetBits( GPIO_PORT_I2C, I2C_SDA_PIN)        /* SDA = 0 */

#define I2C_SDA_READ()  ((GPIO_PORT_I2C->IDR & I2C_SDA_PIN) != 0)       /* read SDA status */
#define I2C_SCL_READ()  ((GPIO_PORT_I2C->IDR & I2C_SCL_PIN) != 0)       /* read SCLs tatus */

/******************************************************************************
* LOCAL FUNCTION DECLARATIONS
******************************************************************************/
static void i2c_Delay(void);

/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/
void  board_i2cInit( void )
{
    GPIO_InitTypeDef  GPIO_InitStructure;
   
    GPIO_InitStructure.GPIO_Pin   = I2C_SCL_PIN|I2C_SDA_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIO_PORT_I2C, &GPIO_InitStructure );
}

void i2c_Init(void)
{
    board_i2cInit();  /* i2c port init */
    i2c_Stop();
}

void i2c_Start(void)
{
    I2C_SDA_1();
    I2C_SCL_1();
    i2c_Delay();
    I2C_SDA_0();
    i2c_Delay();
    I2C_SCL_0();
    i2c_Delay();
}


void i2c_Stop(void)
{
    I2C_SDA_0();
    I2C_SCL_1();
    i2c_Delay();
    I2C_SDA_1();
}

void i2c_SendByte(uint8_t _ucByte)
{
    uint8_t i;

    for (i = 0; i < 8; i++)
    {
          if (_ucByte & 0x80)
          {
                  I2C_SDA_1();
          }
          else
          {
                  I2C_SDA_0();
          }
          i2c_Delay();
          I2C_SCL_1();
          i2c_Delay();
          I2C_SCL_0();
          if (i == 7)
          {
                   I2C_SDA_1();
          }
          _ucByte <<= 1;
          i2c_Delay();
    }
}


uint8_t i2c_ReadByte(void)
{
    uint8_t i;
    uint8_t value;

    value = 0;
    for (i = 0; i < 8; i++)
    {
        value <<= 1;
        I2C_SCL_1();
        i2c_Delay();
        if (I2C_SDA_READ())
        {
                value++;
        }
        I2C_SCL_0();
        i2c_Delay();
    }

    return value;
}

/**
  * @brief  wait for i2c ack.
* @retval result:
  */
i2c_TypeDef i2c_WaitAck(void)
{
    i2c_TypeDef re = I2C_OK;

    I2C_SDA_1();        
    i2c_Delay();
    I2C_SCL_1();        
    i2c_Delay();
    if (I2C_SDA_READ())
    {
        re = I2C_FAIL;
    }
    I2C_SCL_0();
    i2c_Delay();
   
    return re;
}

void i2c_Ack(void)
{
    I2C_SDA_0();
    i2c_Delay();
    I2C_SCL_1();
    i2c_Delay();
    I2C_SCL_0();
    i2c_Delay();
    I2C_SDA_1();
}


void i2c_NAck(void)
{
    I2C_SDA_1();        
    i2c_Delay();
    I2C_SCL_1();
    i2c_Delay();
    I2C_SCL_0();
    i2c_Delay();
}

i2c_TypeDef i2c_CheckDevice(uint8_t _Address)
{
    i2c_TypeDef ucAck = I2C_FAIL;

    if (I2C_SDA_READ() && I2C_SCL_READ())
    {
        i2c_Start();               

        i2c_SendByte(_Address | I2C_WR);
        ucAck = i2c_WaitAck();        

        i2c_Stop();
    }
   
    return ucAck;
}

/******************************************************************************
* LOCAL FUNCTIONS
******************************************************************************/
static void i2c_Delay(void)
{
    uint8_t i;
    for (i = 0; i < 30; i++);
}

/* End of this file */
[/mw_shl_code]

I2C.h 文件:
[mw_shl_code=c,true]/** \file

$Id: I2C.c 40486 2018-08-08 13:50:21Z mingfei.tang2013@126.com $

Copyright (c)mingfei.tang2013@126.com Holding B.V.
All Rights Reserved.

This source code and any compilation or derivative thereof is the proprietary
information of mingfei.tang Holding B.V. and is confidential in nature.
Under no circumstances is this software to be combined with any
Open Source Software in any way or placed under an Open Source License
of any type without the express written permission of mingfei.tang Holding B.V.
*/
#ifndef __I2C_H
#define __I2C_H

/*******************************************************************************
* EXPORT INCLUDE FILES
*******************************************************************************/
#include "stm32f10x.h"

/*******************************************************************************
* LOCAL INCLUDE FILES
*******************************************************************************/


/******************************************************************************
* C++ DECLARATION WRAPPER
******************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
* EXPORTED MACROS AND DEFINITIONS
******************************************************************************/  
#define I2C_WR        0                /* Writing bit */
#define I2C_RD        1                /* read bit */

// measurement i2c status
typedef enum{
  I2C_OK = 0,
  I2C_FAIL = 1,
}i2c_TypeDef;
  
/******************************************************************************
* EXPORTED FUNCTIONS
******************************************************************************/  
void i2c_Init(void);
void i2c_Start(void);
void i2c_Stop(void);
void i2c_SendByte(uint8_t _ucByte);
uint8_t i2c_ReadByte(void);
i2c_TypeDef i2c_WaitAck(void);
void i2c_Ack(void);
void i2c_NAck(void);
i2c_TypeDef i2c_CheckDevice(uint8_t _Address);


/******************************************************************************
* END OF C++ DECLARATION WRAPPER
******************************************************************************/

#ifdef __cplusplus
}
#endif

#endif /* __I2C_H */  [/mw_shl_code]


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

32

主题

883

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4036
金钱
4036
注册时间
2015-11-14
在线时间
545 小时
发表于 2018-8-13 22:39:34 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-18 09:13

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表