testiic.c

来自「单片机的I2C总线开发」· C语言 代码 · 共 81 行

C
81
字号
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char

sbit sda=P1^0;
sbit scl=P1^1;



void start ()
{
	sda=1;
	_nop_();
	scl=1;
	_nop_();
	sda=0;
	_nop_();
}

void stop()
{
	sda=0;
	_nop_();
	scl=1;
	_nop_();
	sda=1;
	_nop_();
}

bit send_byte(uchar date)
{
	uchar i;
	bit ack;
	for(i=0;i<8;i++)
	{

		if(date&0x80)
			sda=1;
		else
			sda=0;
		

		_nop_();
		scl=1;
		_nop_();
		_nop_();
		_nop_();
		date=date<<1;
		scl=0;

	}

	_nop_();
	sda=1;
	_nop_();
	scl=1;
	_nop_();
	_nop_();
	if(sda==1)
		ack=0;
	else
		ack=1;
	scl=0;
	return ack;	
}



void main()
{
	uchar date;
	while(1)
	{
		start();
		send_byte(0x88);
		send_byte(date);
		stop();
		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?