OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 10487|回复: 11

请教一个关于片内EEPROM的问题

[复制链接]

46

主题

1579

帖子

1

精华

金牌会员

Rank: 6Rank: 6

积分
1970
金钱
1970
注册时间
2011-7-17
在线时间
4 小时
发表于 2011-10-1 20:28:44 | 显示全部楼层 |阅读模式
 在WinAVR GCC里如何对EEPROM进行写操作,我成功读出,但就是写不进去。
Time?And?Relative?Dimension?In?Space.
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165377
金钱
165377
注册时间
2010-12-1
在线时间
2111 小时
发表于 2011-10-1 21:08:20 | 显示全部楼层
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

46

主题

1579

帖子

1

精华

金牌会员

Rank: 6Rank: 6

积分
1970
金钱
1970
注册时间
2011-7-17
在线时间
4 小时
 楼主| 发表于 2011-10-2 08:03:49 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
呃,数据手册上的写例程无效,读例程有效。
Time?And?Relative?Dimension?In?Space.
回复 支持 反对

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165377
金钱
165377
注册时间
2010-12-1
在线时间
2111 小时
发表于 2011-10-2 21:43:42 | 显示全部楼层
回复【3楼】Tardis:
---------------------------------
这个百度下吧,应该有其他人用到过的.
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

46

主题

1579

帖子

1

精华

金牌会员

Rank: 6Rank: 6

积分
1970
金钱
1970
注册时间
2011-7-17
在线时间
4 小时
 楼主| 发表于 2011-10-3 15:25:52 | 显示全部楼层
回复 支持 反对

使用道具 举报

1

主题

8

帖子

0

精华

新手上路

积分
32
金钱
32
注册时间
2011-10-7
在线时间
0 小时
发表于 2011-10-13 19:08:40 | 显示全部楼层
我写过stm8的,不知一样吧,做一下小提示,你在写之前是否要对EEPROM解锁,其次写的地址与读的地址是否一致,搞清楚,基本读写时不同芯片读写顺序都是有要求的。主要是后面的事项常常容易被忽视。
我行我思皆因我所爱
回复 支持 反对

使用道具 举报

46

主题

1579

帖子

1

精华

金牌会员

Rank: 6Rank: 6

积分
1970
金钱
1970
注册时间
2011-7-17
在线时间
4 小时
 楼主| 发表于 2011-10-13 19:15:49 | 显示全部楼层
回复【6楼】瀑谷:
---------------------------------
已经解决了,要包含avr/eeprom.h
然后用eeprom_read_byte ((void)*add)来读取,eeprom_write_byte((void)*add,dat)来写。
Time?And?Relative?Dimension?In?Space.
回复 支持 反对

使用道具 举报

5

主题

10

帖子

0

精华

初级会员

Rank: 2

积分
63
金钱
63
注册时间
2012-4-26
在线时间
4 小时
发表于 2012-4-27 09:15:54 | 显示全部楼层
CV相对比较方便,只要定义成eeprom类型的变量或数组就行了
回复 支持 反对

使用道具 举报

2

主题

12

帖子

0

精华

初级会员

Rank: 2

积分
65
金钱
65
注册时间
2012-7-6
在线时间
5 小时
发表于 2012-7-20 15:21:56 | 显示全部楼层
#define     E2_END               E2END
#define     _EEPROM_TYPE_        __attribute__((section(".eeprom")))
#define     EEPROM_BLANK_BYTE    0xFF

//_____ M A C R O S ____________________________________________________________

  //--- Eeprom basic functions
U16 _EPROvertimer ; //= (F_CPU/4e3/10)*100; //100ms超时
#define     Eeprom_prog_completed       {\
_EPROvertimer = (F_CPU/4e3/10)*100; \
while ((EECR & (1<<EEWE))&&(_EPROvertimer > 0)){ \
_EPROvertimer --; \
} \
}

  //--- Eeprom functions
#define     Eeprom_rd_byte(addr)                    \
            ({                                      \
                U8 result;                          \
                asm                                 \
                (                                   \
                    "movw r30, %1"    "\n\t"        \
                    "out %2, r30"     "\n\t"        \
                    "out %3, r31"     "\n\t"        \
                    "sbi %4, %5"      "\n\t"        \
                    "in  %0, %6"      "\n\t"        \
                 : "=r" (result)                 \
                 : "r" ((U16)addr),              \
                      "I" (_SFR_IO_ADDR(EEARL)),    \
                      "I" (_SFR_IO_ADDR(EEARH)),    \
                      "I" (_SFR_IO_ADDR(EECR)),     \
                      "M" (EERE),                   \
                      "I" (_SFR_IO_ADDR(EEDR))      \
                 : "r30", "r31"                  \
                );                                  \
                result;                             \
            })

#define     Eeprom_wr_byte(addr, data)              \
            ({                                      \
                asm                                 \
                (                                   \
                    "movw r30, %0"    "\n\t"        \
                    "out %2, r30"     "\n\t"        \
                    "out %3, r31"     "\n\t"        \
                    "out %4, %1"      "\n\t"        \
                    "sbi %5, %6"      "\n\t"        \
                    "sbi %5, %7"      "\n\t"        \
                 :                               \
                 : "r" ((U16)addr),              \
                      "r" (data),                   \
                      "I" (_SFR_IO_ADDR(EEARL)),    \
                      "I" (_SFR_IO_ADDR(EEARH)),    \
                      "I" (_SFR_IO_ADDR(EEDR)),     \
                      "I" (_SFR_IO_ADDR(EECR)),     \
                      "M" (EEMWE),                  \
                      "M" (EEWE)                    \
                 : "r30", "r31"                  \
                );                                  \
            })

//------------------------------------------------------------------------------
//_____ D E C L A R A T I O N S ________________________________________________

//  @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address to read the byte in Eeprom memory.
//!
//! @return:    Value read in the Eeprom memory.
//!
extern U8 eeprom_rd_byte(U16 addr_byte);

//------------------------------------------------------------------------------
//  @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address in Eeprom memory to write the byte.
//!         value       Value to write in the Eeprom memory.
//!
//! @return none
//!
extern void eeprom_wr_byte(U16 addr_byte, U8 value);

//------------------------------------------------------------------------------
//  @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: src    Start address in Eeprom memory where write data.
//!         *dest  Address of data to write.
//!         n      number of byte to write.
//!
//! @return none
//!
extern void eeprom_rd_block(U16 src, U8* dest, U16 n);

//------------------------------------------------------------------------------
//  @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: *src   Address of data to write.
//!         dest   Start address in Eeprom memory where write data.
//!         n      number of byte to write.
//!
//! @return none
//!
extern void eeprom_wr_block(U8* src, U16 dest, U16 n);

//------------------------------------------------------------------------------
//  @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
extern void eeprom_full_erase(void);



.c

//_____ F U N C T I O N S ______________________________________________________

//------------------------------------------------------------------------------
//  @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address to read the byte in Eeprom memory.
//!
//! @return:    Value read in the Eeprom memory.
//!
//------------------------------------------------------------------------------
U8 eeprom_rd_byte(U16 addr_byte)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
    Eeprom_prog_completed
    return (Eeprom_rd_byte(addr_byte));
    //sei();
}

//------------------------------------------------------------------------------
//  @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address in Eeprom memory to write the byte.
//!         value       Value to write in the Eeprom memory.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_byte(U16 addr_byte, U8 value)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
    Eeprom_prog_completed
    Eeprom_wr_byte(addr_byte, value);
    //sei();
}

//------------------------------------------------------------------------------
//  @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: src    Start address in Eeprom memory where write data.
//!         *dest  Address of data to write.
//!         n      number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_rd_block(U16 src, U8* dest, U16 n)
{
    for (; n != 0 ; n--)
    {
       *dest = eeprom_rd_byte(src);
        src++; dest++;
        //wdt_reset();
    }
}
//------------------------------------------------------------------------------
//  @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: *src   Address of data to write.
//!         dest   Start address in Eeprom memory where write data.
//!         n      number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_block(U8* src, U16 dest, U16 n)
{
    for (; n != 0 ; n--)
    {
        eeprom_wr_byte(dest,*src);
        src++; dest++;
    }
}

//------------------------------------------------------------------------------
//  @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_full_erase(void)
{
U16 addr;

    for(addr = 0; addr < (U16)(E2_END+1); addr++)
    {
        if ((eeprom_rd_byte(addr)) != EEPROM_BLANK_BYTE)
        {
            eeprom_wr_byte(addr, EEPROM_BLANK_BYTE);
        }
    }
}


供参考啊
GPS+GSM+NLJ
回复 支持 反对

使用道具 举报

2

主题

12

帖子

0

精华

初级会员

Rank: 2

积分
65
金钱
65
注册时间
2012-7-6
在线时间
5 小时
发表于 2012-7-20 15:22:32 | 显示全部楼层
#define     E2_END               E2END
#define     _EEPROM_TYPE_        __attribute__((section(".eeprom")))
#define     EEPROM_BLANK_BYTE    0xFF

//_____ M A C R O S ____________________________________________________________

  //--- Eeprom basic functions
U16 _EPROvertimer ; //= (F_CPU/4e3/10)*100; //100ms超时
#define     Eeprom_prog_completed       {\
_EPROvertimer = (F_CPU/4e3/10)*100; \
while ((EECR & (1<<EEWE))&&(_EPROvertimer > 0)){ \
_EPROvertimer --; \
} \
}

  //--- Eeprom functions
#define     Eeprom_rd_byte(addr)                    \
            ({                                      \
                U8 result;                          \
                asm                                 \
                (                                   \
                    "movw r30, %1"    "\n\t"        \
                    "out %2, r30"     "\n\t"        \
                    "out %3, r31"     "\n\t"        \
                    "sbi %4, %5"      "\n\t"        \
                    "in  %0, %6"      "\n\t"        \
                 : "=r" (result)                 \
                 : "r" ((U16)addr),              \
                      "I" (_SFR_IO_ADDR(EEARL)),    \
                      "I" (_SFR_IO_ADDR(EEARH)),    \
                      "I" (_SFR_IO_ADDR(EECR)),     \
                      "M" (EERE),                   \
                      "I" (_SFR_IO_ADDR(EEDR))      \
                 : "r30", "r31"                  \
                );                                  \
                result;                             \
            })

#define     Eeprom_wr_byte(addr, data)              \
            ({                                      \
                asm                                 \
                (                                   \
                    "movw r30, %0"    "\n\t"        \
                    "out %2, r30"     "\n\t"        \
                    "out %3, r31"     "\n\t"        \
                    "out %4, %1"      "\n\t"        \
                    "sbi %5, %6"      "\n\t"        \
                    "sbi %5, %7"      "\n\t"        \
                 :                               \
                 : "r" ((U16)addr),              \
                      "r" (data),                   \
                      "I" (_SFR_IO_ADDR(EEARL)),    \
                      "I" (_SFR_IO_ADDR(EEARH)),    \
                      "I" (_SFR_IO_ADDR(EEDR)),     \
                      "I" (_SFR_IO_ADDR(EECR)),     \
                      "M" (EEMWE),                  \
                      "M" (EEWE)                    \
                 : "r30", "r31"                  \
                );                                  \
            })

//------------------------------------------------------------------------------
//_____ D E C L A R A T I O N S ________________________________________________

//  @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address to read the byte in Eeprom memory.
//!
//! @return:    Value read in the Eeprom memory.
//!
extern U8 eeprom_rd_byte(U16 addr_byte);

//------------------------------------------------------------------------------
//  @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address in Eeprom memory to write the byte.
//!         value       Value to write in the Eeprom memory.
//!
//! @return none
//!
extern void eeprom_wr_byte(U16 addr_byte, U8 value);

//------------------------------------------------------------------------------
//  @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: src    Start address in Eeprom memory where write data.
//!         *dest  Address of data to write.
//!         n      number of byte to write.
//!
//! @return none
//!
extern void eeprom_rd_block(U16 src, U8* dest, U16 n);

//------------------------------------------------------------------------------
//  @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: *src   Address of data to write.
//!         dest   Start address in Eeprom memory where write data.
//!         n      number of byte to write.
//!
//! @return none
//!
extern void eeprom_wr_block(U8* src, U16 dest, U16 n);

//------------------------------------------------------------------------------
//  @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
extern void eeprom_full_erase(void);



.c

//_____ F U N C T I O N S ______________________________________________________

//------------------------------------------------------------------------------
//  @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address to read the byte in Eeprom memory.
//!
//! @return:    Value read in the Eeprom memory.
//!
//------------------------------------------------------------------------------
U8 eeprom_rd_byte(U16 addr_byte)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
    Eeprom_prog_completed
    return (Eeprom_rd_byte(addr_byte));
    //sei();
}

//------------------------------------------------------------------------------
//  @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte   Address in Eeprom memory to write the byte.
//!         value       Value to write in the Eeprom memory.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_byte(U16 addr_byte, U8 value)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
    Eeprom_prog_completed
    Eeprom_wr_byte(addr_byte, value);
    //sei();
}

//------------------------------------------------------------------------------
//  @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: src    Start address in Eeprom memory where write data.
//!         *dest  Address of data to write.
//!         n      number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_rd_block(U16 src, U8* dest, U16 n)
{
    for (; n != 0 ; n--)
    {
       *dest = eeprom_rd_byte(src);
        src++; dest++;
        //wdt_reset();
    }
}
//------------------------------------------------------------------------------
//  @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//!               but we cannot find in the device a source buffer up to
//!               64K bytes.
//!
//! @param: *src   Address of data to write.
//!         dest   Start address in Eeprom memory where write data.
//!         n      number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_block(U8* src, U16 dest, U16 n)
{
    for (; n != 0 ; n--)
    {
        eeprom_wr_byte(dest,*src);
        src++; dest++;
    }
}

//------------------------------------------------------------------------------
//  @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_full_erase(void)
{
U16 addr;

    for(addr = 0; addr < (U16)(E2_END+1); addr++)
    {
        if ((eeprom_rd_byte(addr)) != EEPROM_BLANK_BYTE)
        {
            eeprom_wr_byte(addr, EEPROM_BLANK_BYTE);
        }
    }
}


供参考啊
GPS+GSM+NLJ
回复 支持 反对

使用道具 举报

4

主题

25

帖子

0

精华

初级会员

Rank: 2

积分
61
金钱
61
注册时间
2012-8-7
在线时间
0 小时
发表于 2012-8-27 16:05:55 | 显示全部楼层
先用eeprom_write_byte ((unsigned int)*add,dat)或eeprom_write_word ((unsigned int)*add,dat)(用这个比较保守),将数据写进EEPROM地址保持起来,然后用eeprom_read_word((unsigned int)*add),读取地址的数据,这个在掉电的时候很好用!
回复 支持 反对

使用道具 举报

46

主题

1579

帖子

1

精华

金牌会员

Rank: 6Rank: 6

积分
1970
金钱
1970
注册时间
2011-7-17
在线时间
4 小时
 楼主| 发表于 2012-8-30 22:43:34 | 显示全部楼层
回复【11楼】yhbgbs:
---------------------------------
你说的是AVR吗?
哦,这是AVR的帖子,关于这个,我早就不玩了。
Time?And?Relative?Dimension?In?Space.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-2-27 13:18

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表