Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Writing EEPROM dsPIC30f?

Status
Not open for further replies.

newbie1212

Newbie level 5
Joined
Apr 7, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
57
Hello every body,
I am trying to write in the eeprom, the code example is provided by Micoship. The code works fine, but i want to change the writing adress EE_addr and increment it by 13 each cycle.


Code C - [expand]
1
2
3
4
5
6
7
8
_prog_addressT EE_addr;
    
    /* initialize a variable to represent the Data EEPROM address */
    _init_prog_address(EE_addr, fooArrayInDataEE);
 
I have read that it is not equivalent to c pointer, indeed i tried:
EE_addr= EE_addr+13;
EE_addr=*(EE_addr+13);


any idea how i can change the starting writing adress?
thank you in advance.
 
Last edited by a moderator:

Is _prog_addressT = unsigned int? If yes, EE_addr += 13; is correct but it should be called only once in the while(1) loop. You want to increment by decimal 13 or 0x13? If 0x13 then it is EE_addr += 0x0D;

any idea how i can change the starting writing adress?
thank you in advance.


Code C - [expand]
1
_prog_addressT EE_addr = 0x0D;

 

Unfortunatly it doesnt work. I tried hex/dec values,pointer/scalar, exact adress names (ox7ff00D)/relative (0x0D). But it seems it is not changing any way.
I read in the manual that NVMADRU:NVMADR can be manually loaded to modify the address, i added those two lines before erase and write also (eedate_helper.s in libpic30 for MPLAB C30):

mov w0,NVMADR
mov w1,NVMADRU

Still no success, any suggestion?
 

Mention the exact dsPIC30 device you are using. Are you sure dsPIC30 has internal eeprom so big that it has a address like 0x7FF00D?
 

Internal eeprom size of your device is only 4KB and last address is 0x0FFF. Try attached .hex file. It is compiled for 32 MHz (4 MHz crystal X 8PLL). MCLR is enabled. It writes PORTB with 0x55 and 0xAA after reading those values from EEPROM. Initially those values are written to EEPROM before while(1) loop.

This is the mikroC code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void main() {
 
     TRISB = 0x000;
     LATB = 0x0000;
     
     EEPROM_Write(0x0FFE, 0x55);
     EEPROM_Write(0x0FFF, 0xAA);
           
     while(1) {
     
          LATB = EEPROM_Read(0x0FFE);
          Delay_ms(500);
          LATB = EEPROM_Read(0x0FFF);
          Delay_ms(500);
     
     }
}

 

Attachments

  • eeprom_test.rar
    664 bytes · Views: 60

I am sorry but i think i did not understand you so well. There is no EEPROM_Write function defined in CE017 example, instead there is: _write_eedata_word(EE_addr,EE_data).
And i already tried tried _write_eedata_word(0x00D,0x05) but it did not work. Would you please clearify more?
 

I clearly mentioned that my code is mikroC PRO dsPIC code and not MPLAB C30. For better help zip and post your complete MPLAB C30 project files.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Replace Delay_ms() function with delay ms function provided by C30 compiler
 
void main() {
 
     TRISB = 0x000;
     LATB = 0x0000;
     
     _write_eedata_word(0x0FFE, 0x0055);
     _write_eedata_word(0x0FFF, 0x00AA);
           
     while(1) {
     
          LATB = _read_eedata_word(0x0FFE);
          Delay_ms(500);
          LATB = _read_eedata_word(0x0FFF);
          Delay_ms(500);
     
     }
}

 

Thank you for the advice, here is the code:
Code:
#include <p30f6012a.h>
#include <libpic30.h>

_FOSC(CSW_FSCM_OFF & XT_PLL8); /* Set up for XTxPLL8 mode since */
                                /* we will be tuning the FRC in this example */
_FWDT(WDT_OFF);                 /* Turn off the Watch-Dog Timer.  */
_FBORPOR(MCLR_EN & PWRT_OFF);   /* Enable MCLR reset pin and turn off the power-up timers. */
_FGS(CODE_PROT_OFF);            /* Disable Code Protection */

/*Declare constants/coefficients/calibration data to be stored in DataEEPROM*/
int _EEDATA(32) fooArrayInDataEE[] = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF};
void _ISR _DefaultInterrupt(void);


int main(void)
{
    _prog_addressT EE_addr; //this is what i want to change -increment-
    
    /* initialize a variable to represent the Data EEPROM address */
    _init_prog_address(EE_addr, fooArrayInDataEE);
    _erase_eedata(EE_addr, _EE_ROW);
    _wait_eedata();
    _write_eedata_row(EE_addr, fooArrayInDataEE);
    _wait_eedata();


    while(1); /* Place a breakpoint here, run code and refresh the DataEEPROM window in MPLAB IDE */
}


void __attribute__((interrupt, no_auto_psv)) _DefaultInterrupt(void)
{
    while(1) ClrWdt()
}
 

Attachments

  • CE017_DataEEPROM_write_erase_functions.zip
    14.4 KB · Views: 37

I haven't used C30 much and also have not used eeprom functions with C30. I guess that address of fooArray... is incremented by the function automatically. and I guess this will write 16 words from 0x0000 to 0x001F. Each eeprom address can hold one byte and each data is 1 word, so 2 memory locations are needed for 1 word.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "p30fxxxx.h"
#include "DataEEPROM.h"
 
_FOSC(CSW_FSCM_OFF & XT_PLL8); /* Set up for XTxPLL8 mode since */
                                /* we will be tuning the FRC in this example */
_FWDT(WDT_OFF);                 /* Turn off the Watch-Dog Timer.  */
_FBORPOR(MCLR_EN & PWRT_OFF);   /* Enable MCLR reset pin and turn off the power-up timers. */
_FGS(CODE_PROT_OFF);            /* Disable Code Protection */
 
/*Declare constants/coefficients/calibration data to be stored in DataEEPROM*/
int _EEDATA(32) fooArrayInDataEE[] = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF};
 
/*Declare variables to be stored in RAM*/
int fooArray1inRAM[] = {0xAAAA, 0xBBBB, 0xCCCC, 0xDDDD, 0xEEEE, 0xFFFF, 0xABCD, 0xBCDE,
                       0xCDEF, 0xDEFA, 0x0000, 0x1111, 0x2222, 0x3333, 0x4444, 0x5555};
 
int fooArray2inRAM[16];
 
int main(void);
 
void _ISR _DefaultInterrupt(void);
 
int gPage = 0x0000, gOffset = 0x0000, gSize = 16;
 
int main(void)
{
    
    while(1) {
 
        WriteEE(&fooArray1inRAM, gPage, gOffset, gSize);
 
        gPage = 0x0000;
        gOffset = 0x0000;
        gSize = 16;
 
        ReadEE(gPage, gOffset, &fooArray2inRAM, gSize);
 
    }
}
 
 
void __attribute__((interrupt, no_auto_psv)) _DefaultInterrupt(void)
{
    while(1) ClrWdt()
}
 
 
 
 
 
/*
 * WriteEErow prototype:
 * Parameters Definition:
 * Page:        is the 8 most significant bits of the destination address in EEPROM
 * Offset:      is 16 least significant bits of the destination address in EEPROM
 * DataIn:      is the 16-bit address of the source RAM location or array
 * Size:        is the number of words to read from EEPROM and is a value of 1 or 16
 * Return Value:
 * Function returns ERROREE (or -1) if Size is invalid
 
extern int WriteEE(int* DataIn, int Page, int Offset, int Size);
 
 
 * ReadEErow prototype:
 * Parameters Definition:
 * Page:        is the 8 most significant bits of the source address in EEPROM
 * Offset:      is 16 least significant bits of the source address in EEPROM
 * DataOut:     is the 16-bit address of the destination RAM location or array
 * Size:        is the number of words to read from EEPROM and is a value of 1 or 16
 * Return Value:
 * Function returns ERROREE (or -1) if Size is invalid
 
extern int ReadEE(int Page, int Offset, int* DataOut, int Size);
*/

 
That is what i think also, i tried to write it from scratch using assembly as the datasheet explains with no success as well "FRUSTRATED". I appreciate ur help Milan, but i can not use mikroC compiler.
 

The code I gave in post #10 is for C30 and not mikroC.
 

Hello Milan,
Thank you again for the help. the code you wrote is the older version of CE017 example (2005) and it has the same problem as the one i posted (2007 version). Both of them are exactally the same they only integrated DataEEPROM.h in the libpic30.h.
I am thinking now about using mikroC compiler, but the problem is that big part from the project is already done i am mostly worried about the mixing C and Assembly language in the code. In MPLAB you can write assembly instructions directly in your C code: asm ("mov #15,w0"); for example. As you are experienced with mikroC can you please advice and if possible what i should consider more?
 

In mikroC you can do inline asm like this...


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(1) {
 
        _asm {
                 movlw 0x55
                 movwf PORTB
 
         } 
 
 
         Delay_ms(2000);
 
         _asm {
                movlw 0xAa
                movwf PORTB
         } 
 
}




It also has EEPROM library. Try to port the code to mikroC and if you face any problem then ask in this thread.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top