圣龙扬特-AVR电子

标题: STM8的EEPROM操作 [打印本页]

作者: 铜河    时间: 2013-2-26 11:08
标题: STM8的EEPROM操作
程序如下:
#include "STM8S105C_S.h"

unsigned int eeprom_address;
unsigned char eeprom_value;

unsigned char eeprom_write_block_in_ram[100];

//#define eeadress (*(volatile unsigned intr *)ADR_8254_ARRAY[(x)])

//(*((PointerAttr u8*) Address))


void EEPROM_WRITE_BYTE(unsigned int eeaddress, unsigned char eedata)
{
       
        eeprom_address = eeaddress;
       
        /* 设置编程时间 */
        FLASH_CR1 &= (unsigned char)(~0x01);
  FLASH_CR1 |= 0x01;
       
        /* MASS密钥,解除EEPROM的保护 */
        FLASH_DUKR = 0xAE;  
  FLASH_DUKR = 0x56;                       

        *((unsigned char*) eeprom_address) = eedata;               
               
        while((FLASH_IAPSR & 0x04) != 0x00);   //EOP=1,EEPROM编程结束               
}


void EEPROM_WRITE_WORD(unsigned int eeaddress, unsigned char eedata)
{
       
        /* 设置编程时间 */
        FLASH_CR1 &= (unsigned char)(~0x01);
  FLASH_CR1 |= 0x01;
       
        /* MASS密钥,解除EEPROM的保护 */
        FLASH_DUKR = 0xAE;  
  FLASH_DUKR = 0x56;                       
       
        /* 设置字编程模式 */
    FLASH_CR2 |= 0x40;
    FLASH_NCR2 &= (unsigned char)(~0x40);

  /* 从低地址开始写入4个字节数据 */
    // *((unsigned char *)eeaddress)    = *((unsigned char*)(&eedata));
    // *(((unsigned char *)eeaddress) + 1) = *((unsigned char*)(&eedata)+1);
    // *(((unsigned char *)eeaddress) + 2) = *((unsigned char*)(&eedata)+2);
    // *(((unsigned char *)eeaddress) + 3) = *((unsigned char*)(&eedata)+3);
               
                *((unsigned char *)eeaddress)    =    (eedata);
    *(((unsigned char *)eeaddress) + 1) = (eedata+1);
    *(((unsigned char *)eeaddress) + 2) = (eedata+2);
    *(((unsigned char *)eeaddress) + 3) = (eedata+3);
               
        while((FLASH_IAPSR & 0x04) != 0x00);   //EOP=1,EEPROM编程结束               

        //BRES FLASH_IAPSR,#3                ;Data EEPROM area write protection enabled
       
}



void EEPROM_WRITE_BLOCK(void)
{
        unsigned char count;
        unsigned int eeaddress_start;
       
        eeaddress_start = 0x4000;
               
        /* 设置编程时间 */
        FLASH_CR1 &= (unsigned char)(~0x01);
  FLASH_CR1 |= 0x01;
       
        /* MASS密钥,解除EEPROM的保护 */
        FLASH_DUKR = 0xAE;  
  FLASH_DUKR = 0x56;                       
       
        /* 设置块编程模式 */
        FLASH_CR2 |= 0x01;
  FLASH_NCR2 &= (unsigned char)(~0x01);

   for (count = 0; count < 128; count++)
        {
    *((unsigned char *)(eeaddress_start + count)) = 0x99;
        }

//判断EEPROM块操作是否完成

/* STM8S103, STM8S903 属于低容量,其BLOCK的大小为64字节 */
//        while((FLASH_IAPSR & 0x04) != 0x00);   //EOP=1,EEPROM编程结束               

/* STM8S208,STM8S207,STM8S105 是中大容量,其BLCOK大小为128个字节*/
        while ((FLASH_IAPSR & 0x40) != 0x00 );  //HVOFF=1高压结束

}

void COPY_EEPROMWRITEBLOCK_INTO_RAM(void)
{               
        unsigned char eerom_count;
        eerom_count=0;
       
        while ( *((unsigned char*)EEPROM_WRITE_BLOCK + eerom_count) != 0x81 )
        {
                eeprom_write_block_in_ram[eerom_count] = *((unsigned char*)EEPROM_WRITE_BLOCK + eerom_count);
                eerom_count++;                       
        }
        eeprom_write_block_in_ram[eerom_count] = 0x81; // RET 指令                               
}

main()
{
        COPY_EEPROMWRITEBLOCK_INTO_RAM();
       
        _asm("call _eeprom_write_block_in_ram");

        _asm("nop");       
       
        while (1)
        {
               
        _asm("nop");       
        _asm("nop");
        _asm("nop");
               
        }
}
作者: ba1731    时间: 2013-2-28 14:17
表四鄙人纯属随机路过。。。顺带帮顶。。。




欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/) Powered by Discuz! X2.5