【晒FRAM铁电存储器样片】+ 模拟IIC时序控制成功
- UID
- 753323
- 性别
- 女
|
【晒FRAM铁电存储器样片】+ 模拟IIC时序控制成功
本帖最后由 youki1234 于 2014-8-1 16:48 编辑
要用一块芯片最简单的就是用51来控制啦。下面就是我用的51模拟时序读取这块FRAM的代码
void delay_2us(uchar i)
{
while(--i);
}
————————————————————————————————————
延时2us
————————————————————————————————————
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 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);
}
——————————————————————————————————
发送应答位
——————————————————————————————————
/*--------------------------------------------------------------------------
下面为控制FRAM的代码,写一个字节
---------------------------------------------------------------------------*/
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;
}
}
/*--------------------------------------------------------------------------
//往FRAM中某个地址写一个字节
---------------------------------------------------------------------------*/
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();
}
/*-----------------------------------——————————————————
//读一个字节
---------------------------------------------------------------------------*/
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);
}
/*--------------------------------------------------------------------------
从某个地址开始读取一个字节
---------------------------------------------------------------------------*/
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) // ???
{
while(1)
{
Write24C32('7',0xfff); //往地址中写个字符7
}
}
大家看到,函数都是用24C32来取名的,没错,其实这个程序我本来是用来写24C32的,但是FRAM和24C32实在是太像了,连地址都一样,所以就直接拿来用啦。。。这也从另外一个方面说明FRAM和EEPROM是完全兼容的!
main.rar (759 Bytes)
|
|
|
|
|
|