+ Reply to Thread
Page 1 of 2
1 2 Last
Results 1 to 15 of 20

Thread: C18 structure for 'bits'

  1. #1
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,435

    C18 structure for 'bits'

    Been searching awhile now for example on how to use a C18 structure to emulate the basic "bit" access that BoostC includes automatically, that is;
    Code:
      myvar.0 = 1;        // set bit 0 in variable "myvar" to '1'
    
    Any help would be appreciated.

    TIA, Mike, K8LH


  2. #2
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    Look here:
    Defining single bit variables in Microchip C18

    like btbass from there said:

    "look at the Pic18F header files. They make extensive use of bitfields. You can learn a trick or two by studying the header files."

    heh i must say C18 is the least documented compiler i ever saw lol
    Last edited by AtomSoft; 4th July 2009 at 08:52 PM.

  3. #3
    eng1 Excellent eng1 Excellent eng1 Excellent eng1 Excellent eng1 Excellent
    Join Date
    Apr 2006
    Location
    Italy
    Posts
    938

    Quote Originally Posted by Mike, K8LH View Post
    Been searching awhile now for example on how to use a C18 structure to emulate the basic "bit" access that BoostC includes automatically, that is;
    Code:
      myvar.0 = 1;        // set bit 0 in variable "myvar" to '1'
    
    Any help would be appreciated.

    TIA, Mike, K8LH

    In my opinion the simplest way is to define two macros:
    Code:
    #define bitset(var,bitno) (var|=1<<bitno)
    #define bitclr(var,bitno) (var&=~(1<<bitno)) 
    These macros will be translated into 1-cycle assembly intrusctions by the compiler.
    I use them with 8-bit variables.
    Last edited by eng1; 4th July 2009 at 09:01 PM.

  4. #4
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    Nice!!! Do you mind if i though that in my C18 PDF? Whats your name?

    Its would be like:
    Code:
    #define SpecialBit 2
    unsigned char MyVar;
    
    MyVar = 0;
    bitset(MyVar,SpecialBit);
    
    Then :
    "MyVar would be 0 to start then after the bitset command, MyVar equals 00000100."
    Last edited by AtomSoft; 4th July 2009 at 09:26 PM.

  5. #5
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,435

    Quote Originally Posted by eng1 View Post
    In my opinion the simplest way is to define two macros:
    Code:
    #define bitset(var,bitno) (var|=1<<bitno)
    #define bitclr(var,bitno) (var&=~(1<<bitno)) 
    These macros will be translated into 1-cycle assembly intrusctions by the compiler.
    I use them with 8-bit variables.
    I'm aware of the macros, thank you. Wouldn't help much if I wanted to do something like this though;
    Code:
      mode.0 ^= 1;      ; toggle sideband mode
    
    Probably just go back to BoostC. Thanks guys.

  6. #6
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,399
    Blog Entries
    11

    This works for x.b0 ^= 1;
    Code:
    typedef union {
      struct {
        unsigned b0:1; 
        unsigned b1:1; 
        unsigned b2:1; 
        unsigned b3:1;
        unsigned b4:1; 
        unsigned b5:1; 
        unsigned b6:1; 
        unsigned b7:1;
      };
      struct {
        unsigned asByte:8;
      };
    }  bitByte;  
    ....
      bitByte x;
    
      x.b3=1;
      x.asByte = 0x55;
    
    The downside is that you must say x.asByte=123 and that you have to say x.b0 instead of x.0. Neither is a big deal if you are used to structures.
    The upside is that you can use variables on the RHS;

    Generated asm
    Code:
    131:                 bitByte ii;
    132:                 
    133:                 ii.b3=1;
      0152    0E01     MOVLW 0x1
      0154    86DB     BSF 0xfdb, 0x3, ACCESS
    134:                 
    135:                 ii.asByte = 0x55;
      0156    52DE     MOVF 0xfde, F, ACCESS
      0158    0E55     MOVLW 0x55
      015A    6EDD     MOVWF 0xfdd, ACCESS
    
    3v0
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  7. #7
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    could you not fix the x.asByte=123 by simply defining it after?

    like
    Code:
    #define MyVar x.asByte
    MyVar=123;
    

  8. #8
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,399
    Blog Entries
    11

    It sounds like a good idea but the preprocessor will then change
    MyVar.b0
    to
    MyVar.asByte.b0

    Quote Originally Posted by AtomSoft View Post
    could you not fix the x.asByte=123 by simply defining it after?

    like
    Code:
    #define MyVar x.asByte
    MyVar=123;
    
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  9. #9
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    heh i know hence why i only stated it would fix x.asByte = 123 ;p;

    Could you nest the b0 in the asByte?
    Last edited by AtomSoft; 4th July 2009 at 10:12 PM.

  10. #10
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,435

    I'm just trying to find a way to test and clear individual switch flag bits like I do in BoostC. Suggestions? Thanks.

    Code:
    #define swSet swflags.0
    #define swUp swflags.1
    #define swDn swflags.2
    
    
    while(1)
    { while(swSet)            // while "set" mode
      if(swUp)                // if "up" switch press
      { swUp = 0;             // clear switch flag bit
        incvalue();           // bump value
      }
      if(swDn)                // if "dn" switch press
      { swDn = 0;             // clear switch flag bit
        decvalue();           // bump value
      }
    }
    
    Last edited by Mike, K8LH; 4th July 2009 at 10:38 PM.

  11. #11
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    How about simple ANDing? Note: i didnt test this out lol but seems fine in my head lol

    Code:
    while(1){ 
        while(swflags & (1<<swSet))         // while "set" mode
        if(swflags & (1<<swUp))             // if "up" switch press
        { 
            swflags = ~(1<<swUp);           // clear switch flag bit
            incvalue();                     // bump value
        }
        if(swflags & (1<<swDn))             // if "dn" switch press
        { 
            swflags = ~(1<<swDn);           // clear switch flag bit
            decvalue();                     // bump value
        }
    }
    
    Ill change it into something simpler 1 minute
    Last edited by AtomSoft; 4th July 2009 at 10:39 PM.

  12. #12
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,435

    Jason,

    That will work perfectly with minor correction but it's not as clean or as intuitive as the BoostC example...
    Last edited by Mike, K8LH; 4th July 2009 at 10:46 PM.

  13. #13
    AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent AtomSoft Excellent
    Join Date
    Feb 2008
    Location
    Brooklyn, NY US
    Posts
    3,587

    heh and i made this too:
    Code:
    #define bitset(var,bitno) (var|=1<<bitno);
    #define bitclr(var,bitno) (var&=~(1<<bitno));
    
    unsigned char bitchk(unsigned char var,char bitno){
        unsigned char tmp;
        tmp = var & (1<<bitno);
        return tmp;
    }
    
    while(1){ 
        while(bitchk(swflags,swSet))         // while "set" mode
        if(bitchk(swflags,swUp))             // if "up" switch press
        { 
            bitclr(swflags,swUp);           // clear switch flag bit
            incvalue();                     // bump value
        }
        if(bitchk(swflags,swDn))             // if "dn" switch press
        { 
            bitclr(swflags,swDn);           // clear switch flag bit
            decvalue();                     // bump value
        }
    }
    
    Last edited by AtomSoft; 4th July 2009 at 10:45 PM.

  14. #14
    3v0
    3v0 is offline
    3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent 3v0 Excellent
    Join Date
    Jul 2006
    Location
    USA
    Posts
    6,399
    Blog Entries
    11

    Quote Originally Posted by Mike, K8LH View Post
    I'm just trying to find a way to test and clear individual switch flag bits like I do in BoostC. Suggestions? Thanks.
    What was wrong with my suggestion ? I am not saying it is the greatest but is does what you asked for.
    Last edited by 3v0; 4th July 2009 at 11:26 PM.
    Please post questions to the forums. PM's are for personal communication.

    BCHS/3v0's Tutorials
    Junebug USB PIC programmer kit., USB Bit Whacker,
    The 15 Minute Printed Circuit Board! (+drill time)

  15. #15
    Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent Mike, K8LH Excellent
    Join Date
    Jan 2005
    Location
    Michigan, USA
    Posts
    2,435

    Hi 3v0,

    Thank you very much for for showing me how to build the structure. Unfortunately, if I understand you correctly, accessing the variable as a byte becomes extremely clumsy (ie; x.asbyte = 0x55).

    Right now the portion of my program that manipulates the switch flags variable as a byte works just fine in both BoostC and in C18 (the C18 code below looks almost identical to the BoostC version) so I don't want to muck up that part.

    I really do appreciate the input guys... Thank you!!!

    Mike

    Code:
    unsigned char swold = 0;        // switch state latch
    unsigned char flags = 0;        // short switch flag bits
    unsigned char flong = 0;        // long switch flag bits
    unsigned char tmr1k = 0;        // 1 second timer
    unsigned char tmr25 = 25;       //
    unsigned char beep = 0;         //
    unsigned char bctr = 32;        //
    
    #pragma interrupt isr_hi
    
    /****************************************************************/
    /*  time from 'newhi' (press) to 'newlo' (release) determines   */
    /*  which flag bit, short or long, is toggled                   */
    /*                                                              */
    /*  swnew.0  ___---___---___----------___----------___          */
    /*  swold.0  ____---___---___----------___----------__          */
    /*  delta.0  ___-__-__-__-__-_________-__-_________-__          */
    /*  newhi.0  ___-_____-_____-____________-____________          */
    /*  newlo.0  ______-_____-____________-____________-__          */
    /*  flags.0  _______------____________________________ short    */
    /*  slong.0  __________________________-------------__ long     */
    /*                                                              */
    
    void isr_hi()                   // 1 msec interrupts
    { unsigned char swnew;          //
      unsigned char delta;          //
      unsigned char newhi;          //
      unsigned char newlo;          //
    
      PIR1bits.TMR2IF = 0;          // clr timer2 interrupt flag
    
      if(beep)                      // if beep task running
      { if(!(beep&1))               // if beep is even (b0 = 0)
          PORTB ^= 1<<spkr;         // toggle speaker pin
        if(!--bctr)                 // if end of 32 msec count
        { bctr = 32;                // reset 32 msec timer and
          beep--;                   // decrement beep counter
        }                           //
      }                             //
    
      if(!--tmr25)                  // if 25 msec interval
      { tmr25 = 25;                 // reset for 25 msecs and...
        swnew = ~PORTC;             // sample active lo switches
        swnew &= 0b00000111;        // on RC2, RC1, and RC0 pins
        delta = swnew ^ swold;      // state changes (hi or lo)
        newhi = delta & swnew;      // new hi level changes
        newlo = delta & swold;      // new lo level changes
        swold = swnew;              // update switch state latch
    
        if(tmr1k)                   // if 1 second timer running
          if(!--tmr1k)              // dec it and if timed out
            beep = 4;               // task double beep feedback
        if(newhi)                   // if new press
        { tmr1k = 1000/25;          // start 1 second timer and
          beep = 2;                 // task single beep feedback
        }                           //
        if(newlo)                   // if new release
        { if(!tmr1k)                // if 1 second timeout
            flong ^= newlo;         // toggle 'long' switch flag
          else                      // otherwise
            flags ^= newlo;         // toggle 'short' switch flag
          tmr1k = 0;                // clear 1 second timer
        }                           //
      }                             //
    }
    
    Last edited by Mike, K8LH; 5th July 2009 at 12:46 PM.

+ Reply to Thread
Page 1 of 2
1 2 Last

Similar Threads

  1. C18 Byte Structure
    By AtomSoft in forum Micro Controllers
    Replies: 15
    Latest: 8th December 2008, 02:07 PM
  2. Structure of resistor question
    By jasonbe in forum General Electronics Chat
    Replies: 25
    Latest: 3rd December 2008, 05:40 PM
  3. Programming structure in C
    By demestav in forum Micro Controllers
    Replies: 8
    Latest: 12th May 2007, 04:37 AM
  4. How to initialize CCS static structure
    By janetsmith2000@yahoo.com in forum Micro Controllers
    Replies: 1
    Latest: 6th March 2004, 04:09 PM
  5. Basic structure
    By Win32_under_the_API in forum Micro Controllers
    Replies: 1
    Latest: 2nd September 2003, 05:13 PM

Tags for this Thread