自己按照eeprom的格式进行了设计,单字节的读写没有问题。可是对多字节连续读写,发现 数组中的偶数都丢失为0 了。
void EEPROM_Write_Byte(unsigned int uiAddress, unsigned char ucData)
{
/* 等待上一次写操作结束 */
while(EECR & (1<<EEWE))
;
/* 设置地址和数据寄存器*/
EEAR = uiAddress;
EEDR = ucData;
/* 置位EEMWE */
EECR |= (1<<EEMWE);
/* 置位EEWE 以启动写操作*/
EECR |= (1<<EEWE);
asm("nop");
asm("nop");asm("nop");
asm("nop");
}
//指定地址和指定数组写n个数据
void EEPROM_Write_Nbyte(unsigned int src_addres, unsigned int *dst_addres,unsigned char num)
{ unsigned char i=0;
for(i=0;i<num;i++)
{
EEPROM_Write_Byte((src_addres+i),*(dst_addres+i));
}
}
unsigned char EEPROM_Read_Byte(unsigned int uiAddress)
{
/* 等待上一次写操作结束 */
while(EECR & (1<<EEWE))
;
/* 设置地址寄存器*/
EEAR = uiAddress;
/* 设置EERE 以启动读操作*/
EECR |= (1<<EERE);
/* 自数据寄存器返回数据 */
asm("nop");
asm("nop");asm("nop");
asm("nop");
return EEDR;
}
//读n个数据,从源地址中读n个字节到目的数组
void EEPROM_Read_Nbyte(unsigned int src_addres, unsigned int *dst_addres,unsigned char num)
{unsigned char i;
for(i=0;i<num;i++)
{
*(dst_addres+i)=EEPROM_Read_Byte((src_addres+i));
asm("nop");
asm("nop");asm("nop");
asm("nop");
}
}
int main(void)
{
uchar i;
uchar Read_Buff[8] = {0,0,0,0,0,0,0,0}; //读取数据缓冲区
uchar Write_Buff[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; //写入数据缓冲区
EEPROM_Write_Byte(0x0a,0x02);//仅写一个数据
i=EEPROM_Read_Byte(0x0a);
Uart_Init(9600);//初始化串口
Uart_SendB(i+48);
Uart_Senddata_Changeline();//屏幕换行
EEPROM_Write_Nbyte(0x00,Write_Buff,8);
EEPROM_Read_Nbyte(0x00,Read_Buff,8);
for(i=0;i<8;i++)
{
Uart_SendB(*(Read_Buff+i)+48);//显示数字
_delay_ms(500);
Uart_Senddata_Changeline();//屏幕换行
}
Uart_SendB('a');
Uart_SendB('a');
Uart_SendB('a');
Uart_SendB('a');
while(1)
{
}
} |