Menu

Translate code to GCB

Help
MBB
2021-01-11
2021-01-12
  • MBB

    MBB - 2021-01-11

    I'm using an AHT20 Temperature/Humidity sensor. See page 8 of
    https://cdn.sparkfun.com/assets/d/2/b/e/d/AHT20.pdf

    The temperature section is working but I get humidity values greater than 100%.

    I found this code for the humidity calculation on a web site.

    /* Extract 20-bits of humidity data */
    

    uint32_t reg_humidity = regs[1];
    reg_humidity <<= 8;
    reg_humidity |= regs[2];
    reg_humidity <<= 4;
    reg_humidity |= (regs[3] >> 4);
    *humidity = (reg_humidity / 1048576.0);

    Can anyone translate this into GCB?

     
  • Anobium

    Anobium - 2021-01-11

    Is it assummed that regs[x] are 32 bit numbers? or, what sort of number does reg[x] represent?

     
  • Chris Roper

    Chris Roper - 2021-01-11

    uint32_t reg_humidity = reg[1]
    translates as
    Dim reg_humidity long
    reg_humidity = regs[1]

    reg_humidity <<=8
    translates as
    reg_humidity = fnLSL(reg_humidity, 8)

    reg_humidity |= regs[2]
    translates as
    reg_humidity = reg_humidity OR regs[2]

    reg_humidity <<= 4
    translates as
    reg_humidity = fnLSL(reg_humidity, 4)

    reg_humidity |= (regs[3] >> 4)
    translates as
    reg_humidity = reg_humidity OR fnLSR(regs[3], 4)

    humidity = reg_humidity / 1048576

    You give no indication of the origin and composition of the regs() array but the above should point you in the right direction.

     

    Last edit: Chris Roper 2021-01-11
  • stan cartwright

    stan cartwright - 2021-01-11

    Thanks Chris....what's that in gcb? :)

     
    • Chris Roper

      Chris Roper - 2021-01-12

      That is GCBASIC Stan, have you never used the Logic Shift Functions?

      fnLSL() is Logic shift left the equivalent of the << operator in C.
      fnLSR() is Logic Shift Right and is the same as the >> operator.

      They are very useful tools when working with Logic manipulation or Maths.
      A left shift is equivalent to a binary division and a right shift is a useful multiplication.

      Have a look in the help.

       
  • MBB

    MBB - 2021-01-12

    Thanks to all for your help!

     
    • Anobium

      Anobium - 2021-01-12

      Let us know if it works.

      I would love to put this in the help!

       
    • stan cartwright

      stan cartwright - 2021-01-12

      I 've used them, dunno if rotating left or right is faster than *2 or /2.
      Very useful functions with the carry and stuff.
      I just had "fun" day using linux...not really :)
      I put an os on a usb for rpi and now the stick is bricked. :)
      oh um
      Nice you helped someone.

       

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.