Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Set all bytes in an array to a value - is there a way?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1048

usa.gif
PostPosted: Sat Sep 20, 2014 5:53 am    Post subject: Set all bytes in an array to a value - is there a way? Reply with quote

Hello all,

I want to set all of the bytes in array RxBuff_StrtPointer and RxBuff_ByteCount to zero. Is there a way to set all bytes in an array to the same value, or is a loop required?

Code:

    Dim RxBuffer(254) As Byte
    Dim RxBuff_StrtPointer(30) As Byte
    Dim RxBuff_ByteCount(30) As Byte
 


Thanks,
Tim




(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Sat Sep 20, 2014 6:31 am    Post subject: Reply with quote

After being declared, the global array is filled with zeroes.

To do it in the code, you have to use a loop.
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Sep 20, 2014 7:01 am    Post subject: Reply with quote

Hi,

Have a look in the help text for memcopy

bts = MEMCOPY(source, target , bytes[ , option])

using Option=2 - only the target address will be increased after each copied Byte

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Sat Sep 20, 2014 8:30 am    Post subject: Reply with quote

Have a look at my wiznet web server I do exactly that for a much larger array to zero them
All I did was declare a string variable then overlay the bytes on it so
Code:

dim rxbuffers as string * 253  'one more byte will be at the end making it 254
dim rxbuffer(254) as byte at rxbuffers overlay

rxbuffers = "" 'clear buffer

 


Regards Paul
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Sat Sep 20, 2014 10:28 am    Post subject: Reply with quote

Paulvk wrote:
Have a look at my wiznet web server I do exactly that for a much larger array to zero them
Code:
dim rxbuffers as string * 253  'one more byte will be at the end making it 254
dim rxbuffer(254) as byte at rxbuffers overlay

rxbuffers = "" 'clear buffer

That's nonsense, only the first byte of the string/array is zeroed, which is enough to declare the string empty.
If all of the bytes of the array have to be reset to a certain state, Ian's suggestion is the way to go.
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Sat Sep 20, 2014 10:37 am    Post subject: Reply with quote

If that is so why does is work for the web server when I send the bytes
Note I am not saying you are wrong I was not aware that only the first byte was zeroed
Always willing to learn!

Regards Paul
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Sep 20, 2014 10:51 am    Post subject: Reply with quote

Hi,

A string has a 0 as trailing char so Setting the first char to 0 sets the string to "" even if theres "junk" in Memory after it.

Using memcopy to set all Bytes to a value is the same as using a Loop, but as all the code is in the lib (is machine code) it'll be quicker than writing all Bytes in Basic using a Loop.

Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Sat Sep 20, 2014 11:11 am    Post subject: Reply with quote

This is the problem when you have only yourself to teach you
some of what you learn is only by making an error

Any now I have learnt something to day

but I will now have to look at the send sub and see why it works and learn some more!

Regards Paul
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Sep 20, 2014 11:26 am    Post subject: Reply with quote

Hi,

Try making a small "simple" program and run it in the Simulator looking at the Memory.


Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1198
Location: SYDNEY

australia.gif
PostPosted: Sat Sep 20, 2014 11:32 am    Post subject: Reply with quote

Hi Ian

Have done just that and also found var = STRING(m ,n)

This seems to work

Code:


  $regfile = "m2560def.dat"                                 ' specify the used micro

$crystal = 16000000                                         ' used crystal frequency
$baud = 56700                                               ' use baud rate
$hwstack = 180
$swstack = 250
$framesize = 200



Dim Tests As String * 10

Dim Bytes(11) As Byte At Tests Overlay

Dim X As Byte

Tests = "1234567890"

For X = 1 To 10
 Cls
Locate 1 , 1

Lcd Bytes(x)
 Wait 2
Next

 Cls
 Locate 1 , 1
 Lcd "next"
 Tests = String(10 , 0)
 Wait 5
For X = 1 To 10
 Cls
Locate 1 , 1

Lcd Bytes(x)
Locate 2 , 1
Lcd X
 Wait 2
Next

End
 


Regards Paul
Back to top
View user's profile
i.dobson

Bascom Expert



Joined: 05 Jan 2006
Posts: 1534
Location: Basel, Switzerland

switzerland.gif
PostPosted: Sat Sep 20, 2014 11:44 am    Post subject: Reply with quote

Hi,

There are many ways to Skin a cat.

Looks as if String command does the same thing as memcopy with Option 2.

But looking in the help text I see:

Since a string is terminated by a 0 byte, you can't use 0 for n. where n is the ASCII char to use.


Regards
Ian Dobson

_________________
Home of AVRTimer, the Bascom timer value calculator.
http://www.planet-ian.com

Walking on water and writing software to specification is easy if they're frozen.
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1048

usa.gif
PostPosted: Sun Sep 21, 2014 4:26 am    Post subject: Reply with quote

Hi and thank you for all your replies everyone!

I did not expect such activity on this thread but am thankful for your responses and help.

Tim
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
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
You cannot attach files in this forum
You cannot download files in this forum