News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Is there an issue returning a float from a procedure

Started by RGV250, May 02, 2025, 03:26 PM

Previous topic - Next topic

RGV250

Hi,
As the title says, I just thought I would ask first as if I pass a word it is fine but if I pass a float I get zero.
If it should be possible which the manual suggests I will look at stripping the code down to a small example.
I can do the maths outside the procedure but for neatness would prefer it inside it.

Regards,
Bob

trastikata

Hello Bob,

Not that I know of. Can you post the relevant code that is giving a trouble and indicate if 16b or 8b MCU is being used.

RGV250

Hi,
As I was posting the reply I think i have just figured out what I am doing wrong, I will have a look over a beer tonight, that normally helps :)

Regards,
Bob

RGV250

OK, I haven't got to the beer yet but I do have an issue which is probably how I am implementing it.

If I have a word variable I still have an issue, if I do not have Result = INA260_REG_BUSVOLTAGE * 1 with 5v on the input it returns 3612 which is correct but if I have the Result = INA260_REG_BUSVOLTAGE * 1 line I get zero where it should still be 3612.
So I figure it is how I am implementing it.

' Read in the BUS Voltage value Register.
Proc INA260_Read_BUS_Voltage(), Word
        I2CIn SDA, SCL, INA260_I2C_Address,INA260_REG_BUSVOLTAGE,[Result]
        Result = INA260_REG_BUSVOLTAGE * 1
EndProc

trastikata

INA260_REG_BUSVOLTAGE - what is the type of this variale?

if you mix the variable types in this

Result (Word) = INA260_REG_BUSVOLTAGE * 1 (byte)

you can get unexpected results.

RGV250

Hi,
INA260_REG_BUSVOLTAGE is a word, so I created a temporary local variable as a word and made it 1 but still no good.

' Read in the BUS Voltage value Register.
Proc INA260_Read_BUS_Voltage(), Word
    Dim temp As Word
    temp = 1
        I2CIn SDA, SCL, INA260_I2C_Address,INA260_REG_BUSVOLTAGE,[Result]
        Result = INA260_REG_BUSVOLTAGE * temp
EndProc

Bob

Pepe


It shouldn't be like this

Symbol INA260_REG_BUSVOLTAGE 2
Dim INA260_BUSVOLTAGE as word

INA260_BUSVOLTAGE= INA260_Read_BUS_Voltage()


' Read in the BUS Voltage value Register.
Proc INA260_Read_BUS_Voltage(), Word
        I2CIn SDA, SCL, INA260_I2C_Address,INA260_REG_BUSVOLTAGE,[Result]
EndProc

Pepe

If you need to apply scale

Symbol INA260_REG_BUSVOLTAGE 2
Dim INA260_BUSVOLTAGE as float

INA260_BUSVOLTAGE= INA260_Read_BUS_Voltage()


' Read in the BUS Voltage value Register.
Proc INA260_Read_BUS_Voltage(), float
Dim Voltage as word
        I2CIn SDA, SCL, INA260_I2C_Address,INA260_REG_BUSVOLTAGE,[Voltage]
Result= Voltage* 1.5
EndProc

RGV250

Thanks,
        I2CIn SDA, SCL, INA260_I2C_Address,INA260_REG_BUSVOLTAGE,[Voltage]
Result= Voltage* 1.5
That was the bit I was getting lost with, I am still going to have the beer tonight :)

Bob