这是一个eeprom的读写程序,用的是M68hc12芯片,希望通过它的spi与eeprom(93lc66)进行读写操作.但实际效果是在进行写操作时,从示波器上看,CLK和DI引脚的波形均正常,但查看eeprom却发现并没有数据写入.在进行读操作时,DI,CLK的波形正常,但DO却始终是高电平(感到奇怪).实在不知道哪里错了,还请熟悉这块芯片的大虾帮忙看看!(郁闷中...)
void main(void) { /**初始化**/ DDRS=0X60; DDRP_DDRP2=1; PTP_PTP2=1; SPICR1=0X5C; SPIBR=0X01; SPISR_SPIF=1; WRITE(0xaa,0x55,0x10); //READ(0x20); for(;;); } /**EWDS**/ void EWDS() { buf[1]=0x04; buf[2]=0x00; SPI_InOut(buf,2); }
/**EWEN**/ void EWEN() { buf[1]=0x04; buf[2]=0xc0; SPI_InOut(buf,2); }
/**READ**/ unsigned int READ(byte address) { unsigned char temp; unsigned int read_data; buf[1]=0x06; buf[2]=address; buf[3]=0x00; buf[4]=0x00; buf[5]=0x00; SPI_InOut(buf,5); buf[3]=buf[3]<<1; temp=buf[4]>>7; buf[1]=buf[3]|temp; buf[4]=buf[4]<<1; buf[2]=buf[4]|buf[5]; read_data=buf[1]; read_data=read_data<<8; read_data=read_data|buf[2]; return read_data; }
/**WRITE**/ void WRITE(unsigned char data1,unsigned char data2,unsigned char adress) { EWEN(); buf[1]=0x05; buf[2]=adress; buf[3]=data1; buf[4]=data2; SPI_InOut(buf,4); delay(1000); EWDS(); }
/** SPI数据传送子程序 **/ void SPI_InOut(byte SPI_Data[], byte n) { byte i; for (i=1; i<=n; i++) { SPIDR = SPI_Data; while (SPISR_SPIF == 0); SPI_Data = SPIDR; } }
/** DELAY **/ void delay(unsigned int num) { while(num!=0){num=num-1;} }
|