Menu

Bitwise, Shifting, return value and call prob

Help
2008-03-03
2013-05-30
  • Nobody/Anonymous

    Dear Mr Hugh Considine,
    I really enjoy your great cow Basic work, and actually I'm having some problem that I really dont know who to address to. I hope u would take some of ur busy valuable time out n help me to sort this out, cuz actually my work is due by 16/03 :( so that u can save a life :).
    Anyway thanx alot Hugh for creating this compiler great work, this is d question I would like to ask:
    This is d C code that I would like to translate into GCBASIC, (This one is used for controlling I2C
    bit i2c_tx(unsigned char d)
    {
    char x;
    static bit b;
      for(x=8; x; x--) {
        if(d&0x80) SDA = 1;
        else SDA = 0;
        SCL = 1;
        d <<= 1;
        SCL = 0;
      }
      SDA = 1;
      SCL = 1;
      i2c_dly();
      b = SDA_IN;          // possible ACK bit
      SCL = 0;
      return b;
    }
    And this is how I converted:
    Sub i2c_tx(d as byte)
    dim x as word;
    dim b bit;
      for x = 8 to 0[step -1]
        if d&0x80 then set SDA ON
        set SDA OFF
        Set SCL ON
        Rotate d Left
        set SCL OFF
        loop
      set SDA ON
      set SCL ON
      wait 2 us
      b = SDA_IN;          // possible ACK bit
      set SCL OFF
      return b
    End Sub
    There's a few of them I dont noe how to convert like: AND the byte together, shifting i used rotate is it correct?,  return d value and call this subroutine function out i use: Goto i2c_tx(0xBD) with the variable inside d bracket
    Pls let me noe what do u think
    Thanx alot :) Have a great day... n Thanx for ur great work. Really save me alot of time n effort :)

     
    • Hugh Considine

      Hugh Considine - 2008-03-08

      Here's how I translated it:
      Function i2c_tx(data)
         
          for x = 8 to 0
              if data.7 = 1 Then
                  SDA = 1
              else
                  SDA = 0
              End If
              SCL = 1
              Set STATUS.C Off
              Rotate data Left
              SCL = 0
          Next
          SDA = 1
          SCL = 1
          wait 2 us
          i2c_tx = SDA_IN ' possible ACK bit
          SCL = 0
         
      End Function

      There isn't any need to declare the variables, as GCBASIC declares byte variables automatically. It's also quicker to use byte variables than bit variables, even though doing so wastes a small amount of RAM.

      GCBASIC will step backwards if both the start and end value are constants, and the end value is less than the start value. Otherwise, use step but note that it has no brackets.

      The And is used to check bit 7. Anding with 0x80 clears all bits except bit 7. C treats anything non-zero as true, so the result of the if depends on bit 7.

      You missed the else in the if - this is needed to stop the data pin getting turned on and then off again straight away.

      Rotate is correct, but in GCBASIC you need to clear status bit C to stop it from getting rotated into the end of the variable.

      Next matches the end of a For loop, not Loop. Next and Loop are the odd ones, everything else is End ... (End Sub, End If, End Repeat, etc).

      Return in GCBASIC currently doesn't actually allow for returning values, it's used to return from Gosub. I'm planning on changing this at some stage in the future. To return a value, set the variable with the same name as the function.

      To call the sub, you don't need Goto. Just the name of the sub, and then any parameters. The parameters can be in brackets, but they're not needed. Separate multiple parameters with commas.

      Also, it's not usually good to use single letter variable names. There are a many bits in the PIC which have single letter names (C and Z come to mind, there are others), and GCBASIC will get confused if there is a variable with the same name. I've changed to d to data, but I've not changed x as I can't think of any bits in the PIC with this name. However, if GCBASIC complains you'll need to change it to something a bit longer.

      There are also some software I2C routines already written for GCBASIC, they're in one of the posts in https://sourceforge.net/forum/forum.php?forum_id=629990

       

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.