我使用的是ATmage8芯片,AT24064存儲器, 在進行存儲器讀寫時發生停機。是何問題?
讀寫程序如下:(芯片資料中有例程) unsigned char i2cWrite(unsigned char Wdata,unsigned int RomAddress) { Start(); Wait(); if(TestAck()!=START) return 1; Write8Bit(wr_device_add); Wait(); if(TestAck()!=MT_SLA_ACK) return 1; Write8Bit(RomAddress>>8); Wait(); if(TestAck()!=MT_DATA_ACK) return 1; Write8Bit(RomAddress); Wait(); if(TestAck()!=MT_DATA_ACK) return 1; Write8Bit(Wdata); Wait(); if(TestAck()!=MT_DATA_ACK) return 1; Stop(); delay_ms(10); return 0; }
unsigned char i2cRead(unsigned int RomAddress) { unsigned char temp; Start(); Wait(); if (TestAck()!=START) return 0; Write8Bit(wr_device_add); Wait(); if (TestAck()!=MT_SLA_ACK) return 0; Write8Bit(RomAddress>>8); Wait(); if (TestAck()!=MT_DATA_ACK) return 0; Write8Bit(RomAddress); Wait(); if (TestAck()!=MT_DATA_ACK) return 0; Start(); Wait(); if (TestAck()!=RE_START) return 0; Write8Bit(rd_device_add); Wait(); if(TestAck()!=MR_SLA_ACK) return 0; Twi(); Wait(); if(TestAck()!=MR_DATA_NOACK) return 0; temp=TWDR; Stop(); return temp; } twi.h頭文件 #define START 0x08 #define RE_START 0x10 #define MT_SLA_ACK 0x18 #define MT_SLA_NOACK 0x20 #define MT_DATA_ACK 0x28 #define MT_DATA_NOACK 0x30 #define MR_SLA_ACK 0x40 #define MR_SLA_NOACK 0x48 #define MR_DATA_ACK 0x50 #define MR_DATA_NOACK 0x58 #define Start() (TWCR=(1<#define Stop() (TWCR=(1<#define Wait() {while(!(TWCR&(1<#define TestAck() (TWSR&0xf8) #define SetAck() (TWCR|=(1<#define SetNoAck() (TWCR&=~(1<#define Twi() (TWCR=(1<#define Write8Bit(x) {TWDR=(x);TWCR=(1<