declaring a bit variable..CW 5.7.0.2015...MC9S08RE16

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

declaring a bit variable..CW 5.7.0.2015...MC9S08RE16

3,851 次查看
stevec
Contributor III
I need to use some on/off type flags and using a byte for each flag sems a bit excessive. I'm not sure how to declare a 'bit' variable. Can anyone help?

Regards,

Steve
标签 (1)
0 项奖励
回复
5 回复数

962 次查看
J2MEJediMaster
Specialist I
Go to the MCU-specific header files that CodeWarrior uses when you build a project with the project wizard. Look for these header files the CodeWarrior directory, inside the include directory. Specifically, there's probably a MC9S08RE16.h file (or similar) that you can examine. In this file, bits are defined for the processor's control and status registers. Use this code as a starting point to set up bit assignments within a byte for your code.

---Tom
0 项奖励
回复

962 次查看
stevec
Contributor III
The definitions in this header file seem to use variables at fixed addresses (for the registers).  I don't seem to be able to translate this over to creatiing bit flags within a declared byte e.g. flag_byte. Do I have to go to the lengths of using unions and structures? I'm relatively new to C and haven't got to using these yet.
0 项奖励
回复

962 次查看
CrasyCat
Specialist III
Hello
 
You need to use so called bitfields, which is a standard ANSI C construct.
A bitfiled is a set of bits, which can be accessed separately by the programmer.
 
A bitfield is defined as follows:
 
 struct
{
    unsigned int f0: 1;
    unsigned int f1 : 1;
    unsigned int f2 : 1;
    unsigned int f3 : 1;
    unsigned int f4: 1;
    unsigned int f5 : 1;
    unsigned int f6 : 1;
    unsigned int f7 : 1;
} MyVar;
 
You can than access the single bit as follows:
  MyVar.f1 = 1;
 
This will set f1 to 1.
 
You should be able to find more on bit fields searching on google or even in wikipedia.
I hope it helps.
 
CrasyCat
0 项奖励
回复

962 次查看
Designer11
Contributor IV
Sorry to bring this thread back. But i am facing the same problem with declaring a bit so i can use it for flag. for example.

typedef bit   BIT;

I get this error message

Error: C2450: Expected: ; =,
define.h line 10
Error: Compile failed

BIT debug_flag; //If i change BIT to BYTE the error for this line goes away

It this means that CW or the microprocessor doesn't support individual declaration of single bit?



Message Edited by Designer11 on 2008-08-29 10:51 PM
0 项奖励
回复

962 次查看
bigmac
Specialist III
Hello,
 
The problem with your typedef is that bit is not a valid data type.  For a bit field structure, a specific bit is an element of that structure, and needs to be referenced as such.  As previously described by CrasyCat, the following would seem to represent what you are attempting to achieve.
 
struct {
   word debug  :1;
} flags;
 
#define debug_flag  flag.debug
 
debug_flag = 1;
 
An alternative to using bit fields is to use bit masks.  This might be simplified with the use of the following macros.
 
// Bit control macros:
#define  bset(n,reg)  (reg) |= (1 << (n))
#define  bclr(n,reg)  (reg) &= ~(1 << (n))
#define  btest(n,reg) ((reg) & (1 << (n)))  // Value 0 or 2^n
 
#define debug  0
byte flags;
 
bset(debug, flags);
 
if (btest(debug, flags) ... ;
 
Regards,
Mac
 
0 项奖励
回复