How can i define BIT Array?

Go To Last Post
26 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi to you all freaks
I am tring to define an array that has 1 and 0
I need to define it as an BIT array

I wrote this line:
BIT array[12];

But from some reason te Code vision could not compile

Please advise!

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look up bitfields in the c help. Or just declare an array of chars and figure out which bit like this:

char bits[4]; //32 bits
char ndx,bit

void setbit(char n){
//set bit n

ndx=n >> 2;
bit=n ^ 8;
bit[ndx] |= (1 << bit);
}

void clearbit(char n){
//clear bit n

ndx=n << 2;
bit= n ^ 8;
bit[ndx] &= ~(1 << bit);
}

I typed this off the top of my head, so if its wrong, I didn't do it on purpose.....

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) BIT is not a valid CodeVision keyword or data type

2) Read the CodeVision documentation on bit variables.

3) Read the CodeVision documentation and any standard C text on bit fields.

Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Bob and Lee
Lee...I did went through the things that you told me to But couldnt understand it correctlly
I guess that Bobgave me a good anse
I will try to go through that

Thank you again for your quick replay

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok ok, I see the mistake already. No need to point it out. I'm sorry.... (one of the >)

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"...I wrote this line:
BIT array[12];

But from some reason te Code vision could not compile ..."

So, let me repeat:

1) BIT is not a valid CodeVision keyword or data type

So BIT array[12]; will not compile. Neither will "FROG array[12];" or "TZIF array [12];", since BIT, FROG, nor TZIF are understandable to a C compiler.

Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bit arrays ... google is great on this topic...

How large a bit array do you want to create?

say you need to stash away 16 bits . 16 bts can be encoded into a decimal number by assigning place value to each bit corresponding to binary equivalent of its position in the array.

say an array of bits is to be created

bit[1] = 1 place value 2^1 = 1 * 2 =2
bit[2] = 0 place value 2^2 =
bit[3] = 1 place value 2^3 = 1 * 8 = 8
bit[4] = 0 place value 2^4 =
bit[5] = 0 place value 2^5 =
bit[6] = 0 place value 2^6 =

so the above array could be encrypted as decimal 12

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The alternative is to use a C structure like this

struct MyBitArray
{

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Let's finish that...

struct MyBitArray
{
bit1:1
bit2:1
bits3_5:3
...
}

Caveats and notes
The value after the : specifies the number of bits in each variable field.

You should make the number of bits some multiple of 8 for ease of use.

Bit order is significant. The first bit declared will either be the most significant or least significant, depending on the compiler.

To access the bits, just use normal syntax

e.g.
struct MyBitArray BitArray; // declare

BitArray.bit1 = 1; // assign 1 to bit 1

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I said:

3) Read the CodeVision documentation and any standard C text on bit fields.

Lee

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Lee
bit is a valid keyword i CodeVision!
Please look at page 69 section 3.7.2 in the CodeVision manual.
This section is about bit variables.
bit variables can only be declared global in CodeVision.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you have to write bit without capital letters. I don't think BIT works, but I haven't tried it though.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a related problem which I have not cracked yet.

I have a struct with 8 bits, bit0, bit1, bit2 etc.

I also have a pointer which is dynamically incremented.

I want to output one bit of the structure pointed to by counter to an output pin PINB4

How do I use pointer to access one bit of the structure. I am using an IAR C compiler.

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!
Coud someone have any idea how to organize array of bits in codevision? Each element of array shoud use only 1 bit of memory, not 8, like boolean.
thank :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

code-by wrote:
Hi!
Coud someone have any idea how to organize array of bits in codevision? Each element of array shoud use only 1 bit of memory, not 8, like boolean.
thank :)

I have a better idea:
Please explain, which task you want to solve.

There is no direct way to access a bit by an index.
Not in C and not in assembler.
Nevertheless there a several ways to deal with bits:

To access a bit by its name, you can use a struct.

To access one bit after another, you can use the shift operator.

To access a bit by an index, you can use the byte access with a mask.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Slightly aside/FYI: Some architectures, eg the ARM Cortex-M3, supports "bitbanding" where a a single bit, eg in a I/O register, is mirrored into (the least significant) bit of a (quasi?) register.

Not available on AVRs, though.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Danni, could you give examples of code of all yours three variants?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In C++, you might be able to get away with overloading the inddex-operator... :wink:

Quote:
Danni, could you give examples of code of all yours three variants?

If he cares not to, then here are some tips for you to progresss on your own:
>To access a bit by its name, you can use a struct.
Google/read-in-your-C-book about bitfields.

>To access one bit after another, you can use the shift operator.
Start by reading the Bit Manipulation Tutorial in the Tutorials forum here at AVRfreaks.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Would this work in CV?

typedef struct {
 unsigned char b0:1;
 unsigned char b1:1;
 unsigned char b2:1;
 unsigned char b3:1;
 unsigned char b4:1;
 unsigned char b5:1;
 unsigned char b6:1;
 unsigned char b7:1;
} bits_t;

bits_t bit_array[10];

bit_array[1].b3 = 1;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Would this work in CV?

Well it compiles and seems to generate OK code.

	.MACRO __POINTW2MN
	LDI  R26,LOW(@0+(@1))
	LDI  R27,HIGH(@0+(@1))
	.ENDM


; 0000 00B8 bit_array[1].b3 = 1;
	__POINTW2MN _bit_array,1
	LD   R30,X
	ORI  R30,8
	ST   X,R30

; 0000 00BA bit_array[4].b7 = 1;
	__POINTW2MN _bit_array,4
	LD   R30,X
	ORI  R30,0x80
	ST   X,R30


#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

millwood wrote:

maybe because it is C?

And your point is?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Danni, could you give examples of code of all yours three variants?

Could you tell us what you want to accomplish?

Besides "straight C" alternatives. CodeVision gives you variables of type "bit". While the placement order isn't guaranteed, in practice they are allocated in an orderly fashion as defined.

And one can also place variables at a designated address.

The combination of the above can lead to the below:

/*
 * 	Bit Banks--byte-wide bit manipulation
 *	=====================================
 *
 *	Can be used for banks of bits such as shift registers,
 *	keyswitch inputs, etc.
 *
 *	Ensure that these banks are on known byte boundaries if it
 *	is desired to use the byte-wide access declared below them.
 *
 */

// Bank 0
bit	bank0_0;		// R2 Bit 0 (least significant)
bit	bank0_1;
bit	bank0_2;
bit	bank0_3;
bit	bank0_4;
bit	bank0_5;
bit	bank0_6;
bit	bank0_7;		// R2 Bit 7 (most significant)

volatile unsigned char bank0 @0x02;		// force to R2.

// Bank 1
bit	bank1_0;		// R3 Bit 0 (least significant)
bit	bank1_1;
bit	bank1_2;
bit	bank1_3;
bit	bank1_4;
bit	bank1_5;
bit	bank1_6;
bit	bank1_7;		// R3 Bit 7 (most significant)

volatile unsigned char bank1 @0x03;		// force to R3.  

volatile unsigned int word0 @0x02;		// force to R2.
										// Remap of bank1(high):bank0(low).

Of course, the allocation needs to be checked.

I found that in recent years I have gotten away from the above, and typically just #define masks against register variables.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. Dec 6, 2010 - 03:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have only examples for WINAVR:

1.

danni wrote:
To access a bit by its name, you can use a struct.

#include 

struct bits {
  uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
} __attribute__((__packed__));
#define SBIT_(port,pin) ((*(volatile struct bits*)&port).b##pin)
#define SBIT(x,y)       SBIT_(x,y)


#define LED             SBIT( PORTC, PC0 )
#define KEY_ON          SBIT( PINB,  PB0 )
#define KEY_OFF         SBIT( PINB,  PB1 )

#define KEY_PRESS       0       // low active
#define LED_ON          0       // low active


int main( void )
{
  for(;;){
    if( KEY_ON == KEY_PRESS )
      LED = LED_ON;
    if( KEY_OFF == KEY_PRESS )
      LED = !LED_ON;
  }
}

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

2.

danni wrote:
To access one bit after another, you can use the shift operator.

#include 

struct bits {
  uint8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
} __attribute__((__packed__));
#define SBIT_(port,pin) ((*(volatile struct bits*)&port).b##pin)
#define SBIT(x,y)       SBIT_(x,y)


#define SCK     SBIT( PORTC, PC1 )
#define DOUT    SBIT( PORTC, PC0 )


void shiftOut( uint8_t value)           // send a byte over SPI
{
  for( uint8_t i = 8; i; i-- ){
    SCK = 0;
    DOUT = 0;
    if( value & 0x80 )                  // MSB first
      DOUT = 1;
    SCK = 1;
    value <<= 1;                        // shift next bit to MSB
  }
}

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
And your point is?
That it is standard C so it should work with any C compiler. If your compiler could not compile it or generate reasonable code for it, then get a new compiler.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I only used the term "would this work" rather than "this will work" as I don't know how coherent anything but GCC is to the C standard. I think we've previously established, for example, that not all the compilers support "long long" so you can never be totally sure.