PIC Microcontroller

High-Endurance Flash Data Memory (HEF) 서브루틴

EP 기술연구소 2018. 9. 8. 15:05

/*******************************************************************************

   Project : High-Endurance Flash Data Memory (HEF) of Infrared Lamp Heater Control Board Program

   Version : 

   Date    : 2018-06-06

   Author  : JW Park(kornanp@empal.com)

   Company : EU-Technology Co., Ltd (Electronic Utopia)


   Device   : PIC16F1716 (SSOP 28-pin) 

   Comments:

      1. 


 ********************************************************************************/


//this gets the total size of the program memory from the compiler 

#define PROGRAM_MEMORY_SIZE      getenv("PROGRAM_MEMORY") 

//so define the total amount of memory I want 

#define FLASH_DATA_SIZE      128 

//Define the end point of my block of flash (the very last byte of flash available) 

#define FLASH_DATA_END      PROGRAM_MEMORY_SIZE-1 

//Now define where the start of my data will be by subtracting the amount I want from the 

//size of the memory 

#define FLASH_DATA_START      (PROGRAM_MEMORY_SIZE - FLASH_DATA_SIZE) 



//Now tell the compiler to reserve this for me 

//#org FLASH_DATA_START, FLASH_DATA_END {} 


signed int16 DataArray16[32];     //64 bytes 

signed int16* PointerArray16[32];  //64 bytes                                          

int8 DataArray8[32];         //32 bytes 

int8* PointerArray8[32];   //64 bytes 



//These are a couple of variables initialised to zero so I have something to point the pointers to during the demo 

int8 Default8 = 0; 

signed int16 Default16 = 0; 

    

//Now the function declarations 

void FillArrays(); 

void ClearArrays(); 

void WriteArrayToFlash(); 

void ReadArrayFromFlash(); 



void FillArrays() 

   //fill the arrays with some data 

   int8 n; 


   for(n = 0; n < 32; n++) 

   { 

      //Put some values into each data array 

      DataArray16[n]       = 100+n; 

      //DataArray8[n]       = n; 

       

      //Point each pointer in the array at the values in the data arrays above 

      //PointerArray16[n]    = &DataArray16[n]; 

     // PointerArray8[n]    = &DataArray8[n]; 


   } 


void ClearArrays() 

   //Clear all the arrays 

   int8 n; 


   for(n = 0; n < 32; n++) 

   { 

      //Set the elements in each data array to zero 

      DataArray16[n]       = 0; 

      DataArray8[n]       = 0; 

      //Set the poiters to point at a zero valued variable 

      PointerArray16[n]    = &Default16; 

      PointerArray8[n]    = &Default8; 


   } 


void WriteArrayToFlash() 

    

   //write arrays to flash 

   //work out the start address in flash for the first byte 

   int16 FlashStartAddress; 

    

   //Disable interrupts when reading and writing to flash 

   disable_interrupts(GLOBAL); 

    

   //First work out the address in flash where we want our data to start 

   FlashStartAddress = FLASH_DATA_START; 

    

   //now write the first array into flash, starting from this address 

    

   write_program_memory( FlashStartAddress, DataArray16, 64 ); 

   //Now we add 64 to the flash address variable 

   FlashStartAddress += 64; 

   //Now the same as above but this time we copy the array of pointers - 

   //this will save the actual pointers to flash NOT the variables they point to! 

   //Again there are 64 bytes, two for each 16 bit pointer 

   write_program_memory( FlashStartAddress, PointerArray16, 64 ); 

   FlashStartAddress += 64; 

    

   //Now we can copy the array of int8 data to flash 

   //This array also has 32 elements but each element is only a single byte 

   write_program_memory( FlashStartAddress, DataArray8, 32 ); 

   //Now increase the flash address again, but only by 32 bytes this time 

   FlashStartAddress += 32; 

    

   //Finally we are going to copy the 32 elements in the array of int8 pointers 

   //REMEMBER! Pointers use 16 bits so even though these point to int8 variables 

   //the actual pointers are 16 bit values so we are saving 64 bytes                                        

   write_program_memory( FlashStartAddress, PointerArray8, 64 ); 

    

   //Now re-enable any interrupts 


   enable_interrupts(GLOBAL); 



void ReadArrayFromFlash() 

   int16 FlashStartAddress; 


   disable_interrupts(GLOBAL);    //Disable interrupts

    

   //read all the arrays from flash 

   FlashStartAddress = FLASH_DATA_START; 

   read_program_memory( FlashStartAddress, DataArray16, 64 ); 


   FlashStartAddress += 64; 

   read_program_memory( FlashStartAddress, PointerArray16, 64 ); 

   

   FlashStartAddress += 64; 

   read_program_memory( FlashStartAddress, DataArray8, 32 ); 

    

   FlashStartAddress += 32; 

   //Remember that these int8 pointers use two bytes! 

   read_program_memory( FlashStartAddress, PointerArray8, 64 ); 

    


   enable_interrupts(GLOBAL);    //re-enable any interrupts when finished 


//==============================================================================

void HEFlash_Data_Read()

{

int16 FlashStartAddress;

 

//disable_interrupts(GLOBAL); //Disable interrupts 

    

//read all the arrays from flash 

FlashStartAddress = FLASH_DATA_START; 

read_program_memory( FlashStartAddress, DataArray16, 64 );

   

    SetTemp = DataArray16[0];

    ProtrctCount = DataArray16[1];

      

if(SetTemp >= 40) SetTemp = initial_set_temp;

//enable_interrupts(GLOBAL); //re-enable any interrupts when finished 

}


void HEFlash_Data_Write()

{

int16 FlashStartAddress;

 

disable_interrupts(GLOBAL); //Disable interrupts 

    

DataArray16[0] = SetTemp;

    DataArray16[1] =  ProtrctCount;

//read all the arrays from flash 

FlashStartAddress = FLASH_DATA_START; 

write_program_memory( FlashStartAddress, DataArray16, 64 ); 



enable_interrupts(GLOBAL); //re-enable any interrupts when finished 

}