📄 i2c_2432_0.c
字号:
#include <At89x52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
uchar idata *txdz1;
uchar idata buffer1[10]; // 数据缓冲区
sbit sda=P1^3;
sbit scl=P1^2;
static void dly5us()
{
nop();
nop();
nop();
nop();
nop();
nop();
}
static void start()
{
//EA=0;
sda=1;
nop();
nop();
scl=1;
dly5us();
sda=0;
dly5us();
scl=0;
nop();
nop();
nop();
}
static void stop()
{
sda=0;
nop();
nop();
scl=1;
dly5us();
sda=1;
dly5us();
}
static void sendbyte(uchar c)
{
uchar bitcnt;
for(bitcnt=0;bitcnt<8;bitcnt++)
{
if((c<<bitcnt)&0x80)
sda=1;
else
sda=0;
nop();
nop();
scl=1;
dly5us();
scl=0;
}
nop();
nop();
nop();
sda=1;
nop();
nop();
nop();
scl=1;
nop();
nop();
nop();
nop();
if(sda==1) ;
// ack=0;
else
// ack=1;
scl=0;
nop();
nop();
nop();
}
uchar rcvbyte()
{
uchar retc;
uchar bitcnt;
retc=0;
sda=1;
for(bitcnt=0;bitcnt<8;bitcnt++)
{
nop();
nop();
scl=0;
dly5us();
scl=1;
nop();
nop();
nop();
retc=retc<<1;
if(sda==1)retc=retc+1;
nop();
nop();
nop();
}
scl=0;
nop();
nop();
nop();
return(retc);
}
void zack(bit a)
{
if(a==0)sda=0;
else sda=1;
nop();
nop();
nop();
nop();
scl=1;
dly5us();
scl=0;
nop();
nop();
nop();
}
void readblock(uint address,uchar no)
{
union aa
{
uchar b[2];
uint addr;
}c;
uchar i;
c.addr=address;
start();
sendbyte(0xa0);
//if(ack==0){speak(2,2);return 0;}
sendbyte(c.b[0]);
//if(ack==0){speak(2,2);return 0;}
sendbyte(c.b[1]);
//if(ack==0){speak(2,2);return 0;}
start();
sendbyte(0xa1);
//if(ack==0){speak(2,2);return 0;}
for(i=0;i<no;i++)
{
*txdz1=rcvbyte();
zack(0);
txdz1++;
}
*txdz1=rcvbyte();
zack(1);
stop();
}
void writeblock(uint address,uchar no)
{
union aa
{
uchar b[2];
uint addr;
}c;
uint i;
c.addr=address;
start();
sendbyte(0xa0);
//if(ack==0){speak(2,2);return;}
sendbyte(c.b[0]);
// if(ack==0){speak(2,2);return;}
sendbyte(c.b[1]);
//if(ack==0){speak(2,2);return;}
for(i=0;i<no;i++)
{
sendbyte(*txdz1);
//if(ack==0){speak(2,2);return;}
txdz1++;
}
stop();
}
void tx1(uchar n)
{
uchar m;
for(m=0;m<n;m++)
{
SBUF = *txdz1;
while(TI==0) ;
TI=0;
txdz1++;
}
}
main()
{
TL1=0xfd;
TH1=0xfd;
TMOD=0x21; //tim1 方式二 (8位自动装入
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -