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!
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!
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.....
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
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
ok ok, I see the mistake already. No need to point it out. I'm sorry.... (one of the >)
"...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
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
The alternative is to use a C structure like this
struct MyBitArray
{
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
As I said:
3) Read the CodeVision documentation and any standard C text on bit fields.
Lee
@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.
I think you have to write bit without capital letters. I don't think BIT works, but I haven't tried it though.
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.
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 :)
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
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.
Danni, could you give examples of code of all yours three variants?
In C++, you might be able to get away with overloading the inddex-operator... :wink:
Danni, could you give examples of code of all yours three variants?
>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.
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;
Would this work in CV?
.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
maybe because it is C?
Danni, could you give examples of code of all yours three variants?
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
I have only examples for WINAVR:
1.
To access a bit by its name, you can use a struct.
#includestruct 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
2.
To access one bit after another, you can use the shift operator.
#includestruct 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
And your point is?
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.