Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I2C LCD + Keypad - Adafruit 715
09-01-2014, 06:50 PM,
#21
RE: I2C LCD + Keypad - Adafruit 715
Can I use 2 structures - 1 to hold the data I want to send and the 2nd to be the byte that will be output? I then assign bits of structure A to different bits of structure B. Does this work??
Reply
10-01-2014, 06:59 PM,
#22
RE: I2C LCD + Keypad - Adafruit 715
You can but I'm unclear why you would wish to.

It sounds like you'd use twice the memory, if you're doing it to avoid a union. And to copy one over to the other (if you do) you'd need a cast or clumsy code. Try some code and if you have questions just post it and someone will have a go at explaining.

John
Reply
10-01-2014, 11:10 PM,
#23
RE: I2C LCD + Keypad - Adafruit 715
Thanks John - point taken about the memory bu I think I may only use 2 bytes for this.

The data to be output would be in this byte structure:-

u8 data_byte
struct
{
unsigned P0 :1;
unsigned P1 :1;
unsigned P2 :1;
unsigned P3 :1;
unsigned P4 :1;
unsigned P5 :1;
unsigned P6 :1;
unsigned P7 :1;
} bits;

and the transformed byte would be in
u8 Output_byte
struct
{
unsigned P0 :1;
unsigned P1 :1;
unsigned P2 :1;
unsigned P3 :1;
unsigned P4 :1;
unsigned P5 :1;
unsigned P6 :1;
unsigned P7 :1;
} bits;

such that the the transformation can be done by assignments for each bit

data_byte.p0 = Output_byte.p7
data_byte.p1 = Output byte.p6 etc

Would this work?
Thanks for your patience.
Reply
11-01-2014, 12:43 AM,
#24
RE: I2C LCD + Keypad - Adafruit 715
They look the same?

(Syntax may be a bit off, too.)

John
Reply
11-01-2014, 01:16 AM,
#25
RE: I2C LCD + Keypad - Adafruit 715
They have the same structure (8 bits) but the Output_byte is created from different bits of the data_byte. Does that make sense??
Reply
11-01-2014, 01:46 AM,
#26
RE: I2C LCD + Keypad - Adafruit 715
Oh, sorry I missed that.

Yes should work.

John
Reply
11-01-2014, 01:48 AM,
#27
RE: I2C LCD + Keypad - Adafruit 715
Instead of doubling the type definition (which is missing the semicolon after the u8 as well as the embracing union), just use regis' PCF8574_t:

PCF8574_t data_byte, Output_byte;

With this, your single-bit mirroring would work (although the variable names might be misleading):

data_byte.p0 = Output_byte.p7;
data_byte.p1 = Output byte.p6;
...

Or use my const look-up-table mir4bits[] (find it above)
e.g.
u8 mir8bits (u8 b) {return mir4bits[b>>4] | (mir4bits[b&15]<<4);} // not tested!
Reply
11-01-2014, 08:34 PM,
#28
RE: I2C LCD + Keypad - Adafruit 715
Thanks all I will give it a try.
Reply
15-01-2014, 02:04 PM,
#29
RE: I2C LCD + Keypad - Adafruit 715
I have code that compiles now but when I try to set a bit in the union/structure within a macro - the compiler throws it out.
The code fragment is

union MCP23017_data;

MCP23017_data.bits.p5 = 0x00; THIS IS THE LINE THAT IT GIVES AN ERROR ON

h_address = MCP23017_hardware_address;
r_address = GPIOA;

I2C_send_MCP23017(I2C1, h_address, r_address, value);
Delayus(1);
Error is:-c:197:16: error: expected identifier or '(' before '.' token
Can you give me a clue please?
Reply
15-01-2014, 03:09 PM,
#30
RE: I2C LCD + Keypad - Adafruit 715
trouble starts in the line
union MCP23017_data;
which looks wrong or at least incomplete
(like it breaks the "<type> <name>;" syntax rule for a variable definition)
'union' is no complete type name at all.

I assume you intended to declare a variable "MCP23017_data" of some "union type" defined earlier.

In case you have NO typedef, it would look like this:

union my_union {...};
...
union my_union my_variable;

WITH typedef:

typedef union {...} my_union_t;
...
my_union_t my_variable;

Or, in case you need the union in just one place (define union and declare vars in one):

union {...} var1, var2, var3;

But that is way too little code and no chance to understand what happens.
I do not see any macro?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)