Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 4th July 2009, 07:18 PM   #1
Default 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
Mike, K8LH is offline  
Old 4th July 2009, 08:51 PM   #2
Default

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.
AtomSoft is offline  
Old 4th July 2009, 09:00 PM   #3
Default

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.
eng1 is offline  
Old 4th July 2009, 09:13 PM   #4
Default

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.
AtomSoft is offline  
Old 4th July 2009, 10:01 PM   #5
Default

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.
Mike, K8LH is offline  
Old 4th July 2009, 10:04 PM   #6
Default

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)
3v0 is online now  
Old 4th July 2009, 10:07 PM   #7
Default

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

like
Code:
#define MyVar x.asByte
MyVar=123;
AtomSoft is offline  
Old 4th July 2009, 10:11 PM   #8
Default

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)
3v0 is online now  
Old 4th July 2009, 10:12 PM   #9
Default

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.
AtomSoft is offline  
Old 4th July 2009, 10:31 PM   #10
Default

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.
Mike, K8LH is offline  
Old 4th July 2009, 10:39 PM   #11
Default

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.
AtomSoft is offline  
Old 4th July 2009, 10:44 PM   #12
Default

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.
Mike, K8LH is offline  
Old 4th July 2009, 10:45 PM   #13
Default

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.
AtomSoft is offline  
Old 4th July 2009, 11:18 PM   #14
Default

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.
__________________
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)

Last edited by 3v0; 4th July 2009 at 11:26 PM.
3v0 is online now  
Old 5th July 2009, 03:32 AM   #15
Default

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.
Mike, K8LH is offline  
Reply

Tags
bits, c18, structure

Thread Tools
Display Modes


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



All times are GMT. The time now is 09:08 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker