In the statement "return EEDATH*256 + EEDATL;" what's probably happening is that the variable/register EEDATH is not sized properly to handle being multiplied by 256. Try creating a temporary unsigned int, copying EEDATH to that, then adding EEDATL to it, and then return it.
Cheers,
-Neil.
On 4/6/2012 8:14 AM, AdiCH wrote:
{Quote hidden}> Hi
>
> I try to read the flash ROM of my PIC16F916 to calculate a checksum.
> I use the MPLAB and the PICC v9.83 Compiler.
>
> I tried this c function:
>
> unsigned int ReadFlash( unsigned int address ) {
> EEADRL = address%256;
> EEADRH = address/256;
> EEPGD = 1; // program memory
> RD = 1; // read
> ;
> ;
> return EEDATH*256 + EEDATL;
> }
>
> and everything in assembler:
>
> unsigned char low3;
> unsigned char high3;
>
> unsigned int dataFlash;
>
> void main() {
>
> #asm
> BCF _STATUS, 5
> BCF _STATUS, 6
> MOVF _low3, W
> BSF _STATUS, 6
> MOVWF _EEADR
> BCF _STATUS, 6
> MOVF _high3, W
> BSF _STATUS, 6
> MOVWF _EEADRH
> BSF _STATUS, 5
> BSF _EECON1, 7
> BSF _EECON1, 0
> NOP
> NOP
> BCF _STATUS, 5
> MOVF _EEDATH, W
> MOVWF _dataFlash+1
> CLRF _dataFlash
> MOVF _EEDATA, W
> IORWF _dataFlash, F
> MOVLW 0
> IORWF _dataFlash+1, F
> #endasm
> }
>
> But both are not working, if I debug it most of the time the register EEDATA
> and EEDATH have the right value. But if I copy it to the variable dataFlash
> it is 0.
>
> With the mikroC compiler is a function to read the flash that works. But I
> can not use this compiler because the build hex file is much bigger and has
> no space on the PIC.
>
> Any idea what is wrong or how I can read the flash ROM?
>
> thanks
>
> Adrian