[an error occurred while processing this directive] [an error occurred while processing this directive]
Я с этим сам недавно разбирался:
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)
[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

Отправлено Baser 11 марта 2002 г. 18:48
В ответ на: Вопрос знатокам HT-PICC. Как определить структуру битов? отправлено Xan 11 марта 2002 г. 15:25

и претензий к Hi-Tech не было. Все подробно описано в Manual и FAQ.
Вот выдержка из FAQ-ов:


58. How to map bits onto a RAM variable?
Q: I want to be able to access single bits in a byte, but if I try to define a bit variable using the absolute variable construct, e.g.

static bit bitvar @ ((unsigned)&bytevar)*8+0;

I get a compiler error. How can I do this?

A: The short answer is "you can't do this". The absolute variable construct using @ requires an address known at compile time.
The long (and more useful) answer will depend on what you're actually trying to do. You may find all you need is some simple macros like:

#define testbit(var, bit) ((var) & (1 <<(bit)))
#define setbit(var, bit) ((var) |= (1 << (bit)))
#define clrbit(var, bit) ((var) &= ~(1 << (bit)))

or you may like to define a union, e.g.

union both {
unsigned char byte;
struct {
unsigned bit0:1; // etc.
} bits;
} var;

Now you can refer to var.byte or var.bits.bit0 for example.

Taking this a bit further, you can use unions to map bit "variables"
onto an existing byte, which is getting close to your original question.

First up, define a structure type with bits in it:

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

Now given a char variable, e.g.

char myvar;

define a "variable" to be a single bit in that char like this:

#define mybit (((bitv *)&myvar)->b0)

Now you can refer to mybit and it will access bit 0 of myvar. You can
use this just like you would use a bit variable defined any other way.
The code generated will be just as good as any other way. Example of
use:

if(mybit)
something();
mybit = 1;

To streamline the process a little, you can define a helper macro like
this:

#define _paste(a,b) a##b
#define bitof(var,num) (((bitv *)&(var))->_paste(b,num))

Now defining a bit "variable" is done like this:

#define x4 bitof(myvar, 4)

So now x4 represents bit 4 of myvar.


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru