Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Const - how to Create a Const and set a default value?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Tue Jul 29, 2014 5:16 am    Post subject: Const - how to Create a Const and set a default value? Reply with quote

Hello all,

I want to set a constant as:
Const CommandDoSomething = &H02

But, in addition, I want to set the CommandDoSomething (&H02) with a default Value of &HD4

Is this possible with an Alias? Is this possible at all? Anyone how to do it??


This is what I am trying to emulate:

Properties:
Number Name Default Summary
0x0002 GLOBAL_LOW_BATT_THRESH 0x18 Low battery threshold

Where the GLOBAL_LOW_BATT_THRESH is a Const = &H2; and the default value of this property is &H18.

Thanks all,
Tim


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

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 29, 2014 6:39 am    Post subject: Reply with quote

Hi Tim,
it's not realy clear to me...

A Constant is a non editable constant value. You will define it in Sourcecode and this is, what it is... uneditable.

If you define

Const CommandDoSomething = &H02

then CommandDoSomething is &H02 ... unchangeable!


maybe you're thinking at something like this:

DIM GLOBAL_LOW_BATT_THRESH as BYte
Const Const_LOW_BATT_THRESH = &H02
Const Const_LOW_BATT_THRESH_DEFAULT = &H18

...

GLOBAL_LOW_BATT_THRESH = Const_LOW_BATT_THRESH_DEFAULT

do
...
loop end.


best, michael

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Tue Jul 29, 2014 7:00 am    Post subject: Reply with quote

Hi Six1,

Thank you for your response!

I agree:
Quote:
it's not realy clear to me...

A Constant is a non editable constant value. You will define it in Sourcecode and this is, what it is... uneditable.


That, in and of itself is the question. This is an effort to port C to BASCOM.... So before going through a lot of work to create a seemingly obvious solution, I wanted to ask others. There is a lot in BASCOM that I am not aware of, so it made sense to ask a really crazy question.

In C, they must have some kind of macro that does this:
Quote:
Number Name Default Summary
0x0002 GLOBAL_LOW_BATT_THRESH 0x18 Low battery threshold


Where '0x0002' is represented by the term "GLOBAL_LOW_BATT_THRESH" and the term has a default value - which in and of itself indicates that it is a variable. But again, wanted to ask the group since I am not aware of all the cool features of BASCOM.

Thanks again,
Tim
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 29, 2014 7:21 am    Post subject: Reply with quote

Hi Tim,
If you define a constant in your source, every location named with constant name is replaced by compiler with the constant value!
It's value is well known by the compiler!

A Variable is inside the variable memory (RAM)
It's value can't be known by compiler, because it can change during programm execution.

Theoreticaly you can change values inside ATmega programm memory by calling Bootloader Section... but this is a special case and not what you want to know Smile



If you define a variable, space will be reserved by compiler by the variable type (i.e. WORD will be reserve two Byte)
Every access to the variable during programm execturion will be linked to memory location of Variable.

Do you see the differences?

best, michael

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Tue Jul 29, 2014 7:31 am    Post subject: Reply with quote

Hi Six1.

I do understand. Bottom line, what they do in that C example is not possible in BASCOM.
So be it and confirmed. - again, just looking to be sure that there were no alternatives!

Thank you again!
Tim
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5921
Location: Holland

blank.gif
PostPosted: Tue Jul 29, 2014 9:15 am    Post subject: Reply with quote

you can not change the value of a constant in C either. why did they name it a constant otherwise?
a constant like PI has a fixed value that does not change.

i also do not see a CONST in your code. it appears you have a property. this is just a variable which can have a default value and which you can alter.

_________________
Mark
Back to top
View user's profile Visit poster's website
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Jul 29, 2014 10:05 am    Post subject: Reply with quote

Hi Tim,
maybe your copied code was only part...

if there is a "#define" before this:
0x0002 GLOBAL_LOW_BATT_THRESH 0x18 Low battery threshold

then the variable "GLOBAL_LOW_BATT_THRESH " will be later in sourcecode defined!

i.e.: Byte GLOBAL_LOW_BATT_THRESH = 0xd4

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Jul 29, 2014 1:45 pm    Post subject: Reply with quote

six1 wrote:
maybe your copied code was only part...

if there is a "#define" before this:

This is no copied code, it is the description of the API interface for Silabs Si446x series, which is to reveal by a google for some of the keywords.
http://nicerf.com/manage/upfile/indexbanner/635231050196868750.pdf
Why do I just think, it would have been the duty of the TO to show this, and not let people run around in riddles?

To interface with the API, or maybe rebuild parts of it in Bascom would look like that:
Code:
Const GLOBAL_LOW_BATT_THRESH = 0x0002

A separate table needs to be set up for the defaults, where they can be looked up based on the given constant.

Edit:
Another, more memory saving way would be:
Code:
Const GLOBAL_LOW_BATT_THRESH = 0x0002
Const GLOBAL_LOW_BATT_THRESH_default = 0x18

It's more writing work, but this way the compiler is able to use/resolve only the constants required by code and doesn't has to keep all the defaults in flash.
No other way, as far I can see, as a Bascom constant can use only one argument.
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Tue Jul 29, 2014 8:43 pm    Post subject: Reply with quote

Hello all and thank you for your responses!

Please understand that I am absolutely struggling with this C stuff! Always have but hope one day not to - although I run away from it like the plague Confused

1. I know a Constant is just that, a constant value that cannot change - hence the word.
2. As MWS pointed out, I was not copying code, but the data sheet; but was trying to find a way to set the wording as a Const for the Property in a readable manner with an English Word, and THEN give it a starting value.

Quote:

To interface with the API, or maybe rebuild parts of it in Bascom would look like that:

Code:
Const GLOBAL_LOW_BATT_THRESH = 0x0002

A separate table needs to be set up for the defaults, where they can be looked up based on the given constant.

Edit:
Another, more memory saving way would be:
Code:
Const GLOBAL_LOW_BATT_THRESH = 0x0002
Const GLOBAL_LOW_BATT_THRESH_default = 0x18



MWS, your suggestion may work, it would require sending the GLOBAL_LOW_BATT_THRESH and GLOBAL_LOW_BATT_THRESH_default - so the command is sent along with the value - {this may not be the actual format to send BTW, just for discussion sake herein}.



In terms of sample code (that supposedly works) , what I have is a list of defines (not really related to above or the question).
Code:

#define RADIO_CONFIGURATION_DATA_ARRAY { \
        0x07, RF_POWER_UP, \
        0x07, RF_GPIO_PIN_CFG, \
        0x05, RF_GLOBAL_XO_TUNE_1, \
        0x05, RF_GLOBAL_CONFIG_1, \
        0x06, RF_INT_CTL_ENABLE_2, \
        0x08, RF_FRR_CTL_A_MODE_4, \
        0x0D, RF_PREAMBLE_TX_LENGTH_9, \
        0x09, RF_SYNC_CONFIG_5, \
        0x05, RF_PKT_CRC_CONFIG_1, \
        0x05, RF_PKT_CONFIG1_1, \
        0x07, RF_PKT_LEN_3, \
        0x10, RF_PKT_FIELD_1_LENGTH_12_8_12, \
        0x0C, RF_PKT_FIELD_4_LENGTH_12_8_8, \
        0x10, RF_MODEM_MOD_TYPE_12, \
        0x05, RF_MODEM_FREQ_DEV_0_1, \
        0x0C, RF_MODEM_TX_RAMP_DELAY_8, \
        0x0D, RF_MODEM_BCR_OSR_1_9, \
        0x0B, RF_MODEM_AFC_GEAR_7, \
        0x05, RF_MODEM_AGC_CONTROL_1, \
        0x0D, RF_MODEM_AGC_WINDOW_SIZE_9, \
        0x0F, RF_MODEM_OOK_CNT1_11, \
        0x05, RF_MODEM_RSSI_COMP_1, \
        0x05, RF_MODEM_CLKGEN_BAND_1, \
        0x10, RF_MODEM_CHFLT_RX1_CHFLT_COE13_7_0_12, \
        0x10, RF_MODEM_CHFLT_RX1_CHFLT_COE1_7_0_12, \
        0x10, RF_MODEM_CHFLT_RX2_CHFLT_COE7_7_0_12, \
        0x08, RF_PA_MODE_4, \
        0x0B, RF_SYNTH_PFDCP_CPFF_7, \
        0x10, RF_MATCH_VALUE_1_12, \
        0x0C, RF_FREQ_CONTROL_INTE_8, \
        0x00 \
}

const u8 RFM24_CONFIGURATION_DATA[]=RADIO_CONFIGURATION_DATA_ARRAY;
 



WHAT I am looking for in BASCOM, is a way to set up the Property List, in human readable form, that has a default value that can also be changed. So yes, a combination of a Const (for the humand readable name) and then a Variable. This is one reason why I thought of an Alias - but do not think an Alias can really be used that way.

Something perhaps silly like:
GLOBAL_LOW_BATT_THRESH Alias 0x0002
GLOBAL_LOW_BATT_THRESH = 0x18 'Low battery threshold0x18 'Low battery threshold

I realize I am not explaining this all that well, but that is due to the fact that I do not know what I am doing with this! And not ashamed to admit it!


The whole reason behind doing this, is to make it easy to read and understand. A simple Data command could be used, but that is harder to read and understand down the road when one does not remember what &H02 property is... Oh yea, it was the GLOBAL_LOW_BATT_THRESH property!

See what I mean everyone?

Again, thank you for your responses! Much much appreciated!
Tim
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jul 30, 2014 3:26 am    Post subject: Reply with quote

You might want to work your way through this:
http://community.silabs.com/mgrfq63796/attachments/mgrfq63796/4/894/1/Radio_Header_Migration.pdf
A #define in C does simply text replacement, in this case it connects human readable identifiers with the corresponding value. So far it equals to a Bascom constant, where at compile-time each occurrence of the constant is replaced with its value.
What's different here is, that the const u8 RFM... will put the defined values into flash, which equals Bascom's Data statements. From there it can processed in order for init and other purposes.
An equivalent would be:
Code:
Const myconst1 = 14
Const myconst2 = 33
Const myconst3 = 69
...
const_data:
Data myconst1, myconst2, myconst3

Your const/alias stuff won't work, but guess you found that out already.
That still does not solve the problem with the default values, you can store them in a separate Data statements. Beside sequential processing them in order with the order of constants, the problem is to connect the defaults directly to the constants, means you can't tell a default by the name of the constant.

So another approach would be to use the constants as index value and retrieve corresponding values from Data statements via lookup. The disadvantage is, it blows up the code and the constants can't be directly used, instead in every case the call of a function is neccessary, which retrieves the value(s) from its keys.
Code:
Const myconst1 = 0
Const myconst2 = 1
Const myconst3 = 2
...
const_data:
Data 14, 33, 69
const_data_defaults:
Data 65, 11, 87
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1054

usa.gif
PostPosted: Wed Jul 30, 2014 5:58 am    Post subject: Reply with quote

Thank you MWS.

I appreciate your time and the detailed descriptions.
I actually did not try the Alias since they are normally used to create an alias to a specific port, not a variable etc.

Again, thank you. The descriptive BASCOM equivalents are what I was after and now, having a surface understanding of these, will help tremendously.

BTW, the whole goal was to create code that could be shared, and easily understood quickly without having detailed knowledge of the data sheet(s) or devices. Just plain easy reading.

Thanks again to your and all!
Tim
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Wed Jul 30, 2014 8:13 am    Post subject: Reply with quote

Quote:
really crazy question
Smile
What think about Macro? Macro You can create and name, then use in code. Macro is small but/and don`t have Return
Back to top
View user's profile Visit poster's website
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Wed Jul 30, 2014 8:16 am    Post subject: Reply with quote

Hi Tim,
after reading the AN625 documentation, it is clear to me.

Code:

Number   Name                     Default    Summary
0x0002   GLOBAL_LOW_BATT_THRESH  0x18        Low battery threshold
 



NUMBER seams to be a register inside the Chip,
DEFAULT is the default value before any changes done to the chip.
NAME is the human readable name of this register.

so a NAME has two informations: the register address and the register value.


a way could be:
Code:

'define Registers
Const REG_GLOBAL_LOW_BATT_THRESH   =  &H0002
...

'define variables
DIM GLOBAL_LOW_BATT_THRESH  as Byte
....

GLOBAL_LOW_BATT_THRESH  = reader_function( REG_GLOBAL_LOW_BATT_THRESH   )



 




best, michael

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Wed Jul 30, 2014 8:26 am    Post subject: Reply with quote

TSEYFARTH wrote:
I actually did not try the Alias since they are normally used to create an alias to a specific port, not a variable etc.

The problem with Alias is, that you would get another name for a constant, but what you need here is another value for the same constant.

Maybe, depending on request, this could be something to implement into the compiler. My guess is, that it would take not too many efforts.
An array of constants:
Code:
'classic use:
Const myconst = 123
'advanced use:
Const myconstarr(3) = 15, 57, 32
Const myinit = 1
Const myref = 2
Const myinfo = 3
'...
Dim B As Byte
B = myconstarr(myref)   ' B = 57

But it's only an idea. The benefit is, if something is to resolve in compile time, it won't use up flash. So, if for example 100 constants are defined, but the code uses only one, the compiler will only have to keep this one in flash. If similar is done using Data, a library has to contain all values, which are consequently written into flash, and the right one is selected at run time.

Whether this may help you in your project, I'm not so sure, as a short look told me that the circuit needs some init sequence, which is pushed via SPI into the circuit. For this purpose you need all these values in flash and such an array of constants maybe only makes the code structure more clear, but won't save any bytes.
Quote:
BTW, the whole goal was to create code that could be shared, and easily understood quickly without having detailed knowledge of the data sheet(s) or devices. Just plain easy reading.

That's always the idea behind an interface to a library, as it makes no sense, if the user has to find out what &h03 means.
Back to top
View user's profile
mariofer

Bascom Member



Joined: 07 Aug 2006
Posts: 23
Location: center italy

italy.gif
PostPosted: Wed Sep 16, 2015 12:13 am    Post subject: Reply with quote

....
Maybe, depending on request, this could be something to implement into the compiler. My guess is, that it would take not too many efforts.
An array of constants:

Code:
'classic use:
Const myconst = 123
'advanced use:
Const myconstarr(3) = 15, 57, 32
Const myinit = 1
Const myref = 2
Const myinfo = 3
'...
Dim B As Byte
B = myconstarr(myref)   ' B = 57


Hello
i try to compile this code , because i need to use an array of constant , but don't work .. the compiler get ..() not be used for constant


how can I create an array of byte (mono dim if to easy) of constant like for use same function for passing data array or var in ram/rom (like C) ?

const unsigned char pippo[]={1,2,3,4,5,6,7,8...}

dim pluto(xx) as byte

dim pos as byte
dim val as byte

pluto(1)=1
pluto(2)=2
..

pos=1
val=pippo(pos) it's possible ?
....
sub foo_ram_rom( pluto(),pos ) 'use ram array OK


sub foo_ram_rom(pippo(),pos ) 'use rom array ??

..
it's possible to use this single function for all type ?

b.r.

Mario
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
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