Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Writing a pattern with while loop ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,

How can I write a pattern into serial memory properly ?
Can I use a loop below ? with while ?

Or
Any other suggestions or experiences ?

Thanks

Code:
unsigned char pattern[8] = {0x3C,0x42,0xA5,0xA5,0x81,0xA5,0x5A,0x3C,};
  	 address=0x0000;

while (address<0x0008)
   {
	   
		for(counter=0;counter<8;counter++)
		
		 {
		  
		   AT25_WriteByte(address,pattern[counter]);
		   	_nop_();
			_nop_();
			address=address+0x0001;
		 }

	}
 

Hello!

You should verify with the docs, but depending on the eeprom, you write the address once
and then you can write a sequence of bytes without re-entering the address each time.
And it stops at a page boundary or something like this. Well, red the docs!

Dora.
 

i think you can use While loop to send pattern. and above code seems correct. But what acctually your problem is? please explain.

My problem is, the character is written randomly in the memory, not as I wanted to be
memory1.jpg
 

the code appers to be right....but i think you need to declear the unsigned char before address =0x0000; or declear it global ....also you need probably more dealy in the loop for correct data writting....

Good Luck
 

Hello!

You should verify with the docs, but depending on the eeprom, you write the address once
and then you can write a sequence of bytes without re-entering the address each time.
And it stops at a page boundary or something like this. Well, red the docs!

Dora.

something like this ? Good idea
for(counter=0;counter<8;)

{


AT25_WriteByte(0000,pattern[counter]);
_nop_();
_nop_();

}
 

random data placement is only due to wrong address. since you have written a part of your full code so i can't say what exactly is going on. but the probable mistakes may be that ......

1) the manipulation of function
AT25_WriteByte(address,pattern[counter]);
may be out of Scope of variable "address", because the control of program destroy the memory space reserved for the variable when it jumps out of its scope.

2) in rest of your code you may be manipulating the address passed by the function wrongly; resulting in placement of remaining data wrong memory locations.
 

random data placement is only due to wrong address. since you have written a part of your full code so i can't say what exactly is going on. but the probable mistakes may be that ......

1) the manipulation of function
AT25_WriteByte(address,pattern[counter]);
may be out of Scope of variable "address", because the control of program destroy the memory space reserved for the variable when it jumps out of its scope.

2) in rest of your code you may be manipulating the address passed by the function wrongly; resulting in placement of remaining data wrong memory locations.

[/QUOTE]

void AT25_WriteByte (unsigned int address,unsigned char dat)
{
AT25_WritePage(address,1,&dat);
}

void AT25_WritePage(uint address, //from this address;
uchar num_of_byte, //the number(<32) of bytes to write;
uchar* source //data to write.
)
{
uchar i=0;

/* Filter the parameters */
if(num_of_byte>32) num_of_byte=32;
//if(address>4096) address=0;
if(address>8192) address=0;
/* make sure that the device is write enabled */
AT25320_CS=0;
_nop_();
SPI_WriteByte(WREN);
AT25320_CS=1;
_nop_();
_nop_();

/* write op_code,address and data into the device */

AT25320_CS=0;
_nop_();
SPI_WriteByte(WRITE); //op_code

//SPI_WriteByte((uchar)((address & 0x0F00)>>8)); //write address A11~A0
//SPI_WriteByte((uchar)((address & 0x00F0)>>8)); //write address A13~A0
//SPI_WriteByte((uchar)(address & 0x00FF));
SPI_WriteByte((uchar)(address >>8));
SPI_WriteByte((uchar)(address));

for(i=0;i<num_of_byte;i++)
{
SPI_WriteByte(*source);
source++;
}

AT25320_CS=1;

}


---------- Post added at 12:48 ---------- Previous post was at 12:46 ----------

I tried the new loop and will post the result soon


while (address<0x0008)
{
//for(address=0x0000;address<0x0008;address++)
//{

for(counter=0;counter<8;)

{


AT25_WriteByte(address,pattern[counter]);
_nop_();
_nop_();
address=address+0x0001;
counter=counter+1;
}

}
 

Guys,

How can I write a pattern into serial memory properly ?
Can I use a loop below ? with while ?

Or
Any other suggestions or experiences ?

Thanks

Code:
unsigned char pattern[8] = {0x3C,0x42,0xA5,0xA5,0x81,0xA5,0x5A,0x3C,};
  	 address=0x0000;

while (address<0x0008)
   {
	   
		for(counter=0;counter<8;counter++)
		
		 {
		  
		   AT25_WriteByte(address,pattern[counter]);
		   	_nop_();
			_nop_();
			address=address+0x0001;
		 }

	}

4 characters are missing but already in correct sequence
memory2.jpg
 

random data placement is only due to wrong address. since you have written a part of your full code so i can't say what exactly is going on. but the probable mistakes may be that ......

1) the manipulation of function
AT25_WriteByte(address,pattern[counter]);
may be out of Scope of variable "address", because the control of program destroy the memory space reserved for the variable when it jumps out of its scope.

2) in rest of your code you may be manipulating the address passed by the function wrongly; resulting in placement of remaining data wrong memory locations.

I've posted the code, could you tell me which one, I should modify ?
Thanks
 

the code appers to be right....but i think you need to declear the unsigned char before address =0x0000; or declear it global ....also you need probably more dealy in the loop for correct data writting....

Good Luck

No luck yet,
If I declare it as "unsigned char" I can't do adding operation....
 

So you mean to say you are getting a compilation error.....which compiler r u using ?
 

So you mean to say you are getting a compilation error.....which compiler r u using ?

I'm using keil, I didn't get any compilation error...
but if I use "char" as a counter variable, I can not do looping with this counter, because I can't do numeric operation on "char" variable..
 

this strange and superising....I uses generally SDCC complier I never face this problem........then what you can do is creat an array of addresses like unsigned char[] ={ 0x0000,0x0001....} and try to write but this will end up eating a space in micro-controller memory.....

Good Luck
 

this strange and superising....I uses generally SDCC complier I never face this problem........then what you can do is creat an array of addresses like unsigned char[] ={ 0x0000,0x0001....} and try to write but this will end up eating a space in micro-controller memory.....

Good Luck
Where can I find SDCC ? is it IDE ?

That's why I wanna write those patterns into serial EEPROM and later on uC only read from it...
you have an idea for writing it ?

I tried with two while(s)
uint address,counter;
unsigned char pattern[8] = {0x3C,0x42,0xA5,0xA5,0x81,0xA5,0x5A,0x3C,};
address=0x0000;
counter=0;

while (address<0x0008)
{
while (counter<8)

{


AT25_WriteByte(address,pattern[counter]);
}




} //end of while for address
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top