Software simulation of I2C (based on the Microchip 24LC16B operation function library)

Publisher:星尘之泪Latest update time:2019-11-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This article uses software-simulated I2C to implement a function library for operating 24LC16B on the STM8L15x series chips.


Header file definition:


 #ifndef _24LC16B_H

#define _24LC16B_H


#include "Hal_I2CSoft.h"


#define MAXROM_24LC16B (2048)


extern void Hal24LC16BInit(I2CSoftConfig_t* i2cCfg);

extern u8 Hal24LC16B_ReadByte(u16 addr);

extern void Hal24LC16B_WriteByte(u16 addr, u8 data);

extern void Hal24LC16B_ReadBuf(u16 addr, u8 *buf, u16 len);

extern void Hal24LC16B_WriteBuf(u16 addr, u8 *buf, u16 len);

extern void Hal24L68B_WritePage(u16 addr, u8 *buf);


 #endif


Specific operation function:


/**

  ******************************************************************************

  * @f file 24LC16B.c

  * @Author huangqi

  * @Version V1.0.0

  * @Date 2014-12-20

  * @Description Microchip's 24LC16B operation function library

  ******************************************************************************

  */



#include "Hal_I2CSoft.h"

#include "24LC16B.h"



I2CSoftConfig_t *i2cCfg;


/********************************************************************************

 * @Function name Hal24LC16BInit

 *

 * @Function Initialize 24LC16B

 *

 * @parameter i2cCfg: I2CSoft port information

 *

 * @return value None

 *********************************************************************************/

void Hal24LC16BInit(I2CSoftConfig_t* i2cPortConfig)

{

  i2cCfg = i2cPortConfig;

  I2CSoft_Init(i2cCfg);

}


/********************************************************************************

 * @Function name Hal24LC16B_ReadByte

 *

 * @Function Read a byte from 24LC16B

 *

 * @param addr: address

 *

 * @return value The bytes read

 *********************************************************************************/


u8 Hal24LC16B_ReadByte(u16 addr)

{

  u8 tmp = 0;

  do

  {

    I2CSoft_Start(i2cCfg);

    I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1));//write command

  }

  while(I2CSoft_WaitAck(i2cCfg));

  I2CSoft_SendByte(i2cCfg, addr%256);

  I2CSoft_WaitAck(i2cCfg);


  I2CSoft_Start(i2cCfg);

  I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1)+1); //write command

  I2CSoft_WaitAck(i2cCfg);

  tmp = I2CSoft_RecvByte(i2cCfg,0);

  I2CSoft_Stop(i2cCfg);

  return tmp;

}


/********************************************************************************

 * @Function name Hal24LC16B_WriteByte

 *

 * @Function Write a byte to 24LC16B

 *

 * @param addr: address

 * data: data to be written

 *

 * @return value None

 *********************************************************************************/


void Hal24LC16B_WriteByte(u16 addr, u8 data)

{

  do

  {

    I2CSoft_Start(i2cCfg);

    I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1));//write command

  }

  while(I2CSoft_WaitAck(i2cCfg));

  I2CSoft_SendByte(i2cCfg, addr%256);

  I2CSoft_WaitAck(i2cCfg);

  I2CSoft_SendByte(i2cCfg, data); //write command

  I2CSoft_WaitAck(i2cCfg);

  I2CSoft_Stop(i2cCfg);

  //delay_us(3000);//Must wait for EEPROM writing to complete

}


/********************************************************************************

 * @Function name Hal24LC16B_WriteByte

 *

 * @Function Write a page to 24LC16B (16Byte in total)

 *

 * @param addr: address

 * data: data to be written

 *

 * @return value None

 *********************************************************************************/


void Hal24L68B_WritePage(u16 addr, u8 *buf)

{

  u8 cnt=16;

  if((addr+16)%256<16)

    return;

  do

  {

    I2CSoft_Start(i2cCfg);

    I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1));//write command

  }

  while(I2CSoft_WaitAck(i2cCfg));

  I2CSoft_SendByte(i2cCfg, addr%256);

  I2CSoft_WaitAck(i2cCfg);

  while(cnt--)

  {

    I2CSoft_SendByte(i2cCfg, *buf++); //write command

    I2CSoft_WaitAck(i2cCfg);

  }

  I2CSoft_Stop(i2cCfg);

  //delay_us(3000);//Must wait for EEPROM writing to complete

}


/********************************************************************************

 * @Function name Hal24LC16B_ReadBuf

 *

 * @Function Read data from 24LC16B

 *

 * @param addr: address

 * buf: read data

 * len: the length of the data to be read

 *

 * @return value None

 *********************************************************************************/


void Hal24LC16B_ReadBuf(u16 addr, u8 *buf, u16 len)

{

  u16 i = 0;


  if((len>0)&&(addr+len)>=(MAXROM_24LC16B-1))

    return;


  do

  {

    I2CSoft_Start(i2cCfg);

    I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1));//write command

  }

  while(I2CSoft_WaitAck(i2cCfg));


  I2CSoft_SendByte(i2cCfg, addr%256);

  I2CSoft_WaitAck(i2cCfg);

  I2CSoft_Start(i2cCfg);

  I2CSoft_SendByte(i2cCfg, 0xA0+((addr/256)<<1)+1); //write command

  I2CSoft_WaitAck(i2cCfg);


  for(i = 0; i  {

    *buf++ = I2CSoft_RecvByte(i2cCfg,1);

  }


  *buf = I2CSoft_RecvByte(i2cCfg,0);


  I2CSoft_Stop(i2cCfg);

}



/********************************************************************************

 * @Function name Hal24LC16B_WriteBuf

 *

 * @Function Write data to 24LC16B

 *

 * @param addr: address

 * buf: data to be written

 * len: length of the data to be written

 *

 * @return value None

 *********************************************************************************/


void Hal24LC16B_WriteBuf(u16 addr, u8* buf, u16 len)

{

  u16 i;

  if((len>0)&&(addr+len)>=(MAXROM_24LC16B-1))

    return;

  for(i=0; i  {

    Hal24LC16B_WriteByte(addr++,*buf++);

  }

}


 


I2C software simulation driver:


head File:


#ifndef _HAL_I2CSOFT_H

#define _HAL_I2CSOFT_H


#include "stm8l15x.h"


typedef struct I2CSoftConfig_t

{

  GPIO_TypeDef* SCL_Port;

  GPIO_Pin_TypeDef SCL_Pin;

  GPIO_TypeDef* SDA_Port;

  GPIO_Pin_TypeDef SDA_Pin;

}I2CSoftConfig_t;


//extern void delay_us(u16 time);

extern void I2CSoft_Init(I2CSoftConfig_t* i2cConfg);

extern void I2CSoft_Start(I2CSoftConfig_t *i2cPortConfig);

extern void I2CSoft_Stop(I2CSoftConfig_t *i2cPortConfig);

extern u8 I2CSoft_WaitAck(I2CSoftConfig_t *i2cPortConfig);

extern void I2CSoft_SendByte(I2CSoftConfig_t *i2cPortConfig, u8 sendByte);

extern u8 I2CSoft_RecvByte(I2CSoftConfig_t *i2cPortConfig, u8 ack);

#endif


 


I2C protocol implementation:


/**

  ******************************************************************************

  * @f file hal_Flash.c

  * @Author huangqi

  * @Version V1.0.0

  * @Date 2014-12-23

  * @description Simulate I2C related operations

  ******************************************************************************

  */


#include "Hal_I2CSoft.h"



void delay_us(u32 us)

{

    for( u32 x=us;x>0;x-- )

    {

      nop();nop();nop();nop();

    }

}



void I2CSoft_Init(I2CSoftConfig_t* i2cPortConfig)

{

  GPIO_Init(i2cPortConfig->SCL_Port,i2cPortConfig->SCL_Pin,GPIO_Mode_Out_PP_Low_Slow);//GPIO_Mode_Out_OD_Low_Slow);

  GPIO_Init(i2cPortConfig->SDA_Port,i2cPortConfig->SDA_Pin,GPIO_Mode_Out_OD_Low_Slow);//GPIO_Mode_Out_OD_Low_Slow);

}


void I2CSoft_Start(I2CSoftConfig_t* i2cPortConfig)

{

// i2cPortConfig->SDA_Port->DDR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

  delay_us(2);

  i2cPortConfig->SDA_Port->ODR &= ~i2cPortConfig->SDA_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

}



void I2CSoft_Stop(I2CSoftConfig_t* i2cPortConfig)

{


  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

// i2cPortConfig->SDA_Port->DDR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SDA_Port->ODR &= ~i2cPortConfig->SDA_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

  i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin;

  delay_us(2);

}



u8 I2CSoft_WaitAck(I2CSoftConfig_t* i2cPortConfig)

{

  u8 ucErrTime;


// i2cPortConfig->SDA_Port->DDR &= ~i2cPortConfig->SDA_Pin;


  i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin; //Open-drain output turns off NMOS and uses it as input

  //delay_us(2);

  i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

  delay_us(2);

  while(i2cPortConfig->SDA_Port->IDR & i2cPortConfig->SDA_Pin)

  {

    ucErrTime++;

    if(ucErrTime>250)

    {

      I2CSoft_Stop(i2cPortConfig);

      return 1;

    }

  }

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

//delay_us(2);

  return 0;

}



static void I2CSoft_Ack(I2CSoftConfig_t* i2cPortConfig)

{

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

//i2cPortConfig->SDA_Port->DDR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SDA_Port->ODR &= ~i2cPortConfig->SDA_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

}



static void I2CSoft_NoAck(I2CSoftConfig_t* i2cPortConfig)

{

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

// i2cPortConfig->SDA_Port->DDR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

  delay_us(2);

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

}


 


 


void I2CSoft_SendByte(I2CSoftConfig_t* i2cPortConfig, u8 sendByte)

{

  u8 i=8;

// i2cPortConfig->SDA_Port->DDR |= i2cPortConfig->SDA_Pin;

  i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

  delay_us(2);

  while(i--)

  {

    if(sendByte&(1<    {

      i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin;

    }

    else

    {

      i2cPortConfig->SDA_Port->ODR &= ~i2cPortConfig->SDA_Pin;

    }

    delay_us(2);

    i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

    delay_us(2);

    i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

  }

  delay_us(2);

 // return I2C_RecvACK();

}



//Read 1 byte, when ack=1, send ACK, when ack=0, send nACK

u8 I2CSoft_RecvByte(I2CSoftConfig_t* i2cPortConfig, u8 ack)

{

  u8 i=8,recvByte=0;

  i2cPortConfig->SDA_Port->ODR |= i2cPortConfig->SDA_Pin; //SDA is set as input

// i2cPortConfig->SDA_Port->DDR &= ~i2cPortConfig->SDA_Pin;

  while(i--)

  {


    i2cPortConfig->SCL_Port->ODR &= ~i2cPortConfig->SCL_Pin;

    delay_us(2);

    i2cPortConfig->SCL_Port->ODR |= i2cPortConfig->SCL_Pin;

[1] [2]
Reference address:Software simulation of I2C (based on the Microchip 24LC16B operation function library)

Previous article:STM8L LCD interface detailed explanation and driver
Next article:STM8 tool IAR--debugging

Recommended ReadingLatest update time:2024-11-16 07:37

Microchip's 21st China Masters Conference (MASTERs) is now open for registration
The annual conference will be held in Beijing (November 7-8), Shanghai (November 14-15) and Shenzhen (November 21-22), and will provide 38 technical courses for embedded control engineers. Microchip Technology Inc. (Microchip Technology) announced today that the top technical training event for embedd
[Embedded]
Microchip's 21st China Masters Conference (MASTERs) is now open for registration
What is Microchip's new analog peripheral manager? how to use?
We will discuss the new Analog Peripheral Manager (APM Analog Peripheral Manager) on the PIC18-Q71 device family to understand how it can be used to reduce overall power consumption in embedded designs. In the field of microcontrollers and embedded systems, power consumption has become a major concern for developers
[Microcontroller]
What is Microchip's new analog peripheral manager? how to use?
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号