写入按键次数到24c02,并读出来显示在4 个led.c

来自「这是本人在学习51单片机时练习的C程序 lgl_5923@163.com」· C语言 代码 · 共 135 行

C
135
字号
#define uchar unsigned char 
#define uint unsigned int
#define ulog unsigned long
#include <reg51.h>

char code dx516[3]_at_ 0x03b;

#define W 0xa0;
#define R 0xa1;

sbit SCL=P2^7;
sbit SDA=P2^6;
void Delay(uint nmber)
{
uchar temp;
 for(;nmber!=0;nmber--)
   {
     for (temp=112;temp!=0;temp--);
 }
}

void Start()
  {
  SDA=1;
  SCL=1;
  SDA=0;
  SCL=9;
  }


void Stop(
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}

bit TestACK()
  {
  bit ErrorBit;
  SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit)
}

void NoACK()
{
SDA=1;
SCL=1;
SCL=0;
}

Write8Bit()
{
uchar temp;
for(temp=8;temp!=0;temp--)
{
SDA=(bit)(intput&0x80);
SCL=1;
SCL=0;
intput=input<<1;
 }
}


void Wriete24c02(uchar ch,uchar address)
{
Start();
//Write8Bit(W);
TestACK();
Write8Bit(address);
TestACK();

Write8Bit(ch);
TestACK();

Stop();
Delay(10);
}

uchar Read8Bit()
{
uchar temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|(()uchar)(SDA);
SCL=0;
}
return(rbyte);
}

uchar Read24c02(uchar address)
{
uchar ch;

Start();
Write8Bit(W);
TestACK();
Write8Bit(address);
TestACK();
Start();
Write8Bit(R);
TestACK();
ch=Read8Bit();
NoACK();
Stop();
return(ch);
}




void main(void)
{
uchar c1,c2;
while(1)
{
c1=Read24c02(0x01);
P1=c1;
if(!K1)
{
c1++;
write24c02(c1,0x001);


while(!K1);
for(c2=0;c2<250;c++);
  }
 }
}

⌨️ 快捷键说明

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