Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

RE: [AVR-Chat] Struct ?

2006-06-04 by Larry Barello

You probably want a union:

struct foo
{
	char command;
	union
	{
		char	volume;		//
		char	somethingelse;
		struct			// Byte wide bitfield
		{
			char  A4BitValue:4;
			char	SomeOtherBits:2;
		};
	};
};

The above relies upon anonymous structs and unions, something probably not
supported in all C compilers but supported in GCC.  If it doesn't work, then
give each union and struct their own "name" (between the closing curly brace
and semicolon).  It makes using them more clumsy as you have to include the
"name" before each element when referencing.

With the above you can say:

struct foo MyCommand;		// Declare some storage
...
MyCommand.command = 0x22;	// volume control
MyCommand.volume = 0x01;

or

MyCommand.command = 0x22;	// some other command
MyCommand.A4BitValue = xxx	// Stuff a four bit field
MyCommand.SomeOtherBits = yyy	// stuff the other two bits

Ansi C defines bitfields as being minimum 16 bits (int) wide, but GCC will
allow 8 bit fields.  Your compilers mileage may vary.

Then for spitting out the data:

void SendCommand(void)
{
	char *p = (char*)&MyCommand;  // Alias a byte array to our union.
	SendSPI(*p++);
	SendSPI(*p);
}


-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of Kathy Quinlan
Sent: Saturday, June 03, 2006 3:38 PM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] Struct ?

Hi all,

I am trying to find the best way to work around a few problems.

I am using the New Japan Radio NJW1157B in a project, it is a no
brainier volume / matrix/ EQ chip.

I want to build more modular code, so to this end I am looking at
writing a device driver (Library) for this chip, so I can say things
like Master_Volume(1); etc and it would bump the master volume by 1 Smile

As this chip works in 16 bit words on SPI, The first byte is a constant
for each instruction, I would like to use in my subroutines the
assembler equivalent of Master_Volume (low) and (high), with the high
byte containing the actual volume for the master Smile

Some of these pairs the second pair is more of a 4bit variable and a few
bits. Can I access these parts as Master_Volume,8 etc ?

Can I assign a new name like Master_Volume.8 = BSW ?

What is the best way to describe this so I can just shunt it out the SPI
port, also can I send an int to the SPI port and have CVAVR break it
into 2 8 bit values, to send and receive back my 2 8 bit variables ?


-- 
Regards,

Kat.
---------------------------------------------------------------
  K.A.Q. Electronics     Website: www.kaqelectronics.dyndns.org
  IM: Yahoo: PinkyDwaggy MSN: katinka@kaqelectronics.dyndns.org
  For Everything Electronics 		   Phone: 0419 923 731
---------------------------------------------------------------



 
Yahoo! Groups Links

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.