Menu

Using decimals for calculations

Help
Ryan
2016-05-24
2016-05-27
  • Ryan

    Ryan - 2016-05-24

    Is there a way to use decimals in my internal calculations without having to multiply things to huge numbers to make it work, for example: to find voltage of an a/d with 5vdc supply i have a 0.0048828 vdc per a/d value and i want to have the pic calculate the actual voltage so a/d value * 0.0048828 = voltage?

     
  • Anobium

    Anobium - 2016-05-25

    Hello and welcome,

    Great Cow BASIC does not support floats currently.

    However, this can be worked around. If you factor up your calcualtions and then factor back down after your long divisions (these after factoring up will be integer) you will get the results you require.

    There are many examples of factoring integers to decimals - the DS18B20 is a great example.

     
  • William Roth

    William Roth - 2016-05-27

    Hi Ryan,

    Huge numbers are not really necessary to do the necessary calculations. A Word variable is enough .

    To make the maths easier we can "map" or scale tthe ADC Value so that 1 ADC unit is equal to precisely 10 mV instead of 4.8828 mV.

    First scale the ADC for 1000 units instead of 1024. Then Divide by 2
    Now full scale ADC 0-1023 wil return 0 - 500 ( 0 - 5 volts

    Below is a demo program that shows the maths and displays the Voltage.
    Attach a 10K Pot to the ADC Pin, making the POT Ground and Vdd are as close as possible to the PIC Ground and VDD.

     #chip 18F25K22, 16
     #CONFIG MCLRE = ON
    
      'Software Serial
     #Define SendAHigh Set PORTB.5 ON
     #Define SendALow Set PORTB.5 OFF
     #Define CR 13    'Carriage Return
     #Define LF 10    'Line Feed
     #Define BS 8     'Backspace
     Dir PortB.5 OUT
     Set PORTB.5 ON   'Idles High
    
     InitSer 1, r9600, 1+WaitForStart, 8, 1, none, normal
     SerPrint 1, "Voltmeter Demo"
     Sersend 1, CR
     Sersend 1, LF
     Sersend 1, BS
    
      Do
          DIM ADC_Val as Word
          ADC_Val = readad10(AN4)
          ADC_VAL = ((ADC_VAL * 44)/45) / 2 ;Map ADC
          ;Can simplify to: ((ADC_VAL * 22)/45)
    
          Volts = ADC_VAL / 100           ;Extract Volts
          Tenths = (ADC_Val MOD 100) / 10 ;Extract tenths
          Hundredths = ADC_Val Mod 10     ;Extract hundredths
    
          'Formatting for Brays Terminal
          Repeat 4
             Sersend 1, BS
          end repeat
    
          SerPrint 1, Volts
          Serprint 1, "."
          Serprint 1, tenths
          Serprint 1, hundredths
          wait 200 ms
      loop
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.