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
RE:STM8L152 IO口模拟IIC程序
谢谢分享。;PRE:STM8L152 IO口模拟IIC程序
你做过IIC 从机中断接收数据的实验吗RE:STM8L152 IO口模拟IIC程序
好东西 怎么代码不全呢?还有如何实现一次发送多个字节数据? 代码不全啊
页:
[1]