ATmega328p Xplained Mini EEPROM的读写

作者: ddllxxrr
上传时间为: 2015-01-08 09:06 AM
2015-01-08
阅读:

根据数据手册上说,ATmega328P应带有1K的EEPROM,那么好不好用呢,今天来试一下。

数据手册上关于EEPROM有三个寄存器。

The EEPROM Address Registers – EEARH and EEARL specify the EEPROM address in the

256/512/512/1Kbytes EEPROM space. The EEPROM data bytes are addressed linearly between 0 and

255/511/511/1023. The initial value of EEAR is undefined. A proper value must be written before the EEPROM

may be accessed.

EEDR – The EEPROM Data Register

EECR – The EEPROM Control Register

然后,打开Studio6.2加入以下函数。

见程序清单:

#define  F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
	/* Wait for completion of previous write */
	while(EECR & (1<<EEPE))
	;
	/* Set up address and Data Registers */
	EEAR = uiAddress;
	EEDR = ucData;
	/* Write logical one to EEMPE */
	EECR |= (1<<EEMPE);
	/* Start eeprom write by setting EEPE */
	EECR |= (1<<EEPE);
}

unsigned char EEPROM_read(unsigned int uiAddress)
{
	/* Wait for completion of previous write */
	while(EECR & (1<<EEPE))
	;
	/* Set up address register */
	EEAR = uiAddress;
	/* Start eeprom read by writing EERE */
	EECR |= (1<<EERE);
	/* Return data from Data Register */
	return EEDR;
}

 unsigned char what;
int main(void)
{
    
	EEPROM_write(50,0x55);
	what = EEPROM_read(50);
	EEPROM_write(51,0x56);
	what = EEPROM_read(51);
	EEPROM_write(52,0x57);
	what = EEPROM_read(57);
	while(1)
    {
        
    }
}

运行结果如下:

经过计算器检查,0x56十进制就是86,一点错也没有

全部评论 ()
条评论
写评论

创建讨论帖子

登录 后参与评论
系统提示