CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

reading flash of 16F76

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
simon630



Joined: 07 Sep 2003
Posts: 17

View user's profile Send private message

reading flash of 16F76
PostPosted: Thu Sep 04, 2003 4:12 am     Reply with quote

Dear friends,

Following is the code of Hamlett for accessing flash memory of 16LF76.

:=#byte PMCON1=0x18C
:=#byte PMDATA=0x10C
:=#byte PMADR=0x10D
:=#byte PMADRH=0x10F
:=#byte PMDATH=0x10E
:=
:=union split {
:= int16 word;
:= int8 b[2];
:=}
:=
:=int16 read_word(union split address) {
:= union split data;
:= //Set address to read
:= PMADRH=address.b[1];
:= PMADR=address.b[0];
:= //Trigger read cycle
:= BIT_SET(PMCON1,0);
:= //delay to allow the read to happen - vital...
:= delay_cycles(2);
:= //Now read the data
:= data.b[0]=PMDATA;
:= data.b[1]=PMDATH;
:= //and return to the caller
:= return(data.word);
:=}

Thanks Hamlett for helping me so much by posting the code. I tried out the code, but unfortunately, the emulator halts after delay_cycles(2). then i scanned through page 32 of datasheet for PIC16F7X. there is a similar assembly code provided on that page. then i put it into one C routine called int16 check_sum(int16 address):

union {
int16 word;
int8 bytes[2];
} mem_add;

int16 check_sum(int16 address){

mem_add.word = address;
#asm
BSF STATUS, RP1
BCF STATUS, RP0
MOVF mem_add.bytes[1], W
MOVWF PMADRH
MOVF mem_add.bytes[0], W
MOVWF PMADR
BSF STATUS, RP0

BSF PMCON1, RD
NOP
NOP

BCF STATUS, RP0
MOVF PMDATA, W
MOVWF mem_add.bytes[0]
MOVF PMDATH, W
MOVWF mem_add.bytes[1]
#endasm
return mem_add.word;
}

then i try to run
test=check_sum(0x01);
in my main().
i also found that the same problem occurred. after the latter NOP, the emulator halts there. then i guessed something with the set of RD bit of PMCON1 register. i commented the BSF PMCON1, RD
statement, then the emulator did not halt anymore. but of course the data in the flash was not read successfully.
i hope that i am confusing you all. anyone knows why? thank you in advance.
Simon
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517533
R.J.Hamlett
Guest







Re: reading flash of 16F76
PostPosted: Thu Sep 04, 2003 9:53 am     Reply with quote

:=Dear friends,
:=
:=Following is the code of Hamlett for accessing flash memory of 16LF76.
:=
:=:=#byte PMCON1=0x18C
:=:=#byte PMDATA=0x10C
:=:=#byte PMADR=0x10D
:=:=#byte PMADRH=0x10F
:=:=#byte PMDATH=0x10E
:=:=
:=:=union split {
:=:= int16 word;
:=:= int8 b[2];
:=:=}
:=:=
:=:=int16 read_word(union split address) {
:=:= union split data;
:=:= //Set address to read
:=:= PMADRH=address.b[1];
:=:= PMADR=address.b[0];
:=:= //Trigger read cycle
:=:= BIT_SET(PMCON1,0);
:=:= //delay to allow the read to happen - vital...
:=:= delay_cycles(2);
:=:= //Now read the data
:=:= data.b[0]=PMDATA;
:=:= data.b[1]=PMDATH;
:=:= //and return to the caller
:=:= return(data.word);
:=:=}
:=
:= Thanks Hamlett for helping me so much by posting the code. I tried out the code, but unfortunately, the emulator halts after delay_cycles(2). then i scanned through page 32 of datasheet for PIC16F7X. there is a similar assembly code provided on that page. then i put it into one C routine called int16 check_sum(int16 address):
:=
:=union {
:=int16 word;
:=int8 bytes[2];
:=} mem_add;
:=
:=int16 check_sum(int16 address){
:=
:=mem_add.word = address;
:=#asm
:=BSF STATUS, RP1
:=BCF STATUS, RP0
:=MOVF mem_add.bytes[1], W
:=MOVWF PMADRH
:=MOVF mem_add.bytes[0], W
:=MOVWF PMADR
:=BSF STATUS, RP0
:=
:=BSF PMCON1, RD
:=NOP
:=NOP
:=
:=BCF STATUS, RP0
:=MOVF PMDATA, W
:=MOVWF mem_add.bytes[0]
:=MOVF PMDATH, W
:=MOVWF mem_add.bytes[1]
:=#endasm
:=return mem_add.word;
:=}
:=
:=then i try to run
:=test=check_sum(0x01);
:=in my main().
:=i also found that the same problem occurred. after the latter NOP, the emulator halts there. then i guessed something with the set of RD bit of PMCON1 register. i commented the BSF PMCON1, RD
:=statement, then the emulator did not halt anymore. but of course the data in the flash was not read successfully.
:=i hope that i am confusing you all. anyone knows why? thank you in advance.
:=Simon

I'd suggest you look at the emulator 'limitations' page. On MPLAB, if I select the ICD option, and look at the limitations, it displays:
"If a single step sets the read bit of the PMCON1 register, or if you use the modify window to set the read bit of the PMCON1 register, then no further reads of program memory will occur".

Basically the emulator is using this read ability to allow _it_ to read the memory, and as soon as you use the same ability in the code, then the emulator stops...
This is where a hardware ICE, has an edge.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517543
simon630



Joined: 07 Sep 2003
Posts: 17

View user's profile Send private message

Re: reading flash of 16F76
PostPosted: Thu Sep 04, 2003 7:38 pm     Reply with quote

:=I'd suggest you look at the emulator 'limitations' page. On MPLAB, if I select the ICD option, and look at the limitations, it displays:
:="If a single step sets the read bit of the PMCON1 register, or if you use the modify window to set the read bit of the PMCON1 register, then no further reads of program memory will occur".
:=
:=Basically the emulator is using this read ability to allow _it_ to read the memory, and as soon as you use the same ability in the code, then the emulator stops...
:=This is where a hardware ICE, has an edge.
:=
:=Best Wishes

So does it mean that i would not be able to test my checksum routine on the emulator? actually what i want to do is to verify whether the data which read_word() reads back is the same as the one in the object file. but the emulator stops whenever i set the RD bit of this register. then i guess the only way to test it is on the pic itself.
Simon
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517567
simon630



Joined: 07 Sep 2003
Posts: 17

View user's profile Send private message

Re: reading flash of 16F76
PostPosted: Fri Sep 05, 2003 3:31 am     Reply with quote

<font face="Courier New" size=-1>Good. data could be verified on the pic already although not on the emulator. i just compare the data read by the software and the one inside the object file. when they are equal i just light up one LED. it works.
but another question, if i am now using XOR for my checksum. that means i XOR every byte from the flash memory, and then store it back inside the ROM. (if this is done by XOR the bytes in the HEX file by myself). later when i want to do a ROM test, i just call the calculation routine, and then compare the value previously calculated.
The question is i do not know how to store the so-called 'checksum' inside the ROM. If i am using #define directive, this will affect the content of the ROM code.
maybe a program may help you to understand. here it is:

#include <16F76.h>
#fuses XT,NOWDT,NOPROTECT,PUT


#define CS 0x79 ??

#use delay(clock=4000000)


#byte STATUS_REG=0x03
#byte PMCON1=0x18C
#byte PMDATA=0x10C
#byte PMADR=0x10D
#byte PMADRH=0x10F
#byte PMDATH=0x10E

union {
int16 word;
int8 bytes[2];
} adr_data;

void read_word(int16 address) {

//Set address to read
adr_data.word = address;

PMADRH=adr_data.bytes[1];
PMADR=adr_data.bytes[0];

//Trigger read cycle
BIT_SET(PMCON1,0);

//delay to allow the read to happen - vital...
delay_cycles(2);

//Now read the data
adr_data.bytes[0]=PMDATA;
adr_data.bytes[1]=PMDATH;

}



main() {


long b;
char checksum;

while (1) {
checksum = 0;
for( b=0; b<8192; b++ ){
read_word(b);
checksum=checksum^adr_data.bytes[0]^adr_data.bytes[1];
}
if(checksum == CS) output_high(PIN_C0);

} //end while
}

/******************************************************************************************/

The question is how to store the CS inside this program? because it will affect the ROM code, i could not define it when it is not present yet. previously i thought putting it inside the RAM. but data inside the RAM tends to corrupt quite easily. that's why i want to store it inside the ROM.
by the way, i notice there is a directive for CCS called #ID CHECKSUM. anyone know how this checksum generate? is it calculated by adding up the bytes and then do 2's complement?
thank you in advance
Simon ___________________________
This message was ported from CCS's old forum
Original Post ID: 144517575
Guest








Re: reading flash of 16F76
PostPosted: Mon Sep 08, 2003 10:08 am     Reply with quote

simon630 wrote:
Old Post ID # 144517575<BR> Parrent Post ID # 144517533 <BR> Message sender: simon630 <BR> <BR><font face="Courier New" size=-1>Good. data could be verified on the pic already although not on the emulator. i just compare the data read by the software and the one inside the object file. when they are equal i just light up one LED. it works. <BR>but another question, if i am now using XOR for my checksum. that means i XOR every byte from the flash memory, and then store it back inside the ROM. (if this is done by XOR the bytes in the HEX file by myself). later when i want to do a ROM test, i just call the calculation routine, and then compare the value previously calculated. <BR>The question is i do not know how to store the so-called 'checksum' inside the ROM. If i am using #define directive, this will affect the content of the ROM code. <BR>maybe a program may help you to understand. here it is: <BR> <BR>#include


Hi Simon,
I suggest you repost on the new board, since your program has been lost (or at least is not visible), following the board upgrade.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group