查看: 1860|回复: 0

ATmega328p Xplained Mini EEPROM的读写

[复制链接]
  • TA的每日心情
    奋斗
    2023-12-3 18:51
  • 签到天数: 772 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2015-1-8 09:06:29 | 显示全部楼层 |阅读模式
    分享到:
    根据数据手册上说,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,一点错也没有
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-18 14:48 , Processed in 0.110983 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.