Hi all.
First post, so please forgive any breach of etiquette.
I'm making a wattmeter for an electric bicyle & have been trying to use 32 bit multiplication to get an accurate result using * & ** on a 20X Picaxe.
I've done a search of this forum & have found explanations, but when I attempt to run them in the simulator, the results are not what I am expecting.
The bike runs at 48 volts (60 maximum) & will draw a maximum of 35 amps.
I have been able to convert the ADC values to actual voltage values & it's these values I trying to multiply. At maximum current & voltage, I'm trying to multiply 600 x 350.
Once calculation is complete, I intended to reduce the result to a four figure value.
The 32 bit math code I am using I found elsewhere on this site & is as follows:
I am entering values using the "generic" window in the simulator (individual ADC's not working).
Consequently, I am getting the square of the numbers I enter, but this doesn't matter for testing.....
This code works fine till I enter a value over 255. The results with values over 255 indicate that the word variable has "overflowed". The result returns to zero.
I have worked out another routine that works but is variable "hungry" & I would like a more elegant solution.
Alternative code is below. Please excuse the mess! I need to "trim" the code a little.
I'm at a loss!
Unfortunately math is not my strong point....
Is there an issue with ** in the simulator?
Any help would be greatly appreciated.
Cheers!
First post, so please forgive any breach of etiquette.
I'm making a wattmeter for an electric bicyle & have been trying to use 32 bit multiplication to get an accurate result using * & ** on a 20X Picaxe.
I've done a search of this forum & have found explanations, but when I attempt to run them in the simulator, the results are not what I am expecting.
The bike runs at 48 volts (60 maximum) & will draw a maximum of 35 amps.
I have been able to convert the ADC values to actual voltage values & it's these values I trying to multiply. At maximum current & voltage, I'm trying to multiply 600 x 350.
Once calculation is complete, I intended to reduce the result to a four figure value.
The 32 bit math code I am using I found elsewhere on this site & is as follows:
Code:
'Program to calculate power from voltage input & current input.
Symbol LCD = 4
Main:
Readadc10 1, W0 'voltage
Readadc10 2, W1 'current
'Code needs to go here to convert adc readings to actual decimal value.
Let W2=W0*W1/100
Let W3=W0**W1/100
Let W0=W2+W3
Let b9= W0 DIG 0
Let b11= W0 DIG 1
Let b10= W0 DIG 2
Let b8= W0 DIG 3
Serout lcd, t2400, (254,1)
Pause 100
Serout lcd, t2400, (#b8, #b10, #b11, #b9, "Watts")
Pause 3000
Goto Main:
Consequently, I am getting the square of the numbers I enter, but this doesn't matter for testing.....
This code works fine till I enter a value over 255. The results with values over 255 indicate that the word variable has "overflowed". The result returns to zero.
I have worked out another routine that works but is variable "hungry" & I would like a more elegant solution.
Alternative code is below. Please excuse the mess! I need to "trim" the code a little.
Code:
Symbol LCD = 4
Input A.0
Input A.1
Input A.2
Let ADCsetup= 3 'enable ADC on pins 0,1,2.
Main:
Readadc10 1, W0 'current or voltage
Readadc10 2, W1 'current or voltage
'Code needs to go here to convert adc readings to actual decimal value.
Let b4 = W0//10 'leaves decimal portion of voltage value as remainder
Let b5 = W1//10 'leaves decimal portion of current value as remainder
Let b6 = W0/10 'this gives the whole number portion of the voltage value
Let b7 = W1/10 'this gives the whole number portion of the current value
Let W1 = b6*b7*100 'this combines the whole number portion of the voltage & current
Let W7 = b4*b7*10 'this multiplies decimal component of voltage by whole number portion of current
Let W8 = b5*b6*10 'this multiplies decimal component of current by whole number portion of voltage
Let W9 = b4*b5
Let W2 = W1+W7+W8+W9 'adding all values together gives final wattage value
Let b30=W2 DIG 0
Let b31=W2 DIG 1
Let b32=W2 DIG 2
Let b33=W2 DIG 3
Serout lcd, t2400, (254,1)
Pause 100
Serout lcd, t2400, (#b33, #b32, #b31, #b30, "Watts")
Pause 3000
Goto Main:
Unfortunately math is not my strong point....
Is there an issue with ** in the simulator?
Any help would be greatly appreciated.
Cheers!