当前位置:首页 > 单片机 > 单片机
[导读]/**************************文件所用资源1.端口:P0.2,P0.32.调用delay_ms函数**************************//************************端口定义************************/sbit i2c_dat =P0^2;sbit i2c_clk =P0^3;#de

/**************************
文件所用资源
1.端口:P0.2,P0.3
2.调用delay_ms函数
**************************/
/************************
端口定义
************************/
sbit i2c_dat =P0^2;
sbit i2c_clk =P0^3;
#define IIC_TIME10//IIC操作延时
//========在此设定芯片地址=============
#define W_ADD_COM 0xa0//写字节命令及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 0
#define R_ADD_COM 0xa1//读命令字节及器件地址(根据地址实际情况改变), 1010 A2 A1 A0 1
//=======在此设定芯片型号, 1代表24C01; 16代表24C16; 512代表24C512
#define e2prom 256// <---在此设定芯片型号, 1代表24C01; 16代表24C16; 512代表24C512
#if e2prom==1
#define PAGE_SIZE 8
#define SIZE 0x007f
#elif e2prom==2
#define PAGE_SIZE 8
#define SIZE 0x00ff
#elif e2prom==4
#define PAGE_SIZE 16
#define SIZE 0x01ff
#elif e2prom==8
#define PAGE_SIZE 16
#define SIZE 0x03ff
#elif e2prom==16
#define PAGE_SIZE 16
#define SIZE 0x07ff
#elif e2prom==32
#define PAGE_SIZE 32
#define SIZE 0x0fff
#elif e2prom==64
#define PAGE_SIZE 32
#define SIZE 0x1fff
#elif e2prom==128
#define PAGE_SIZE 64
#define SIZE 0x3fff
#elif e2prom==256
#define PAGE_SIZE 64
#define SIZE 0x7fff
#elif e2prom==512
#define PAGE_SIZE 128
#define SIZE 0xffff
#endif
/************************
24c256 start
************************/
void start()
{
i2c_dat=1;//数据
i2c_clk=1;//时钟
i2c_dat=0;//数据
i2c_clk=0;//时钟
}
/************************
24c256 stop
************************/
void stop()
{
i2c_dat=0;//数据
i2c_clk=1;//时钟
i2c_dat=1;//数据
}
/************************
24c256 mack
************************/
void mack()
{
i2c_dat=0;//数据
i2c_clk=1;//时钟
i2c_clk=0;//时钟
}
/************************
24c256 mnack
************************/
void mnack()
{
i2c_dat=1;//数据
i2c_clk=1;//时钟
i2c_clk=0;//时钟
}
/************************
24c256 ack
************************/
void ask()
{
uint i;
i2c_dat=1;//数据
i2c_clk=1;//时钟
for(i=0;i<200;i++)
{
if(!i2c_dat)break;
}
i2c_clk=0;//时钟
}
/************************
24c256写一个字节
************************/
void write_i2c_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if(dat & 0x80)
i2c_dat=1;//数据
else i2c_dat=0;
i2c_clk=1;//时钟
dat = dat << 1;
i2c_clk=0;//时钟
}
}
/************************
24c256写一个字
************************/
void write_i2c_word(uint x)
{
uchar i,j;
for(j=0;j<2;j++)
{
for(i=0;i<8;i++)
{
if(x & 0x8000)
i2c_dat=1;//数据
else i2c_dat=0;
i2c_clk=1;//时钟
x = x << 1;
i2c_clk=0;//时钟
}
ask();
}
}
/************************
24c256读一个字节
************************/
uchar read_i2c_byte()
{
uchar i,dat=0;
for(i=0;i<8;i++)
{
i2c_dat=1;//数据
i2c_clk=1;//时钟
dat=dat<<1;
if(i2c_dat)dat=dat|0x01;
i2c_clk=0;//时钟
}
return(dat);
}
/*****************************
读24c256指定地址的一个字节
*****************************/
uchar read_i2c_dat(uint addrr)
{
uchar dat;
start();
write_i2c_byte(W_ADD_COM);
ask();
write_i2c_word(addrr);
start();
write_i2c_byte(R_ADD_COM);
ask();
dat=read_i2c_byte();
mnack();
stop();
return (dat);
}
/*****************************
写24c256指定地址的一个字节
*****************************/
void write_i2c_dat(uint addrr,uchar dat)
{
start();
write_i2c_byte(W_ADD_COM);
ask();
write_i2c_word(addrr);
write_i2c_byte(dat);
ask();
stop();
}
/***********************************
读24c256指定地址连续读取N个字节
***********************************/
void read_i2c_n_dat(uint addrr,uint number,uchar *p)
{
uint i;
start();
write_i2c_byte(W_ADD_COM);
ask();
write_i2c_word(addrr);
start();
write_i2c_byte(R_ADD_COM);
ask();
for(i=0;i<(number-1);i++)
{
*p++=read_i2c_byte();
mack();
}
*p++=read_i2c_byte();
mnack();
stop();
}
/*********************************
从24c256指定地址连续写N个字节
*********************************/
void write_i2c_n_dat(uint addrr,uint number,uchar *p)
{
uint i;
start();
write_i2c_byte(W_ADD_COM);
ask();
write_i2c_word(addrr);
for(i=0;i{
write_i2c_byte(*p++);
ask();
addrr++;
if(addrr%PAGE_SIZE==0)
{
stop();
delay_ms(IIC_TIME);
start();
write_i2c_byte(W_ADD_COM);
ask();
write_i2c_word(addrr);
}
}
stop();
}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

/**************************文件所用资源1.端口:P22.调用delay_ms函数**************************/#include #include #define key...

关键字: 4*4 c51程序 矩阵键盘

/**************************文件所用资源1.端口:P0.4,P0.5,P0.6,P0.72.调用delay_ms函数**************************//***********...

关键字: c51程序 io口 模拟spi通信

/********************************文件所用资源1.外部中断0、1 定时中断0、1 串口中断2.端口:P3.0,P3.1,P3.3,P3.4,P3.5********************...

关键字: c51程序 串口中断 外部中断 定时中断

模块内接口:使用如下标志符:asm汇编语句endasm注意:如果在程序中使用了,注意在Keil编译器中需要激活Properties中的“GenerateAssemblerFile”和“AssemblerFile”两个选项...

关键字: keil c51程序 嵌入汇编

关于spi协议见:http://hi.baidu.com/gilbertjuly/blog/item/0be222eeac9abae5cf1b3e38.html ISD4002芯片资料参考:http://downloa...

关键字: c51程序 spi总线 单片机 发送数据

typedef struct PID{ double SetPoint; // Desired Value double Proportion; // Proportional Const double Integral...

关键字: c51程序 pid调节

/*--------------------------24C01的IIC 读写的c51程序---------------------程序中很多NOP是冗余的,希望读者能进一步精简,但必须经过验证。 Atmel 24C...

关键字: c51程序 单片机 读写24c01

void X5045SpiOpen(void);//打开X5045片选void X5045SpiClose(void);//关闭X5045片选 void X5045WriteEnable(void);//软件使能X50...

关键字: c51程序 x5045 读写一体化

/*------------------------------------------------------------------------------为了安全起见,程序中很多NOP是冗余的,希望读者能进一步精简...

关键字: c51程序 iic 读写 24c01

/**************************文件所用资源1.外部中断02.端口:P3.3、P3.4**************************/sbit BT_REC =P3^3;//接收 P3.0sb...

关键字: c51程序 io口 模拟串口
关闭
关闭