IIC通信


#include "reg52.h"

sbit SDA = P2^0;
sbit SCL = P2^1;

void IIC_Start(void);
void IIC_Stop(void);
void IIC_SendOneByte(unsigned char);

void IIC_delayus(unsigned int num)
{
	while(num--);
}

void main()
{
	IIC_Start();
	IIC_SendOneByte(0x78);
	IIC_SendOneByte(0xff);
	IIC_Stop();
	
	IIC_Start();
	IIC_SendOneByte(0x89);
	IIC_SendOneByte(0xee);
	IIC_Stop();
	
	while(1);
}

void IIC_Start(void)
{
	SDA = 1;
	SCL = 1;
	IIC_delayus(2);
	SDA = 0;
	SCL = 0; // 拉下来
}

void IIC_Stop(void)
{
	SDA = 0;
	SCL = 1;
	IIC_delayus(2);
	SDA = 1;
	SCL = 1;
}

void IIC_SendOneByte(unsigned char dat)
{
	unsigned char i;
	for(i = 0; i < 8; i++)
	{
		if(dat & 0x80)
			SDA = 1;
		else
			SDA = 0;
		SCL = 1;
		IIC_delayus(2);
		SCL = 0;
		dat = dat << 1;
	}
	SDA = 0; // 有需要的话这里也许要拉成1
	SCL = 1;
	IIC_delayus(1);
	SCL = 0;
}