• AVR Freaks

PIC12F1501 - Help with writing & reading flash memory (Posting again)

Author
JeyakrishnaSridhar
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2019/10/02 22:49:23
  • Location: 0
  • Status: offline
2019/11/29 02:44:30 (permalink)
0

PIC12F1501 - Help with writing & reading flash memory (Posting again)

Dear all, 
 
I couldnt find my first post on the same topic in the forum and therefore, I am posting this again. 
 
For my project, using PIC12F1501, I am using NCO peripheral to pulse my circuit and I will be operating it at 2 modes of operation, namely,  Calibration mode & Normal operation mode. The underlying circuit operates best at a particular frequency and to find out that particular frequency, I need to have a frequency sweep once in lifetime, store that frequency value in flash and every other time, it operates in normal operation where it fetches the operating frequency from the flash. The switch between calibration and normal operation mode is done by a Digital input. 
 
Calibration mode:
Sweep the NCO frequency from 800kHz to 999kHz at a duty cycle of about 50% using PFM mode
- Find the best operating point (using AN pins)
- Store values in Flash. 
 
Normal operation:
Read the values from flash
- Initialize NCO frequency values with the read variables
- Do other functions. 
 
As mentioned before, the calibraiton is done only once and the value has to be stored in a particular location in flash. Every other time, when the circuit is (re)started, the normal operation mode reads the flash and initializes the NCO accordingly. 
 
I tried using the routines given in memory.c but the FLASH_WriteWord() requires me to declare a variable unit16_t ramBuf[16] which already takes 32 bytes and with other declarations, i am running out of space. I tried to modify this write word as below, but still i wasnt able to get it working. 
 
Could someone help me to write 2 16bit data in the flash only during calibration and can be fetched while normal operation mode.
 
Thanks! 
 
main.c
 
/* Flash Memory */
#define NCOFreq_addr 0x0380

/* Definitions for RAM Memory Allocation */

uint16_t ramBuf[WRITE_FLASH_BLOCKSIZE]; // Needed for writing block
uint16_t PWMValue[2];

/* Re-using ramBuf */
#define StartFreq ramBuf[15]
#define PWMSweep ramBuf[14]
#define VGain_Freq ramBuf[13]
#define FreqStep ramBuf[11]
#define Calib_DONE ramBuf[10]


void Calibrate()
{
   
    /* Constants initialization */
    
    StartFreq = 65529; // start at 999 kHz
    FreqStep = 655; // Freq step of 10 kHz
    
    /* Supporting variables */
    PWMSweep = StartFreq;
    VGain_Freq = 0;
    
    // Find the maximum ADC value in a given frequency sweep and write the value for NCO in Flash
    while (1)
    {
        NCO1INCH = PWMSweep >> 8; // Load values to NCO register
        NCO1INCL = PWMSweep & 0xFF;
        __delay_ms(100);
           
        if ( ADC_GetConversion(3) <= VGain_Freq)
        {
            PWMValue[0] = PWMSweep;
            PWMValue[1] = ADC_GetConversion(0);
            break;
        }
        
        VGain_Freq = ADC_GetConversion(3);
        PWMSweep = PWMSweep - FreqStep;
        __delay_ms(1000);
        
    }
    
        
    FLASH_WriteWord(NCOFreq_addr, ramBuf, PWMValue); // Write Values to Flash
    __delay_ms(1000);
    
    Calib_DONE = 0x01; // Set this so that the calibration is done only once.
    
}


void ReadData()
{
    PWMValue[0] = FLASH_ReadWord(NCOFreq_addr);
    __delay_ms(1000);
      
    PWMValue[1] = FLASH_ReadWord(NCOFreq_addr+1);
    __delay_ms(1000);
    
    
       
    NCO1INCH = PWMValue[0] >> 8;
    NCO1INCL = PWMValue[0] & 0xFF;
    
    
    Freq_read = 0x01; // Set this when the frequency is read once.
}




/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();
    

 
    Calib_DONE = 0x00;
    Freq_read = 0x00;


    while (1)
    {
        
        if ( EN_CALIB_GetValue() == 0) // Calibration mode
        {
           
            if ( (Calib_DONE & 0x01) != 1) // If calibration is not done, call calibrate function.
            {
                Calibrate();
            }
         
        }
        
        else // Normal operation mode
        {
            
            if ((Freq_read & 0x01) != 1) // If frequency is not read, call calibrate function.
            {
                ReadData();
            }
            

            // Other operations
           
            }
         }
        // Add your application code
    }
}

 
memory.c
 
void FLASH_WriteWord(uint16_t flashAddr, uint16_t *ramBuf, uint16_t *word)
{
    uint16_t blockStartAddr = (uint16_t)(flashAddr & ((END_FLASH-1) ^ (ERASE_FLASH_BLOCKSIZE-1)));
    uint8_t offset = (uint8_t)(flashAddr & (ERASE_FLASH_BLOCKSIZE-1));
    uint8_t i;

    // Entire row will be erased, read and save the existing data
    for (i=0; i<ERASE_FLASH_BLOCKSIZE; i++)
    {
        ramBuf[i] = FLASH_ReadWord((blockStartAddr+i));
    }

    // Write at offset
    ramBuf[offset] = word[0];
    ramBuf[offset+1] = word[1];

    // Writes ramBuf to current block
    FLASH_WriteBlock(blockStartAddr, ramBuf);
}

 
 
 
 
 
#1

3 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1469
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: PIC12F1501 - Help with writing & reading flash memory (Posting again) 2019/12/02 08:58:45 (permalink)

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    JeyakrishnaSridhar
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2019/10/02 22:49:23
    • Location: 0
    • Status: offline
    Re: PIC12F1501 - Help with writing & reading flash memory (Posting again) 2019/12/03 02:09:07 (permalink)
    0
    Sorry, I had to try multiple times to post this as I was getting an error. 
     
    Admin: Please delete this
     
    Thank you! 
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 18061
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: PIC12F1501 - Help with writing & reading flash memory (Posting again) 2019/12/03 10:45:04 (permalink)
    +3 (3)
    The Admin will not read your post, or delete it.
    #4
    Jump to:
    © 2019 APG vNext Commercial Version 4.5