Need help: Constant calculated by an other constant - how to do ?

albatros

New Member
Hi

I want to define a constant value by a mathematic operation using a predefined constant.
Something like this

#define cUmax 20000 ;constant to be given by user
#define cImax 3000 ;constant to be given by user

#define cT1 cUmax/19 ;const cT1 = CUmax/19
#define cT2 cUmax/100 + cT1 ;const cT2 = cUmax/100 + cT1
#define cT3 cT2 + cImax/100 ;const cT3 = cT2 + cImax/100

But the code (3 last lines) above is not possible in PicAxeBasic.

Currently I see only these 2 solutions:
1) Using Symbols and variables - calculate them with word/byte variables
=> As I need all variables for the rest of the program - I can't use this
2) Calculation as in 1) and store the results in the EEPROM.
=> But this needs later on also the word/byte variables for the code
and leads to a lot of Store/Load-procedures
Furthermore - I assume, that the read/write procedures to the EEPROM
are a little bit "longer" then those from the stack....

Is there an other solution ? (as I need the calculation only once a time and then
using the calculated values like constant)

I read several times the manuals - but I couldn't find any other solution/hint.
Any idea how to solve this ?

kind regards
Albatros

P.S: My current solution is to claclulate all the values by myself and put them into the code like below...
(which works ok - but that could be faulty...)
#define cUmax 20000 ;constant to be given by user
#define cT1 1052 ;const cT1 = CUmax/19
#define cT2 1252 ;const cT2 = cUmax/100 + cT1
...
 

westaust55

Moderator
Have a look at this thread using SYMBOLS
http://www.picaxeforum.co.uk/showthread.php?27753-Help-with-calculations

This method is good for calculating derived values based on 1 given value where they need only be calculated once at program download.

EDIT:

but be aware that values and results cannot be greater than 65535.
The example in the linked thread will not work but for example this modified extract will"

Code:
Symbol freq = 56100 ; 96.1 MHz
Symbol freqSmall = freq / 100 ;for freq in terminal
Symbol freqValue = freq + 2250 ;for HSI
Symbol freqVal = freqValue / 8192 ;32.768kHz / 4
Symbol freqVal.msb = freqVal / 256
Symbol freqVal.lsb = freqVal & 255
sertxd ("Start",13,10)

w0 = freqsmall
w1 = freqValue
w2 = freqval

pause 1000
 
Last edited:

AllyCat

Senior Member
Hi,

... I need all variables for the rest of the program - I can't use this
Which PICaxe, and how many do you need? M2s have at least 6 extra S_W1 ... S_W6 words, and most have 512 bytes of RAM (see PEEK/POKE and @bptr) for variables.

The ** operator can multipy by any word (1 ... 65535) and then divides by 65536. That can be very useful for pre-calculated scaling (i.e. multiply+divide), provided that the result can be smaller than the source variable/constant.

Cheers, Alan.
 

albatros

New Member
Hi,

thanks a lot !

Both examples are good way forwad - and exactly what I need ;)
The math-examples looks very interestring - and I think I need that, because
AllyCat already identified, that I need these maths for my LabBench-Project :rolleyes:

Albatros:

P.S: I'm currently using a 14M2 and/or 20M2
P.S.S: Again - thanks a lot - best forum - although my/the questions are stupid - I'll get a reply in friendly kind of way
 

hippy

Ex-Staff (retired)
#define cUmax 20000 ;constant to be given by user
#define cImax 3000 ;constant to be given by user

#define cT1 cUmax/19 ;const cT1 = CUmax/19
#define cT2 cUmax/100 + cT1 ;const cT2 = cUmax/100 + cT1
#define cT3 cT2 + cImax/100 ;const cT3 = cT2 + cImax/100

But the code (3 last lines) above is not possible in PicAxeBasic.
You can, as suggested, use SYMBOL commands, but as maths in a SYMBOL assignment is limited to only one maths operation you need to do additional intermediate calculations, for example -

Code:
Symbol cUmax = 20000      ; constant to be given by user
Symbol cImax = 3000       ; constant to be given by user

Symbol cT1   = cUmax/19   ; const cT1 = CUmax/19
Symbol tmpA  = cUmax/100
Symbol cT2   = tmpA + cT1 ; const cT2 = cUmax/100 + cT1
Symbol tmpB  = cImax/100
Symbol cT3   = cT2 + tmpB ; const cT3 = cT2 + cImax/100

SerTxd( "cUmax = ", #cUmax, CR, LF )
SerTxd( "cImax = ", #cImax, CR, LF )
SerTxd( "cT1   = ", #cT1,   CR, LF )
SerTxd( "cT2   = ", #cT2,   CR, LF )
SerTxd( "cT3   = ", #cT3,   CR, LF )

Stop
 

marks

Senior Member
nicely done hippy
it does look correct to me too
but perhaps just have to verify ct3 = ct2 + cImax / 100
Code:
Symbol cUmax = 20000      ; constant to be given by user
Symbol cImax = 3000       ; constant to be given by user

Symbol cT1   = cUmax/19   ; const cT1 = CUmax/19
Symbol tmpA  = cUmax/100
Symbol cT2   = tmpA + cT1 ; const cT2 = cUmax/100 + cT1
Symbol tmpB  = cT2 + cImax
Symbol cT3   =  tmpB /100 ; const cT3 = cT2 + cImax/100

SerTxd( "cUmax = ", #cUmax, CR, LF )
SerTxd( "cImax = ", #cImax, CR, LF )
SerTxd( "cT1   = ", #cT1,   CR, LF )
SerTxd( "cT2   = ", #cT2,   CR, LF )
SerTxd( "cT3   = ", #cT3,   CR, LF )
often you see formula's like ct1 & ct2 used to avoid overflow
maybe can be rewritten as ct2 = cUmax *119 /1900
or ct2 = cUmax / 15.96
adc's have a base of2 so should likely be 16 unless maybe allowing for slope errors

ct3 = cUmax /16 + cImax /100 ( if ct1 and ct2 not required)
 
Top