electronics forum

Rules | Recent posts | topic RSS | Search | Register  | Log in

Defining single bit variables in Microchip C18


Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers > Defining single bit variables in Microchip C18
Author Message
Nemesis77



Joined: 20 Feb 2009
Posts: 3
Location: South Africa


Post11 May 2009 10:56   

bit z charu c18


I want to define flags in my code. In assembler I used to do it as follows:

flags equ 0x020 ; Define variable

#define flag0 flags,0 ; Flag0
#define flag1 flags,1 ; Flag0
etc.

How would I do this in C18. i.e. I want to define a CHAR variable and I want to be able to set or clear each bit of the 8bit CHAR variable. C18 has some predefined for example... INTCONbits.GIE = 1; This statement sets only 1 bit in the INTCON register.
Back to top
btbass



Joined: 20 Jul 2001
Posts: 1267
Helped: 115
Location: Oberon


Post11 May 2009 17:49   

c18 bits


You use bitfields.

Code:

/*--- Relay Structure ---*/

struct RELAYBITS
  {
  unsigned rca:1;     /* SEL_UNBAL relay */
  unsigned earth:1;   /* EARTHSEL relay */
  unsigned ground:1;  /* GND_RLY relay */
  unsigned phase:1;   /* Phase invert relay */
  unsigned bridge:1;  /* Mono amplifier mode */
  unsigned dummy:2;   /* Alignment bits */
  unsigned mute:1;    /* MUTE relay */
  };

/*--- Relay bits union ---*/

typedef union
  {
  struct RELAYBITS Bits;
  unsigned char data;
  }RELAY;
 
volatile RELAY RELAYbits;


You can set and clear the bits like this:

Code:

RELAYbits.data = 0;  /* Clear all bits */

RELAYbits.Bits.ground = 0;  /* Clear single bit */
RELAYbits.Bits.rca = 1;     /* Set a bit */


Bit fields dont have to be single bits, you could define a bitfield with 3 bits, or any number of bits

Code:

typedef struct
  {
  unsigned threebits : 3;
  unsigned twobits : 2;
  }BITS,

volatile BITS Bits;

Bits.threebits = 7;
Bits.twobits = 3;


then you could set them to any value between 0 and the maximum value the bits can hold.
Back to top
Nemesis77



Joined: 20 Feb 2009
Posts: 3
Location: South Africa


Post11 May 2009 23:01   

microchip c18


Thanx btbass. This is a great answer and even beter than I actualy needed.
Back to top
Google
AdSense
Google Adsense




Post11 May 2009 23:01   

Ads




Back to top
btbass



Joined: 20 Jul 2001
Posts: 1267
Helped: 115
Location: Oberon


Post16 May 2009 19:38   

mcc18 bit field


Look at the Pic18F header files. They make extensive use of bitfields. You can learn a trick or two by studying the header files.
Back to top
Arabic versionBulgarian versionCatalan versionCzech versionDanish versionGerman versionGreek versionEnglish versionSpanish versionFinnish versionFrench versionHindi versionCroatian versionIndonesian versionItalian versionHebrew versionJapanese versionKorean versionLithuanian versionLatvian versionDutch versionNorwegian versionPolish versionPortuguese versionRomanian versionRussian versionSlovak versionSlovenian versionSerbian versionSwedish versionTagalog versionUkrainian versionVietnamese versionChinese version
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers > Defining single bit variables in Microchip C18
Page 1 of 1 All times are GMT + 1 Hour
Similar topics:
Bit manipulation in C18 (2)
Microchip C18 C Compiler Tutorial (1)
Microchip C18 Config Macros (1)
Microchip C18 linker file (1)
[Microchip C18/30] What is LATB? (2)
A 8 Bit Shift Register in a Single Slice (3)
How to test a single bit in Visual Basic? (3)
can dynamic dither use in single bit sigma-delta ADC? (5)
Has Microchip reached #1 sales at 8 bit uC's? (3)
Difference in Writing Code between 8-bit/16-bit/32-bit Micro (1)


Abuse || Administrator || Moderators || Support us || sitemap
topic RSS