哪位有at24c32的驱动程序啊 帮我个忙 单片机虚拟iic总线的 我的邮箱18947115719@189..cn谢谢 5
谢谢。。。 展开
#include<reg52.h>
#include<D:\C51_H\LCD1602.H> //我的自定义LCD1602头文件
/*---------------------------------------------------------------
24C32可存储4K(1024*4)个字节(8bit),因此寻址地址最大为0x0FFF,24C32为从机。
----------------------------------------------------------------*/
#define WriteDeviceAddress 0x0A2 //定义器件在I2C总线中的写地址(注意:根据自定义从机地址接口改变)
#define ReadDviceAddress 0x0A3 //定义器件在I2C总线中的读地址(注意:根据自定义从机地址接口改变)
sbit SCL=P0^4; //我的24C32接口设置
sbit SDA=P3^7;
//2us延时子程序
void delay_2us(uchar i)
{
while(--i);
}
//开始总线
void Start()
{
SDA=1;
SCL=1;
delay_2us(1);
SDA=0;
delay_2us(1);
SCL=0;
}
//结束总线
void Stop()
{
SDA=0;
SCL=1;
delay_2us(1);
SDA=1;
delay_2us(1);
SDA=0;
SCL=0;
}
//发送应答位信号
//void MACK()
//{
//SDA=0;
//SCL=1;
//delay_2us(1);
//SCL=0;
//SDA=1;
//}
//发送非应答位信号
void MNACK()
{
SDA=1;
SCL=1;
delay_2us(1);
SCL=0;
SDA=0;
}
//检测从机应答信号
bit CACK()
{
bit flag;
SDA=1;
SCL=1;
delay_2us(1);
flag=SDA;
SCL=0;
return(flag);
}
/*--------------------------------------------------------------------------
//写入8个bit到24C32
---------------------------------------------------------------------------*/
Write8Bit(uchar input)
{
uchar temp;
for(temp=0;temp<8;temp++)
{
SDA=(bit)(input&0x80);
SCL=1;
delay_2us(1);
SCL=0;
input=input<<1;
}
}
/*--------------------------------------------------------------------------
//写入一个字节到24C32中
---------------------------------------------------------------------------*/
void Write24C32(uchar ch,uint address)
{
Start();
Write8Bit(WriteDeviceAddress);
while(CACK());
Write8Bit(address/256);(注意:地址分两次传送,高位在前!)
while(CACK());
Write8Bit(address%256);
while(CACK());
Write8Bit(ch);
while(CACK());
Stop();
}
/*--------------------------------------------------------------------------
//从24C32中读出8个bit
---------------------------------------------------------------------------*/
uchar Read8Bit()
{
unsigned char temp,rbyte=0;
for(temp=0;temp<8;temp++)
{
SDA=1;
SCL=1;
rbyte=rbyte|((unsigned char)(SDA));
if(temp<7) //注意:仅左移7次
{
rbyte=rbyte<<1;
}
SCL=0;
}
return(rbyte);
}
/*--------------------------------------------------------------------------
从24c32中读出1个字节
---------------------------------------------------------------------------*/
uchar Read24C32(uint address)
{
uchar ch;
Start();
Write8Bit(WriteDeviceAddress);
while(CACK());
Write8Bit(address/256);
while(CACK());
Write8Bit(address%256);
while(CACK());
Start();
Write8Bit(ReadDviceAddress);
while(CACK());
ch=Read8Bit();
MNACK();
Stop();
return(ch);
}
/*--------------------------------------------------------------------------
主函数
---------------------------------------------------------------------------*/
void main(void) // 主程序
{
uchar newchar;
LCD1602_init(); //1602初始化
Write24C32('8',0x01FF); //写'5'进0x1FF
LCD1602_puts(0,0,"24C32 W_Test OK!");
newchar=Read24C32(0x01FF); //从0x1FF接收数据
LCD1602_puts(0,1,"ReadValue=");
LCD1602_puts(10,1,newchar);
while(1);
}
邮箱的我也发了,这里还是要贴出来。可以给大家一个参考!
注明一下:我这里使用的是LCD1602显示的是ReadValue,使用时SCL和SDA加10K的上拉电阻,否则无法确定总线空闲哦!!
2023-06-12 广告