Is there a bit data type in CW?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is there a bit data type in CW?

Jump to solution
1,956 Views
syf
Contributor I
Hi,bros!
I'm alway wondering wether CW have a bit data type?
And if I need to declear a bit type data ,then how?
If i need to use S12's a parallel prot's(common port ,just like port a or port b,not port s or port j) one bit as a seriea communication pin,and datas are translated bit by bit on the line,then how to write the programm?
Hope to get some kind help! Thanks!


Message Edited by syf on 2008-11-06 05:01 AM
Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
663 Views
Lundin
Senior Contributor IV
I would strongly advise against using bitfields. They come with a large number of undefined and unspecified behavior, making them completely non-portable and unreliable. Professional embedded programmers never use them.

The way bitfields are defined in the CW headers is also non-standard code where they use char instead of int for bitfields. This is not allowed in C, and the CW headers will not compile in an ISO compliant C compiler. Set CW to "Strict ANSI" and see for yourself.


Instead, I strongly recommend to use bit masks:

#define PORTX (*(volatile unsigned char*)0x1234)

#define PORTX_PIN1 0x01
#define PORTX_PIN2 0x02
#define PORTX_PIN3 0x04
...

PORTX |= PORTX_PIN1; /* set pin to active */
PORTX &= ~PORTX_PIN1; /* set pin to passive */

This code is pure ISO C and is portable to any compiler or computer in the world.

Message Edited by Lundin on 2008-11-13 10:39 AM

View solution in original post

0 Kudos
Reply
2 Replies
663 Views
J2MEJediMaster
Specialist I
Take a look at the appropriate derivative header file, say mcs912dt256.h, that's located in the directory {CodeWarrior Install}\lib\hc12c\include\. In it you will find bit-fields declared for the various processor registers. When you use the CodeWarrior Project wizard to build a project, it includes this derivative header file for you automatically. Check out some of the example programs found in (CodeWarrior_Examples) to see how the code accesses the MCU's registers. HTH.

---Tom
664 Views
Lundin
Senior Contributor IV
I would strongly advise against using bitfields. They come with a large number of undefined and unspecified behavior, making them completely non-portable and unreliable. Professional embedded programmers never use them.

The way bitfields are defined in the CW headers is also non-standard code where they use char instead of int for bitfields. This is not allowed in C, and the CW headers will not compile in an ISO compliant C compiler. Set CW to "Strict ANSI" and see for yourself.


Instead, I strongly recommend to use bit masks:

#define PORTX (*(volatile unsigned char*)0x1234)

#define PORTX_PIN1 0x01
#define PORTX_PIN2 0x02
#define PORTX_PIN3 0x04
...

PORTX |= PORTX_PIN1; /* set pin to active */
PORTX &= ~PORTX_PIN1; /* set pin to passive */

This code is pure ISO C and is portable to any compiler or computer in the world.

Message Edited by Lundin on 2008-11-13 10:39 AM
0 Kudos
Reply