Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Writing to Flash memory in 18f452

Status
Not open for further replies.

buddhikaneel

Member level 1
Member level 1
Joined
Jul 27, 2011
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,542
Hi.
Flash Address 0x4000; is used for Next Address to be Data and updto 0x4100; data location is use for data save when i need.

When i want to store data in flash
1. Read Data location 0x4000; into Long Addr;
2. Then write in Addr location with Array[8];
3. updata 0x4000; with (addr+8)

i used flash Library. but writing to 2nd time in 0x4000; wil not change.
Can any; body help me using MikroC pro

Thanks.
 

Hi,

Typically, flash requires an erase block command to the same address prior to successive writes, which you have not mentioned.

With regards to Mickro C Pro am not exactly familiar but typically there should be something like ADC_ERASE or just ERASE.
 

As Kalyanasv mentioned runtime access to Flash can be tricky. Are you sure you don't what to store the data in EEPROM?

The PIC18f452 has 256 byte of EEPROM available for storage.

If you do intend to read/write to Flash you will need to use MikroC Flash Library:


The following example demonstrates simple write to the flash memory for PIC16F887, then reads the data and displays it on PORTB and PORTC.

Code:
char i = 0;
unsigned int addr, data_, dataAR[4][4] = {{0x3FAA+0,  0x3FAA+1,  0x3FAA+2,  0x3FAA+3},
                                          {0x3FAA+4,  0x3FAA+5,  0x3FAA+6,  0x3FAA+7},
                                          {0x3FAA+8,  0x3FAA+9,  0x3FAA+10, 0x3FAA+11},
                                          {0x3FAA+12, 0x3FAA+13, 0x3FAA+14, 0x3FAA+15}};

void main() {
  ANSEL  = 0;                         // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                       // Disable comparators
  C2ON_bit = 0;
  PORTB = 0;                          // Initial PORTB value
  TRISB = 0;                          // Set PORTB as output
  PORTC = 0;                          // Initial PORTC value
  TRISC = 0;                          // Set PORTC as output
  Delay_ms(500);

  // All block writes
  // to program memory are done as 16-word erase by
  // eight-word write operations. The write operation is
  // edge-aligned and cannot occur across boundaries.
  // Therefore it is recommended to perform flash writes in 16-word chunks.
  // That is why lower 4 bits of start address [3:0] must be zero.
  // Since FLASH_Write routine performs writes in 4-word chunks,
  // we need to call it 4 times in a row.
  addr = 0x0430;                      // starting Flash address, valid for P16F887
  for (i = 0; i < 4; i++){            // Write some data to Flash
    Delay_ms(100);
    FLASH_Write(addr+i*4, dataAR[i]);
  }
  Delay_ms(500);

  addr = 0x0430;
  for (i = 0; i < 16; i++){
    data_ = FLASH_Read(addr++);       // P16's FLASH is 14-bit wide, so
    Delay_us(10);                     //   two MSB's will always be '00'
    PORTB = data_;                    // Display data on PORTB (LS Byte)
    PORTC = data_ >> 8;               // and PORTC (MS Byte)
    Delay_ms(500);
  }
}

Note this library is device dependent, as the ability of various PICs to read/write flash varies.

Important : Due to the P16/P18 family flash specifics, flash library is MCU dependent. Since the P18 family differ significantlly in number of bytes that can be erased and/or written to specific MCUs, the appropirate suffix is added to the names of functions in order to make it easier to use them.

MikroC Pro User Manual - Flash Library Details

BigDog
 

I found wrong in my work. Because of i did not erase flash memory before rewrite.

Thanks my dear friends.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top