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.

internal flash memory read and write of pic micrcontroller

Status
Not open for further replies.

piyushpandey

Member level 4
Joined
Mar 26, 2012
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
2,051
Hi guys

I am programming on the pic16f1526 micrcontrolller with the hi tech 9.83 compiler .

Here I want to read and write data in the flash memory of the PIC microcontroller, ans for that I have written the following function so can you guys please check my this code and can tell me that how should I test my this code snippet.


I have also make the EUSART functions inside it in order to check the flash functions which I have written are working correct or not.


Here is the code snippet of my functions whcih I have written:

Code:
/*This file contains the Flash routine for the pic16f1526 through which we can write to the internal flash memory of the
 PIC16F1526 micrcontroller

 It also contains the ADC module in order to check the vlaue written to the flsh is correct or not*/

#include <pic.h>


// Function to read the flash memory

unsigned int flash_read(unsigned char Hi_addr,unsigned char Low_addr)
{
    // variable to read the data
    unsigned int mem_data;
    // Select COnfiguration Memory
    PMCON1bits.CFGS = 0;

    // Select Word Address
    PMADRH = Hi_addr;
    PMADRL = Low_addr;

    //Initiate read operation
    PMCON1bits.RD = 1;

    // do nothing
    _nop();
    _nop();

    // read the data
    mem_data = PMDATH;
    mem_data = (mem_data<<8);
    mem_data = (mem_data|PMDATL);

    return mem_data;
}

// the flash unlock function

void flash_unlock(void)
{
    PMCON2 = 0x55;

    _nop();
    _nop();

    PMCON2 = 0xAA;

    // Set the write bit in the PMCON1 register

    WR = 1;

    _nop();
    _nop();
}

// Program to erase the flash memory content

void flash_erase(unsigned char high_addr,unsigned char low_addr)
{
    // Disable the interrupts
    GIE = 0;

    // Select the configuration memory

    PMCON1bits.CFGS = 0;

    // Put the address to be erased

    PMADRH = high_addr;
    PMADRL = low_addr;

    // Select the erase operation

    FREE = 1;

    // Enable the Erase operation
    WREN = 1;

    // Call the unlock sequence of the Flash
    flash_unlock();

    __delay_ms(10);

    // Disable the erase operation

    WREN = 0;

    // reenable the interrupts

    GIE = 1;


}
// Function to write to the Flash Memory

void flash_write(unsigned char Hi_addr,unsigned char Low_addr,unsigned int flash_data)
{
    // Disable interrupts
    GIE = 0;
    
    // Select the program memory
    CFGS = 0;
    
    // Select the address to be written with the data
    PMADRH = Hi_addr;
    PMADRL = Low_addr;
    
    // Select the write operation
    FREE = 0;
    
    // Load write Latches only
    LWLO = 1;
    
    // Enable the write operation 
    WREN = 1;
    
    // Load the data to the register to be written
    PMDATH&=flash_data;
    flash_data = (flash_data>>8);
    PMDATL&=flash_data;
    
    // Write Latches to flash
    LWLO = 0;
    
    // call the unlock sequence 
    flash_unlock();
    
    __delay_ms(10);
    
    // Disable the write operation
    WREN = 0;
    
    // Enable the interrupts
    
    GIE = 1;

}


/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/*this is the USART functions to check the memory content of the flash memory*/
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////


void Usart_init(void)
{
    // Enable the transmitter output
    TRISGbits.TRISG1 = 1;
    // Disable the analog function
    ANSELGbits.ANSG1 = 0;
    // Enable the reciever input
    TRISGbits.TRISG2 = 1;
    // Disable the analog function of tthe receiver pin
    ANSELGbits.ANSG2 = 0;

    // Select the 8 bit asynchronous mode of baud rate
    TX2STAbits.SYNC = 0;
    BAUDCONbits.BRG16 = 0;
    TX2STAbits.BRGH = 1;

    // Select the baud rate
    SP2BRGH = 0x00;    // Baud rate is of 9600 at 16MHZ
    SP2BRGL = 0x67;

    // Disable all interrupts
    GIE = 0;
    PEIE = 0;

    // only 8 bit mode for receiver
    RC2STAbits.RX9 = 0;

    // Enable the serial port
    RC2STAbits.SPEN = 1;

    // Enable the transmitter and the receiver
    RC2STAbits.CREN = 1;
    TX2STAbits.TXEN = 1;
}

void Usart_transmit(unsigned char transmit_data)
{
    while(!TXIF)
        continue;
        NOP();
    _nop();
    _nop();

    TX2REG = transmit_data;
    return;
}

unsigned char EUSART_recieve(void)
{
    unsigned char data;

    while(!RCIF)
        continue;
    data = RC2REG;

    return data ;
}

Also if anybody have written the c code to read ans write the flash memory of the PIC micrcontroller than could he please share his code with me .



Thanks
 

Hi guys

I studied the memory specification of the pic16f1526 microcontroller , it only has the Flash memory and not the EEPROM memory and therefore I need to utilise the flash memory for the storing of the data .

Actually what I want to know alongwith the c code to read and write to the flash memory is that since the main program are also stored in the flash so how come I would know that from where onwards the memory location would be free so that the location could be used for the storing of the data..

Please guys help me regarding this.


Thanks
 

If you are using Hi-Tech compiler, if you look in 'pic.h' file, you will find macros and prototypes for functions to read and write to flash memory provided by Hi-Tech libs.
The compiler manual might give you some ideas on how you can reserve space in program memory.
 

Hi BtBass

First of all thanks for your reply.

I have seen the pic.h file but there is no source code for the reading and writing of the flash memory only the functions have been defined but here I want to make my own functions in order to read and write the flash memory but the problem which is coming in my way is that how can I know the start address from where I should start to store the data in the flash memory as you know that flash memory is also used to store the constants and the main executable program of the controller.

I am writing just a single word in the flash write now that is I am just using a single row of the flash memory for the storing of the data and reading that memory.

and I am not using the array of data to be stored.


If anybody know the articles or webpages dedicated to this work could please share it with me, I have browsed a lot but don't get the information or codes regarding the use of internal flash memory of the PIC micrcontroller.

I also want to know if there is any errata or article by the microchip on the flash memory especially and how to use it for personal purpose apart from burning the program to it.



Thanks
 

The 'pic.h' file just defines the prototypes so you can use the functions in your code.
You will find the source code in the 'sources' directory. These are compiled into the libs and linked in when you compile your program.

One thing you could try is to define a constant array, constants are normally stored in flash.
Such as

const unsigned char myarray[] = {0xfa, 0xfb, 0xfc, 0xfd}; @ 0x400

Then after you compile, look at the memory map to see where they are stored.

In the manual, section 3.4.3 'Objects in program space' might help.
 

Hi brass


I tried your option and included the inbuilt function of the pic.h but the following error comes out and it's not working:

Code:
"C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 I:/flash_program.X/flash_test.c  -q --chip=16F1526 -P  --outdir="build/Debug/production\_ext\1075010585" -N31 --warn=0 --runtime=default,+clear,+init,-keep,+osccal,-resetbits,-download,-stackcall,+clib --summary=default,-psect,-class,+mem,-hex --opt=default,+asm,-asmfile,-speed,+space,-debug,9  --double=24 --float=24 --addrqual=ignore --mode=lite -g --asmlist "--errformat=%%f:%%l: error: %%s" "--msgformat=%%f:%%l: advisory: %%s" "--warnformat=%%f:%%l warning: %%s"
I:/flash_program.X/flash_test.c:11 warning: redefining preprocessor macro "__FLASHTYPE" ((null): 0)
I:/flash_program.X/flash_test.c:11 warning: redefining preprocessor macro "__FLASHTYPE" ((null): 0)
"C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe"  -odist/Debug/production/flash.X.production.cof  -mdist/Debug/production/flash.X.production.map --summary=default,-psect,-class,+mem,-hex --chip=16F1526 -P --runtime=default,+clear,+init,-keep,+osccal,-resetbits,-download,-stackcall,+clib --summary=default,-psect,-class,+mem,-hex --opt=default,+asm,-asmfile,-speed,+space,-debug,9 -N31 --warn=0  --double=24 --float=24 --addrqual=ignore --mode=lite --output=default,-inhx032 -g --asmlist "--errformat=%%f:%%l: error: %%s" "--msgformat=%%f:%%l: advisory: %%s" "--warnformat=%%f:%%l warning: %%s" build/Debug/production/_ext/1075010585/flash.p1 build/Debug/production/_ext/1075010585/flash_test.p1  
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode)  V9.83
Copyright (C) 2011 Microchip Technology Inc.
[b][COLOR="#FF0000"](1273) Omniscient Code Generation not available in Lite mode (warning)
:0: error: undefined symbol:
        _flash_read(dist/Debug/production\flash.X.production.obj) [/b][/COLOR]
make[2]: *** [dist/Debug/production/flash.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `C:/Users/ACCOUNTS/MPLABXProjects/flash.X'
make[1]: Leaving directory `C:/Users/ACCOUNTS/MPLABXProjects/flash.X'

BUILD FAILED (exit value 2, total time: 3s)



I googled a little bit about this error and what I have got is that the support for the following fuction has been disabled in the Hi-Tech Compiler.



Thanks
 
Last edited:

What you could do, if you are an adventurous soul is to edit the ini file.
In the 'dat' directory of your Hi-Tech install, you will find a file called 'picc.ini'
This is the file the linker uses to allocate memory.
I dont have the entry for the Pic16f1526 as I only have version 9.7 of the compiler,
but a typical entry is

[16LF1826]
MAKE=MICROCHIP
ARCH=PIC14E
PROCID=A826
FLASH_READ=1
FLASH_WRITE=8
FLASH_ERASE=20
FLASHTYPE=READWRITE_A
IDLOC=8000-8003
CONFIG=8007-8008
EEPROMSIZE=100
ROMSIZE=2000
COMMON=70-7F
BANKS=20
RAMBANK=20-7F,A0-EF,120-16F
DATABANK=3
ICD2RAM=165-16F

A # is a comment, the values are HEX, so if you change the ROMSIZE by subtracting 100, you would reserve the top 256 bytes in the memory for your own use.

For example

[16LF1826]
MAKE=MICROCHIP
ARCH=PIC14E
PROCID=A826
FLASH_READ=1
FLASH_WRITE=8
FLASH_ERASE=20
FLASHTYPE=READWRITE_A
IDLOC=8000-8003
CONFIG=8007-8008
EEPROMSIZE=100
#ROMSIZE=2000 comment out
ROMSIZE=1F00
COMMON=70-7F
BANKS=20
RAMBANK=20-7F,A0-EF,120-16F
DATABANK=3
ICD2RAM=165-16F

After compiling your code view the program memory to see whats happened. Unless your program is very big, the top of the memory space is usualy unused.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top