piyushpandey
Member level 4
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:
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
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