View previous topic :: View next topic |
Author |
Message |
titus
Joined: 16 Jan 2013 Posts: 3
|
write_eeprom source code |
Posted: Wed Jan 16, 2013 1:43 am |
|
|
Hello,
Can anyone know where can I find source code for write_eeprom and read_eeprom functions?
I'm using CCS v4.02.
Thank you very much,
Roberto |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jan 16, 2013 3:31 am |
|
|
There isn't.
These are written in assembler, not C, and basically exactly match the examples given in the Microchip notes. Turn off nolist, and look at the generated assembler.
The same applies to almost all the internal functions.
Best Wishes |
|
|
titus
Joined: 16 Jan 2013 Posts: 3
|
|
Posted: Wed Jan 16, 2013 7:28 am |
|
|
Thank you for your answer,
I look at the assembly code but didn't found something looks like reading and writing to and from EEPROM.
I saw that someone wrote the following function:
Code: |
unsigned char ReadEEPROM (unsigned char address)
{
EECON1=0; //ensure CFGS=0 and EEPGD=0
EEADR = address;
EECON1bits.RD = 1;
return(EEDATA);
}
void WriteEEPROM (unsigned char address,unsigned char data)
{
char SaveInt;
SaveInt=INTCON; //save interrupt status
EECON1=0; //ensure CFGS=0 and EEPGD=0
EECON1bits.WREN = 1; //enable write to EEPROM
EEADR = address; //setup Address
EEDATA = data; //and data
INTCONbits.GIE=0; //No interrupts
EECON2 = 0x55; //required sequence #1
EECON2 = 0xaa; //#2
EECON1bits.WR = 1; //#3 = actual write
INTCON=SaveInt; //restore interrupts
while(!PIR2bits.EEIF); //wait until finished
EECON1bits.WREN = 0; //disable write to EEPROM
}
|
In this code there are some command that I don't know if they exist in the assembly code. Such as "wait until finish", enable/disable interrupts etc.
Therefore I need to know how exactlly the write_eeprom and read_eeprom function works. _________________ Roberto |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jan 16, 2013 8:32 am |
|
|
Of course they exist, the functions wouldn't work if they didn't.
Have a look in the data sheet for (say) the PIC16F877. Section 4-3 'writing to the EEPROM data memory'.
Then compiler a single line program, calling write_eeprom, and look at the assembler generated.
The only difference, is that the CCS code (as your version does), waits for write to finish after writing a byte, while the example, checks and waits before writing a byte (more efficient at heart, since you can then get on with other things while the write is actually occurring).
The code you have, has a potential fault, checking EEIF, rather than the WR bit.
You need also to be aware, that the function will _differ_ on some other chips.
Best Wishes |
|
|
titus
Joined: 16 Jan 2013 Posts: 3
|
|
Posted: Wed Jan 16, 2013 8:39 am |
|
|
Thank you again Ttelmah,
So after all I'll stick with CSS functions.
Best regards,
Roberto _________________ Roberto |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jan 16, 2013 8:59 am |
|
|
Just 'FYI', I've documented the standard function:
Code: |
.................... write_eeprom(0,0xFF);
0033: BCF 03.5 //bank selection
0034: MOVF 0B,W //load the contents of the INTCON register
0035: MOVWF 77 //save it
0036: BCF 0B.7 //Turn off GIE
0037: BSF 03.6 //bank selection
0038: CLRF 0D //0 to EEDATA (different if we write a variable)
0039: MOVLW FF
003A: MOVWF 0C //0xFF to EEADR (again different if a variable is used)
003B: BSF 03.5 //bank selection
003C: BCF 0C.7 //select EEPROM as write target
003D: BSF 0C.2 //enable writes
003E: MOVLW 55 //Now send the trigger sequence
003F: MOVWF 0D //0x55 to EECON2
0040: MOVLW AA
0041: MOVWF 0D //and then 0xAA
0042: BSF 0C.1 //trigger the write to start
0043: BTFSC 0C.1 //Now test the WR bit
0044: GOTO 043 //Loop till it clears - write complete
0045: BCF 0C.2 //turn off the write enable
0046: MOVF 77,W //load value that was in INTCON
0047: BCF 03.5 //switch banks again
0048: BCF 03.6
0049: IORWF 0B,F //turn GIE back on if it was on originally
|
It really does do everything.
CCS had some problems with functions, something like 10+ years ago, and do still have problems sometimes with new features on new chips, but the standard functions have been basically 'solid' for many years.
Best Wishes |
|
|
|