etangmail 发表于 2011-9-27 20:20:05

STM8L152 IO口模拟IIC程序

//IIC.h
#ifndef IIC_H
 #define IIC_H
 /********************************************************/
 /* Include File                    */
 /********************************************************/
 #include "stm8l15x.h"
 /********************************************************/
 /* Macro Define                   */
 /********************************************************/
 /********************************************************/
 /* Extern reference variable         */
 /********************************************************/
 /********************************************************/
 /* Extern reference Function Prototype      */
 /********************************************************/
 extern  void I2Cstart(void);
 extern void I2CStop(void);
 extern unsigned char I2cWriteByte(unsigned char fx_data);
 extern unsigned char I2cReadByte(unsigned char end_k);
 extern void IICDelay(void);
#endif
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////copy follow text and save as IIC.c file////////////
//IIC.c

/********************************************************/
/* Include File                    */
/********************************************************/
#include "IIC.h"
/********************************************************/
/* Macro Define                   */
/********************************************************/
/********************************************************/
/*     Prototype of function          */
/********************************************************/
void IICDelay(void);
/********************************************************/
/* RAM Define                    */
/********************************************************/
/********************************************************/
/* Function Define                */
/********************************************************/
/*******************************************************/
void I2Cstart(void)
{
 GPIO_Init(GPIOG,GPIO_Pin_5,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
 GPIO_Init(GPIOG,GPIO_Pin_4,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
 IICDelay();
 GPIO_Init(GPIOG,GPIO_Pin_4,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
}
//-------------------------------------------------------------------------
unsigned char I2cWriteByte(unsigned char ByteData)
{
 unsigned char BitCount;
 unsigned char Data;
 Data=ByteData;
 GPIO_Init(GPIOG,GPIO_Pin_4,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
 for (BitCount = 0; BitCount < 8; BitCount++)
 {
  GPIO_Init(GPIOG,GPIO_Pin_5,GPIO_Mode_Out_PP_Low_Slow);//set SCL pin as output low
  IICDelay();
  if (0x80 &Data)
  {
   GPIO_Init(GPIOG,GPIO_Pin_4,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
  }
  else
  {
   GPIO_Init(GPIOG,GPIO_Pin_4,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
  }
  IICDelay();                           
  GPIO_Init(GPIOG,GPIO_Pin_5,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
  Data

废鱼 发表于 2011-9-27 20:41:49

RE:STM8L152 IO口模拟IIC程序

谢谢分享。;P

jack-352831 发表于 2012-4-13 11:36:56

RE:STM8L152 IO口模拟IIC程序

你做过IIC 从机中断接收数据的实验吗

wj0755 发表于 2012-8-30 18:07:32

RE:STM8L152 IO口模拟IIC程序

好东西

zwchina111 发表于 2014-12-11 19:31:41

怎么代码不全呢?
还有如何实现一次发送多个字节数据?

无e所谓 发表于 2015-6-29 16:48:04

代码不全啊
页: [1]
查看完整版本: STM8L152 IO口模拟IIC程序