Hot!Internal EEPROM and EUSART

Page: 12 > Showing page 1 of 2
Author
Kdt
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2018/04/04 07:13:26
  • Location: 0
  • Status: offline
2018/10/03 07:21:00 (permalink)
0

Internal EEPROM and EUSART

Hi all,
 
I am having some trouble to keep my internal eeprom memory. i am using :
 
       - PIC18f47k40 , and
       - MCP2200 (UART to USB converter) 
       - XC8 compiler v2.00
       - MCC for memory and eusart configuration .
 
I am switching on/off a Led on my system by pressing a touch of my keyboard and i create a counter which will be incremented when the Led is ON (1) and i want the value of the counter to be kept in the internal eeprom and i want to display this value on my terminal (CoolTerm) wherever i want by touching another keyboard touch
This works but when i cut off  the supply voltage my counter value is not kept, the value of the counter restarts to 1 ... so my question is : is there a way to keep my value in the internal eeprom even if my system is not supplied? 
 
here is my code :
 
void EUSART_Demo_Command_INT(void) 
{
    if(eusart1RxCount!=0)
    {
       
        temp=EUSART1_Read(); // read a byte for RX
        EUSART1_Write(temp); // send a byte to TX (from Rx)
       

    switch(temp) // check command
    {
     case 'H':
     case 'h':
        {
            LED_SetHigh();
            printf(" -> LED On!! \r");
            number_count++;
            DATAEE_WriteByte(eeprom_address,number_count);
            
            break;
        }
     case 'L':
     case 'l':
        {
            LED_SetLow();
            printf(" -> LED Off!! \r");
            break;
        }
     
     case 'A':
     case 'a':
        {
            printf("\r\nCompiled on %s at %s UTC by XC8 version %u\r\n",
            __DATE__, __TIME__, __XC8_VERSION);
            printf("UART Communications 8-bit Rx and Tx\r\n\n");
            printf("Keyboard Type H : LED ON Type E : EEPROM Type L: LED OFF \r\n\n\n");
            
            break;
        }
     
     case 'e':
     case 'E':
        {
            read_char = DATAEE_ReadByte(eeprom_address);
            printf("\rEeprom read : %i ", read_char);
            
            break;
        }
     
     default:
        {
            printf(" -> Fail Command!! \r");
            break;
        }
    }
    }
    }
    
/**
 End of File
 */

 
Thanks everyone for your help! 
#1

39 Replies Related Threads

    Jim Nickerson
    User 452
    • Total Posts : 5280
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:28:00 (permalink)
    +1 (1)
    Maybe you are looking for a "persistent" variable.
    The manual details the use of a "persistent" variable.
    Or maybe you could make use of your chips EEPROM.
    [Edit], re read the question
    re read again, you are using the eeprom, are you saying on restart the eeprom has not retained the value you wrote ?
    post edited by Jim Nickerson - 2018/10/03 07:30:35
    #2
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:32:01 (permalink)
    0
    Thanks for your replay.
     
    i am already using my chip's EEPROM.
     
    I am going to take a look on this persistent variable thing...
    #3
    Jim Nickerson
    User 452
    • Total Posts : 5280
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:34:04 (permalink)
    +2 (2)
    Maybe there is a debugger option to clear the eeprom each time the chip is programmed ?
    #4
    Jim Nickerson
    User 452
    • Total Posts : 5280
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:34:58 (permalink)
    +1 (1)
    The persistent variable retains the value in Ram between restarts, not power cycles, my error.
    #5
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:42:03 (permalink)
    0
     "are you saying on restart the eeprom has not retained the value you wrote ?"
     
    Yes the value returns to "0" when i restart a power cycle but i want the real value incremented to stay in the eeprom...  
     
    "The persistent variable retains the value in Ram between restarts, not power cycles, my error."
     
    Yeah unfortunately...
    #6
    NKurzman
    A Guy on the Net
    • Total Posts : 16442
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 07:53:34 (permalink)
    +1 (1)
    Is the Brown at Reset turned on?
    How are you telling it is wrong?
    A power cycle on the debugger, or a normal power cycle?
    The EEPROM does what you want to do.
    #7
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 08:02:36 (permalink)
    0
     
    "Is the Brown at Reset turned on?"
     
    Yes (#pragma config BOREN = SBORDIS    // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored)
     
    How are you telling it is wrong?
     
    i cut off the supply voltage with a value in "number_count"( different of 0) and when i put on the supply voltage again the value in "number_count" is 0
     
    "A power cycle on the debugger, or a normal power cycle?"
     
    A normal power cycle
     
    "The EEPROM does what you want to do."
     
    No because my value in "number_count" is not kept.
    #8
    jack@kksound
    code tags!
    • Total Posts : 2761
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 09:08:49 (permalink)
    +1 (1)
    I suspect the issue is not in the code segment you posted but in some other part.
    #9
    Jim Nickerson
    User 452
    • Total Posts : 5280
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 09:35:05 (permalink)
    0
    Maybe you could verify the data just after writing it to ee ?
    #10
    jtemples
    Super Member
    • Total Posts : 10956
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 10:00:18 (permalink)
    +1 (1)
    i put on the supply voltage again the value in "number_count" is 0

     
    You haven't shown any code that reads EEPROM and writes the value to number_count.
    #11
    NKurzman
    A Guy on the Net
    • Total Posts : 16442
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/03 10:05:36 (permalink)
    +2 (2)
    Kdt
     
     
     
    How are you telling it is wrong?
     
    i cut off the supply voltage with a value in "number_count"( different of 0) and when i put on the supply voltage again the value in "number_count" is 0
    And you are looking at the Chip with your Xray Vision to see? What are you checking it with ? The serial Port? the Debugger?
     
     
     
    "The EEPROM does what you want to do."
     
    No because my value in "number_count" is not kept.
     
    No It does, you need to find out what is wrong with your Code to stop it.




    #12
    crosland
    Super Member
    • Total Posts : 1257
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: online
    Re: Internal EEPROM and EUSART 2018/10/03 11:27:28 (permalink)
    +2 (2)
    If you want help you need to show the code that reads and writes the eeprom and the code that initialises eeprom_address.
    #13
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/07 23:51:06 (permalink)
    0
    Hello,
     
    here is the code to read end write in the eeprom,this was directly generated with mcc
     
    #include <xc.h>
    #include "memory.h"

    /**
    Section: Flash Module APIs
    */
    uint8_t FLASH_ReadByte(uint32_t flashAddr)
    {
    NVMCON1bits.NVMREG = 2;
    TBLPTRU = (uint8_t)((flashAddr & 0x00FF0000) >> 16);
    TBLPTRH = (uint8_t)((flashAddr & 0x0000FF00)>> 8);
    TBLPTRL = (uint8_t)(flashAddr & 0x000000FF);
    asm("TBLRD");
    return (TABLAT);
    }
    uint16_t FLASH_ReadWord(uint32_t flashAddr)
    {
    return ((((uint16_t)FLASH_ReadByte(flashAddr+1))<<8)|(FLASH_ReadByte(flashAddr)));
    }
    void FLASH_WriteByte(uint32_t flashAddr, uint8_t *flashRdBufPtr, uint8_t byte)
    {
    uint32_t blockStartAddr = (uint32_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++)
    {
    flashRdBufPtr = FLASH_ReadByte((blockStartAddr+i));
    }
    // Load byte at offset
    flashRdBufPtr[offset] = byte;
    // Writes buffer contents to current block
    FLASH_WriteBlock(blockStartAddr, flashRdBufPtr);
    }
    int8_t FLASH_WriteBlock(uint32_t writeAddr, uint8_t *flashWrBufPtr)
    {
    uint32_t blockStartAddr = (uint32_t )(writeAddr & ((END_FLASH-1) ^ (ERASE_FLASH_BLOCKSIZE-1)));
    uint8_t GIEBitValue = INTCONbits.GIE; // Save interrupt enable
    uint8_t i;
    // Flash write must start at the beginning of a row
    if( writeAddr != blockStartAddr )
    {
    return -1;
    }
    // Block erase sequence
    FLASH_EraseBlock(writeAddr);
    // Block write sequence
    TBLPTRU = (uint8_t)((writeAddr & 0x00FF0000) >> 16); // Load Table point register
    TBLPTRH = (uint8_t)((writeAddr & 0x0000FF00)>> 8);
    TBLPTRL = (uint8_t)(writeAddr & 0x000000FF);
    // Write block of data
    for (i=0; i<WRITE_FLASH_BLOCKSIZE; i++)
    {
    TABLAT = flashWrBufPtr; // Load data byte
    if (i == (WRITE_FLASH_BLOCKSIZE-1))
    {
    asm("TBLWT");
    }
    else
    {
    asm("TBLWTPOSTINC");
    }
    }
    NVMCON1bits.NVMREG = 2;
    NVMCON1bits.WREN = 1;
    INTCONbits.GIE = 0; // Disable interrupts
    NVMCON2 = 0x55;
    NVMCON2 = 0xAA;
    NVMCON1bits.WR = 1; // Start program
    NVMCON1bits.WREN = 0; // Disable writes to memory
    INTCONbits.GIE = GIEBitValue; // Restore interrupt enable

    return 0;
    }
    void FLASH_EraseBlock(uint32_t baseAddr)
    {
    uint8_t GIEBitValue = INTCONbits.GIE; // Save interrupt enable

    TBLPTRU = (uint8_t)((baseAddr & 0x00FF0000) >> 16);
    TBLPTRH = (uint8_t)((baseAddr & 0x0000FF00)>> 8);
    TBLPTRL = (uint8_t)(baseAddr & 0x000000FF);
    NVMCON1bits.NVMREG = 2;
    NVMCON1bits.WREN = 1;
    NVMCON1bits.FREE = 1;
    INTCONbits.GIE = 0; // Disable interrupts
    NVMCON2 = 0x55;
    NVMCON2 = 0xAA;
    NVMCON1bits.WR = 1;
    INTCONbits.GIE = GIEBitValue; // Restore interrupt enable
    }
    /**
    Section: Data EEPROM Module APIs
    */
    void DATAEE_WriteByte(uint16_t bAdd, uint8_t bData)
    {
    uint8_t GIEBitValue = INTCONbits.GIE; // Save interrupt enable
    NVMADRH = ((bAdd >> 8) & 0x03);
    NVMADRL = (bAdd & 0xFF);
    NVMDAT = bData;
    NVMCON1bits.NVMREG = 0;
    NVMCON1bits.WREN = 1;
    INTCONbits.GIE = 0; // Disable interrupts
    NVMCON2 = 0x55;
    NVMCON2 = 0xAA;
    NVMCON1bits.WR = 1;
    // Wait for write to complete
    while (NVMCON1bits.WR)
    {
    }
    NVMCON1bits.WREN = 0;
    INTCONbits.GIE = GIEBitValue; // Restore interrupt enable
    }
    uint8_t DATAEE_ReadByte(uint16_t bAdd)
    {
    NVMADRH = ((bAdd >> 8) & 0x03);
    NVMADRL = (bAdd & 0xFF);
    NVMCON1bits.NVMREG = 0;
    NVMCON1bits.RD = 1;
    NOP(); // NOPs may be required for latency at high frequencies
    NOP();
    return (NVMDAT);
    }
    void MEMORY_Tasks( void )
    {
    /* TODO : Add interrupt handling code */
    PIR7bits.NVMIF = 0;
    }
    /**
    End of File
    */
    #14
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/08 00:00:23 (permalink)
    0
     
    And you are looking at the Chip with your Xray Vision to see? What are you checking it with ? The serial Port? the Debugger?
     
    The serial Port
    #15
    jtemples
    Super Member
    • Total Posts : 10956
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/08 09:31:53 (permalink)
    +1 (1)
    here is the code to read end write in the eeprom

     
    Which still doesn't load a value into number_count.
    #16
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/08 22:59:47 (permalink)
    0
    "Which still doesn't load a value into number_count."
     
    I load the value in another function (it is in the first code i posted )
     
     case 'H':
         case 'h':
            {
                LED_SetHigh();
                printf(" -> LED On!! \r");
                number_count++;
                DATAEE_WriteByte(eeprom_address,number_count);
                break;
            }

     case 'e':
         case 'E':
            {
                read_char = DATAEE_ReadByte(eeprom_address);
                printf("\rEeprom read : %i ", read_char);
                
                break;
            }

    #17
    jtemples
    Super Member
    • Total Posts : 10956
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/09 00:09:54 (permalink)
    +1 (1)
    You are reading the value into read_char, not number_count.  You never put any value into number_count.
    #18
    Kdt
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2018/04/04 07:13:26
    • Location: 0
    • Status: offline
    Re: Internal EEPROM and EUSART 2018/10/09 02:43:26 (permalink)
    0
     
     
     
    number_count = number_count +1  everytime  i set my LED high.
     
    then I store the value of number_count in the eeprom_adress and i come read the value in the eeprom adress using read_char 
     
     
    #19
    qhb
    Superb Member
    • Total Posts : 7157
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Internal EEPROM and EUSART 2018/10/09 02:46:58 (permalink)
    +1 (1)
    something unknown +1 = something still unknown
     

    Worst forum problems are now fixed, but the damn firewall is still there.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2018 APG vNext Commercial Version 4.5