11-06-2008
01:57 PM
2,064 Views
syf
Contributor I
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
1 Solution
11-13-2008
10:38 AM
771 Views
Lundin
Senior Contributor IV
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
2 Replies
11-06-2008
04:42 PM
771 Views
J2MEJediMaster
Specialist I
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
---Tom
Reply
11-13-2008
10:38 AM
772 Views
Lundin
Senior Contributor IV
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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