• AVR Freaks

Hot!PIC10F320 storing in memory

Author
lmagalhaes
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2016/01/08 10:38:43
  • Location: 0
  • Status: offline
2019/12/12 14:30:24 (permalink)
0

PIC10F320 storing in memory

Hi there,
 
I'm trying to use the flash write ability of the PIC10F320 to change a variable each time the system is powered up. I've written the following code:
 
Apparently I can't post the code =\ Access denied thingy
 
But everytime the system is powered, it will always land on the case 0 of the switch. I've followed the datasheet and the code generated by MCC to write the flash write routine so it should be working. One thing I noticed is that in Window -> Target Memory Views -> Program Memory, the memory space in F0-FF is filled with some instructions. Could this be why I can't write to those sections ? Since the code is only taking E9 words, is there any way of telling the compiler to fill the memory from 0 to E9 instead of having it between 0-D8 and then from F0 to FF ?
 
Thanks,
 
Luís
post edited by lmagalhaes - 2019/12/12 14:31:47
#1

9 Replies Related Threads

    lmagalhaes
    Starting Member
    • Total Posts : 44
    • Reward points : 0
    • Joined: 2016/01/08 10:38:43
    • Location: 0
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 14:33:26 (permalink)
    0
    Here goes the code ?
     
    /*
    * File: main.c
    * Author: Luis
    *
    * Created on December 6, 2019, 8:48 AM
    */
    // CONFIG
    #pragma config FOSC = INTOSC // Oscillator Selection bits->INTOSC oscillator: CLKIN function disabled
    #pragma config BOREN = OFF // Brown-out Reset Enable->Brown-out Reset enabled
    #pragma config WDTE = OFF // Watchdog Timer Enable->WDT disabled
    #pragma config PWRTE = OFF // Power-up Timer Enable bit->PWRT disabled
    #pragma config MCLRE = OFF // MCLR Pin Function Select bit->MCLR pin function is MCLR
    #pragma config CP = OFF // Code Protection bit->Program memory code protection is disabled
    #pragma config LVP = ON // Low-Voltage Programming Enable->Low-voltage programming enabled
    #pragma config LPBOR = OFF // Brown-out Reset Selection bits->BOR enabled
    #pragma config BORV = LO // Brown-out Reset Voltage Selection->Brown-out Reset Voltage (Vbor), low trip point selected.
    #pragma config WRT = OFF // Flash Memory Self-Write Protection->Write protection off
    #include <xc.h>
    #define _XTAL_FREQ 31000
    #define ERASE_FLASH_BLOCKSIZE 16
    unsigned int Buf[ERASE_FLASH_BLOCKSIZE];
    const unsigned int segmentToBurn __at(0xF0);
    void FLASH_WriteWord(int val){
    for(int i=0; i<16;i++){
    PMADRL = 0xF0 + i;
    PMADRH = 0;
    PMCON1bits.CFGS = 0; // Deselect Configuration space
    PMCON1bits.RD = 1; // Initiate Read
    NOP();
    NOP();

    Buf[i] = ((unsigned int)((PMDATH << 8) | PMDATL));
    }
    Buf[0] = val;

    // Load lower 8 bits of erase address boundary
    PMADRL = 0xF0;
    // Load upper 6 bits of erase address boundary
    PMADRH = 0;
    // Block erase sequence
    PMCON1bits.CFGS = 0; // Deselect Configuration space
    PMCON1bits.FREE = 1; // Specify an erase operation
    PMCON1bits.WREN = 1; // Allows erase cycles
    // Start of required sequence to initiate erase
    PMCON2 = 0x55;
    PMCON2 = 0xAA;
    PMCON1bits.WR = 1; // Set WR bit to begin erase
    NOP();
    NOP();
    PMCON1bits.WREN = 0; // Disable writes
    // Block write sequence
    PMCON1bits.CFGS = 0; // Deselect Configuration space
    PMCON1bits.WREN = 1; // Enable wrties
    PMCON1bits.LWLO = 1; // Only load write latches

    for(int i=0; i<16;i++){
    // Load lower 8 bits of write address
    PMADRL = 0xF0 + i;
    // Load upper 6 bits of write address
    PMADRH = 0;
    // Load data in current address
    PMDATL = Buf[i];
    PMDATH = ((Buf[i] & 0xFF00) >> 8);
    if(i == 15)
    {
    // Start Flash program memory write
    PMCON1bits.LWLO = 0;
    }
    PMCON2 = 0x55;
    PMCON2 = 0xAA;
    PMCON1bits.WR = 1;
    NOP();
    NOP();
    }
    PMCON1bits.WREN = 0; // Disable writes
    }
    void delay_seconds(int n)
    {
    for(n=n; n!=0; n--){
    CLRWDT();
    __delay_ms(1000);
    }
    }
    /*
    Main application
    */
    void main(void)
    {

    // initialize the device
    /**
    TRISx registers
    */
    TRISA = 0;
    /**
    LATx registers
    */
    LATA = 0x07;
    /**
    ANSELx registers
    */
    ANSELA = 0;
    /**
    WPUx registers
    */
    WPUA = 0x00;
    OPTION_REGbits.nWPUEN = 0;

    // LFIOFR 31.25KHz_osc_not_ready; HFIOFS unstable; HFIOFR 16MHz_osc_not_ready; IRCF 31KHz;
    OSCCON = 0x00;
    // CLKROE disabled;
    CLKRCON = 0x00;
    // SBOREN disabled; BORFS disabled; BORRDY BOR Circuit is inactive;
    BORCON = 0x00;

    __delay_ms(200);
    switch(segmentToBurn){
    case 0:
    LATA = 0x06;
    FLASH_WriteWord(1);
    delay_seconds(120);
    break;
    case 1:
    LATA = 0x05;
    FLASH_WriteWord(2);
    delay_seconds(120);
    break;
    case 2:
    LATA = 0x03;
    FLASH_WriteWord(0);
    delay_seconds(120);
    break;
    }

    }

    #2
    ric
    Super Member
    • Total Posts : 28335
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 15:01:04 (permalink)
    +1 (1)
    In your project setup, go to the "XC8 Linker" options, select the "Memory Model" dropdown, then use the "ROM ranges" option to select which ROM the compiler can use.
    In your case, "default,-F0-FF" will tell it to use all the default ROM except for the range F0 to FF
     
    https://microchipdeveloper.com/tip:33
     
    post edited by ric - 2019/12/12 15:03:00

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    lmagalhaes
    Starting Member
    • Total Posts : 44
    • Reward points : 0
    • Joined: 2016/01/08 10:38:43
    • Location: 0
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 15:06:57 (permalink)
    0
    Thank you ric, but even after that, it still has two instructions, one at F0 and one at F1:
     
    https://imgur.com/a/uQIllZ5
    #4
    ric
    Super Member
    • Total Posts : 28335
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 15:29:40 (permalink)
    +1 (1)
    You put them there!
    It is this variable:
    const unsigned int segmentToBurn __at(0xF0);

    The compiler uses multiple RETLW instructions to store the data in the bottom 8 bits of each instruction.
     
    post edited by ric - 2019/12/12 15:31:45

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    lmagalhaes
    Starting Member
    • Total Posts : 44
    • Reward points : 0
    • Joined: 2016/01/08 10:38:43
    • Location: 0
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 15:57:37 (permalink)
    0
    So that means that I can't use it like that ? I'd like to have the switch decide on the value at F0, which is what I'm trying to change. Do I need something like:
     
     

    int *addr = 0xF0;
    int segmentToBurn = *addr;

     
    But this gives me main.c:129:10: warning: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int' [-Wint-conversion
     
    My pointer game is not that strong =\
    #6
    ric
    Super Member
    • Total Posts : 28335
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 17:19:16 (permalink)
    +1 (1)
    You can add an "extern" qualifier to the definition. You also need a "volatile" qualifier on it, or the optimiser will short circuit the code to just use the initial value.
    This will leave those two locations containing 0x3FFF
    extern volatile const unsigned int segmentToBurn __at(0xF0);

    Or this will leave those two locations containing "RETLW 0xFF" instructions
    volatile const unsigned int segmentToBurn __at(0xF0)=0xFFFF;

     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    ric
    Super Member
    • Total Posts : 28335
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/12 17:25:31 (permalink)
    0
    Looking at your logic, it wouldn't matter if you left it how it was. The first test would see the value as zero.
    You DO need the "volatile" qualifier though, or it could assume the value is ALWAYS zero, and throw away the code for the other cases.
     
    post edited by ric - 2019/12/12 17:27:09

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    lmagalhaes
    Starting Member
    • Total Posts : 44
    • Reward points : 0
    • Joined: 2016/01/08 10:38:43
    • Location: 0
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/13 03:34:37 (permalink)
    0
    Even with the volatile qualifier, I only get the first case of the switch. I'm wondering if it's writing correctly to the memory, is there a way to get the flash memory ? I'm using the Read Device Memory but I'm not getting anything different in the Program Memory view, which is kinda obvious if it isn't writing properly to the memory.
     
    I can't debug it either, because I'm getting the "A debug header is required to debug this device." error...
    #9
    ric
    Super Member
    • Total Posts : 28335
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC10F320 storing in memory 2019/12/13 14:15:45 (permalink)
    +1 (1)
    lmagalhaes
    I'm wondering if it's writing correctly to the memory, is there a way to get the flash memory ? I'm using the Read Device Memory

    Yes, that is the way to check it.
     
    I can't debug it either, because I'm getting the "A debug header is required to debug this device." error...

    That's a limitation of these simple low pin count devices unfortunately.
     
    I've not used a PIC10F320 myself. Your code does appear to follow the datasheet.
    I would recommend breaking the problem down into simple steps.
    Right now your FLASH_WriteWord() function does three separate actions. i.e.
    1. Read data into a buffer
    2. Erase FLASH
    3. Program FLASH
    So you should test each of those steps one at a time to see which is failing.
    Note, you can start your program with any arbitrary value at location 0xF0 using this version:
    volatile const unsigned int segmentToBurn __at(0xF0)= 1;  // initialise to "0x0001"

     
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    Jump to:
    © 2020 APG vNext Commercial Version 4.5