Обращение к байтам int описано в Keil support "C51: ACCESS LOW/HIGH BYTES OF A INTEGER VARIABLE" (+)
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено Vollan 13 августа 2004 г. 15:20
В ответ на: Здравствуйте, уважаемые! Вопрос по СИ(+) отправлено RUAL 13 августа 2004 г. 09:34

Following are two solutions to your problem.
The following example uses shift operations to perform byte accesses. This is Standard ANSI C and it may be easily ported to any other microcontroller plattfrom regardless of the byte order (little endian or big endian) in the target hardware.

define LOWBYTE(v) ((unsigned char) (v))
#define HIGHBYTE(v) ((unsigned char) (((unsigned int) (v)) >> 8))

unsigned int x;
unsigned char cl, ch;

void test (void) {
cl = LOWBYTE(x);
ch = HIGHBYTE(x);
}

The previous example only handles read accesses. If you need write accesses at the byte level, you may use the following pointer definitions. The following example is written for architectures with big endian byte order (like the 8051) and must be adjusted to run on little endian architectures.

#define BYTELOW(v) (*(((unsigned char *) (&v) + 1)))
#define BYTEHIGH(v) (*((unsigned char *) (&v)))

void test1 (void) {
BYTELOW(x) = BYTEHIGH(x);
BYTEHIGH (x) = 5;
}

по аналогии второго метода легко делается доступ к любому байту и long типа к примеру...


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

Ответы



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

E-mail: info@telesys.ru