|
如下面例程,我调用写一个字节函数AT24C64WritePage()成功写入AT24C256,用示波器看,写一个字字节波形正确.
然后调用AT24C256ReadPage() 读个这个字节,能够成功读出并返回正确值,但用示波器看,发出结束指今后,总线SDA没有被拉高,一直为低.而且再次调用AT24C64WritePage() 或 AT24C256ReadPage() 则不能退出.请大家指点一下,问题出在那了呢?谢谢.
void AT24C256StartPage(uint page)
{
uchar i;
//------开始-----
TWCR = (1<<TWINT)|(1<<TWSTA)| (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
i=TWSR;
if ((i & 0xF8) != 0x08) return;
UART_putHex(i&0xf8);
//------从机地址-----
TWDR = 0xa0;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x18) return;
UART_putHex(i&0xf8);
//------存储器地址H-----
TWDR = (page*64)/256;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x28) return;
UART_putHex(i&0xf8);
//------存储器地址L-----
TWDR = (page*64)%256;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x28) return;
UART_putHex(i&0xf8);
}
void AT24C256ReadPage(unsigned char page,unsigned char *p,unsigned char size)
{
uchar i;
AT24C256StartPage((page>>1));
//------开始-----
TWCR = (1<<TWINT)|(1<<TWSTA)| (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
i=TWSR;
if ((i & 0xF8) != 0x10) return;
UART_putHex(i&0xf8);
//------从机地址-----
TWDR = 0xa1;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x40) return;
UART_putHex(i&0xf8);
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x50) return;
UART_putHex(i&0xf8);
*p = TWDR;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
void AT24C256WritePage(uint page,unsigned char *p,unsigned char size)
{
uchar i;
AT24C256StartPage(page>>1);
TWDR = *p;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))) ;
i=TWSR;
if ((i & 0xF8) != 0x28) return;
UART_putHex(i&0xf8);
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|