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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
| *********************************** dsPIC30F4011_Flash program *************************************/
#include <p30f4011.h>
#include <libpic30.h>
#include <stdio.h>
#include "lcd.h"
#define ERASE_WORD 0x4041 //#define ERASE_WORD 0x4044
#define WRITE_WORD 0x4001 //#define WRITE_WORD 0x4004
#define ADDRESS_HI 0x007F
#define EEPROM_LOW_START_ADDRESS 0x0000
#define TRUE 1
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_DIS); // Disable reset pin
_FGS(CODE_PROT_OFF);
int main(void)
{
int value=0, digit=0;
lcd_init();
//Setup UART for debugging
U1BRG = 48; // Setup UART for debugging;;; 38400 baud @ 30 MIPS;;
U1MODEbits.UARTEN = 1; // Enable UART
while(1)
{
//digit = Eeprom_ReadWord(0);
//LCDWriteIntXY(0,0,digit,4); // Eeprom_WriteWord(13,0x4612 );Eeprom_WriteWord(84,0xf123 );Eeprom_WriteWord(30,0x4444 );
//digit++;
//Eeprom_WriteWord(0,digit);
//del(300); // original loop
digit = Eeprom_ReadWord(5);
if(digit == 255)
// lcd_data('P');
LCDWriteIntXY(0,0,digit,3);
digit++;
Eeprom_WriteWord(5,digit);
del(400);
}
return 0;
}
void Eeprom_WriteWord(unsigned short pushAddressOffset, int value)
{
unsigned short pushAddress;
pushAddressOffset = pushAddressOffset*2; //word offset , byte addressable so multiply by 2
pushAddress = pushAddressOffset + EEPROM_LOW_START_ADDRESS;
TBLPAG = ADDRESS_HI;
NVMADRU = ADDRESS_HI; // Write address of word to be erased into NVMADRU, NVMADR registers.
NVMADR = (unsigned short) pushAddress;
NVMCON = ERASE_WORD; // Setup NVMCON register to erase one EEPROM word.
//PROTECT_CODE_FROM_INTERRUPTS_START // Disable interrupts while the KEY sequence is written
NVMKEY = 0x55; // Write the KEY sequence step1
NVMKEY = 0xAA; // step2
NVMCONbits.WR = TRUE; // Start the erase cycle
//PROTECT_CODE_FROM_INTERRUPTS_STOP // Enable interrupts
while (NVMCONbits.WR == TRUE); // wait for the EEPROMS
NVMCON = WRITE_WORD; // Setup NVMCON register to write one EEPROM word.
{
int eedata_addr;
unsigned char eedata_val;
eedata_addr = (unsigned short)pushAddress; // write low word of address
eedata_val = value; // write data
__asm__ volatile ("TBLWTL %[val], [%[addr]]" : [val]"+r"(eedata_val) : [addr]"r"(eedata_addr));
}
NVMCON = WRITE_WORD;
//PROTECT_CODE_FROM_INTERRUPTS_START // Disable interrupts while the KEY sequence is written
NVMKEY = 0x55; // Write the KEY sequence step1
NVMKEY = 0xAA; // step2
NVMCONbits.WR = TRUE;
//NVMCONbits.WREN = 0; // Start the erase cycle
//PROTECT_CODE_FROM_INTERRUPTS_STOP // Enable interrupts
while (NVMCONbits.WR == TRUE); // wait for the word to be written
}
int Eeprom_ReadWord(unsigned short pushAddressOffset)
{
int Result;
register int eedata_addr;
register unsigned char eedata_val;
unsigned short pushAddress;
pushAddressOffset = pushAddressOffset*2; //word offset , byte addressable so multiply by 2
pushAddress = pushAddressOffset + EEPROM_LOW_START_ADDRESS;
TBLPAG = ADDRESS_HI; // __builtin_tblpage()
eedata_addr = (unsigned short)pushAddress; // __builtin_tbloffset()
__asm__("TBLRDL [%[addr]], %[val]" : [val]"=r"(eedata_val) : [addr]"r"(eedata_addr));
Result = eedata_val;
return Result;
} |