Pic Basic string array

Hallo.

Om een keuzemenu voor de ingang van mijn voorversterker te maken wil ik in Proton Plus Pic Basic gebruik maken van een array. Zo iets dus:

Keuzemenu[0]="CD speler"
Keuzemenu[1]="Streamer"
Keuzemenu[2]="AUX 1"
etc.

Maar volgens mij ondersteunt Pic Basic zoiets niet ? In de handleiding lees ik alleen een voorbeeld over een array waarbij elk element bestaat uit één letter. Dat schiet niet op dus !
Bovendien gebruik ik een 16F887 en daarin schijnen uitgebreide string-operaties ook niet mogelijk te zijn ?

Wie weet raad ?

Dirk

vraag 1. hoe activeer je het keuzemenu. met een draaischakelaar? dan zou je er een binary encoder achter moeten schakelen bv een 74F148. dan kan je 8 standen vertalen naar 3-bits.

vraag 2 hoe ga je je keuze presenteren, opdruk op de frontplaat met een ledje of een LCD-scherm. indien een LCD-scherm dan kan je in picbasic een routine schrijven met "case select". en dan je ingangen uitlezen en een mededeling naar je scherm schrijven.

print at 1,1, cd-speler.

arrays heb je voor deze toepassing niet nodig. mvg A

Arco

Special Member

Daarvoor heb je een 2-dimensional array nodig, ik weet niet of Picbasic dat ondersteunt (voorbeeld is Mikrobasic)...

pic basic code:


dim dat as string[3][10]   ' 2-dimensional array of size 3x10

dat[0][0] = "CD speler"
dat[1][0] = "Streamer"
dat[2][0] = "AUX 1"
Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Lambiek

Special Member

Heb je dit al bekeken?

Creating and using Arrays
The Proton compiler supports multi part Byte, Word, Dword, SByte, SWord and SDword
variables named arrays (Dword and SDword arrays are only supported with 18F or enhanced
14-bit core devices). An array is a group of variables of the same size (8-bits, 16-bits or 32-bits
wide), sharing a single name, but split into numbered cells, called elements.
An array is defined using the following syntax: -
Dim Name[ length ] as Byte
Dim Name[ length ] as Word
Dim Name[ length ] as Dword
Dim Name[ length ] as SByte
Dim Name[ length ] as SWord
Dim Name[ length ] as SDword
where Name is the variable's given name, and the new argument, [ length ], informs the compiler
how many elements you want the array to contain. For example: -
Dim MyArray[10] as Byte ' Create a 10 element unsigned byte array.
Dim MyArray[10] as Word ' Create a 10 element unsigned word array.
Dim MyArray[10] as Dword ' Create a 10 element unsigned dword array.
Dim sMyArray[10] as SByte ' Create a 10 element signed byte array.
Dim sMyArray[10] as SWord ' Create a 10 element signed word array.
Dim sMyArray[10] as SDword ' Create a 10 element signed dword array.
On 18F or enhanced core devices, arrays may have as many elements as RAM permits, however,
with 12-bit core and standard 14-bit core devices, arrays may contain a maximum of 256
elements, (128 for word arrays when using standard 14-bit core devices). Because of the rather
complex way that some PICmicro's RAM cells are organised (i.e. Banks), there are a few rules
that need to be observed when creating arrays with standard 14-bit core devices.
PICmicro™ Memory Map Complexities.
Some microcontrollers have more RAM available for variable storage, however, accessing the
RAM on the standard 14-bit core devices is not as straightforward as one might expect. The
RAM is organised in Banks, where each Bank is 128 bytes in length. Crossing these Banks requires
bits 5 and 6 of the STATUS register to be manipulated. The larger devices such as the
16F877 have 512 RAM locations, but only 368 of these are available for variable storage, the
rest are known as Special Function Registers (SFRs) and are used to control certain aspects of
the microcontroller i.e. TRIS, IO ports, USART etc. The compiler attempts to make this complex
system of Bank switching as transparent to the user as possible, and succeeds where standard
Bit, Byte, Word, and Dword variables are concerned. However, Array variables will inevitably
need to cross the Banks in order to create arrays larger than 96 bytes, which is the largest section
of RAM within Bank0. Coincidently, this is also the largest array size permissible by most
other compilers at the time of writing this manual.

Large arrays (normally over 96 elements) require that their Starting address be located within
the first 255 bytes of RAM (i.e. within Bank0 and Bank2), the array itself may cross this boundary.
This is easily accomplished by declaring them at, or near the top of the list of variables.
The compiler does not manipulate the variable declarations. If a variable is placed first in the
list, it will be placed in the first available RAM slot within the microcontroller. This way, you, the
programmer maintains finite control of the variable usage. For example, commonly used variables
should be placed near the top of the list of declared variables. An example of declaring
an array is illustrated below: -
Device 16F877 ' Choose a microcontroller with extra RAM
Dim Small_Array[20] as Byte ' Create a small array of 20 elements
Dim Var1 as Byte ' Create a standard Byte variable
Dim Large_Array[256] as Byte ' Create a Byte array of 256 elements
or
Dim Array1[120] as Byte ' Create an array of 120 elements
Dim Array2[100] as Byte ' Create another smaller array of 100 elements
If an array cannot be resolved, then a warning will be issued informing you of the offending line:
Warning Array ‘array name' is declared at address ‘array address'. Which is over the 255
RAM address limit, and crosses Bank3 boundary!
Ignoring this warning will spell certain failure of your program.
The following array declaration will produce a warning when compiled for a 16F877 device: -
Device 16F877 ' Choose a microcontroller with extra RAM
Dim Array1[200] as Byte ' Create an array of 200 elements
Dim Array2[100] as Byte ' Create another smaller array of 100 elements
Examining the assembler code produced, will reveal that Array1 starts at address 32 and finishes
at address 295. This is acceptable and the compiler will not complain. Now look at Array2,
its start address is at 296 which is over the 255 address limit, thus producing a warning
message.
The above warning is easily remedied by re-arranging the variable declaration list: -
Dim Array2[100] as Byte ' Create a small array of 100 elements
Dim Array1[200] as Byte ' Create an array of 200 elements
Again, examining the asm code produced, now reveals that Array2 starts at address 32 and finishes
at address 163. everything OK there then. And Array1 starts at address 164 and finishes
at address 427, again, its starting address was within the 255 limit so everything's OK there as
well, even though the array itself crossed several Banks. A simple re-arrangement of code
meant the difference between a working and not working program.
Of course, the smaller microcontrollers do not have this limitation as they do not have 255 RAM
cells anyway. Therefore, arrays may be located anywhere in the variable declaration list. The
same goes for the 18F devices, as these can address any area of their RAM.

[Bericht gewijzigd door Henry S. op woensdag 6 mei 2015 20:47:23 (0%)

Als je haar maar goed zit, GROETEN LAMBIEK.

Waarom gebruik je geen Select Case?

pic basic code:



 Select Case menunaam
                Case = 1               
                       GoSub menu_1
                Case = 2               
                       GoSub menu_2
                Case = 3
                       GoSub menu_3
                Endselect
Arco

Special Member

Ondersteunt Picbasic dan geen multi-dimensional arrays?

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Lambiek

Special Member

Zover ik weet niet, maar kan het mis hebben.

Als je haar maar goed zit, GROETEN LAMBIEK.
Hewlett

Honourable Member

Voor zover ik weet heeft PicBasic geen ondersteuning voor string arrays, dat zal via byte arrays moeten. String operaties zijn in het geheel wat ondermaats. Ongeloofelijk, maar waar. Reden zal wel samenhangen met het beperkte RAM geheugen ruimte in veel pic micro's, daar ga je met een paar strings en mogelijke tussentijdse string resultaten snel overheen.

Gewoon in C implementeren :-)

Edit naar hadv hieronder: Niet dat C beter is, maar C heeft ondersteuning voor karakter arrays met uiteraard dezelfde hardware berperking. Het is maar net wat je gewend bent, in mijn geval is dat C en daarom staat er ook een smily. ;-)

[Bericht gewijzigd door Hewlett op donderdag 7 mei 2015 13:00:11 (23%)

HamRadio PA2HK // Keep Calm and Carry On.

String arrays worden inderdaad niet direct ondersteund.
Er zijn echter wel truukjes om hier een oplossing voor te vinden door gebruik te maken van arrays van DWords. Die DWords wijzen dan naar de geheugenplaats waar de string staat.
Zeker voor een array van strings die een menu moet voorstellen, dus statische strings, is dit een prima oplossing,

Voor de drie opties uit de startpost kom je uit op het volgende:

pic basic code:

CDspeler:

  CData "CD speler"
Streamer:

  CData "Streamer"
Aux1:

  CData "Aux 1"

Aangezien CData gebruik maakt van het programmageheugen zijn deze strings in ieder geval niet te verlengen (de compiler berekent de adressen).

Je menutabel kun je nu opbouwen met het volgende:

pic basic code:

Dim MenuTabel[3] as DWord
MenuTabel[0] = AddressOf(CDspeler)
MenuTabel[1] = AddressOf(Streamer)
MenuTabel[2] = AddressOf(Aux1)

Op deze manier krijg je dus een tabel met strings van verschillende lengtes.

Als je de strings tijdens het programma zou willen aanpassen heb je natuurlijk RAM nodig. Stel je gebruikt, zoals Arco in zijn voorbeeld, een vaste lengte van 10.

pic basic code:

Dim string1 as string * 10
Dim string2 as string * 10
Dim string3 as string * 10
...
Dim menu[3] as DWord
menu[0] = addressof(string1)
menu[1] = addressof(string2)
menu[2] = addressof(string3)

Overigens zie ik weinig reden om voor een menu een tweedimensionale tabel te gebruiken. Eéndimensionaal is voldoende want wat wil je anders weten dan de string?

@Hewlett: als C met dezelfde beperkte ruimte moet werken als PicBasic waarom zou C dan beter zijn?

Just find out what you like and let it kill you

Dank voor jullie meedenken. Omdat ik de strings toch alleen maar nodig heb om te tonen bij de ingangskeuze kies ik maar voor de simpele select-case methode. Jammer dat ik het niet wat "luxer" kan programmeren in deze basic-versie.

Dirk

Arco

Special Member

Voor strings zijn multi-dimensional arrays niet erg geschikt. Omdat ze allemaal een vaste gelijke lengte hebben, verspil je veel geheugen.
Statische teksten in ram arrays is sowieso verspilling; constants kun je beter in flash opslaan.

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com