Code writing question

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

Code writing question

1,057 Views
Juls
Contributor III

Hi

Im working with cw 6.3 and a JM60 mcu

 

Im asking myself how to write a part of my app

I have 10 pin used as output

I will use them depending on the communication with the pc (setting or resetting them)

hey are on different port

 

I wish to have an array that let me change only one 1 something like

Output[4] = 0   //Reset output 4

using it the same way as PTBD_PTBD6 = 0;

 

And I do have this in the header file:

/*** PTBD - Port B Data Register; 0x00000002 ***/
typedef union {
  byte Byte;
  struct {
    byte PTBD0       :1;                                       /* Port B Data Register Bit 0 */
    byte PTBD1       :1;                                       /* Port B Data Register Bit 1 */
    byte PTBD2       :1;                                       /* Port B Data Register Bit 2 */
    byte PTBD3       :1;                                       /* Port B Data Register Bit 3 */
    byte PTBD4       :1;                                       /* Port B Data Register Bit 4 */
    byte PTBD5       :1;                                       /* Port B Data Register Bit 5 */
    byte PTBD6       :1;                                       /* Port B Data Register Bit 6 */
    byte PTBD7       :1;                                       /* Port B Data Register Bit 7 */
  } Bits;
} PTBDSTR;
extern volatile PTBDSTR _PTBD @0x00000002;
#define PTBD                            _PTBD.Byte
#define PTBD_PTBD0                      _PTBD.Bits.PTBD0
#define PTBD_PTBD1                      _PTBD.Bits.PTBD1
#define PTBD_PTBD2                      _PTBD.Bits.PTBD2
#define PTBD_PTBD3                      _PTBD.Bits.PTBD3
#define PTBD_PTBD4                      _PTBD.Bits.PTBD4
#define PTBD_PTBD5                      _PTBD.Bits.PTBD5
#define PTBD_PTBD6                      _PTBD.Bits.PTBD6
#define PTBD_PTBD7                      _PTBD.Bits.PTBD7

 

is there a way to create an arrawy from _PTBD.Bits.PTBD3???? what type should it be?

 

Thanks

this question is probably more related to C than to codewarrior itself but I have no idea on how to do it

Labels (1)
Tags (1)
0 Kudos
3 Replies

623 Views
Juls
Contributor III

Thanks both for your answers

ill use if and elseif (probably switch)

 

but valuable info on bitwise operation and macro

0 Kudos

623 Views
bigmac
Specialist III

Hello,

 

A "bit array" type does not exist within C.

 

There are two different methods of addressing a single bit within a register -

  1. Bit field method, as used within the device header files.
  2. Bit mask method.

For the bit field method, the macro that is used to define each bit within the bit field may be used for all bit operations (e.g. set bit, clear bit and test bit state), but a separate macro is required for each bit within the register, as you have observed.

 

Conversely, the bit mask method requires a separate macro for each bit operation, but a common macro may be used for each bit within the register.  This method is probably closer to what you require.

 

The attached file contains a range of macros that make use of the bit mask method.  For your specific example, you might create some further macros:

#define SET_OUTPUT(n)  b_bset( n, PTBD)

#define CLR_OUTPUT(n)  b_bclr( n, PTBD)

 

Now some general comments:

 

The most efficient code for setting or clearing a single bit within a page zero register, will make use of the assembly instructions BSET and BCLR.  It is known that the CW compiler does do this for the usual bit field method, but I am uncertain whether this remains so for the bit mask method.  You would need to check tha ASM output from the compiler.

 

Some programmers avoid the use of bit fields.  Their reason is that, a bit field applied to a 8-bit register is "non-standard" C (16 bits is usually required), and this will be problematic for some non-CW compilers.  These programmers would always utilize the bit mask method instead.

 

Regards,

Mac

 

0 Kudos

623 Views
kef
Specialist I

You may define array of structs, containing address of port and bitmask like this

typedef struct{char * port; char mask;} bit;#define setbit(bit) (*(bit).port = *(bit).port |= (bit).mask)#define clrbit(bit) (*(bit).port = *(bit).port &= ~(bit).mask)const bit bits[]={   {&PTAD, 1<<0}, //porta bit 0   {&PTAD, 1<<1},   {&PTAD, 1<<2},   {&PTAD, 1<<3},   {&PTAD, 1<<4},   {&PTAD, 1<<5},   {&PTAD, 1<<6},   {&PTAD, 1<<7},   {&PTBD, 1<<1},   {&PTBD, 1<<2},};void main(void) {  setbit(bits[3]);  clrbit(bits[3]);

 In case you need to access only ports in zero page, you may reduce memory size required to store address of one bit from 3 bytes to 2 adding near keyword in bit typedef:

 

typedef struct{char * near port; char mask;} bit;

0 Kudos