微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 前辈们帮帮忙,一个关于24c04的程序

前辈们帮帮忙,一个关于24c04的程序

时间:10-02 整理:3721RD 点击:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop4() {_nop_();_nop_();_nop_();_nop_();}
sbit scl=P1^0;
sbit sda=P1^1;
uchar dis[]={0xc0,0xa4,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dis2[];
//延时***********************************************************************************
void delay_ms(uint i)
{
uint j;
while(i--)
for (j=0;j<120;j++);
}
//启动信号********************************************************************************
void start()
{
sda=1;//sda和scl拉高释放总线
scl=1;
_nop_();
sda=0;//scl高电平期间scl负跳变表示开始
_nop_();
scl=0;
}
//停止信号****************************************************************************
void end()
{
sda=0;
scl=1;
_nop_();
sda=1;//scl高电平期间将sda拉高表示停止(总线释放)
_nop_();
}
//非应答**************************************
void no_ack()
{
sda=1;
scl=1;
nop4();
scl=0;
sda=0;
}
//应答信号************************************************************************************
void respon()
{
uchar i;
scl=1;
_nop_();
_nop_();
while((sda==1)&(i<120))
i++;
scl=0;
_nop_();
}
//写一个字节********************************************************************************
void write_byte(uchar dat)
{
uchar temp,i;
temp=dat;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
_nop_();
sda=CY;
scl=1;
_nop_();
}
  
}
//读一字节**********************************************************************************
int read_byte()
{
  uchar i,b;
  for(i=0;i<8;i++)
  {
   scl=1;
_nop_();
b=(b<<1)|sda;
scl=0;
nop4();
  }
  //_nop_();
// no_ack();
  return b;
}
//向某地址写入一个字节************************************************************************************
void write_a_byte(uchar add,uchar dat)
{
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
write_byte(dat);
respon();
end();

}
//向某地址读取一字节的数据*********************************************************************************
int read_a_byte(uchar add)
{   
    uint t;
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
start();
write_byte(0xa1);
respon();
t=read_byte();
no_ack();
end();
return t;
  
}
//主函数********************************************************************************
void main()
{
uint i,j,n;
for(i=0;i<10;i++)
{
   write_a_byte(i,dis[i]);
   
}
   
   
    //delay_ms(100);
     for(j=0;j<10;j++)
    {
     dis2[j]=read_a_byte(j);
   
  while(1)
    {
   for(n=0;n<10;n++)
   P2=dis2[n];
   delay_ms(50);
     }
   
  }
}
想把0到9写到24c04里然后读取通过数码管显示,数码管只显示了一个乱码

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top