首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据未存储在EEPROM中

数据未存储在EEPROM中
EN

Stack Overflow用户
提问于 2017-08-08 16:54:37
回答 2查看 461关注 0票数 0

我使用以下代码访问eeprom:

代码语言:javascript
复制
void EEPROM_write(unsigned int uiAddress,unsigned char ucData)
{
   while(EECR & (1<<EEWE))
   {
      //do nothing
   }

   while(SPMCR & (1<<SPMEN));
   EEAR = uiAddress;
   EEDR = ucData;
   EECR |= (1<<EEMWE);
   EECR |= (1<<EEWE);
}

char EEPROM_read(unsigned int uiAddress)
{
   while(EECR & (1<<EEWE))
   {
      //do nothing
   }

   EEAR = uiAddress;
   EECR |=(1<<EERE);
   return EEDR;
}

void EEPROM_write_str(unsigned int uiAddress,unsigned char* string,unsigned int size)
{
   int i;
   for(i=0;i<size;i++)
   {
      EEPROM_write(uiAddress+i,string[i]);
   }
}

void EEPROM_read_str(unsigned int uiAddress,unsigned char* string,unsigned int size)
{
   int i;
   for(i=0;i<size;i++)
   {
      string[i] = EEPROM_read(uiAddress+i);
   }
}

char str[]="hello ";
char str2[20];

int main()
{
   usart_init(12);
   //EEPROM_write_str(0,str,6);
   EEPROM_read_str(0,str2,6);
   usart_puts(str2,6);
}

在上面的代码中,我首先注释了EEPROM_read_str和usart_puts,然后注释了EEPROM_write_str函数,并从其他两个函数中删除了注释,并刷新了again.despite this,数据没有被存储,并且在终端中显示的输出是yyyyy (十六进制FF)。这里有什么问题?(在这里,USART_puts以第二个参数作为字符数来传输字符串)

EN

回答 2

Stack Overflow用户

发布于 2017-08-08 17:25:14

我的旧avr项目的工作代码。我已经多年没有使用它们了,因为我现在不记得这些比特的确切含义了。

代码语言:javascript
复制
void EEPROM_write(uint8_t addr, uint8_t value) {
    while(EECR & (1 << EEPE)) ;             //wait for write enable bit to clear
    EECR &= ~((1 << EEPM1) | (1 << EEPM0)); // (erase & write in one operation)
    EEARL = addr;                           // set the address
    EEDR = value;                           // set value to be written
    EECR |= (1 << EEMPE);                       // set EEPROM Master Write Enable
    EECR |= (1 << EEPE);                        // set EEPROM Master Write Enable
}

uint8_t EEPROM_read(uint8_t addr) {
    while(EECR & (1 << EEPE)) ;             
    EEARL = addr;                           // set the address
    EECR |= (1 << EERE);
    return EEDR;
}
票数 0
EN

Stack Overflow用户

发布于 2017-08-10 22:33:10

这篇文章来自AVR103 AVR EEPROM Application Note的源代码配套文件,它是设备制造商的权威出版物。

代码语言:javascript
复制
char EEPROM_GetChar( unsigned int addr )
{
    do {} while( EECR & (1<<EEPE) ); // Wait for completion of previous write.
    EEAR = addr; // Set EEPROM address register.
    EECR = (1<<EERE); // Start EEPROM read operation.
    return EEDR; // Return the byte read from EEPROM.
}


void EEPROM_PutChar( unsigned int addr, char new_value )
{
    char old_value; // Old EEPROM value.
    char diff_mask; // Difference mask, i.e. old value XOR new value.

    unsigned char old_interrupt; // Stores interrupt flag while programming.
    old_interrupt = __save_interrupt(); // Save interrupt flag state.
    __disable_interrupt(); // Ensure atomic operation for the write operation.

    do {} while( EECR & (1<<EEPE) ); // Wait for completion of previous write.
    #ifndef EEPROM_IGNORE_SELFPROG
    do {} while( SPMCSR & (1<<SELFPRGEN) ); // Wait for completion of SPM.
    #endif

    EEAR = addr; // Set EEPROM address register.
    EECR = (1<<EERE); // Start EEPROM read operation.
    old_value = EEDR; // Get old EEPROM value.
    diff_mask = old_value ^ new_value; // Get bit differences.

    // Check if any bits are changed to '1' in the new value.
    if( diff_mask & new_value ) {
        // Now we know that _some_ bits need to be erased to '1'.

        // Check if any bits in the new value are '0'.
        if( new_value != 0xff ) {
            // Now we know that some bits need to be programmed to '0' also.

            EEDR = new_value; // Set EEPROM data register.
            EECR = (1<<EEMPE) | // Set Master Write Enable bit...
                   (0<<EEPM1) | (0<<EEPM0); // ...and Erase+Write mode.
            EECR |= (1<<EEPE);  // Start Erase+Write operation.
        } else {
            // Now we know that all bits should be erased.

            EECR = (1<<EEMPE) | // Set Master Write Enable bit...
                   (1<<EEPM0);  // ...and Erase-only mode.
            EECR |= (1<<EEPE);  // Start Erase-only operation.
        }
    } else {
        // Now we know that _no_ bits need to be erased to '1'.

        // Check if any bits are changed from '1' in the old value.
        if( diff_mask ) {
            // Now we know that _some_ bits need to the programmed to '0'.

            EEDR = new_value;   // Set EEPROM data register.
            EECR = (1<<EEMPE) | // Set Master Write Enable bit...
                   (1<<EEPM1);  // ...and Write-only mode.
            EECR |= (1<<EEPE);  // Start Write-only operation.
        }
    }

    __restore_interrupt( old_interrupt ); // Restore interrupt flag state.
}


void main()
{
    char t; // Temporary byte.
    unsigned int addr = 0x10; // EEPROM address to use.

    // Test the EEPROM_GetChar() function.
    t = EEPROM_GetChar( addr );

    // Try erasing the whole byte.
    EEPROM_PutChar( addr, 0xff );

    // Try changing a few bits to '0'.
    EEPROM_PutChar( addr, 0x0f );

    // Try changing bits both ways.
    EEPROM_PutChar( addr, 0xf0 );

    // Try changing nothing.
    EEPROM_PutChar( addr, 0xf0 );

    // Restore old value.
    EEPROM_PutChar( addr, t );

    for(;;); // Loop forever.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45563780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档