> -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf > Of AdiCH > Sent: 06 April 2012 13:15 > To: piclist@mit.edu > Subject: [PIC] Read Flash of PIC16F916 >=20 >=20 > Hi >=20 > I try to read the flash ROM of my PIC16F916 to calculate a checksum. > I use the MPLAB and the PICC v9.83 Compiler. >=20 > I tried this c function: >=20 > unsigned int ReadFlash( unsigned int address ) { > EEADRL =3D address%256; > EEADRH =3D address/256; > EEPGD =3D 1; // program memory > RD =3D 1; // read > ; > ; > return EEDATH*256 + EEDATL; > } >=20 > and everything in assembler: >=20 > unsigned char low3; > unsigned char high3; >=20 > unsigned int dataFlash; >=20 > void main() { >=20 > #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 > } >=20 > 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. I'm not surprised the assembler version is not working, the code for transf= erring the data from EEDATH/EEDATA into 'dataflash' is visibly wrong; I hav= e added comments below: MOVF _EEDATH, W ; EEDATH is now in W MOVWF _dataFlash+1 ; EEDATH is now in MSB of 'dataflash' CLRF _dataFlash ; LSB of 'dataflash' is now zero MOVF _EEDATA, W ; EEDATA is now in W IORWF _dataFlash, F ; EEDATA is now in LSB of 'dataflash' MOVLW 0 ; W=3D0 IORWF _dataFlash+1, F ; MSB of 'dataflash' is now zero. Why not do this? MOVF _EEDATH, W ; EEDATH is now in W MOVWF _dataFlash+1 ; EEDATH is now in MSB of 'dataflash' MOVF _EEDATA, W ; EEDATA is now in W MOVWF _dataFlash, F ; EEDATA is now in LSB of 'dataflash' Please note I have not checked that your bank switching is correct; it woul= d be much better to use macros to do this if your compiler supports this in= inline assembler. In the C version it looks like you are assuming that a line that contains o= nly a semicolon will be converted into a NOP operation, are you certain thi= s is the case? In the compilers I use a semicolon by itself will not produc= e any code, and you might need to use inline assembler to insert a NOP (or = your compiler may provide a macro to do this). Also be very careful using the modulo operator to extract high and low byte= s from a word. A modulo operation is very expensive in terms of CPU cycles.= The optimiser may fix this if it notices you are doing a simple byte move= operation, but don't count on it. A simple bit shift/mask is a much bette= r idea: EEADRL =3D address & 0xFF; EEADRH =3D address >> 8; Cheers Mike =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .